Add profile theme and profile effects to the DiscordUser model
All checks were successful
Deploy API / deploy (ubuntu-latest, 2.44.0) (push) Successful in 1m12s

This commit is contained in:
Braydon 2024-09-10 18:09:35 -04:00
parent 3516fbc273
commit e8bfc88e1e
4 changed files with 99 additions and 6 deletions

@ -4,7 +4,10 @@ import kong.unirest.core.json.JSONArray;
import kong.unirest.core.json.JSONObject;
import lombok.*;
import java.util.*;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
* A linked connection to a {@link DiscordUser}.

@ -79,6 +79,16 @@ public final class DiscordUser {
*/
private final String bannerColor;
/**
* The profile theme of this user, if any.
*/
private final ProfileTheme profileTheme;
/**
* The profile effect this user has, if any.
*/
private final ProfileEffect profileEffect;
/**
* The custom status of this user, if any.
*/
@ -199,11 +209,12 @@ public final class DiscordUser {
// Finally return the constructed user
return new DiscordUser(
snowflake, detailsJson.getString("username"), legacyUsername, detailsJson.optString("global_name", null),
discriminator, UserFlags.fromJson(detailsJson), Avatar.fromJson(snowflake, discriminator, legacyUsername != null, detailsJson), avatarDecoration,
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)
discriminator, UserFlags.fromJson(detailsJson), Avatar.fromJson(snowflake, discriminator, legacyUsername != null, detailsJson),
avatarDecoration, Banner.fromJson(snowflake, detailsJson), detailsJson.optString("banner_color", null),
ProfileTheme.fromJson(profileJson), ProfileEffect.fromJson(profileJson), 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)
);
}
}

@ -0,0 +1,39 @@
package me.braydon.tether.model.user;
import kong.unirest.core.json.JSONObject;
import lombok.*;
/**
* A profile effect for a {@link DiscordUser}.
*
* @author Braydon
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE) @Getter @EqualsAndHashCode
public class ProfileEffect {
/**
* The id of the user's profile effect.
*/
@NonNull private final String id;
/**
* The unix time of when this profile effect expires, null if permanent.
*/
private final Long expires;
/**
* Construct a profile effect for a user.
*
* @param userProfileJson the user profile json
* @return the constructed profile effect, null if none
*/
protected static ProfileEffect fromJson(JSONObject userProfileJson) {
JSONObject effectJson;
if (userProfileJson == null || ((effectJson = userProfileJson.isNull("profile_effect") ? null
: userProfileJson.getJSONObject("profile_effect")) == null)) {
return null;
}
String id = effectJson.getString("id");
long expires = effectJson.optLong("expires_at", -1L);
return new ProfileEffect(id, expires == -1L ? null : expires);
}
}

@ -0,0 +1,40 @@
package me.braydon.tether.model.user;
import kong.unirest.core.json.JSONArray;
import kong.unirest.core.json.JSONObject;
import lombok.*;
/**
* The theme of a {@link DiscordUser}'s profile.
*
* @author Braydon
*/
@AllArgsConstructor(access = AccessLevel.PRIVATE) @Getter @EqualsAndHashCode
public class ProfileTheme {
/**
* The primary (hex) color of this theme.
*/
@NonNull private final String primary;
/**
* The accent (hex) color of this theme.
*/
@NonNull private final String accent;
/**
* Construct a profile theme for a user.
*
* @param userProfileJson the user profile json
* @return the constructed profile theme, null if none
*/
protected static ProfileTheme fromJson(JSONObject userProfileJson) {
JSONArray themeJson;
if (userProfileJson == null || ((themeJson = userProfileJson.isNull("theme_colors") ? null
: userProfileJson.getJSONArray("theme_colors")) == null)) {
return null;
}
String primaryHex = String.format("#%06X", (0xFFFFFF & themeJson.getInt(0)));
String accentHex = String.format("#%06X", (0xFFFFFF & themeJson.getInt(1)));
return new ProfileTheme(primaryHex, accentHex);
}
}