APIv3/src/main/java/net/frozenorb/apiv3/model/Punishment.java

116 lines
3.2 KiB
Java
Raw Normal View History

2016-02-12 02:40:06 +01:00
package net.frozenorb.apiv3.model;
2016-03-06 05:21:49 +01:00
import com.mongodb.async.SingleResultCallback;
2016-02-12 02:40:06 +01:00
import lombok.Getter;
import lombok.ToString;
2016-02-12 06:31:57 +01:00
import net.frozenorb.apiv3.accessor.Punishments;
2016-02-12 02:40:06 +01:00
import org.bson.Document;
2016-02-12 21:12:05 +01:00
import java.time.Instant;
import java.util.UUID;
2016-02-12 02:40:06 +01:00
@ToString
2016-02-12 06:31:57 +01:00
public final class Punishment extends BaseModel {
2016-02-12 02:40:06 +01:00
@Getter private String id;
2016-02-12 21:12:05 +01:00
@Getter private UUID target;
@Getter private String reason;
@Getter private PunishmentType type;
@Getter private Instant expiresAt;
@Getter private UUID addedBy;
@Getter private Instant addedAt;
@Getter private String addedOn;
@Getter private UUID removedBy;
@Getter private Instant removedAt;
@Getter private String removalReason;
2016-02-12 02:40:06 +01:00
public Punishment(Document json) {
2016-02-12 06:31:57 +01:00
super(Punishments.COLLECTION_NAME);
2016-02-12 02:40:06 +01:00
this.id = json.getString("_id");
2016-02-12 21:12:05 +01:00
this.target = UUID.fromString(json.getString("target"));
this.reason = json.getString("reason");
this.type = PunishmentType.valueOf(json.getString("type"));
this.expiresAt = (Instant) json.get("expiresAt");
this.addedBy = UUID.fromString(json.getString("addedBy"));
this.addedAt = (Instant) json.get("addedAt");
this.addedOn = json.getString("addedOn");
if (json.containsKey("removedBy")) {
this.removedBy = UUID.fromString(json.getString("removedBy"));
this.removedAt = (Instant) json.get("removedAt");
this.removalReason = json.getString("removalReason");
}
2016-02-12 06:31:57 +01:00
setId(id);
2016-02-12 02:40:06 +01:00
}
2016-03-06 05:21:49 +01:00
public void delete(UUID removedBy, String reason, SingleResultCallback<Void> callback) {
2016-02-12 21:12:05 +01:00
this.removedBy = removedBy;
this.removedAt = Instant.now();
this.removalReason = reason;
Document set = new Document();
set.put("removedBy", removedBy.toString());
set.put("removedAt", removedAt);
set.put("removalReason", removalReason);
2016-03-06 05:21:49 +01:00
super.update(new Document("$set", set), callback);
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 {
return expiresAt.isAfter(Instant.now());
}
}
public boolean isRemoved() {
return removedBy != null;
}
2016-02-12 02:40:06 +01:00
public Document toLiteJson() {
Document json = new Document();
2016-02-12 21:12:05 +01:00
json.put("id", id);
json.put("target", target.toString());
json.put("reason", reason);
json.put("type", type.name());
json.put("expiresAt", expiresAt == null ? null : expiresAt.toString());
2016-02-12 02:40:06 +01:00
2016-02-12 21:12:05 +01:00
json.put("addedBy", addedBy.toString());
json.put("addedAt", addedAt.toString());
2016-02-12 02:40:06 +01:00
2016-02-12 21:12:05 +01:00
Document statusJson = new Document();
2016-02-12 02:40:06 +01:00
2016-02-12 21:12:05 +01:00
statusJson.put("active", isActive());
statusJson.put("expired", isExpired());
statusJson.put("removed", isRemoved());
2016-02-12 02:40:06 +01:00
2016-02-12 21:12:05 +01:00
json.put("status", statusJson);
if (removedBy != null) {
json.put("removedBy", removedBy.toString());
json.put("removedAt", removedAt.toString());
json.put("removalReason", removalReason);
}
2016-02-12 02:40:06 +01:00
return json;
}
2016-02-12 21:12:05 +01:00
public enum PunishmentType {
BAN, MUTE, WARN
}
2016-02-12 02:40:06 +01:00
}