APIv3/src/main/java/net/frozenorb/apiv3/models/Punishment.java
2016-04-08 07:12:31 -04:00

75 lines
1.9 KiB
Java

package net.frozenorb.apiv3.models;
import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import java.util.Date;
import java.util.UUID;
@Entity(value = "punishments", noClassnameStored = true)
public final class Punishment {
@Id private ObjectId id;
@Getter private UUID target;
@Getter private String reason;
@Getter private PunishmentType type;
@Getter private Date expiresAt;
@Getter private UUID addedBy;
@Getter private Date addedAt;
@Getter private String addedOn;
@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(new ObjectId(id)).get();
}
public Punishment() {} // For Morphia
public Punishment(User target, String reason, PunishmentType type, Date expiresAt, User addedBy, Server addedOn) {
this.target = target.getId();
this.reason = reason;
this.type = type;
this.expiresAt = expiresAt;
this.addedBy = addedBy.getId();
this.addedAt = new Date();
this.addedOn = addedOn.getId();
}
public void delete(UUID removedBy, String reason) {
this.removedBy = removedBy;
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.after(new Date());
}
}
public boolean isRemoved() {
return removedBy != null;
}
public enum PunishmentType {
BAN, MUTE, WARN
}
}