2016-04-08 13:12:31 +02:00
|
|
|
package net.frozenorb.apiv3.models;
|
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-03-21 23:28:17 +01:00
|
|
|
import org.bson.types.ObjectId;
|
|
|
|
import org.mongodb.morphia.annotations.Entity;
|
|
|
|
import org.mongodb.morphia.annotations.Id;
|
2016-05-03 01:34:30 +02:00
|
|
|
import org.mongodb.morphia.annotations.Indexed;
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
import java.util.Date;
|
2016-02-12 21:12:05 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
@Entity(value = "punishments", noClassnameStored = true)
|
|
|
|
public final class Punishment {
|
2016-02-12 02:40:06 +01:00
|
|
|
|
2016-05-02 23:35:05 +02:00
|
|
|
@Getter @Id private String id;
|
2016-05-03 01:34:30 +02:00
|
|
|
@Getter @Indexed private UUID target;
|
2016-02-12 21:12:05 +01:00
|
|
|
@Getter private String reason;
|
2016-05-03 01:34:30 +02:00
|
|
|
@Getter @Indexed private PunishmentType type; // Type is indexed for the rank dump
|
2016-03-21 23:28:17 +01:00
|
|
|
@Getter private Date expiresAt;
|
2016-02-12 21:12:05 +01:00
|
|
|
|
|
|
|
@Getter private UUID addedBy;
|
2016-05-03 01:34:30 +02:00
|
|
|
@Getter @Indexed 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-04-08 13:12:31 +02:00
|
|
|
public static Punishment byId(String id) {
|
2016-05-02 23:35:05 +02:00
|
|
|
return APIv3.getDatastore().createQuery(Punishment.class).field("id").equal(id).get();
|
2016-04-08 13:12:31 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
public Punishment() {} // For Morphia
|
2016-02-12 06:31:57 +01:00
|
|
|
|
2016-05-05 22:00:32 +02:00
|
|
|
public Punishment(User target, String reason, PunishmentType type, Date expiresAt, User addedBy, Actor actor) {
|
2016-05-02 23:35:05 +02:00
|
|
|
this.id = new ObjectId().toString();
|
2016-04-08 13:12:31 +02:00
|
|
|
this.target = target.getId();
|
2016-03-21 23:28:17 +01:00
|
|
|
this.reason = reason;
|
|
|
|
this.type = type;
|
2016-05-01 02:08:58 +02:00
|
|
|
this.expiresAt = (Date) expiresAt.clone();
|
2016-04-08 13:12:31 +02:00
|
|
|
this.addedBy = 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-02-12 02:40:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-17 21:23:02 +02:00
|
|
|
public void delete(User removedBy, String reason) {
|
|
|
|
this.removedBy = removedBy.getId();
|
2016-03-21 23:28:17 +01:00
|
|
|
this.removedAt = new Date();
|
2016-02-12 21:12:05 +01:00
|
|
|
this.removalReason = reason;
|
|
|
|
|
2016-03-21 23:28:17 +01:00
|
|
|
APIv3.getDatastore().save(this);
|
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-03-21 23:28:17 +01:00
|
|
|
return expiresAt.after(new Date());
|
2016-02-12 21:12:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isRemoved() {
|
|
|
|
return removedBy != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|