Cleanup
All checks were successful
Deploy API / deploy (ubuntu-latest, 2.44.0) (push) Successful in 54s
All checks were successful
Deploy API / deploy (ubuntu-latest, 2.44.0) (push) Successful in 54s
This commit is contained in:
parent
4bb5554a71
commit
2d5890eacb
@ -1,10 +1,13 @@
|
|||||||
package me.braydon.tether.model.user;
|
package me.braydon.tether.model.user;
|
||||||
|
|
||||||
|
import kong.unirest.core.json.JSONArray;
|
||||||
import kong.unirest.core.json.JSONObject;
|
import kong.unirest.core.json.JSONObject;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A linked connection to a {@link DiscordUser}.
|
* A linked connection to a {@link DiscordUser}.
|
||||||
@ -39,25 +42,33 @@ public class ConnectedAccount {
|
|||||||
private final boolean verified;
|
private final boolean verified;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a connected account for a user.
|
* Construct the connected accounts for a user.
|
||||||
*
|
*
|
||||||
* @param accountJson the connected account json
|
* @param userJson the user's json
|
||||||
* @return the constructed account
|
* @return the constructed accounts
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
protected static ConnectedAccount fromJson(@NonNull JSONObject accountJson) {
|
public static Set<ConnectedAccount> fromJson(@NonNull JSONObject userJson) {
|
||||||
String id = accountJson.getString("id");
|
Set<ConnectedAccount> connectedAccounts = new HashSet<>();
|
||||||
String type = accountJson.getString("type");
|
if (userJson.has("connected_accounts")) {
|
||||||
String name = accountJson.getString("name");
|
JSONArray accountsArray = userJson.getJSONArray("connected_accounts");
|
||||||
|
for (int i = 0; i < accountsArray.length(); i++) {
|
||||||
|
JSONObject accountJson = accountsArray.getJSONObject(i);
|
||||||
|
String id = accountJson.getString("id");
|
||||||
|
String type = accountJson.getString("type");
|
||||||
|
String name = accountJson.getString("name");
|
||||||
|
|
||||||
Map<String, String> metadata = new HashMap<>();
|
Map<String, String> metadata = new HashMap<>();
|
||||||
if (accountJson.has("metadata")) {
|
if (accountJson.has("metadata")) {
|
||||||
for (Map.Entry<String, Object> entry : accountJson.getJSONObject("metadata").toMap().entrySet()) {
|
for (Map.Entry<String, Object> entry : accountJson.getJSONObject("metadata").toMap().entrySet()) {
|
||||||
metadata.put(entry.getKey(), entry.getValue().toString());
|
metadata.put(entry.getKey(), entry.getValue().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean verified = accountJson.getBoolean("verified");
|
||||||
|
connectedAccounts.add(new ConnectedAccount(id, type, name, metadata, verified));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return connectedAccounts;
|
||||||
boolean verified = accountJson.getBoolean("verified");
|
|
||||||
return new ConnectedAccount(id, type, name, metadata, verified);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package me.braydon.tether.model.user;
|
||||||
|
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import net.dv8tion.jda.api.entities.Activity;
|
||||||
|
import net.dv8tion.jda.api.entities.emoji.EmojiUnion;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The custom status of a {@link DiscordUser}.
|
||||||
|
*
|
||||||
|
* @author Braydon
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor(access = AccessLevel.PRIVATE) @Getter
|
||||||
|
public class CustomStatus {
|
||||||
|
/**
|
||||||
|
* The value of this status.
|
||||||
|
*/
|
||||||
|
@NonNull private final String value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The unicode emoji for this status.
|
||||||
|
*/
|
||||||
|
private final String emoji;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a custom status for a user.
|
||||||
|
*
|
||||||
|
* @param activities the user's activities
|
||||||
|
* @return the constructed status, null if none
|
||||||
|
*/
|
||||||
|
protected static CustomStatus fromActivities(@NonNull List<Activity> activities) {
|
||||||
|
for (Activity activity : activities) {
|
||||||
|
if (activity.getType() != Activity.ActivityType.CUSTOM_STATUS) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
EmojiUnion emoji = activity.getEmoji();
|
||||||
|
return new CustomStatus(activity.getName(), emoji == null ? null : emoji.asUnicode().getFormatted());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
package me.braydon.tether.model.user;
|
package me.braydon.tether.model.user;
|
||||||
|
|
||||||
import kong.unirest.core.json.JSONArray;
|
|
||||||
import kong.unirest.core.json.JSONObject;
|
import kong.unirest.core.json.JSONObject;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
import me.braydon.tether.common.DiscordUtils;
|
import me.braydon.tether.common.DiscordUtils;
|
||||||
@ -14,7 +13,10 @@ import net.dv8tion.jda.api.entities.Activity;
|
|||||||
import net.dv8tion.jda.api.entities.ClientType;
|
import net.dv8tion.jda.api.entities.ClientType;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Collections;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A model of a Discord user.
|
* A model of a Discord user.
|
||||||
@ -37,6 +39,11 @@ public final class DiscordUser {
|
|||||||
*/
|
*/
|
||||||
@NonNull private final String username;
|
@NonNull private final String username;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This user's legacy username, if any.
|
||||||
|
*/
|
||||||
|
private final String legacyUsername;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The display name of this user, if any.
|
* The display name of this user, if any.
|
||||||
*/
|
*/
|
||||||
@ -72,6 +79,11 @@ public final class DiscordUser {
|
|||||||
*/
|
*/
|
||||||
private final String bannerColor;
|
private final String bannerColor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The custom status of this user, if any.
|
||||||
|
*/
|
||||||
|
private final CustomStatus customStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The user's bio, if any.
|
* The user's bio, if any.
|
||||||
*/
|
*/
|
||||||
@ -132,16 +144,6 @@ public final class DiscordUser {
|
|||||||
*/
|
*/
|
||||||
private final boolean bot;
|
private final boolean bot;
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether this user is a legacy user.
|
|
||||||
* <p>
|
|
||||||
* A user is "legacy" if they haven't yet
|
|
||||||
* moved to the new username system and got
|
|
||||||
* rid of their discriminator.
|
|
||||||
* </p>
|
|
||||||
*/
|
|
||||||
private final boolean legacy;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The unix time of when this user joined Discord.
|
* The unix time of when this user joined Discord.
|
||||||
*/
|
*/
|
||||||
@ -160,17 +162,11 @@ public final class DiscordUser {
|
|||||||
JSONObject detailsJson = userJson.has("user") ? userJson.getJSONObject("user") : userJson;
|
JSONObject detailsJson = userJson.has("user") ? userJson.getJSONObject("user") : userJson;
|
||||||
|
|
||||||
long snowflake = Long.parseLong(detailsJson.getString("id"));
|
long snowflake = Long.parseLong(detailsJson.getString("id"));
|
||||||
String username = detailsJson.getString("username");
|
String legacyUsername = userJson.optString("legacy_username", null);
|
||||||
String displayName = detailsJson.optString("global_name", null);
|
|
||||||
int discriminator = Integer.parseInt(detailsJson.getString("discriminator"));
|
int discriminator = Integer.parseInt(detailsJson.getString("discriminator"));
|
||||||
boolean isUserLegacy = discriminator > 0;
|
|
||||||
UserFlags flags = UserFlags.fromJson(detailsJson);
|
|
||||||
|
|
||||||
Avatar avatar = Avatar.fromJson(snowflake, discriminator, isUserLegacy, detailsJson);
|
|
||||||
AvatarDecoration avatarDecoration = detailsJson.isNull("avatar_decoration_data") ? null
|
AvatarDecoration avatarDecoration = detailsJson.isNull("avatar_decoration_data") ? null
|
||||||
: AvatarDecoration.fromJson(detailsJson.getJSONObject("avatar_decoration_data"));
|
: AvatarDecoration.fromJson(detailsJson.getJSONObject("avatar_decoration_data"));
|
||||||
Banner banner = Banner.fromJson(snowflake, detailsJson);
|
|
||||||
String bannerColor = detailsJson.optString("banner_color", null);
|
|
||||||
|
|
||||||
String bio = detailsJson.optString("bio", null);
|
String bio = detailsJson.optString("bio", null);
|
||||||
if (bio != null && (bio = bio.trim()).isEmpty()) {
|
if (bio != null && (bio = bio.trim()).isEmpty()) {
|
||||||
@ -191,50 +187,23 @@ public final class DiscordUser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean bot = detailsJson.optBoolean("bot", false);
|
|
||||||
long created = DiscordUtils.getTimeCreated(snowflake);
|
|
||||||
|
|
||||||
// Get the user's online status
|
// Get the user's online status
|
||||||
OnlineStatus onlineStatus = member == null ? OnlineStatus.OFFLINE : member.getOnlineStatus();
|
OnlineStatus onlineStatus = member == null ? OnlineStatus.OFFLINE : member.getOnlineStatus();
|
||||||
if (onlineStatus == OnlineStatus.UNKNOWN) {
|
if (onlineStatus == OnlineStatus.UNKNOWN) {
|
||||||
onlineStatus = OnlineStatus.OFFLINE;
|
onlineStatus = OnlineStatus.OFFLINE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the user's active clients and activities
|
// Get the user's activities
|
||||||
EnumSet<ClientType> activeClients = member == null ? EnumSet.noneOf(ClientType.class) : member.getActiveClients();
|
|
||||||
List<Activity> activities = member == null ? Collections.emptyList() : member.getActivities();
|
List<Activity> activities = member == null ? Collections.emptyList() : member.getActivities();
|
||||||
SpotifyActivity spotify = null;
|
|
||||||
for (Activity activity : activities) {
|
|
||||||
if (!activity.getName().equals("Spotify") || !activity.isRich()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
spotify = SpotifyActivity.fromActivity(Objects.requireNonNull(activity.asRichPresence()));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the user's badges
|
|
||||||
Set<UserBadge> badges = new HashSet<>();
|
|
||||||
if (userJson.has("badges")) {
|
|
||||||
JSONArray badgesArray = userJson.getJSONArray("badges");
|
|
||||||
for (int i = 0; i < badgesArray.length(); i++) {
|
|
||||||
badges.add(UserBadge.fromJson(badgesArray.getJSONObject(i)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the user's connected accounts
|
|
||||||
Set<ConnectedAccount> connectedAccounts = new HashSet<>();
|
|
||||||
if (userJson.has("connected_accounts")) {
|
|
||||||
JSONArray accountsArray = userJson.getJSONArray("connected_accounts");
|
|
||||||
for (int i = 0; i < accountsArray.length(); i++) {
|
|
||||||
connectedAccounts.add(ConnectedAccount.fromJson(accountsArray.getJSONObject(i)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Finally return the constructed user
|
// Finally return the constructed user
|
||||||
return new DiscordUser(
|
return new DiscordUser(
|
||||||
snowflake, username, displayName, discriminator, flags, avatar, avatarDecoration, banner, bannerColor, bio,
|
snowflake, detailsJson.getString("username"), legacyUsername, detailsJson.optString("global_name", null),
|
||||||
pronouns, accentColor, onlineStatus, activeClients, activities, spotify, badges, connectedAccounts, clan,
|
discriminator, UserFlags.fromJson(detailsJson), Avatar.fromJson(snowflake, discriminator, legacyUsername != null, detailsJson), avatarDecoration,
|
||||||
nitroSubscription, bot, isUserLegacy, created
|
Banner.fromJson(snowflake, detailsJson), detailsJson.optString("banner_color", null), CustomStatus.fromActivities(activities),
|
||||||
|
bio, pronouns, accentColor, onlineStatus, member == null ? EnumSet.noneOf(ClientType.class) : member.getActiveClients(), activities,
|
||||||
|
SpotifyActivity.fromActivities(activities), UserBadge.fromJson(userJson), ConnectedAccount.fromJson(userJson), clan, nitroSubscription,
|
||||||
|
detailsJson.optBoolean("bot", false), DiscordUtils.getTimeCreated(snowflake)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,8 +4,10 @@ import lombok.AccessLevel;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
import net.dv8tion.jda.api.entities.Activity;
|
||||||
import net.dv8tion.jda.api.entities.RichPresence;
|
import net.dv8tion.jda.api.entities.RichPresence;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,24 +71,31 @@ public class SpotifyActivity {
|
|||||||
/**
|
/**
|
||||||
* Construct a Spotify activity for a user.
|
* Construct a Spotify activity for a user.
|
||||||
*
|
*
|
||||||
* @param richPresence the raw Discord data
|
* @param activities the user's activities
|
||||||
* @return the constructed activity
|
* @return the constructed activity, null if none
|
||||||
*/
|
*/
|
||||||
@NonNull @SuppressWarnings("DataFlowIssue")
|
@SuppressWarnings("DataFlowIssue")
|
||||||
protected static SpotifyActivity fromActivity(@NonNull RichPresence richPresence) {
|
protected static SpotifyActivity fromActivities(@NonNull List<Activity> activities) {
|
||||||
String trackUrl = "https://open.spotify.com/track/" + richPresence.getSyncId();
|
for (Activity activity : activities) {
|
||||||
|
if (!activity.getName().equals("Spotify") || !activity.isRich()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
RichPresence richPresence = activity.asRichPresence();
|
||||||
|
String trackUrl = "https://open.spotify.com/track/" + richPresence.getSyncId();
|
||||||
|
|
||||||
// Track progress
|
// Track progress
|
||||||
long started = Objects.requireNonNull(richPresence.getTimestamps()).getStart();
|
long started = Objects.requireNonNull(richPresence.getTimestamps()).getStart();
|
||||||
long ends = richPresence.getTimestamps().getEnd();
|
long ends = richPresence.getTimestamps().getEnd();
|
||||||
|
|
||||||
long trackLength = ends - started;
|
long trackLength = ends - started;
|
||||||
long trackProgress = Math.min(System.currentTimeMillis() - started, trackLength);
|
long trackProgress = Math.min(System.currentTimeMillis() - started, trackLength);
|
||||||
|
|
||||||
return new SpotifyActivity(
|
return new SpotifyActivity(
|
||||||
richPresence.getSyncId(), richPresence.getDetails(), richPresence.getState().replace(";", ","),
|
richPresence.getSyncId(), richPresence.getDetails(), richPresence.getState().replace(";", ","),
|
||||||
richPresence.getLargeImage().getText(), richPresence.getLargeImage().getUrl(), trackUrl, trackProgress,
|
richPresence.getLargeImage().getText(), richPresence.getLargeImage().getUrl(), trackUrl, trackProgress,
|
||||||
trackLength, started, ends
|
trackLength, started, ends
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,13 @@
|
|||||||
package me.braydon.tether.model.user.badge;
|
package me.braydon.tether.model.user.badge;
|
||||||
|
|
||||||
|
import kong.unirest.core.json.JSONArray;
|
||||||
import kong.unirest.core.json.JSONObject;
|
import kong.unirest.core.json.JSONObject;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
import me.braydon.tether.model.user.DiscordUser;
|
import me.braydon.tether.model.user.DiscordUser;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link DiscordUser}'s avatar.
|
* A {@link DiscordUser}'s avatar.
|
||||||
*
|
*
|
||||||
@ -32,17 +36,26 @@ public class UserBadge {
|
|||||||
private final String link;
|
private final String link;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a badge for a user.
|
* Construct the badges for a user.
|
||||||
*
|
*
|
||||||
* @param badgeJson the badge json
|
* @param userJson the user's json
|
||||||
* @return the constructed user badge
|
* @return the constructed badges
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public static UserBadge fromJson(@NonNull JSONObject badgeJson) {
|
public static Set<UserBadge> fromJson(@NonNull JSONObject userJson) {
|
||||||
String id = badgeJson.getString("id");
|
Set<UserBadge> badges = new HashSet<>();
|
||||||
String description = badgeJson.getString("description");
|
if (!userJson.has("badges")) {
|
||||||
UserBadgeIcon icon = UserBadgeIcon.fromJson(badgeJson);
|
return badges;
|
||||||
String link = badgeJson.optString("link", null);
|
}
|
||||||
return new UserBadge(id, description, icon, link);
|
JSONArray badgesArray = userJson.getJSONArray("badges");
|
||||||
|
for (int i = 0; i < badgesArray.length(); i++) {
|
||||||
|
JSONObject badgeJson = badgesArray.getJSONObject(i);
|
||||||
|
String id = badgeJson.getString("id");
|
||||||
|
String description = badgeJson.getString("description");
|
||||||
|
UserBadgeIcon icon = UserBadgeIcon.fromJson(badgeJson);
|
||||||
|
String link = badgeJson.optString("link", null);
|
||||||
|
badges.add(new UserBadge(id, description, icon, link));
|
||||||
|
}
|
||||||
|
return badges;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user