diff --git a/pom.xml b/pom.xml index 3696dfe..8551651 100644 --- a/pom.xml +++ b/pom.xml @@ -48,24 +48,29 @@ - io.vertx - vertx-core - 3.2.0 - - - io.vertx - vertx-web - 3.2.0 + com.sparkjava + spark-core + 2.3 com.google.guava guava 19.0 + + com.google.code.gson + gson + 2.6.2 + org.mongodb - mongodb-driver-async - 3.0.4 + mongo-java-driver + 3.2.2 + + + org.mongodb.morphia + morphia + 1.1.0 org.projectlombok diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 55317c0..fdb8ef3 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -1,49 +1,120 @@ package net.frozenorb.apiv3; import com.google.common.collect.ImmutableList; +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.mongodb.MongoClient; +import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; -import com.mongodb.async.client.MongoDatabase; -import io.vertx.core.AbstractVerticle; -import io.vertx.ext.web.Router; import lombok.Getter; -import net.frozenorb.apiv3.routes.*; -import net.frozenorb.apiv3.util.MongoUtils; +import net.frozenorb.apiv3.model.*; +import net.frozenorb.apiv3.weirdStuff.ExcludeFromReplies; +import net.frozenorb.apiv3.weirdStuff.ObjectIdTypeAdapter; +import org.bson.types.ObjectId; +import org.mongodb.morphia.Datastore; +import org.mongodb.morphia.Morphia; -public final class APIv3 extends AbstractVerticle { +import java.util.*; - @Getter private static MongoDatabase mongo; +import static spark.Spark.*; - // TODO: review the find* methods in models to make sure they're good (and sometimes not too broad, ex findAll on Users) - // TODO: consistency -- sometimes we refer to a user as user or target. +public final class APIv3 { - public void start() { + @Getter private static Datastore datastore; + private final Gson gson = new GsonBuilder().registerTypeAdapter(ObjectId.class, new ObjectIdTypeAdapter()).setExclusionStrategies(new ExclusionStrategy() { + + @Override + public boolean shouldSkipField(FieldAttributes fieldAttributes) { + return fieldAttributes.getAnnotation(ExcludeFromReplies.class) != null; + } + + @Override + public boolean shouldSkipClass(Class aClass) { + return false; + } + + }).create(); + + APIv3() { setupDatabase(); setupHttp(); } - public void setupDatabase() { - mongo = MongoUtils.initializeConnection(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)), "minehqapi", "test", "test".toCharArray()); + private void setupDatabase() { + MongoClient mongoClient = new MongoClient(new ServerAddress("ds055505.mongolab.com", 55505), + ImmutableList.of( + MongoCredential.createCredential("test", "minehqapi", "test".toCharArray()) + )); + + Morphia morphia = new Morphia(); + morphia.mapPackage("net.frozenorb.apiv3.accessor"); + + datastore = morphia.createDatastore(mongoClient, "minehqapi"); + datastore.ensureIndexes(); } - public void setupHttp() { - Router rootRouter = Router.router(vertx); + private void setupHttp() { + port(80); - // We always reply in JSON. - rootRouter.route("/*").handler(ctx -> { - ctx.response().putHeader("content-type", "application/json"); - ctx.next(); + before((req, res) -> { + req.attribute("server", Server.byId(req.queryParams("server"))); }); - rootRouter.mountSubRouter("/auditLog", AuditLogRouter.create(vertx)); - rootRouter.mountSubRouter("/grants", GrantsRouter.create(vertx)); - rootRouter.mountSubRouter("/ipLog", IPLogRouter.create(vertx)); - rootRouter.mountSubRouter("/notifications", NotificationsRouter.create(vertx)); - rootRouter.mountSubRouter("/punishments", PunishmentsRouter.create(vertx)); - rootRouter.mountSubRouter("/serverGroups", ServerGroupsRouter.create(vertx)); - rootRouter.mountSubRouter("/servers", ServersRouter.create(vertx)); - rootRouter.mountSubRouter("/users", UsersRouter.create(vertx)); + get("/announcements", (req, res) -> { + Server sender = req.attribute("server"); + ServerGroup senderGroup = sender.resolveGroup(); - vertx.createHttpServer().requestHandler(rootRouter::accept).listen(8080); + return senderGroup.getAnnouncements(); + }, gson::toJson); + + get("/chatFilterList", (req, res) -> { + Server sender = req.attribute("server"); + ServerGroup senderGroup = sender.resolveGroup(); + + return senderGroup.getChatFilterList(); + }, gson::toJson); + + get("/servers", (req, res) -> { + return APIv3.getDatastore().createQuery(Server.class).asList(); + }, gson::toJson); + + get("/users", (req, res) -> { + return APIv3.getDatastore().createQuery(User.class).asList(); + }, gson::toJson); + + get("/user/:name", (req, res) -> { + User u = new User(UUID.randomUUID(), req.params("name")); + + APIv3.getDatastore().save(u); + return u; + }, gson::toJson); + + get("/staff", (req, res) -> { + Map staffRanks = new HashMap<>(); + + for (Rank rank : APIv3.getDatastore().createQuery(Rank.class).asList()) { + if (rank.isStaff()) { + staffRanks.put(rank.getId(), rank); + } + } + + Map staffGrants = new HashMap<>(); + + for (Grant staffGrant : APIv3.getDatastore().createQuery(Grant.class).field("rank").in(staffRanks.keySet()).asList()) { + if (staffGrant.isActive()) { + staffGrants.put(staffGrant.getTarget(), staffGrant.getRank()); + } + } + + //Map> + return "idk"; + }, gson::toJson); + + after((req, res) -> { + res.header("content-type", "application/json"); + }); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/LiteFullJson.java b/src/main/java/net/frozenorb/apiv3/LiteFullJson.java deleted file mode 100644 index d1c11be..0000000 --- a/src/main/java/net/frozenorb/apiv3/LiteFullJson.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.frozenorb.apiv3; - -import org.bson.Document; - -public interface LiteFullJson { - - Document toLiteJson(); - default Document toFullJson() { - return toLiteJson(); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/Main.java b/src/main/java/net/frozenorb/apiv3/Main.java index 9b9e044..c52414d 100644 --- a/src/main/java/net/frozenorb/apiv3/Main.java +++ b/src/main/java/net/frozenorb/apiv3/Main.java @@ -1,11 +1,9 @@ package net.frozenorb.apiv3; -import io.vertx.core.Vertx; - public final class Main { public static void main(String[] args) { - Vertx.vertx().deployVerticle(new APIv3()); + new APIv3(); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java b/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java deleted file mode 100644 index ad9c968..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java +++ /dev/null @@ -1,54 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.AuditLogEntry; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; -import org.bson.types.ObjectId; - -import java.util.Collection; -import java.util.Date; -import java.util.UUID; - -@UtilityClass -public class AuditLog { - - public static final String COLLECTION_NAME = "auditLog"; - private static final Document TIME_BASED_SORT = new Document("performedAt", -1); - - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, AuditLogEntry::new, callback); - } - - public static void findByPerformer(UUID performer, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedBy", performer.toString()), TIME_BASED_SORT, AuditLogEntry::new, callback); - } - - public static void findByPerformerIp(String performerIp, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedFrom", performerIp), TIME_BASED_SORT, AuditLogEntry::new, callback); - } - - public static void findByActionType(String actionType, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), TIME_BASED_SORT, AuditLogEntry::new, callback); - } - - public static void log(UUID user, String userIp, String actionType, SingleResultCallback callback) { - log(user, userIp, actionType, callback); - } - - public static void log(UUID user, String userIp, String actionType, Document data, SingleResultCallback callback) { - Document insert = new Document(); - - insert.put("_id", new ObjectId().toString()); - insert.put("performedBy", user.toString()); - insert.put("performedAt", new Date()); - insert.put("performedFrom", userIp); - insert.put("actionType", actionType); - insert.put("actionData", data); - - APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Grants.java b/src/main/java/net/frozenorb/apiv3/accessor/Grants.java deleted file mode 100644 index 07f391e..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/Grants.java +++ /dev/null @@ -1,50 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.Grant; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; -import org.bson.types.ObjectId; - -import java.time.Instant; -import java.util.Collection; -import java.util.Set; -import java.util.UUID; - -@UtilityClass -public class Grants { - - public static final String COLLECTION_NAME = "grant"; - private static final Document TIME_BASED_SORT = new Document("addedAt", -1); - - public static void findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Grant::new, callback); - } - - public static void findByTarget(UUID target, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), TIME_BASED_SORT, Grant::new, callback); - } - - public static void findByAddedBy(UUID addedBy, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), TIME_BASED_SORT, Grant::new, callback); - } - - public static void createGrant(UUID target, Set scopes, String rank, Instant expiresAt, UUID addedBy, String reason, SingleResultCallback callback) { - Document insert = new Document(); - - insert.put("_id", new ObjectId().toString()); - insert.put("target", target.toString()); - insert.put("reason", reason); - insert.put("scopes", scopes); - insert.put("rank", rank); - insert.put("expiresAt", expiresAt); - - insert.put("addedBy", addedBy.toString()); - insert.put("addedAt", Instant.now()); - - APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/IPBans.java b/src/main/java/net/frozenorb/apiv3/accessor/IPBans.java deleted file mode 100644 index dcfaa82..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/IPBans.java +++ /dev/null @@ -1,24 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.model.IPBan; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@UtilityClass -public class IPBans { - - public static final String COLLECTION_NAME = "ipBan"; - - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPBan::new, callback); - } - - public static void findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPBan::new, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java b/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java deleted file mode 100644 index 46a0d49..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java +++ /dev/null @@ -1,52 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.IPLogEntry; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; -import org.bson.types.ObjectId; - -import java.time.Instant; -import java.util.Collection; -import java.util.UUID; - -@UtilityClass -public class IPLog { - - public static final String COLLECTION_NAME = "ipLog"; - private static final Document TIME_BASED_SORT = new Document("lastSeen", -1); - - public static void findByUser(UUID user, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("user", user.toString()), TIME_BASED_SORT, IPLogEntry::new, callback); - } - - public static void findByIp(String ip, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("ip", ip), TIME_BASED_SORT, IPLogEntry::new, callback); - } - - public static void log(UUID user, String ip , SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("user", user.toString()).append("ip", ip), IPLogEntry::new, (ipLogEntry, error) -> { - if (error != null) { - callback.onResult(null, error); - } else if (ipLogEntry != null) { - ipLogEntry.used(callback); - } else { - Document insert = new Document(); - - insert.put("_id", new ObjectId().toString()); - insert.put("user", user.toString()); - insert.put("ip", ip); - insert.put("lastSeen", Instant.now()); - insert.put("firstSeen", Instant.now()); - insert.put("uses", 1); - - APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, (ignored, error2) -> { - callback.onResult(null, error2); - }); - } - }); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/NotificationLog.java b/src/main/java/net/frozenorb/apiv3/accessor/NotificationLog.java deleted file mode 100644 index b107701..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/NotificationLog.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.NotificationLogEntry; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; -import org.bson.types.ObjectId; - -import java.time.Instant; -import java.util.Collection; -import java.util.UUID; - -@UtilityClass -public class NotificationLog { - - public static final String COLLECTION_NAME = "notificationLog"; - - public static void findByTarget(UUID target, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), NotificationLogEntry::new, callback); - } - - public static void logEmail(UUID user, String title, String body, SingleResultCallback callback) { - log(user, NotificationLogEntry.NotificationType.EMAIL, title, body, callback); - } - - public static void logSMS(UUID user, String title, String body, SingleResultCallback callback) { - log(user, NotificationLogEntry.NotificationType.SMS, title, body, callback); - } - - private static void log(UUID user, NotificationLogEntry.NotificationType type, String title, String body, SingleResultCallback callback) { - Document insert = new Document(); - - insert.put("_id", new ObjectId().toString()); - insert.put("target", user.toString()); - insert.put("sentAt", Instant.now()); - insert.put("type", type.name()); - insert.put("title", title); - insert.put("body", body); - - APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/NotificationTemplates.java b/src/main/java/net/frozenorb/apiv3/accessor/NotificationTemplates.java deleted file mode 100644 index 0bfdf40..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/NotificationTemplates.java +++ /dev/null @@ -1,39 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.NotificationTemplate; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; - -import java.time.Instant; -import java.util.Collection; -import java.util.UUID; - -@UtilityClass -public class NotificationTemplates { - - public static final String COLLECTION_NAME = "notificationTemplate"; - - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationTemplate::new, callback); - } - - public static void findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), NotificationTemplate::new, callback); - } - - public static void createNotificationTemplate(String id, String title, String body, UUID creator, SingleResultCallback callback) { - Document insert = new Document(); - - insert.put("_id", id); - insert.put("title", title); - insert.put("body", body); - insert.put("lastUpdatedAt", Instant.now()); - insert.put("lastUpdatedBy", creator.toString()); - - APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java b/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java deleted file mode 100644 index d9df1c8..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java +++ /dev/null @@ -1,50 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.Grant; -import net.frozenorb.apiv3.model.Punishment; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; -import org.bson.types.ObjectId; - -import java.time.Instant; -import java.util.Collection; -import java.util.UUID; - -@UtilityClass -public class Punishments { - - public static final String COLLECTION_NAME = "punishment"; - private static final Document TIME_BASED_SORT = new Document("addedAt", -1); - - public static void findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Punishment::new, callback); - } - - public static void findByTarget(UUID target, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), TIME_BASED_SORT, Punishment::new, callback); - } - - public static void findByAddedBy(UUID addedBy, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), TIME_BASED_SORT, Grant::new, callback); - } - - public static void createPunishment(UUID target, Punishment.PunishmentType type, Instant expiresAt, UUID addedBy, String addedOn, String reason, SingleResultCallback callback) { - Document insert = new Document(); - - insert.put("_id", new ObjectId().toString()); - insert.put("target", target.toString()); - insert.put("reason", reason); - insert.put("type", type.name()); - insert.put("expiresAt", expiresAt); - - insert.put("addedBy", addedBy.toString()); - insert.put("addedAt", Instant.now()); - insert.put("addedOn", addedOn); - - APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/ServerGroups.java b/src/main/java/net/frozenorb/apiv3/accessor/ServerGroups.java deleted file mode 100644 index ea67ec3..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/ServerGroups.java +++ /dev/null @@ -1,24 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.model.ServerGroup; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@UtilityClass -public class ServerGroups { - - public static final String COLLECTION_NAME = "serverGroup"; - - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), ServerGroup::new, callback); - } - - public static void findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), ServerGroup::new, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Servers.java b/src/main/java/net/frozenorb/apiv3/accessor/Servers.java deleted file mode 100644 index 71e8b9f..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/Servers.java +++ /dev/null @@ -1,28 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.model.Server; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; - -import java.util.Collection; - -@UtilityClass -public class Servers { - - public static final String COLLECTION_NAME = "server"; - - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Server::new, callback); - } - - public static void findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Server::new, callback); - } - - public static void findByGroup(String groupId, SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document("group", groupId), Server::new, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Users.java b/src/main/java/net/frozenorb/apiv3/accessor/Users.java deleted file mode 100644 index 2b628de..0000000 --- a/src/main/java/net/frozenorb/apiv3/accessor/Users.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.frozenorb.apiv3.accessor; - -import com.mongodb.async.SingleResultCallback; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.model.User; -import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; - -import java.util.Collection; -import java.util.UUID; - -@UtilityClass -public class Users { - - public static final String COLLECTION_NAME = "user"; - - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), User::new, callback); - } - - public static void findById(UUID id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id.toString()), User::new, callback); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java index 84b02ce..c6d90a8 100644 --- a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java @@ -1,47 +1,36 @@ package net.frozenorb.apiv3.model; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.AuditLog; import org.bson.Document; +import org.bson.types.ObjectId; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; -import java.time.Instant; +import java.util.Date; import java.util.UUID; -@ToString -public final class AuditLogEntry extends BaseModel { +@Entity(value = "auditLog", noClassnameStored = true) +public final class AuditLogEntry { - @Getter private String id; + @Id private ObjectId id; @Getter private UUID performedBy; - @Getter private Instant performedAt; - @Getter private String performedFrom; + @Getter private Date performedAt; + @Getter private String performedFromIp; @Getter private String actionType; @Getter private Document actionData; - public AuditLogEntry(Document json) { - super(AuditLog.COLLECTION_NAME); + public AuditLogEntry() {} // For Morphia - this.id = json.getString("_id"); - this.performedBy = UUID.fromString(json.getString("performedBy")); - this.performedAt = (Instant) json.get("performedAt"); - this.performedFrom = json.getString("performedFrom"); - this.actionType = json.getString("actionType"); - this.actionData = (Document) json.get("actionData"); - - setId(id); + public AuditLogEntry(UUID performedBy, String performedFromIp, String actionType) { + this(performedBy, performedFromIp, actionType, new Document()); } - public Document toLiteJson() { - Document json = new Document(); - - json.put("id", id); - json.put("performedBy", performedBy.toString()); - json.put("performedAt", performedAt.toString()); - json.put("performedFrom", performedFrom); - json.put("actionType", actionType); - json.put("actionData", actionData); - - return json; + public AuditLogEntry(UUID performedBy, String performedFromIp, String actionType, Document actionData) { + this.performedBy = performedBy; + this.performedAt = new Date(); + this.performedFromIp = performedFromIp; + this.actionType = actionType; + this.actionData = actionData; } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/BaseModel.java b/src/main/java/net/frozenorb/apiv3/model/BaseModel.java deleted file mode 100644 index 3a10dbf..0000000 --- a/src/main/java/net/frozenorb/apiv3/model/BaseModel.java +++ /dev/null @@ -1,30 +0,0 @@ -package net.frozenorb.apiv3.model; - -import com.mongodb.async.SingleResultCallback; -import lombok.Setter; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.LiteFullJson; -import org.bson.Document; - -public abstract class BaseModel implements LiteFullJson { - - private final String collectionName; - @Setter private String id; - - public BaseModel(String collectionName) { - this.collectionName = collectionName; - } - - protected void update(Document update, SingleResultCallback callback) { - APIv3.getMongo().getCollection(collectionName).updateOne(new Document("_id", id), update, (updateResult, error) -> { - callback.onResult(null, error); - }); - } - - protected void delete(SingleResultCallback callback) { - APIv3.getMongo().getCollection(collectionName).deleteOne(new Document("_id", id), (deleteResult, error) -> { - callback.onResult(null, error); - }); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/Grant.java b/src/main/java/net/frozenorb/apiv3/model/Grant.java index 13bd62c..2d714e8 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Grant.java +++ b/src/main/java/net/frozenorb/apiv3/model/Grant.java @@ -1,68 +1,51 @@ package net.frozenorb.apiv3.model; -import com.google.common.collect.ImmutableSet; -import com.mongodb.async.SingleResultCallback; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.Grants; -import org.bson.Document; +import net.frozenorb.apiv3.APIv3; +import org.bson.types.ObjectId; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; import java.time.Instant; -import java.util.Collection; +import java.util.Date; import java.util.Set; import java.util.UUID; -@ToString -public final class Grant extends BaseModel { +@Entity(value = "grants", noClassnameStored = true) +public final class Grant { - @Getter private String id; + @Id private ObjectId id; @Getter private UUID target; @Getter private String reason; @Getter private Set scopes; @Getter private String rank; - @Getter private Instant expiresAt; + @Getter private Date expiresAt; @Getter private UUID addedBy; - @Getter private Instant addedAt; + @Getter private Date addedAt; @Getter private UUID removedBy; - @Getter private Instant removedAt; + @Getter private Date removedAt; @Getter private String removalReason; - public Grant(Document json) { - super(Grants.COLLECTION_NAME); + public Grant() {} // For Morphia - this.id = json.getString("_id"); - this.target = UUID.fromString(json.getString("target")); - this.reason = json.getString("reason"); - this.scopes = ImmutableSet.copyOf((Collection) json.get("scopes")); // This is a safe cast, the collection's type is always String - this.rank = json.getString("rank"); - this.expiresAt = (Instant) json.get("expiresAt"); - - this.addedBy = UUID.fromString(json.getString("addedBy")); - this.addedAt = (Instant) json.get("addedAt"); - - if (json.containsKey("removedBy")) { - this.removedBy = UUID.fromString(json.getString("removedBy")); - this.removedAt = (Instant) json.get("removedAt"); - this.removalReason = json.getString("removalReason"); - } - - setId(id); + public Grant(UUID target, String reason, Set scopes, String rankId, Date expiresAt, UUID addedBy) { + this.target = target; + this.reason = reason; + this.scopes = scopes; + this.rank = rankId; + this.expiresAt = expiresAt; + this.addedBy = addedBy; + this.addedAt = new Date(); } - public void delete(UUID removedBy, String reason, SingleResultCallback callback) { + public void delete(UUID removedBy, String reason) { this.removedBy = removedBy; - this.removedAt = Instant.now(); + this.removedAt = new Date(); this.removalReason = reason; - Document set = new Document(); - - set.put("removedBy", removedBy.toString()); - set.put("removedAt", removedAt); - set.put("removalReason", removalReason); - - super.update(new Document("$set", set), callback); + APIv3.getDatastore().save(this); } public boolean isActive() { @@ -73,7 +56,7 @@ public final class Grant extends BaseModel { if (expiresAt == null) { return false; // Never expires } else { - return expiresAt.isAfter(Instant.now()); + return expiresAt.after(new Date()); } } @@ -81,34 +64,4 @@ public final class Grant extends BaseModel { return removedBy != null; } - public Document toLiteJson() { - Document json = new Document(); - - json.put("id", id); - json.put("target", target.toString()); - json.put("reason", reason); - json.put("scopes", scopes); - json.put("rank", rank); - json.put("expiresAt", expiresAt == null ? null : expiresAt.toString()); - - json.put("addedBy", addedBy.toString()); - json.put("addedAt", addedAt.toString()); - - Document statusJson = new Document(); - - statusJson.put("active", isActive()); - statusJson.put("expired", isExpired()); - statusJson.put("removed", isRemoved()); - - json.put("status", statusJson); - - if (removedBy != null) { - json.put("removedBy", removedBy.toString()); - json.put("removedAt", removedAt.toString()); - json.put("removalReason", removalReason); - } - - return json; - } - } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/IPBan.java b/src/main/java/net/frozenorb/apiv3/model/IPBan.java index cff38dc..14bddd3 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IPBan.java +++ b/src/main/java/net/frozenorb/apiv3/model/IPBan.java @@ -1,37 +1,14 @@ package net.frozenorb.apiv3.model; -import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.IPBans; -import org.bson.Document; +import org.bson.types.ObjectId; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; -@ToString -public final class IPBan extends BaseModel { +@Entity(value = "ipBans", noClassnameStored = true) +public final class IPBan { - @Getter private String id; + @Id private ObjectId id; - public IPBan(Document json) { - super(IPBans.COLLECTION_NAME); - - this.id = json.getString("_id"); - - setId(id); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("_id", id); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - - - return json; - } + public IPBan() {} // For Morphia } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java index 672084d..6fca6ab 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java @@ -1,62 +1,40 @@ package net.frozenorb.apiv3.model; -import com.mongodb.async.SingleResultCallback; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.IPLog; -import org.bson.Document; +import net.frozenorb.apiv3.APIv3; +import org.bson.types.ObjectId; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; import java.time.Instant; +import java.util.Date; import java.util.UUID; -@ToString -public final class IPLogEntry extends BaseModel { +@Entity(value = "ipLog", noClassnameStored = true) +public final class IPLogEntry { - @Getter private String id; + @Id private ObjectId id; @Getter private UUID user; @Getter private String ip; - @Getter private Instant firstSeen; - @Getter private Instant lastSeen; + @Getter private Date firstSeen; + @Getter private Date lastSeen; @Getter private int uses; - public IPLogEntry(Document json) { - super(IPLog.COLLECTION_NAME); + public IPLogEntry() {} // For Morphia - this.id = json.getString("_id"); - this.user = UUID.fromString(json.getString("user")); - this.ip = json.getString("ip"); - this.lastSeen = (Instant) json.get("lastSeen"); - this.firstSeen = (Instant) json.get("firstSeen"); - this.uses = json.getInteger("uses"); - - setId(id); + public IPLogEntry(UUID user, String ip) { + this.user = user; + this.ip = ip; + this.firstSeen = new Date(); + this.lastSeen = new Date(); + this.uses = 0; } - public void used(SingleResultCallback callback) { - this.lastSeen = Instant.now(); + public void used() { + this.lastSeen = new Date(); this.uses++; - Document set = new Document(); - - set.put("lastSeen", lastSeen); - set.put("uses", uses); - - super.update(new Document("$set", set), (updateResult, error) -> { - callback.onResult(null, error); - }); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("id", id); - json.put("user", user.toString()); - json.put("ip", ip); - json.put("firstSeen", firstSeen.toString()); - json.put("lastSeen", lastSeen.toString()); - json.put("uses", uses); - - return json; + APIv3.getDatastore().save(this); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java index e81be2c..96eaf94 100644 --- a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java @@ -1,47 +1,32 @@ package net.frozenorb.apiv3.model; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.NotificationLog; -import org.bson.Document; +import org.bson.types.ObjectId; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; import java.time.Instant; +import java.util.Date; import java.util.UUID; -@ToString -public final class NotificationLogEntry extends BaseModel { +@Entity(value = "notificationLog", noClassnameStored = true) +public final class NotificationLogEntry { - @Getter private String id; + @Id private ObjectId id; @Getter private UUID target; - @Getter private Instant sentAt; + @Getter private Date sentAt; @Getter private NotificationType type; @Getter private String title; @Getter private String body; - public NotificationLogEntry(Document json) { - super(NotificationLog.COLLECTION_NAME); + public NotificationLogEntry() {} // For Morphia - this.id = json.getString("_id"); - this.target = UUID.fromString(json.getString("target")); - this.sentAt = (Instant) json.get("sentAt"); - this.type = NotificationType.valueOf(json.getString("type")); - this.title = json.getString("title"); - this.body = json.getString("body"); - - setId(id); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("id", id); - json.put("target", target.toString()); - json.put("sentAt", sentAt.toString()); - json.put("type", type.name()); - json.put("title", title); - json.put("body", body); - - return json; + public NotificationLogEntry(UUID target, NotificationType type, String title, String body) { + this.target = target; + this.sentAt = new Date(); + this.type = type; + this.title = title; + this.body = body; } public enum NotificationType { diff --git a/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java b/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java index 304c62b..90dddcd 100644 --- a/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java @@ -1,68 +1,44 @@ package net.frozenorb.apiv3.model; -import com.mongodb.async.SingleResultCallback; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.NotificationTemplates; -import org.bson.Document; +import net.frozenorb.apiv3.APIv3; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; import java.time.Instant; +import java.util.Date; import java.util.Map; import java.util.UUID; -@ToString -public final class NotificationTemplate extends BaseModel { +@Entity(value = "notificationTemplates", noClassnameStored = true) +public final class NotificationTemplate { - @Getter private String id; + @Id private String id; @Getter private String title; @Getter private String body; - @Getter private Instant lastUpdatedAt; + @Getter private Date lastUpdatedAt; @Getter private UUID lastUpdatedBy; - public NotificationTemplate(Document json) { - super(NotificationTemplates.COLLECTION_NAME); + public NotificationTemplate() {} // For Morphia - this.id = json.getString("_id"); - this.title = json.getString("title"); - this.body = json.getString("body"); - this.lastUpdatedAt = (Instant) json.get("lastUpdatedAt"); - this.lastUpdatedBy = UUID.fromString(json.getString("lastUpdatedBy")); - - setId(id); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("id", id); - json.put("title", title); - json.put("body", body); - json.put("lastUpdatedAt", lastUpdatedAt.toString()); - json.put("lastUpdatedBy", lastUpdatedBy.toString()); - - return json; - } - - public void update(String title, String body, UUID updatedBy, SingleResultCallback callback) { + public NotificationTemplate(String title, String body, UUID creator) { this.title = title; this.body = body; - this.lastUpdatedAt = Instant.now(); - this.lastUpdatedBy = updatedBy; - - Document set = new Document(); - - set.put("title", title); - set.put("body", body); - set.put("lastUpdatedAt", lastUpdatedAt.toString()); - set.put("lastUpdatedBy", lastUpdatedBy.toString()); - - super.update(new Document("$set", set), (updateResult, error) -> { - callback.onResult(null, error); - }); + this.lastUpdatedAt = new Date(); + this.lastUpdatedBy = creator; } - public void delete(SingleResultCallback callback) { - super.delete(callback); + public void update(String title, String body, UUID updatedBy) { + this.title = title; + this.body = body; + this.lastUpdatedAt = new Date(); + this.lastUpdatedBy = updatedBy; + + APIv3.getDatastore().save(this); + } + + public void delete() { + APIv3.getDatastore().delete(this); } public String fillTitle(Map replacements) { diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java index 98b6cd1..a30b7cc 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -2,64 +2,50 @@ package net.frozenorb.apiv3.model; import com.mongodb.async.SingleResultCallback; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.Punishments; -import org.bson.Document; +import net.frozenorb.apiv3.APIv3; +import org.bson.types.ObjectId; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; import java.time.Instant; +import java.util.Date; import java.util.UUID; -@ToString -public final class Punishment extends BaseModel { +@Entity(value = "punishments", noClassnameStored = true) +public final class Punishment { - @Getter private String id; + @Id private ObjectId id; @Getter private UUID target; @Getter private String reason; @Getter private PunishmentType type; - @Getter private Instant expiresAt; + @Getter private Date expiresAt; @Getter private UUID addedBy; - @Getter private Instant addedAt; + @Getter private Date addedAt; @Getter private String addedOn; @Getter private UUID removedBy; - @Getter private Instant removedAt; + @Getter private Date removedAt; @Getter private String removalReason; - public Punishment(Document json) { - super(Punishments.COLLECTION_NAME); + public Punishment() {} // For Morphia - this.id = json.getString("_id"); - this.target = UUID.fromString(json.getString("target")); - this.reason = json.getString("reason"); - this.type = PunishmentType.valueOf(json.getString("type")); - this.expiresAt = (Instant) json.get("expiresAt"); - - this.addedBy = UUID.fromString(json.getString("addedBy")); - this.addedAt = (Instant) json.get("addedAt"); - this.addedOn = json.getString("addedOn"); - - if (json.containsKey("removedBy")) { - this.removedBy = UUID.fromString(json.getString("removedBy")); - this.removedAt = (Instant) json.get("removedAt"); - this.removalReason = json.getString("removalReason"); - } - - setId(id); + public Punishment(UUID target, String reason, PunishmentType type, Date expiresAt, UUID addedBy, String addedOn) { + this.target = target; + this.reason = reason; + this.type = type; + this.expiresAt = expiresAt; + this.addedBy = addedBy; + this.addedAt = new Date(); + this.addedOn = addedOn; } - public void delete(UUID removedBy, String reason, SingleResultCallback callback) { + public void delete(UUID removedBy, String reason) { this.removedBy = removedBy; - this.removedAt = Instant.now(); + this.removedAt = new Date(); this.removalReason = reason; - Document set = new Document(); - - set.put("removedBy", removedBy.toString()); - set.put("removedAt", removedAt); - set.put("removalReason", removalReason); - - super.update(new Document("$set", set), callback); + APIv3.getDatastore().save(this); } public boolean isActive() { @@ -70,7 +56,7 @@ public final class Punishment extends BaseModel { if (expiresAt == null) { return false; // Never expires } else { - return expiresAt.isAfter(Instant.now()); + return expiresAt.after(new Date()); } } @@ -78,35 +64,6 @@ public final class Punishment extends BaseModel { return removedBy != null; } - public Document toLiteJson() { - Document json = new Document(); - - json.put("id", id); - json.put("target", target.toString()); - json.put("reason", reason); - json.put("type", type.name()); - json.put("expiresAt", expiresAt == null ? null : expiresAt.toString()); - - json.put("addedBy", addedBy.toString()); - json.put("addedAt", addedAt.toString()); - - Document statusJson = new Document(); - - statusJson.put("active", isActive()); - statusJson.put("expired", isExpired()); - statusJson.put("removed", isRemoved()); - - json.put("status", statusJson); - - if (removedBy != null) { - json.put("removedBy", removedBy.toString()); - json.put("removedAt", removedAt.toString()); - json.put("removalReason", removalReason); - } - - return json; - } - public enum PunishmentType { BAN, MUTE, WARN diff --git a/src/main/java/net/frozenorb/apiv3/model/Rank.java b/src/main/java/net/frozenorb/apiv3/model/Rank.java new file mode 100644 index 0000000..22170ce --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/model/Rank.java @@ -0,0 +1,33 @@ +package net.frozenorb.apiv3.model; + +import lombok.Getter; +import net.frozenorb.apiv3.APIv3; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; + +@Entity(value = "ranks", noClassnameStored = true) +public final class Rank { + + @Getter @Id private String id; + @Getter private int weight; + @Getter private String displayName; + @Getter private String gameColor; + @Getter private String websiteColor; + @Getter private boolean staff; + + public Rank() {} // For Morphia + + public Rank(String id, int weight, String displayName, String gameColor, String websiteColor, boolean staff) { + this.id = id; + this.weight = weight; + this.displayName = displayName; + this.gameColor = gameColor; + this.websiteColor = websiteColor; + this.staff = staff; + } + + public void delete() { + APIv3.getDatastore().delete(this); + } + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/Server.java b/src/main/java/net/frozenorb/apiv3/model/Server.java index 768b563..9ea1e75 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Server.java +++ b/src/main/java/net/frozenorb/apiv3/model/Server.java @@ -1,70 +1,48 @@ package net.frozenorb.apiv3.model; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.Servers; +import net.frozenorb.apiv3.APIv3; import org.bson.Document; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; +import java.lang.annotation.Documented; import java.time.Instant; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.UUID; -import java.util.stream.Collectors; +import java.util.*; -@ToString -public final class Server extends BaseModel { +@Entity(value = "servers", noClassnameStored = true) +public final class Server { - @Getter private String id; + @Id private String id; @Getter private String bungeeId; @Getter private String displayName; @Getter private String secret; @Getter private String group; @Getter private String ip; - @Getter private Instant lastUpdate; + @Getter private Date lastUpdate; @Getter private double lastTps; - @Getter private List players; + @Getter private Set players; - public Server(Document json) { - super(Servers.COLLECTION_NAME); - - this.id = json.getString("_id"); - this.bungeeId = json.getString("bungeeId"); - this.displayName = json.getString("displayName"); - this.secret = json.getString("secret"); - this.group = json.getString("group"); - this.ip = json.getString("ip"); - this.lastUpdate = (Instant) json.get("lastUpdate"); - this.lastTps = ((Number) json.get("lastTps")).doubleValue(); - this.players = new ArrayList<>(); - - for (Object uuidString : (Collection) json.get("players")) { - players.add(UUID.fromString((String) uuidString)); - } - - setId(id); + public static Server byId(String id) { + return APIv3.getDatastore().createQuery(Server.class).field("id").equal(id).get(); } - public Document toLiteJson() { - Document json = new Document(); + public Server() {} // For Morphia - json.put("id", id); - json.put("bungeeId", bungeeId); - json.put("displayName", displayName); - json.put("group", group); - json.put("ip", ip); - - return json; + public Server(String id, String bungeeId, String displayName, String secret, String group, String ip) { + this.id = id; + this.bungeeId = bungeeId; + this.displayName = displayName; + this.secret = secret; + this.group = group; + this.ip = ip; + this.lastUpdate = new Date(); + this.lastTps = 0; + this.players = new HashSet<>(); } - public Document toFullJson() { - Document json = toLiteJson(); - - json.put("lastUpdate", lastUpdate.toString()); - json.put("lastTps", lastTps); - json.put("players", players.stream().map(UUID::toString).collect(Collectors.toList())); - - return json; + public ServerGroup resolveGroup() { + return ServerGroup.byId(group); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java index 09477b1..c0684ad 100644 --- a/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java +++ b/src/main/java/net/frozenorb/apiv3/model/ServerGroup.java @@ -1,37 +1,42 @@ package net.frozenorb.apiv3.model; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.ServerGroups; -import org.bson.Document; +import net.frozenorb.apiv3.APIv3; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; -@ToString -public final class ServerGroup extends BaseModel { +import java.util.HashSet; +import java.util.Set; - @Getter private String id; +@Entity(value = "serverGroups", noClassnameStored = true) +public final class ServerGroup { - public ServerGroup(Document json) { - super(ServerGroups.COLLECTION_NAME); + @Getter @Id private String id; + @Getter private String displayName; + @Getter private Set announcements; + @Getter private Set chatFilterList; - this.id = json.getString("_id"); - - setId(id); + public static ServerGroup byId(String id) { + return APIv3.getDatastore().createQuery(ServerGroup.class).field("id").equal(id).get(); } - public Document toLiteJson() { - Document json = new Document(); + public ServerGroup() {} // For Morphia - json.put("_id", id); - - return json; + public ServerGroup(String id, String displayName) { + this.id = id; + this.displayName = displayName; + this.announcements = new HashSet<>(); + this.chatFilterList = new HashSet<>(); } - public Document toFullJson() { - Document json = toLiteJson(); + public void setAnnouncements(Set announcements) { + this.announcements = announcements; + APIv3.getDatastore().save(this); + } - - - return json; + public void setChatFilterList(Set chatFilterList) { + this.chatFilterList = chatFilterList; + APIv3.getDatastore().save(this); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 8f15603..6fe45c2 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -1,67 +1,47 @@ package net.frozenorb.apiv3.model; import lombok.Getter; -import lombok.ToString; -import net.frozenorb.apiv3.accessor.Users; -import org.bson.Document; +import net.frozenorb.apiv3.weirdStuff.ExcludeFromReplies; +import org.mongodb.morphia.annotations.Entity; +import org.mongodb.morphia.annotations.Id; import java.time.Instant; +import java.util.Date; +import java.util.HashMap; import java.util.Map; import java.util.UUID; -@ToString -public final class User extends BaseModel { +@Entity(value = "users", noClassnameStored = true) +public final class User { - @Getter private UUID id; + @Id private UUID id; @Getter private String lastName; - @Getter private Map aliases; + @ExcludeFromReplies @Getter private Map aliases; @Getter private String otpCode; @Getter private String password; @Getter private String passwordSalt; @Getter private String email; @Getter private int phoneNumber; @Getter private String lastSeenOn; - @Getter private Instant lastSeenAt; - @Getter private Instant firstSeen; + @Getter private Date lastSeenAt; + @Getter private Date firstSeen; - public User(Document json) { - super(Users.COLLECTION_NAME); + public User() {} // For Morphia - this.id = UUID.fromString(json.getString("_id")); - this.lastName = json.getString("lastName"); - this.aliases = (Map) json.get("aliases"); - this.otpCode = json.getString("otpCode"); - this.password = json.getString("password"); - this.passwordSalt = json.getString("passwordSalt"); - this.email = json.getString("email"); - this.phoneNumber = json.getInteger("phoneNumber"); - this.lastSeenOn = json.getString("lastSeenOn"); - this.lastSeenAt = (Instant) json.get("lastSeenAt"); - this.firstSeen = (Instant) json.get("firstSeen"); + public User(UUID id, String lastName) { + this.id = id; + this.lastName = lastName; + this.aliases = new HashMap<>(); + this.otpCode = null; + this.password = null; + this.passwordSalt = null; + this.email = null; + this.phoneNumber = 0; + this.lastSeenOn = "Unknown"; + this.lastSeenAt = new Date(); + this.firstSeen = new Date(); - setId(id.toString()); - } - - public Document toLiteJson() { - Document json = new Document(); - - json.put("id", id.toString()); - json.put("lastName", lastName); - json.put("email", email); - json.put("phoneNumber", phoneNumber); - json.put("lastSeenOn", lastSeenOn); - json.put("lastSeenAt", lastSeenAt.toString()); - json.put("firstSeen", firstSeen.toString()); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - - json.put("aliases", aliases); - - return json; + aliases.put(lastName, new Date()); } } \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/mongoCodec/InstantCodec.java b/src/main/java/net/frozenorb/apiv3/mongoCodec/InstantCodec.java deleted file mode 100644 index df1cbff..0000000 --- a/src/main/java/net/frozenorb/apiv3/mongoCodec/InstantCodec.java +++ /dev/null @@ -1,26 +0,0 @@ -package net.frozenorb.apiv3.mongoCodec; - -import org.bson.BsonReader; -import org.bson.BsonWriter; -import org.bson.codecs.Codec; -import org.bson.codecs.DecoderContext; -import org.bson.codecs.EncoderContext; - -import java.time.Instant; - -public final class InstantCodec implements Codec { - - public void encode(BsonWriter writer, Instant value, EncoderContext context) { - writer.writeDateTime(value.toEpochMilli()); - } - - public Instant decode(BsonReader reader, DecoderContext context) { - long epochMillis = reader.readDateTime(); - return Instant.ofEpochMilli(epochMillis); - } - - public Class getEncoderClass() { - return Instant.class; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/AuditLogRouter.java b/src/main/java/net/frozenorb/apiv3/routes/AuditLogRouter.java deleted file mode 100644 index 5736b07..0000000 --- a/src/main/java/net/frozenorb/apiv3/routes/AuditLogRouter.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.frozenorb.apiv3.routes; - -import io.vertx.core.Vertx; -import io.vertx.ext.web.Router; -import lombok.experimental.UtilityClass; - -@UtilityClass -public class AuditLogRouter { - - public static Router create(Vertx vertx) { - Router router = Router.router(vertx); - - - - return router; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/GrantsRouter.java b/src/main/java/net/frozenorb/apiv3/routes/GrantsRouter.java deleted file mode 100644 index 419bf78..0000000 --- a/src/main/java/net/frozenorb/apiv3/routes/GrantsRouter.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.frozenorb.apiv3.routes; - -import io.vertx.core.Vertx; -import io.vertx.ext.web.Router; -import lombok.experimental.UtilityClass; - -@UtilityClass -public class GrantsRouter { - - public static Router create(Vertx vertx) { - Router router = Router.router(vertx); - - - - return router; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/IPLogRouter.java b/src/main/java/net/frozenorb/apiv3/routes/IPLogRouter.java deleted file mode 100644 index dc3836d..0000000 --- a/src/main/java/net/frozenorb/apiv3/routes/IPLogRouter.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.frozenorb.apiv3.routes; - -import io.vertx.core.Vertx; -import io.vertx.ext.web.Router; -import lombok.experimental.UtilityClass; - -@UtilityClass -public class IPLogRouter { - - public static Router create(Vertx vertx) { - Router router = Router.router(vertx); - - - - return router; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/NotificationsRouter.java b/src/main/java/net/frozenorb/apiv3/routes/NotificationsRouter.java deleted file mode 100644 index 7c68739..0000000 --- a/src/main/java/net/frozenorb/apiv3/routes/NotificationsRouter.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.frozenorb.apiv3.routes; - -import io.vertx.core.Vertx; -import io.vertx.ext.web.Router; -import lombok.experimental.UtilityClass; - -@UtilityClass -public class NotificationsRouter { - - public static Router create(Vertx vertx) { - Router router = Router.router(vertx); - - - - return router; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/PunishmentsRouter.java b/src/main/java/net/frozenorb/apiv3/routes/PunishmentsRouter.java deleted file mode 100644 index 88b66d1..0000000 --- a/src/main/java/net/frozenorb/apiv3/routes/PunishmentsRouter.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.frozenorb.apiv3.routes; - -import io.vertx.core.Vertx; -import io.vertx.ext.web.Router; -import lombok.experimental.UtilityClass; - -@UtilityClass -public class PunishmentsRouter { - - public static Router create(Vertx vertx) { - Router router = Router.router(vertx); - - - - return router; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/ServerGroupsRouter.java b/src/main/java/net/frozenorb/apiv3/routes/ServerGroupsRouter.java deleted file mode 100644 index 93a0909..0000000 --- a/src/main/java/net/frozenorb/apiv3/routes/ServerGroupsRouter.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.frozenorb.apiv3.routes; - -import io.vertx.core.Vertx; -import io.vertx.ext.web.Router; -import lombok.experimental.UtilityClass; - -@UtilityClass -public class ServerGroupsRouter { - - public static Router create(Vertx vertx) { - Router router = Router.router(vertx); - - - - return router; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/ServersRouter.java b/src/main/java/net/frozenorb/apiv3/routes/ServersRouter.java deleted file mode 100644 index b9cfe78..0000000 --- a/src/main/java/net/frozenorb/apiv3/routes/ServersRouter.java +++ /dev/null @@ -1,87 +0,0 @@ -package net.frozenorb.apiv3.routes; - -import io.vertx.core.Vertx; -import io.vertx.ext.web.Router; -import lombok.experimental.UtilityClass; - -@UtilityClass -public class ServersRouter { - - public static Router create(Vertx vertx) { - Router router = Router.router(vertx); - - - - return router; - } - - /* - coreHttpRouter.get("/servers").handler(ctx -> { - Servers.findAll((servers, error) -> { - if (error != null) { - ctx.response().end(ErrorUtils.createResponse(error).toJson()); - } else { - ctx.response().end(JsonUtils.toLiteJsonString(servers)); - } - }); - }); - - coreHttpRouter.get("/server/:server").handler(ctx -> { - String serverId = ctx.request().getParam("server"); - - Servers.findById(serverId, (server, error) -> { - if (error != null) { - ctx.response().end(ErrorUtils.createResponse(error).toJson()); - } else if (server != null) { - ctx.response().end(JsonUtils.toLiteJsonString(server)); - } else { - ctx.response().end(ErrorUtils.createResponse("Server '" + serverId + "' not found.").toJson()); - } - }); - }); - - coreHttpRouter.get("/user/:user").handler(ctx -> { - UUID target = UUID.fromString(ctx.request().getParam("user")); - - Users.findById(target.toString(), (user, error) -> { - if (error != null) { - ctx.response().end(ErrorUtils.createResponse(error).toJson()); - } else if (user != null) { - ctx.response().end(JsonUtils.toLiteJsonString(user)); - } else { - ctx.response().end(ErrorUtils.createResponse("User '" + target + "' not found.").toJson()); - } - }); - }); - - coreHttpRouter.get("/user/:user/grants").handler(ctx -> { - UUID target = UUID.fromString(ctx.request().getParam("user")); - - Grants.findByTarget(target, (grants, error) -> { - if (error != null) { - ctx.response().end(ErrorUtils.createResponse(error).toJson()); - } else { - ctx.response().end(JsonUtils.toLiteJsonString(grants)); - } - }); - }); - - coreHttpRouter.get("/auditLog").handler(ctx -> { - AuditLog.findAll((auditLogEntries, error) -> { - if (error != null) { - ctx.response().end(ErrorUtils.createResponse(error).toJson()); - } else { - ctx.response().end(JsonUtils.toLiteJsonString(auditLogEntries)); - } - }); - }); - - coreHttpRouter.get("/doAction/:actionType").handler(ctx -> { - String actionType = ctx.request().getParam("actionType"); - - //AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document()); - ctx.response().end("{'logged': true}"); - }); - */ - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/routes/UsersRouter.java b/src/main/java/net/frozenorb/apiv3/routes/UsersRouter.java deleted file mode 100644 index e470138..0000000 --- a/src/main/java/net/frozenorb/apiv3/routes/UsersRouter.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.frozenorb.apiv3.routes; - -import io.vertx.core.Vertx; -import io.vertx.ext.web.Router; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.accessor.Users; -import net.frozenorb.apiv3.util.ErrorUtils; -import net.frozenorb.apiv3.util.JsonUtils; - -import java.util.UUID; - -@UtilityClass -public class UsersRouter { - - public static Router create(Vertx vertx) { - Router router = Router.router(vertx); - - router.get("/info/:user").handler(ctx -> { - UUID target = UUID.fromString(ctx.request().getParam("user")); - - Users.findById(target, (user, error) -> { - if (error != null) { - ctx.response().end(ErrorUtils.toResponseString(error)); - } else if (user != null) { - ctx.response().end(JsonUtils.toLiteJsonString(user)); - } else { - ctx.response().end(ErrorUtils.toResponseString("User '" + target + "' not found.")); - } - }); - }); - - return router; - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java b/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java deleted file mode 100644 index 7cd0ae5..0000000 --- a/src/main/java/net/frozenorb/apiv3/util/ErrorUtils.java +++ /dev/null @@ -1,38 +0,0 @@ -package net.frozenorb.apiv3.util; - -import lombok.experimental.UtilityClass; -import org.bson.Document; - -import java.util.UUID; - -@UtilityClass -public class ErrorUtils { - - public static String toResponseString(Throwable throwable) { - // We do this identifier thing so we can easily search logs for the exception. - // We can't send the stack trace to the user, so this is a good alternative. - String identifier = UUID.randomUUID().toString(); - System.out.println("[Caught exception] Identifier=" + identifier); - throwable.printStackTrace(); - - return toResponseString(throwable.getClass().getSimpleName(), identifier); - } - - public static String toResponseString(String reason) { - return toResponseString(reason, null); - } - - private static String toResponseString(String reason, String identifier) { - Document json = new Document(); - - json.put("failed", true); - json.put("reason", reason); - - if (identifier != null) { - json.put("identifier", identifier); - } - - return json.toJson(); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/util/JsonUtils.java b/src/main/java/net/frozenorb/apiv3/util/JsonUtils.java deleted file mode 100644 index ed6e00e..0000000 --- a/src/main/java/net/frozenorb/apiv3/util/JsonUtils.java +++ /dev/null @@ -1,40 +0,0 @@ -package net.frozenorb.apiv3.util; - -import io.vertx.core.json.JsonArray; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.LiteFullJson; - -import java.util.Collection; - -@UtilityClass -public class JsonUtils { - - public static String toLiteJsonString(Collection encode) { - JsonArray result = new JsonArray(); - - for (LiteFullJson entry : encode) { - result.add(entry.toLiteJson()); - } - - return result.encode(); - } - - public static String toLiteJsonString(LiteFullJson encode) { - return encode.toLiteJson().toJson(); - } - - public static String toFullJsonString(Collection encode) { - JsonArray result = new JsonArray(); - - for (LiteFullJson entry : encode) { - result.add(entry.toFullJson()); - } - - return result.encode(); - } - - public static String toFullJsonString(LiteFullJson encode) { - return encode.toFullJson().toJson(); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java b/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java deleted file mode 100644 index bbb9c96..0000000 --- a/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java +++ /dev/null @@ -1,102 +0,0 @@ -package net.frozenorb.apiv3.util; - -import com.google.common.base.Function; -import com.google.common.collect.Collections2; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.mongodb.MongoCredential; -import com.mongodb.ServerAddress; -import com.mongodb.async.SingleResultCallback; -import com.mongodb.async.client.FindIterable; -import com.mongodb.async.client.MongoClientSettings; -import com.mongodb.async.client.MongoClients; -import com.mongodb.async.client.MongoDatabase; -import com.mongodb.connection.ClusterConnectionMode; -import com.mongodb.connection.ClusterSettings; -import lombok.experimental.UtilityClass; -import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.mongoCodec.InstantCodec; -import org.bson.BsonType; -import org.bson.Document; -import org.bson.codecs.BsonTypeClassMap; -import org.bson.codecs.BsonValueCodecProvider; -import org.bson.codecs.DocumentCodecProvider; -import org.bson.codecs.ValueCodecProvider; -import org.bson.codecs.configuration.CodecRegistries; -import org.bson.codecs.configuration.CodecRegistry; - -import java.time.Instant; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -@UtilityClass -public class MongoUtils { - - public static MongoDatabase initializeConnection(List hosts, String databaseName, String databaseUsername, char[] databasePassword) { - BsonTypeClassMap codecMap = new BsonTypeClassMap(ImmutableMap.of( - BsonType.DATE_TIME, Instant.class - )); - CodecRegistry codecRegistry = CodecRegistries.fromRegistries( - CodecRegistries.fromCodecs(new InstantCodec()), - CodecRegistries.fromProviders(new DocumentCodecProvider(codecMap)), - CodecRegistries.fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider()) - ); - MongoClientSettings settings = - MongoClientSettings.builder() - .clusterSettings( - ClusterSettings.builder() - .mode(hosts.size() == 1 ? ClusterConnectionMode.SINGLE : ClusterConnectionMode.MULTIPLE) - .hosts(hosts) - .build() - ) - .credentialList(ImmutableList.of(MongoCredential.createCredential(databaseUsername, databaseName, databasePassword))) - .codecRegistry(codecRegistry) - .build(); - - return MongoClients.create(settings).getDatabase(databaseName); - } - - private static FindIterable createFindIterable(String collection, Document query, Document sort) { - return APIv3.getMongo().getCollection(collection).find(query).sort(sort); - } - - public static void findOneAndTransform(String collection, Document query, Function transformation, SingleResultCallback callback) { - createFindIterable(collection, query, new Document()).first((result, error) -> { - if (error != null) { - callback.onResult(null, error); - } else if (result != null) { - try { - T transformed = transformation.apply(result); - callback.onResult(transformed, null); - } catch (Exception ex) { - callback.onResult(null, ex); - } - } else { - callback.onResult(null, null); - } - }); - } - - public static void findAndTransform(String collection, Document query, Function transformation, SingleResultCallback> callback) { - findAndTransform(collection, query, new Document(), transformation, callback); - } - - public static void findAndTransform(String collection, Document query, Document sort, Function transformation, SingleResultCallback> callback) { - createFindIterable(collection, query, sort).into(new ArrayList<>(), (result, error) -> { - if (error != null) { - callback.onResult(null, error); - } else if (!result.isEmpty()) { - try { - Collection transformed = Collections2.transform(result, transformation); - callback.onResult(transformed, null); - } catch (Exception ex) { - callback.onResult(null, ex); - } - } else { - callback.onResult(ImmutableList.of(), null); - } - }); - } - -} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/weirdStuff/ExcludeFromReplies.java b/src/main/java/net/frozenorb/apiv3/weirdStuff/ExcludeFromReplies.java new file mode 100644 index 0000000..a92cdda --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/weirdStuff/ExcludeFromReplies.java @@ -0,0 +1,12 @@ +package net.frozenorb.apiv3.weirdStuff; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface ExcludeFromReplies { + +} \ No newline at end of file diff --git a/src/main/java/net/frozenorb/apiv3/weirdStuff/ObjectIdTypeAdapter.java b/src/main/java/net/frozenorb/apiv3/weirdStuff/ObjectIdTypeAdapter.java new file mode 100644 index 0000000..8c6c547 --- /dev/null +++ b/src/main/java/net/frozenorb/apiv3/weirdStuff/ObjectIdTypeAdapter.java @@ -0,0 +1,20 @@ +package net.frozenorb.apiv3.weirdStuff; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import org.bson.types.ObjectId; + +import java.io.IOException; + +public final class ObjectIdTypeAdapter extends TypeAdapter { + + public void write(JsonWriter writer, ObjectId write) throws IOException { + writer.value(write.toString()); + } + + public ObjectId read(JsonReader reader) { + throw new IllegalArgumentException(); + } + +} \ No newline at end of file