Add proper chat filter. Finishes #41

This commit is contained in:
Colin McDonald 2016-07-14 20:40:48 -04:00
parent ad0140d29f
commit fba48cf223
9 changed files with 184 additions and 17 deletions

View File

@ -61,7 +61,10 @@ import net.frozenorb.apiv3.route.bannedCellCarriers.DELETEBannedCellCarriersId;
import net.frozenorb.apiv3.route.bannedCellCarriers.GETBannedCellCarriers;
import net.frozenorb.apiv3.route.bannedCellCarriers.GETBannedCellCarriersId;
import net.frozenorb.apiv3.route.bannedCellCarriers.POSTBannedCellCarriers;
import net.frozenorb.apiv3.route.chatFilterList.GETChatFilter;
import net.frozenorb.apiv3.route.chatFilter.DELETEChatFilterId;
import net.frozenorb.apiv3.route.chatFilter.GETChatFilter;
import net.frozenorb.apiv3.route.chatFilter.GETChatFilterId;
import net.frozenorb.apiv3.route.chatFilter.POSTChatFilter;
import net.frozenorb.apiv3.route.disposableLoginTokens.POSTDisposableLoginTokens;
import net.frozenorb.apiv3.route.disposableLoginTokens.POSTDisposableLoginTokensIdUse;
import net.frozenorb.apiv3.route.emailTokens.GETEmailTokensIdOwner;
@ -306,7 +309,11 @@ public final class APIv3 extends AbstractVerticle {
//http.put("/bannedCellCarriers/:bannedCellCarrier").blockingHandler(new PUTBannedCellCarriersId(), false);
http.delete("/bannedCellCarriers/:bannedCellCarrier").blockingHandler(new DELETEBannedCellCarriersId(), false);
http.get("/chatFilter/:chatFilterEntryId").handler(new GETChatFilterId());
http.get("/chatFilter").handler(new GETChatFilter());
http.post("/chatFilter").blockingHandler(new POSTChatFilter(), false);
//http.put("/chatFilter/:chatFilterEntryId").blockingHandler(new PUTChatFilterId(), false);
http.delete("/chatFilter/:chatFilterEntryId").blockingHandler(new DELETEChatFilterId(), false);
http.post("/disposableLoginTokens").blockingHandler(new POSTDisposableLoginTokens(), false);
http.post("/disposableLoginTokens/:disposableLoginToken/use").blockingHandler(new POSTDisposableLoginTokensIdUse(), false);

View File

@ -31,6 +31,9 @@ public enum AuditLogActionType {
NOTIFICATION_TEMPLATE_CREATE(false),
NOTIFICATION_TEMPLATE_UPDATE(false),
NOTIFICATION_TEMPLATE_DELETE(false),
CHAT_FILTER_ENTRY_CREATE(false),
CHAT_FILTER_ENTRY_UPDATE(false),
CHAT_FILTER_ENTRY_DELETE(false),
PUNISHMENT_CREATE(true) {
@Override

View File

@ -0,0 +1,47 @@
package net.frozenorb.apiv3.model;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoCollection;
import fr.javatic.mongo.jacksonCodec.Entity;
import fr.javatic.mongo.jacksonCodec.objectId.Id;
import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.unsorted.MongoToVoidMongoCallback;
import net.frozenorb.apiv3.util.SyncUtils;
import org.bson.Document;
import java.util.LinkedList;
import java.util.List;
@Entity
public final class ChatFilterEntry {
private static final MongoCollection<ChatFilterEntry> chatFilterCollection = APIv3.getDatabase().getCollection("chatFilter", ChatFilterEntry.class);
@Getter @Id private String id;
@Getter private String regex;
public static void findById(String id, SingleResultCallback<ChatFilterEntry> callback) {
chatFilterCollection.find(new Document("_id", id)).first(SyncUtils.vertxWrap(callback));
}
public static void findAll(SingleResultCallback<List<ChatFilterEntry>> callback) {
chatFilterCollection.find().into(new LinkedList<>(), SyncUtils.vertxWrap(callback));
}
private ChatFilterEntry() {} // For Jackson
public ChatFilterEntry(String id, String regex) {
this.id = id;
this.regex = regex;
}
public void insert(SingleResultCallback<Void> callback) {
chatFilterCollection.insertOne(this, SyncUtils.vertxWrap(callback));
}
public void delete(SingleResultCallback<Void> callback) {
chatFilterCollection.deleteOne(new Document("_id", id), SyncUtils.vertxWrap(new MongoToVoidMongoCallback<>(callback)));
}
}

View File

@ -0,0 +1,42 @@
package net.frozenorb.apiv3.route.chatFilter;
import com.google.common.collect.ImmutableMap;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.auditLog.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.ChatFilterEntry;
import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils;
public final class DELETEChatFilterId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
ChatFilterEntry chatFilterEntry = SyncUtils.runBlocking(v -> ChatFilterEntry.findById(ctx.request().getParam("chatFilterEntryId"), v));
if (chatFilterEntry == null) {
ErrorUtils.respondNotFound(ctx, "Chat filter entry", ctx.request().getParam("chatFilterEntryId"));
return;
}
SyncUtils.<Void>runBlocking(v -> chatFilterEntry.delete(v));
JsonObject requestBody = ctx.getBodyAsJson();
if (requestBody.containsKey("removedBy")) {
AuditLog.log(UuidUtils.parseUuid(requestBody.getString("removedBy")), requestBody.getString("removedByIp"), ctx, AuditLogActionType.CHAT_FILTER_ENTRY_DELETE, ImmutableMap.of("chatFilterEntryId", chatFilterEntry.getId()), (ignored, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
} else {
APIv3.respondJson(ctx, 200, chatFilterEntry);
}
});
} else {
APIv3.respondJson(ctx, 200, chatFilterEntry);
}
}
}

View File

@ -0,0 +1,21 @@
package net.frozenorb.apiv3.route.chatFilter;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.ChatFilterEntry;
import net.frozenorb.apiv3.util.ErrorUtils;
public final class GETChatFilter implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
ChatFilterEntry.findAll((chatFilterEntries, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
} else {
APIv3.respondJson(ctx, 200, chatFilterEntries);
}
});
}
}

View File

@ -0,0 +1,21 @@
package net.frozenorb.apiv3.route.chatFilter;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.ChatFilterEntry;
import net.frozenorb.apiv3.util.ErrorUtils;
public final class GETChatFilterId implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
ChatFilterEntry.findById(ctx.request().getParam("chatFilterEntryId"), (notificationTemplate, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
} else {
APIv3.respondJson(ctx, 200, notificationTemplate);
}
});
}
}

View File

@ -0,0 +1,38 @@
package net.frozenorb.apiv3.route.chatFilter;
import com.google.common.collect.ImmutableMap;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.auditLog.AuditLog;
import net.frozenorb.apiv3.auditLog.AuditLogActionType;
import net.frozenorb.apiv3.model.ChatFilterEntry;
import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.SyncUtils;
import net.frozenorb.apiv3.util.UuidUtils;
public final class POSTChatFilter implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
JsonObject requestBody = ctx.getBodyAsJson();
String id = requestBody.getString("id");
String regex = requestBody.getString("regex");
ChatFilterEntry chatFilterEntry = new ChatFilterEntry(id, regex);
SyncUtils.<Void>runBlocking(v -> chatFilterEntry.insert(v));
if (requestBody.containsKey("addedBy")) {
AuditLog.log(UuidUtils.parseUuid(requestBody.getString("addedBy")), requestBody.getString("addedByIp"), ctx, AuditLogActionType.CHAT_FILTER_ENTRY_CREATE, ImmutableMap.of("chatFilterEntryId", id), (ignored, error) -> {
if (error != null) {
ErrorUtils.respondInternalError(ctx, error);
} else {
APIv3.respondJson(ctx, 200, chatFilterEntry);
}
});
} else {
APIv3.respondJson(ctx, 200, chatFilterEntry);
}
}
}

View File

@ -0,0 +1,4 @@
package net.frozenorb.apiv3.route.chatFilter;
public class PUTChatFilterId {
}

View File

@ -1,16 +0,0 @@
package net.frozenorb.apiv3.route.chatFilterList;
import com.google.common.collect.ImmutableList;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import net.frozenorb.apiv3.APIv3;
public final class GETChatFilter implements Handler<RoutingContext> {
public void handle(RoutingContext ctx) {
// TODO
// WARNING: THIS IS REGISTERED ASYNC SO DONT MESS IT UP
APIv3.respondJson(ctx, 200, ImmutableList.of());
}
}