APIv3/src/main/java/net/frozenorb/apiv3/models/Punishment.java
Colin McDonald d062ab5718 More stuff
2016-05-08 23:18:55 -04:00

104 lines
3.2 KiB
Java

package net.frozenorb.apiv3.models;
import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.actors.Actor;
import net.frozenorb.apiv3.actors.ActorType;
import net.frozenorb.apiv3.utils.TimeUtils;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Indexed;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
@Entity(value = "punishments", noClassnameStored = true)
public final class Punishment {
@Getter @Id private String id;
@Getter @Indexed private UUID target;
@Getter private String reason;
@Getter @Indexed private PunishmentType type; // Type is indexed for the rank dump
@Getter private Date expiresAt;
@Getter private Map<String, Object> meta;
@Getter private UUID addedBy;
@Getter @Indexed 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 Punishment byId(String id) {
return APIv3.getDatastore().createQuery(Punishment.class).field("id").equal(id).get();
}
public Punishment() {} // For Morphia
public Punishment(User target, String reason, PunishmentType type, Date expiresAt, User addedBy, Actor actor, Map<String, Object> meta) {
this.id = new ObjectId().toString();
this.target = target.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.meta = meta;
}
public void delete(User removedBy, String reason) {
this.removedBy = removedBy.getId();
this.removedAt = new Date();
this.removalReason = reason;
APIv3.getDatastore().save(this);
}
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 enum PunishmentType {
BLACKLIST, BAN, MUTE, WARN
}
}