From 1cea5d415ec8deca9bc0b1a5454acc5de080742e Mon Sep 17 00:00:00 2001 From: Khakers <22665282+khakers@users.noreply.github.com> Date: Sun, 4 Jun 2023 22:03:06 -0700 Subject: [PATCH] fix: add support for migrated discord usernames --- .../modmailviewer/util/DiscordUtils.java | 78 +++++++++++++++++++ src/main/jte/macros/navbar.jte | 3 +- src/main/jte/macros/username.jte | 13 +++- src/main/jte/pages/LogEntryView.jte | 9 ++- src/main/jte/pages/homepage.jte | 9 ++- 5 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/github/khakers/modmailviewer/util/DiscordUtils.java diff --git a/src/main/java/com/github/khakers/modmailviewer/util/DiscordUtils.java b/src/main/java/com/github/khakers/modmailviewer/util/DiscordUtils.java new file mode 100644 index 00000000..5b0f3477 --- /dev/null +++ b/src/main/java/com/github/khakers/modmailviewer/util/DiscordUtils.java @@ -0,0 +1,78 @@ +package com.github.khakers.modmailviewer.util; + +import com.github.khakers.modmailviewer.auth.UserToken; +import com.github.khakers.modmailviewer.data.User; + +public class DiscordUtils { + /** + * Returns true if the user is a migrated user (i.e. has no discriminator) + * + * @return True if the user is a migrated user + */ + public static boolean isMigratedUserName(UserToken user) { + return user.getDiscriminator().equals("0") || user.getDiscriminator().isBlank(); + } + + public static boolean isLegacyUsername(UserToken user) { + return !(user.getDiscriminator().equals("0") || user.getDiscriminator().isBlank()); + + } + + /** + * Returns true if the user is a migrated user (i.e. has no discriminator) + * + * @param user The user to check + * @return True if the user is a migrated user + */ + public static boolean isMigratedUserName(User user) { + return user.discriminator().equals("0") || user.discriminator().isBlank(); + } + + public static boolean isLegacyUsername(User user) { + return !(user.discriminator().equals("0") || user.discriminator().isBlank()); + } + + /** + * @return The discriminator string for the user starting with a # or an empty string if the user is a migrated user + */ + public static String getDiscriminatorString(UserToken user) { + if (isMigratedUserName(user)) + return ""; + else + return "#" + user.getDiscriminator(); + } + + /** + * @param user The user to get the Discord discriminator string for + * @return The discriminator string for the user starting with a # or an empty string if the user is a migrated user + */ + public static String getDiscriminatorString(User user) { + if (isMigratedUserName(user)) + return ""; + else + return "#" + user.discriminator(); + } + + /** + * Returns the avatar ID for the user + * + * @param user The user to get the avatar ID for + * @return The avatar ID for the user + */ + public static int getAvatarId(User user) { + if (isMigratedUserName(user)) + return (int) ((Long.parseLong(user.id()) >> 22) % 5); + else + return Integer.parseInt(user.discriminator()) % 5; + } + + /** + * Returns the avatar URL for the user + * + * @param user The user to get the avatar URL for + * @return The avatar URL for the user + */ + public static String getAvatarUrl(User user) { + return "https://cdn.discordapp.com/embed/avatars/" + getAvatarId(user) + ".png"; + } +} diff --git a/src/main/jte/macros/navbar.jte b/src/main/jte/macros/navbar.jte index 7a0f1668..281fe780 100644 --- a/src/main/jte/macros/navbar.jte +++ b/src/main/jte/macros/navbar.jte @@ -1,5 +1,6 @@ @import com.github.khakers.modmailviewer.Config @import com.github.khakers.modmailviewer.auth.UserToken +@import com.github.khakers.modmailviewer.util.DiscordUtils @import io.javalin.http.Context @param Context ctx @@ -88,7 +89,7 @@