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 @@
@if(user != null && user.isRealUser()) - Signed in as ${user.getUsername()}#${user.getDiscriminator()} + Signed in as ${user.getUsername()}${DiscordUtils.getDiscriminatorString(user)} signout @endif diff --git a/src/main/jte/macros/username.jte b/src/main/jte/macros/username.jte index 1debde5d..a3b389cb 100644 --- a/src/main/jte/macros/username.jte +++ b/src/main/jte/macros/username.jte @@ -1,9 +1,14 @@ @import com.github.khakers.modmailviewer.data.User +@import com.github.khakers.modmailviewer.util.DiscordUtils + @param User user ${user.name()}#${user.discriminator()} - \ No newline at end of file + data-bs-title="${user.id()}"> + ${user.name()} + @if(!DiscordUtils.isMigratedUserName(user)) + #${user.discriminator()} + + @endif + \ No newline at end of file diff --git a/src/main/jte/pages/LogEntryView.jte b/src/main/jte/pages/LogEntryView.jte index 2b35aa72..55e0d14e 100644 --- a/src/main/jte/pages/LogEntryView.jte +++ b/src/main/jte/pages/LogEntryView.jte @@ -1,5 +1,6 @@ @import com.github.khakers.modmailviewer.auth.UserToken @import com.github.khakers.modmailviewer.data.MessageType +@import com.github.khakers.modmailviewer.util.DiscordUtils @import io.javalin.http.Context @@ -96,7 +97,7 @@
user avatar
@@ -104,7 +105,7 @@

${message.get().getAuthor().name()} + title="${message.get().getAuthor().name()+DiscordUtils.getDiscriminatorString(message.get().getAuthor())}">${message.get().getAuthor().name()} @if(message.get().getAuthor().mod()) @@ -172,7 +173,7 @@
user avatar
@@ -180,7 +181,7 @@

${modmailLog.getCloser().get().name()} + title="${modmailLog.getCloser().get().name()+DiscordUtils.getDiscriminatorString(modmailLog.getCloser().get())}">${modmailLog.getCloser().get().name()} closed the thread @if(modmailLog.getCloser().get().mod()) ${entry.get().getCreator().name()}#${entry.get().getCreator().discriminator()} + class="text-muted">${DiscordUtils.getDiscriminatorString(entry.get().getCreator())} ${entry.get().getCreator().name()}#${entry.get().getCreator().discriminator()} + class="text-muted">${DiscordUtils.getDiscriminatorString(entry.get().getCreator())} was closed ${entry.get().getCloser().get().name()}#${entry.get().getCloser().get().discriminator()} + class="text-muted">${DiscordUtils.getDiscriminatorString(entry.get().getCloser().get())} @endif @@ -145,7 +146,7 @@ ${entry.get().getMessages().get(entry.get().getMessages().size()-1).getAuthor().name()}#${entry.get().getMessages().get(entry.get().getMessages().size()-1).getAuthor().discriminator()} + class="text-muted">${DiscordUtils.getDiscriminatorString(entry.get().getMessages().get(entry.get().getMessages().size()-1).getAuthor())} @endif