essentially rewrite the entire api lol
This commit is contained in:
parent
15a8239afe
commit
e47e140bd3
25
pom.xml
25
pom.xml
@ -48,24 +48,29 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
<version>3.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<groupId>com.sparkjava</groupId>
|
||||
<artifactId>spark-core</artifactId>
|
||||
<version>2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>19.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.6.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-async</artifactId>
|
||||
<version>3.0.4</version>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>3.2.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb.morphia</groupId>
|
||||
<artifactId>morphia</artifactId>
|
||||
<version>1.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
@ -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<String, Rank> staffRanks = new HashMap<>();
|
||||
|
||||
for (Rank rank : APIv3.getDatastore().createQuery(Rank.class).asList()) {
|
||||
if (rank.isStaff()) {
|
||||
staffRanks.put(rank.getId(), rank);
|
||||
}
|
||||
}
|
||||
|
||||
Map<UUID, String> 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<Rank, Set<User>>
|
||||
return "idk";
|
||||
}, gson::toJson);
|
||||
|
||||
after((req, res) -> {
|
||||
res.header("content-type", "application/json");
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package net.frozenorb.apiv3;
|
||||
|
||||
import org.bson.Document;
|
||||
|
||||
public interface LiteFullJson {
|
||||
|
||||
Document toLiteJson();
|
||||
default Document toFullJson() {
|
||||
return toLiteJson();
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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<Collection<AuditLogEntry>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, AuditLogEntry::new, callback);
|
||||
}
|
||||
|
||||
public static void findByPerformer(UUID performer, SingleResultCallback<Collection<AuditLogEntry>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedBy", performer.toString()), TIME_BASED_SORT, AuditLogEntry::new, callback);
|
||||
}
|
||||
|
||||
public static void findByPerformerIp(String performerIp, SingleResultCallback<Collection<AuditLogEntry>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedFrom", performerIp), TIME_BASED_SORT, AuditLogEntry::new, callback);
|
||||
}
|
||||
|
||||
public static void findByActionType(String actionType, SingleResultCallback<Collection<AuditLogEntry>> 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<Void> callback) {
|
||||
log(user, userIp, actionType, callback);
|
||||
}
|
||||
|
||||
public static void log(UUID user, String userIp, String actionType, Document data, SingleResultCallback<Void> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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<Grant> callback) {
|
||||
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Grant::new, callback);
|
||||
}
|
||||
|
||||
public static void findByTarget(UUID target, SingleResultCallback<Collection<Grant>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), TIME_BASED_SORT, Grant::new, callback);
|
||||
}
|
||||
|
||||
public static void findByAddedBy(UUID addedBy, SingleResultCallback<Collection<Grant>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), TIME_BASED_SORT, Grant::new, callback);
|
||||
}
|
||||
|
||||
public static void createGrant(UUID target, Set<String> scopes, String rank, Instant expiresAt, UUID addedBy, String reason, SingleResultCallback<Void> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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<Collection<IPBan>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPBan::new, callback);
|
||||
}
|
||||
|
||||
public static void findById(String id, SingleResultCallback<IPBan> callback) {
|
||||
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPBan::new, callback);
|
||||
}
|
||||
|
||||
}
|
@ -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<Collection<IPLogEntry>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("user", user.toString()), TIME_BASED_SORT, IPLogEntry::new, callback);
|
||||
}
|
||||
|
||||
public static void findByIp(String ip, SingleResultCallback<Collection<IPLogEntry>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("ip", ip), TIME_BASED_SORT, IPLogEntry::new, callback);
|
||||
}
|
||||
|
||||
public static void log(UUID user, String ip , SingleResultCallback<Void> 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);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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<Collection<NotificationLogEntry>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), NotificationLogEntry::new, callback);
|
||||
}
|
||||
|
||||
public static void logEmail(UUID user, String title, String body, SingleResultCallback<Void> callback) {
|
||||
log(user, NotificationLogEntry.NotificationType.EMAIL, title, body, callback);
|
||||
}
|
||||
|
||||
public static void logSMS(UUID user, String title, String body, SingleResultCallback<Void> callback) {
|
||||
log(user, NotificationLogEntry.NotificationType.SMS, title, body, callback);
|
||||
}
|
||||
|
||||
private static void log(UUID user, NotificationLogEntry.NotificationType type, String title, String body, SingleResultCallback<Void> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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<Collection<NotificationTemplate>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationTemplate::new, callback);
|
||||
}
|
||||
|
||||
public static void findById(String id, SingleResultCallback<NotificationTemplate> 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<Void> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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<Punishment> callback) {
|
||||
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Punishment::new, callback);
|
||||
}
|
||||
|
||||
public static void findByTarget(UUID target, SingleResultCallback<Collection<Punishment>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), TIME_BASED_SORT, Punishment::new, callback);
|
||||
}
|
||||
|
||||
public static void findByAddedBy(UUID addedBy, SingleResultCallback<Collection<Grant>> 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<Void> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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<Collection<ServerGroup>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), ServerGroup::new, callback);
|
||||
}
|
||||
|
||||
public static void findById(String id, SingleResultCallback<ServerGroup> callback) {
|
||||
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), ServerGroup::new, callback);
|
||||
}
|
||||
|
||||
}
|
@ -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<Collection<Server>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Server::new, callback);
|
||||
}
|
||||
|
||||
public static void findById(String id, SingleResultCallback<Server> callback) {
|
||||
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Server::new, callback);
|
||||
}
|
||||
|
||||
public static void findByGroup(String groupId, SingleResultCallback<Collection<Server>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("group", groupId), Server::new, callback);
|
||||
}
|
||||
|
||||
}
|
@ -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<Collection<User>> callback) {
|
||||
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), User::new, callback);
|
||||
}
|
||||
|
||||
public static void findById(UUID id, SingleResultCallback<User> callback) {
|
||||
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id.toString()), User::new, callback);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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<Void> callback) {
|
||||
APIv3.getMongo().getCollection(collectionName).updateOne(new Document("_id", id), update, (updateResult, error) -> {
|
||||
callback.onResult(null, error);
|
||||
});
|
||||
}
|
||||
|
||||
protected void delete(SingleResultCallback<Void> callback) {
|
||||
APIv3.getMongo().getCollection(collectionName).deleteOne(new Document("_id", id), (deleteResult, error) -> {
|
||||
callback.onResult(null, error);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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<String> 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");
|
||||
public Grant(UUID target, String reason, Set<String> 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();
|
||||
}
|
||||
|
||||
setId(id);
|
||||
}
|
||||
|
||||
public void delete(UUID removedBy, String reason, SingleResultCallback<Void> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
|
||||
}
|
@ -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<Void> 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);
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
|
@ -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<Void> 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<Void> 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<String, Object> replacements) {
|
||||
|
@ -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");
|
||||
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;
|
||||
}
|
||||
|
||||
setId(id);
|
||||
}
|
||||
|
||||
public void delete(UUID removedBy, String reason, SingleResultCallback<Void> 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
|
||||
|
33
src/main/java/net/frozenorb/apiv3/model/Rank.java
Normal file
33
src/main/java/net/frozenorb/apiv3/model/Rank.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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<UUID> players;
|
||||
@Getter private Set<UUID> 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));
|
||||
public static Server byId(String id) {
|
||||
return APIv3.getDatastore().createQuery(Server.class).field("id").equal(id).get();
|
||||
}
|
||||
|
||||
setId(id);
|
||||
public Server() {} // For Morphia
|
||||
|
||||
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 toLiteJson() {
|
||||
Document json = new Document();
|
||||
|
||||
json.put("id", id);
|
||||
json.put("bungeeId", bungeeId);
|
||||
json.put("displayName", displayName);
|
||||
json.put("group", group);
|
||||
json.put("ip", ip);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -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<String> announcements;
|
||||
@Getter private Set<String> 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<String> announcements) {
|
||||
this.announcements = announcements;
|
||||
APIv3.getDatastore().save(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return json;
|
||||
public void setChatFilterList(Set<String> chatFilterList) {
|
||||
this.chatFilterList = chatFilterList;
|
||||
APIv3.getDatastore().save(this);
|
||||
}
|
||||
|
||||
}
|
@ -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<String, Instant> aliases;
|
||||
@ExcludeFromReplies @Getter private Map<String, Date> 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<String, Instant>) 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());
|
||||
}
|
||||
|
||||
}
|
@ -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<Instant> {
|
||||
|
||||
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<Instant> getEncoderClass() {
|
||||
return Instant.class;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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}");
|
||||
});
|
||||
*/
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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<? extends LiteFullJson> 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<? extends LiteFullJson> 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();
|
||||
}
|
||||
|
||||
}
|
@ -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<ServerAddress> 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<Document> createFindIterable(String collection, Document query, Document sort) {
|
||||
return APIv3.getMongo().getCollection(collection).find(query).sort(sort);
|
||||
}
|
||||
|
||||
public static <T> void findOneAndTransform(String collection, Document query, Function<Document, T> transformation, SingleResultCallback<T> 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 <T> void findAndTransform(String collection, Document query, Function<Document, T> transformation, SingleResultCallback<Collection<T>> callback) {
|
||||
findAndTransform(collection, query, new Document(), transformation, callback);
|
||||
}
|
||||
|
||||
public static <T> void findAndTransform(String collection, Document query, Document sort, Function<Document, T> transformation, SingleResultCallback<Collection<T>> callback) {
|
||||
createFindIterable(collection, query, sort).into(new ArrayList<>(), (result, error) -> {
|
||||
if (error != null) {
|
||||
callback.onResult(null, error);
|
||||
} else if (!result.isEmpty()) {
|
||||
try {
|
||||
Collection<T> transformed = Collections2.transform(result, transformation);
|
||||
callback.onResult(transformed, null);
|
||||
} catch (Exception ex) {
|
||||
callback.onResult(null, ex);
|
||||
}
|
||||
} else {
|
||||
callback.onResult(ImmutableList.of(), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
|
||||
}
|
@ -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<ObjectId> {
|
||||
|
||||
public void write(JsonWriter writer, ObjectId write) throws IOException {
|
||||
writer.value(write.toString());
|
||||
}
|
||||
|
||||
public ObjectId read(JsonReader reader) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user