package net.frozenorb.apiv3.models; import com.mongodb.async.SingleResultCallback; import com.mongodb.async.client.MongoCollection; import com.mongodb.client.result.DeleteResult; import fr.javatic.mongo.jacksonCodec.objectId.Id; import lombok.AllArgsConstructor; import lombok.Getter; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.actors.Actor; import net.frozenorb.apiv3.actors.ActorType; import net.frozenorb.apiv3.unsorted.BlockingCallback; import net.frozenorb.apiv3.utils.SyncUtils; import net.frozenorb.apiv3.utils.TimeUtils; import org.bson.Document; import org.bson.types.ObjectId; import java.util.*; @AllArgsConstructor public final class Punishment { private static final MongoCollection punishmentsCollection = APIv3.getDatabase().getCollection("punishments", Punishment.class); @Getter @Id private String id; @Getter private UUID user; @Getter private String reason; @Getter private PunishmentType type; @Getter private Date expiresAt; @Getter private Map metadata; @Getter private UUID addedBy; @Getter private Date addedAt; @Getter private String actorName; @Getter private ActorType actorType; @Getter private UUID removedBy; @Getter private Date removedAt; @Getter private String removalReason; public static List findAllSync() { return SyncUtils.blockMulti(punishmentsCollection.find()); } public static List findAllPaginatedSync(int skip, int pageSize) { return SyncUtils.blockMulti(punishmentsCollection.find().sort(new Document("addedAt", 1)).skip(skip).limit(pageSize)); } public static List findByTypeSync(Iterable types) { return SyncUtils.blockMulti(punishmentsCollection.find(new Document("type", new Document("$in", types)))); } public static Punishment findByIdSync(String id) { return SyncUtils.blockOne(punishmentsCollection.find(new Document("_id", id))); } public static List findByUserSync(User user) { return findByUserSync(user.getId()); } public static List findByUserSync(UUID user) { return SyncUtils.blockMulti(punishmentsCollection.find(new Document("user", user))); } public static List findByUserAndTypeSync(User user, Iterable types) { return findByUserAndTypeSync(user.getId(), types); } public static List findByUserAndTypeSync(UUID user, Iterable types) { return SyncUtils.blockMulti(punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", types)))); } public static void findAll(SingleResultCallback> callback) { punishmentsCollection.find().into(new ArrayList<>(), callback); } public static void findAllPaginated(int skip, int pageSize, SingleResultCallback> callback) { punishmentsCollection.find().sort(new Document("addedAt", 1)).skip(skip).limit(pageSize).into(new ArrayList<>(), callback); } public static void findByType(Iterable types, SingleResultCallback> callback) { punishmentsCollection.find(new Document("type", new Document("$in", types))).into(new ArrayList<>(), callback); } public static void findById(String id, SingleResultCallback callback) { punishmentsCollection.find(new Document("_id", id)).first(callback); } public static void findByUser(User user, SingleResultCallback> callback) { findByUser(user.getId(), callback); } public static void findByUser(UUID user, SingleResultCallback> callback) { punishmentsCollection.find(new Document("user", user)).into(new ArrayList<>(), callback); } public static void findByUserGrouped(Iterable users, SingleResultCallback>> callback) { punishmentsCollection.find(new Document("user", new Document("$in", users))).into(new ArrayList<>(), (punishments, error) -> { if (error != null) { callback.onResult(null, error); } else { Map> 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 types, SingleResultCallback> callback) { findByUserAndType(user.getId(), types, callback); } public static void findByUserAndType(UUID user, Iterable types, SingleResultCallback> callback) { punishmentsCollection.find(new Document("user", user).append("type", new Document("$in", types))).into(new ArrayList<>(), callback); } public Punishment() {} // For Morphia public Punishment(User user, String reason, PunishmentType type, Date expiresAt, User addedBy, Actor actor, Map metadata) { this.id = new ObjectId().toString(); this.user = user.getId(); this.reason = reason; this.type = type; this.expiresAt = expiresAt; this.addedBy = addedBy == null ? null : addedBy.getId(); this.addedAt = new Date(); this.actorName = actor.getName(); this.actorType = actor.getType(); this.metadata = metadata; } public boolean isActive() { return !(isExpired() || isRemoved()); } public boolean isExpired() { if (expiresAt == null) { return false; // Never expires } else { return expiresAt.before(new Date()); } } public boolean isRemoved() { return removedBy != null; } 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; } } public void insert() { BlockingCallback 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 callback = new BlockingCallback<>(); punishmentsCollection.deleteOne(new Document("_id", id), callback); callback.get(); } public enum PunishmentType { BLACKLIST, BAN, MUTE, WARN } }