2016-04-08 13:12:31 +02:00
|
|
|
package net.frozenorb.apiv3.models;
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
import com.mongodb.async.SingleResultCallback;
|
2016-06-10 04:39:23 +02:00
|
|
|
import com.mongodb.async.client.MongoCollection;
|
|
|
|
import com.mongodb.client.result.DeleteResult;
|
2016-06-12 22:36:11 +02:00
|
|
|
import fr.javatic.mongo.jacksonCodec.objectId.Id;
|
2016-05-14 05:23:46 +02:00
|
|
|
import lombok.AllArgsConstructor;
|
2016-02-12 02:40:06 +01:00
|
|
|
import lombok.Getter;
|
2016-03-21 23:28:17 +01:00
|
|
|
import net.frozenorb.apiv3.APIv3;
|
2016-05-05 22:00:32 +02:00
|
|
|
import net.frozenorb.apiv3.actors.Actor;
|
|
|
|
import net.frozenorb.apiv3.actors.ActorType;
|
2016-06-10 04:39:23 +02:00
|
|
|
import net.frozenorb.apiv3.unsorted.BlockingCallback;
|
|
|
|
import net.frozenorb.apiv3.utils.SyncUtils;
|
2016-05-07 03:57:32 +02:00
|
|
|
import net.frozenorb.apiv3.utils.TimeUtils;
|
2016-06-10 04:39:23 +02:00
|
|
|
import org.bson.Document;
|
2016-03-21 23:28:17 +01:00
|
|
|
import org.bson.types.ObjectId;
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-05-29 08:44:10 +02:00
|
|
|
import java.util.*;
|
2016-02-12 21:12:05 +01:00
|
|
|
|
2016-05-14 05:23:46 +02:00
|
|
|
@AllArgsConstructor
|
2016-03-21 23:28:17 +01:00
|
|
|
public final class Punishment {
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
private static final MongoCollection<Punishment> punishmentsCollection = APIv3.getDatabase().getCollection("punishments", Punishment.class);
|
|
|
|
|
2016-05-02 23:35:05 +02:00
|
|
|
@Getter @Id private String id;
|
2016-06-10 04:39:23 +02:00
|
|
|
@Getter private UUID user;
|
2016-02-12 21:12:05 +01:00
|
|
|
@Getter private String reason;
|
2016-06-10 04:39:23 +02:00
|
|
|
@Getter private PunishmentType type;
|
2016-03-21 23:28:17 +01:00
|
|
|
@Getter private Date expiresAt;
|
2016-05-11 00:37:07 +02:00
|
|
|
@Getter private Map<String, Object> metadata;
|
2016-02-12 21:12:05 +01:00
|
|
|
|
|
|
|
@Getter private UUID addedBy;
|
2016-06-10 04:39:23 +02:00
|
|
|
@Getter private Date addedAt;
|
2016-05-05 22:00:32 +02:00
|
|
|
@Getter private String actorName;
|
|
|
|
@Getter private ActorType actorType;
|
2016-02-12 21:12:05 +01:00
|
|
|
|
|
|
|
@Getter private UUID removedBy;
|
2016-03-21 23:28:17 +01:00
|
|
|
@Getter private Date removedAt;
|
2016-02-12 21:12:05 +01:00
|
|
|
@Getter private String removalReason;
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
public static List<Punishment> findAllSync() {
|
2016-06-10 04:39:23 +02:00
|
|
|
return SyncUtils.blockMulti(punishmentsCollection.find());
|
2016-04-08 13:12:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
public static List<Punishment> findAllPaginatedSync(int skip, int pageSize) {
|
|
|
|
return SyncUtils.blockMulti(punishmentsCollection.find().sort(new Document("addedAt", 1)).skip(skip).limit(pageSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<Punishment> findByTypeSync(Iterable<PunishmentType> types) {
|
2016-06-10 04:39:23 +02:00
|
|
|
return SyncUtils.blockMulti(punishmentsCollection.find(new Document("type", new Document("$in", types))));
|
2016-05-29 08:44:10 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
public static Punishment findByIdSync(String id) {
|
2016-06-10 04:39:23 +02:00
|
|
|
return SyncUtils.blockOne(punishmentsCollection.find(new Document("_id", id)));
|
|
|
|
}
|
2016-05-29 08:44:10 +02:00
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
public static List<Punishment> findByUserSync(User user) {
|
|
|
|
return findByUserSync(user.getId());
|
2016-06-10 04:39:23 +02:00
|
|
|
}
|
2016-05-29 08:44:10 +02:00
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
public static List<Punishment> findByUserSync(UUID user) {
|
2016-06-10 04:39:23 +02:00
|
|
|
return SyncUtils.blockMulti(punishmentsCollection.find(new Document("user", user)));
|
|
|
|
}
|
2016-05-29 08:44:10 +02:00
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
public static List<Punishment> findByUserAndTypeSync(User user, Iterable<PunishmentType> types) {
|
|
|
|
return findByUserAndTypeSync(user.getId(), types);
|
2016-06-10 04:39:23 +02:00
|
|
|
}
|
2016-05-29 08:44:10 +02:00
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
public static List<Punishment> findByUserAndTypeSync(UUID user, Iterable<PunishmentType> types) {
|
2016-06-10 04:39:23 +02:00
|
|
|
return SyncUtils.blockMulti(punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", types))));
|
2016-05-29 08:44:10 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 22:36:11 +02:00
|
|
|
public static void findAll(SingleResultCallback<List<Punishment>> callback) {
|
|
|
|
punishmentsCollection.find().into(new ArrayList<>(), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void findAllPaginated(int skip, int pageSize, SingleResultCallback<List<Punishment>> callback) {
|
|
|
|
punishmentsCollection.find().sort(new Document("addedAt", 1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void findByType(Iterable<PunishmentType> types, SingleResultCallback<List<Punishment>> callback) {
|
|
|
|
punishmentsCollection.find(new Document("type", new Document("$in", types))).into(new ArrayList<>(), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void findById(String id, SingleResultCallback<Punishment> callback) {
|
|
|
|
punishmentsCollection.find(new Document("_id", id)).first(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void findByUser(User user, SingleResultCallback<List<Punishment>> callback) {
|
|
|
|
findByUser(user.getId(), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void findByUser(UUID user, SingleResultCallback<List<Punishment>> callback) {
|
|
|
|
punishmentsCollection.find(new Document("user", user)).into(new ArrayList<>(), callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void findByUserGrouped(Iterable<UUID> users, SingleResultCallback<Map<UUID, List<Punishment>>> callback) {
|
|
|
|
punishmentsCollection.find(new Document("user", new Document("$in", users))).into(new ArrayList<>(), (punishments, error) -> {
|
|
|
|
if (error != null) {
|
|
|
|
callback.onResult(null, error);
|
|
|
|
} else {
|
|
|
|
Map<UUID, List<Punishment>> result = new HashMap<>();
|
|
|
|
|
|
|
|
for (UUID user : users) {
|
|
|
|
result.put(user, new ArrayList<>());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Punishment punishment : punishments) {
|
|
|
|
result.get(punishment.getUser()).add(punishment);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.onResult(result, null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void findByUserAndType(User user, Iterable<PunishmentType> types, SingleResultCallback<List<Punishment>> callback) {
|
|
|
|
findByUserAndType(user.getId(), types, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void findByUserAndType(UUID user, Iterable<PunishmentType> types, SingleResultCallback<List<Punishment>> callback) {
|
|
|
|
punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", types))).into(new ArrayList<>(), callback);
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
public Punishment() {} // For Morphia
|
2016-02-12 06:31:57 +01:00
|
|
|
|
2016-05-11 00:37:07 +02:00
|
|
|
public Punishment(User user, String reason, PunishmentType type, Date expiresAt, User addedBy, Actor actor, Map<String, Object> metadata) {
|
2016-05-02 23:35:05 +02:00
|
|
|
this.id = new ObjectId().toString();
|
2016-05-10 18:30:21 +02:00
|
|
|
this.user = user.getId();
|
2016-03-21 23:28:17 +01:00
|
|
|
this.reason = reason;
|
|
|
|
this.type = type;
|
2016-05-06 01:35:45 +02:00
|
|
|
this.expiresAt = expiresAt;
|
|
|
|
this.addedBy = addedBy == null ? null : addedBy.getId();
|
2016-03-21 23:28:17 +01:00
|
|
|
this.addedAt = new Date();
|
2016-05-05 22:00:32 +02:00
|
|
|
this.actorName = actor.getName();
|
|
|
|
this.actorType = actor.getType();
|
2016-05-11 00:37:07 +02:00
|
|
|
this.metadata = metadata;
|
2016-02-12 02:40:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-12 21:12:05 +01:00
|
|
|
public boolean isActive() {
|
|
|
|
return !(isExpired() || isRemoved());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isExpired() {
|
|
|
|
if (expiresAt == null) {
|
|
|
|
return false; // Never expires
|
|
|
|
} else {
|
2016-05-06 01:35:45 +02:00
|
|
|
return expiresAt.before(new Date());
|
2016-02-12 21:12:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isRemoved() {
|
|
|
|
return removedBy != null;
|
|
|
|
}
|
|
|
|
|
2016-05-07 03:57:32 +02:00
|
|
|
public String getAccessDenialReason() {
|
|
|
|
switch (type) {
|
|
|
|
case BLACKLIST:
|
|
|
|
return "Your account has been blacklisted from the MineHQ Network. \n\nThis type of punishment cannot be appealed.";
|
|
|
|
case BAN:
|
|
|
|
String accessDenialReason = "Your account has been suspended from the MineHQ Network. \n\n";
|
|
|
|
|
|
|
|
if (getExpiresAt() != null) {
|
|
|
|
accessDenialReason += "Expires in " + TimeUtils.formatIntoDetailedString(TimeUtils.getSecondsBetween(getExpiresAt(), new Date()));
|
|
|
|
} else {
|
|
|
|
accessDenialReason += "Appeal at MineHQ.com/appeal";
|
|
|
|
}
|
|
|
|
|
|
|
|
return accessDenialReason;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 04:39:23 +02:00
|
|
|
public void insert() {
|
|
|
|
BlockingCallback<Void> callback = new BlockingCallback<>();
|
|
|
|
punishmentsCollection.insertOne(this, callback);
|
|
|
|
callback.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void delete(User removedBy, String reason) {
|
|
|
|
this.removedBy = removedBy.getId();
|
|
|
|
this.removedAt = new Date();
|
|
|
|
this.removalReason = reason;
|
|
|
|
|
|
|
|
BlockingCallback<DeleteResult> callback = new BlockingCallback<>();
|
|
|
|
punishmentsCollection.deleteOne(new Document("_id", id), callback);
|
|
|
|
callback.get();
|
|
|
|
}
|
|
|
|
|
2016-02-12 21:12:05 +01:00
|
|
|
public enum PunishmentType {
|
|
|
|
|
2016-04-23 03:04:15 +02:00
|
|
|
BLACKLIST, BAN, MUTE, WARN
|
2016-02-12 21:12:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-12 02:40:06 +01:00
|
|
|
}
|