cleanup
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m42s
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m42s
This commit is contained in:
123
src/main/java/cc/fascinated/bat/common/model/BatUser.java
Normal file
123
src/main/java/cc/fascinated/bat/common/model/BatUser.java
Normal file
@ -0,0 +1,123 @@
|
||||
package cc.fascinated.bat.common.model;
|
||||
|
||||
import cc.fascinated.bat.BatApplication;
|
||||
import cc.fascinated.bat.common.ProfileHolder;
|
||||
import cc.fascinated.bat.common.Serializable;
|
||||
import cc.fascinated.bat.common.UserUtils;
|
||||
import cc.fascinated.bat.namehistory.profile.user.NameHistoryProfile;
|
||||
import cc.fascinated.bat.service.DiscordService;
|
||||
import cc.fascinated.bat.service.MongoService;
|
||||
import com.mongodb.client.model.ReplaceOptions;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
public class BatUser extends ProfileHolder {
|
||||
/**
|
||||
* The document that belongs to this user
|
||||
*/
|
||||
private final org.bson.Document document;
|
||||
|
||||
/**
|
||||
* The ID of the user
|
||||
*/
|
||||
@NonNull
|
||||
@Id
|
||||
private final String id;
|
||||
|
||||
/**
|
||||
* The global name of the user
|
||||
*/
|
||||
private String globalName;
|
||||
|
||||
/**
|
||||
* The time this user was created
|
||||
*/
|
||||
private Date createdAt;
|
||||
|
||||
/**
|
||||
* The discord user associated with this user
|
||||
*/
|
||||
private User user;
|
||||
|
||||
public BatUser(@NonNull String id, User user, @NonNull org.bson.Document document) {
|
||||
this.id = id;
|
||||
this.document = document;
|
||||
boolean newAccount = this.document.isEmpty();
|
||||
this.createdAt = newAccount ? new Date() : document.getDate("createdAt");
|
||||
|
||||
// User was not passed through
|
||||
if (user == null) {
|
||||
user = UserUtils.getUser(id);
|
||||
}
|
||||
this.user = user;
|
||||
this.globalName = user.getGlobalName();
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of the user
|
||||
*/
|
||||
public String getName() {
|
||||
return this.getGlobalName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the guild as the JDA Guild
|
||||
*
|
||||
* @return the guild
|
||||
*/
|
||||
public User getDiscordUser() {
|
||||
if (user == null) {
|
||||
user = DiscordService.JDA.getUserById(id);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user's name history profile
|
||||
*
|
||||
* @return the user's name history profile
|
||||
*/
|
||||
public NameHistoryProfile getNameHistoryProfile() {
|
||||
return getProfile(NameHistoryProfile.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the user
|
||||
*/
|
||||
public void save() {
|
||||
org.bson.Document document = new org.bson.Document();
|
||||
document.put("_id", id);
|
||||
document.put("createdAt", createdAt);
|
||||
|
||||
Map<String, org.bson.Document> profileDocuments = this.document.get("profiles", new HashMap<>());
|
||||
for (Serializable profile : getProfiles().values()) {
|
||||
profileDocuments.put(profile.getClass().getSimpleName(), profile.serialize(BatApplication.GSON));
|
||||
}
|
||||
document.put("profiles", profileDocuments);
|
||||
|
||||
MongoService.INSTANCE.getUsersCollection().replaceOne(
|
||||
new org.bson.Document("_id", id),
|
||||
document,
|
||||
new ReplaceOptions().upsert(true)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Serializable> T getProfile(Class<T> clazz) {
|
||||
return getProfileFromDocument(clazz, document);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user