diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index e5b5801..4fc5a7b 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -13,7 +13,6 @@ import net.frozenorb.apiv3.accessor.Users; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.JsonUtils; import net.frozenorb.apiv3.util.MongoUtils; -import org.bson.Document; import java.util.UUID; @@ -21,6 +20,9 @@ public final class APIv3 extends AbstractVerticle { @Getter private static MongoDatabase mongo; + // 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 void start() { Router coreHttpRouter = Router.router(vertx); mongo = MongoUtils.initializeConnection(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)), "minehqapi", "test", "test".toCharArray()); @@ -94,7 +96,7 @@ public final class APIv3 extends AbstractVerticle { coreHttpRouter.get("/doAction/:actionType").handler(ctx -> { String actionType = ctx.request().getParam("actionType"); - AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document()); + //AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document()); ctx.response().end("{'logged': true}"); }); diff --git a/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java b/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java index 0eaedac..ad9c968 100644 --- a/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java +++ b/src/main/java/net/frozenorb/apiv3/accessor/AuditLog.java @@ -34,8 +34,8 @@ public class AuditLog { MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), TIME_BASED_SORT, AuditLogEntry::new, callback); } - public static void findByEntryId(String entryId, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", entryId), 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) { diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Grants.java b/src/main/java/net/frozenorb/apiv3/accessor/Grants.java index e3642d8..07f391e 100644 --- a/src/main/java/net/frozenorb/apiv3/accessor/Grants.java +++ b/src/main/java/net/frozenorb/apiv3/accessor/Grants.java @@ -10,7 +10,6 @@ import org.bson.types.ObjectId; import java.time.Instant; import java.util.Collection; -import java.util.Date; import java.util.Set; import java.util.UUID; @@ -20,12 +19,8 @@ public class Grants { public static final String COLLECTION_NAME = "grant"; private static final Document TIME_BASED_SORT = new Document("addedAt", -1); - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, Grant::new, callback); - } - public static void findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), TIME_BASED_SORT, Grant::new, callback); + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Grant::new, callback); } public static void findByTarget(UUID target, SingleResultCallback> callback) { diff --git a/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java b/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java index 223ce8d..fc55019 100644 --- a/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java +++ b/src/main/java/net/frozenorb/apiv3/accessor/IPLog.java @@ -2,24 +2,53 @@ 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.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.Date; +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 findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPLogEntry::new, callback); + 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 findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), 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 index 725ef8c..b107701 100644 --- a/src/main/java/net/frozenorb/apiv3/accessor/NotificationLog.java +++ b/src/main/java/net/frozenorb/apiv3/accessor/NotificationLog.java @@ -2,23 +2,44 @@ 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 findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationLogEntry::new, callback); + public static void findByTarget(UUID target, SingleResultCallback> callback) { + MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), NotificationLogEntry::new, callback); } - public static void findById(String id, SingleResultCallback callback) { - MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), 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/EmailTemplates.java b/src/main/java/net/frozenorb/apiv3/accessor/NotificationTemplates.java similarity index 62% rename from src/main/java/net/frozenorb/apiv3/accessor/EmailTemplates.java rename to src/main/java/net/frozenorb/apiv3/accessor/NotificationTemplates.java index 108cdef..0bfdf40 100644 --- a/src/main/java/net/frozenorb/apiv3/accessor/EmailTemplates.java +++ b/src/main/java/net/frozenorb/apiv3/accessor/NotificationTemplates.java @@ -3,7 +3,7 @@ package net.frozenorb.apiv3.accessor; import com.mongodb.async.SingleResultCallback; import lombok.experimental.UtilityClass; import net.frozenorb.apiv3.APIv3; -import net.frozenorb.apiv3.model.EmailTemplate; +import net.frozenorb.apiv3.model.NotificationTemplate; import net.frozenorb.apiv3.util.MongoUtils; import org.bson.Document; @@ -12,19 +12,19 @@ import java.util.Collection; import java.util.UUID; @UtilityClass -public class EmailTemplates { +public class NotificationTemplates { - public static final String COLLECTION_NAME = "emailTemplate"; + public static final String COLLECTION_NAME = "notificationTemplate"; - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), EmailTemplate::new, callback); + 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), EmailTemplate::new, callback); + public static void findById(String id, SingleResultCallback callback) { + MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), NotificationTemplate::new, callback); } - public static void createTemplate(String id, String title, String body, UUID creator, SingleResultCallback callback) { + public static void createNotificationTemplate(String id, String title, String body, UUID creator, SingleResultCallback callback) { Document insert = new Document(); insert.put("_id", id); diff --git a/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java b/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java index e6bb35d..d15118a 100644 --- a/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java +++ b/src/main/java/net/frozenorb/apiv3/accessor/Punishments.java @@ -2,23 +2,50 @@ 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.Set; +import java.util.UUID; @UtilityClass public class Punishments { public static final String COLLECTION_NAME = "punishment"; - - public static void findAll(SingleResultCallback> callback) { - MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Punishment::new, callback); - } + 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/model/AuditLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java index 88aa73d..84b02ce 100644 --- a/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/AuditLogEntry.java @@ -39,13 +39,6 @@ public final class AuditLogEntry extends BaseModel { json.put("performedAt", performedAt.toString()); json.put("performedFrom", performedFrom); json.put("actionType", actionType); - - return json; - } - - public Document toFullJson() { - Document json = toLiteJson(); - json.put("actionData", actionData); return json; diff --git a/src/main/java/net/frozenorb/apiv3/model/BaseModel.java b/src/main/java/net/frozenorb/apiv3/model/BaseModel.java index ce224a8..eac7770 100644 --- a/src/main/java/net/frozenorb/apiv3/model/BaseModel.java +++ b/src/main/java/net/frozenorb/apiv3/model/BaseModel.java @@ -17,12 +17,16 @@ public abstract class BaseModel implements LiteFullJson { this.collectionName = collectionName; } - protected void update(Document update, SingleResultCallback callback) { - APIv3.getMongo().getCollection(collectionName).updateOne(new Document("_id", id), update, callback); + 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), callback); + 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 b4a7f57..7b0b741 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Grant.java +++ b/src/main/java/net/frozenorb/apiv3/model/Grant.java @@ -2,7 +2,6 @@ package net.frozenorb.apiv3.model; import com.google.common.collect.ImmutableSet; import com.mongodb.async.SingleResultCallback; -import com.mongodb.client.result.DeleteResult; import com.mongodb.client.result.UpdateResult; import lombok.Getter; import lombok.ToString; @@ -53,7 +52,7 @@ public final class Grant extends BaseModel { setId(id); } - public void delete(UUID removedBy, String reason, SingleResultCallback callback) { + public void delete(UUID removedBy, String reason, SingleResultCallback callback) { this.removedBy = removedBy; this.removedAt = Instant.now(); this.removalReason = reason; diff --git a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java index 7a4fa9a..a5e60e2 100644 --- a/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/IPLogEntry.java @@ -1,9 +1,10 @@ package net.frozenorb.apiv3.model; +import com.mongodb.async.SingleResultCallback; +import com.mongodb.client.result.UpdateResult; import lombok.Getter; import lombok.ToString; import net.frozenorb.apiv3.accessor.IPLog; -import net.frozenorb.apiv3.util.IPUtils; import org.bson.Document; import java.time.Instant; @@ -32,7 +33,7 @@ public final class IPLogEntry extends BaseModel { setId(id); } - public void used() { + public void used(SingleResultCallback callback) { this.lastSeen = Instant.now(); this.uses++; @@ -41,7 +42,9 @@ public final class IPLogEntry extends BaseModel { set.put("lastSeen", lastSeen); set.put("uses", uses); - update(new Document("$set", set), (result, error) -> {}); + super.update(new Document("$set", set), (updateResult, error) -> { + callback.onResult(null, error); + }); } public Document toLiteJson() { @@ -49,7 +52,7 @@ public final class IPLogEntry extends BaseModel { json.put("id", id); json.put("user", user.toString()); - json.put("ip", IPUtils.machineToHuman(ip)); + json.put("ip", ip); json.put("firstSeen", firstSeen.toString()); json.put("lastSeen", lastSeen.toString()); json.put("uses", uses); diff --git a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java index 13d696d..e81be2c 100644 --- a/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationLogEntry.java @@ -46,7 +46,7 @@ public final class NotificationLogEntry extends BaseModel { public enum NotificationType { - EMAIL, TEXT + EMAIL, SMS } diff --git a/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java b/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java similarity index 81% rename from src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java rename to src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java index ccf26c6..304c62b 100644 --- a/src/main/java/net/frozenorb/apiv3/model/EmailTemplate.java +++ b/src/main/java/net/frozenorb/apiv3/model/NotificationTemplate.java @@ -1,11 +1,9 @@ package net.frozenorb.apiv3.model; import com.mongodb.async.SingleResultCallback; -import com.mongodb.client.result.DeleteResult; -import com.mongodb.client.result.UpdateResult; import lombok.Getter; import lombok.ToString; -import net.frozenorb.apiv3.accessor.EmailTemplates; +import net.frozenorb.apiv3.accessor.NotificationTemplates; import org.bson.Document; import java.time.Instant; @@ -13,7 +11,7 @@ import java.util.Map; import java.util.UUID; @ToString -public final class EmailTemplate extends BaseModel { +public final class NotificationTemplate extends BaseModel { @Getter private String id; @Getter private String title; @@ -21,8 +19,8 @@ public final class EmailTemplate extends BaseModel { @Getter private Instant lastUpdatedAt; @Getter private UUID lastUpdatedBy; - public EmailTemplate(Document json) { - super(EmailTemplates.COLLECTION_NAME); + public NotificationTemplate(Document json) { + super(NotificationTemplates.COLLECTION_NAME); this.id = json.getString("_id"); this.title = json.getString("title"); @@ -45,7 +43,7 @@ public final class EmailTemplate extends BaseModel { return json; } - public void update(String title, String body, UUID updatedBy, SingleResultCallback callback) { + public void update(String title, String body, UUID updatedBy, SingleResultCallback callback) { this.title = title; this.body = body; this.lastUpdatedAt = Instant.now(); @@ -58,10 +56,12 @@ public final class EmailTemplate extends BaseModel { set.put("lastUpdatedAt", lastUpdatedAt.toString()); set.put("lastUpdatedBy", lastUpdatedBy.toString()); - super.update(new Document("$set", set), callback); + super.update(new Document("$set", set), (updateResult, error) -> { + callback.onResult(null, error); + }); } - public void delete(SingleResultCallback callback) { + public void delete(SingleResultCallback callback) { super.delete(callback); } diff --git a/src/main/java/net/frozenorb/apiv3/model/Punishment.java b/src/main/java/net/frozenorb/apiv3/model/Punishment.java index cf589b9..98b6cd1 100644 --- a/src/main/java/net/frozenorb/apiv3/model/Punishment.java +++ b/src/main/java/net/frozenorb/apiv3/model/Punishment.java @@ -1,5 +1,6 @@ package net.frozenorb.apiv3.model; +import com.mongodb.async.SingleResultCallback; import lombok.Getter; import lombok.ToString; import net.frozenorb.apiv3.accessor.Punishments; @@ -47,7 +48,7 @@ public final class Punishment extends BaseModel { setId(id); } - public void delete(UUID removedBy, String reason) { + public void delete(UUID removedBy, String reason, SingleResultCallback callback) { this.removedBy = removedBy; this.removedAt = Instant.now(); this.removalReason = reason; @@ -58,7 +59,7 @@ public final class Punishment extends BaseModel { set.put("removedAt", removedAt); set.put("removalReason", removalReason); - super.update(new Document("$set", set), (result, error) -> {}); + super.update(new Document("$set", set), callback); } public boolean isActive() { diff --git a/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java b/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java index bf8b5b4..bbb9c96 100644 --- a/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java +++ b/src/main/java/net/frozenorb/apiv3/util/MongoUtils.java @@ -61,12 +61,8 @@ public class MongoUtils { return APIv3.getMongo().getCollection(collection).find(query).sort(sort); } - public static void findOneAndTransform(String collection, Document query, Function transformation, SingleResultCallback callback) { - findOneAndTransform(collection, query, new Document(), transformation, callback); - } - - public static void findOneAndTransform(String collection, Document query, Document sort, Function transformation, SingleResultCallback callback) { - createFindIterable(collection, query, sort).first((result, error) -> { + 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) {