Skip to content

Commit

Permalink
fix: add support for migrated discord usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
khakers committed Jun 10, 2023
1 parent 2508d69 commit 1cea5d4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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";
}
}
3 changes: 2 additions & 1 deletion src/main/jte/macros/navbar.jte
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -88,7 +89,7 @@
<div class="d-flex mx-2 color text-primary-emphasis">
@if(user != null && user.isRealUser())
<span class="navbar-text me-3 text-emphasis">
Signed in as <span>${user.getUsername()}</span><span>#${user.getDiscriminator()}</span>
Signed in as <span>${user.getUsername()}</span><span>${DiscordUtils.getDiscriminatorString(user)}</span>
</span>
<a class="btn btn-danger" href="/logout">signout</a>
@endif
Expand Down
13 changes: 9 additions & 4 deletions src/main/jte/macros/username.jte
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
@import com.github.khakers.modmailviewer.data.User
@import com.github.khakers.modmailviewer.util.DiscordUtils


@param User user

<span class="user-select-all" data-bs-toggle="tooltip"
data-bs-title="${user.id()}"><span
class="fw-semibold">${user.name()}</span><span
class="text-muted">#${user.discriminator()}</span>
</span>
data-bs-title="${user.id()}">
<span class="fw-semibold">${user.name()}</span>
@if(!DiscordUtils.isMigratedUserName(user))
<span class="text-muted">#${user.discriminator()}</span>

@endif
</span>
9 changes: 5 additions & 4 deletions src/main/jte/pages/LogEntryView.jte
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -96,15 +97,15 @@
<div class="row ">
<div class="col-2 col-md-1 text-center">
<img width="50" src="${message.get().getAuthor().avatarUrl()}"
onerror="this.src='https://cdn.discordapp.com/embed/avatars/${Integer.parseInt(message.get().getAuthor().discriminator()) % 5}.png';this.onerror=null"
onerror="this.src='https://cdn.discordapp.com/embed/avatars/${DiscordUtils.getAvatarId(message.get().getAuthor())}.png';this.onerror=null"
class="rounded-circle" style="height: inherit;" alt="user avatar">
</div>
<div class="col">
<div class="row ">
<div class="col">
<h3>
<span data-bs-toggle="tooltip"
title="${message.get().getAuthor().name()+"#"+message.get().getAuthor().discriminator()}">${message.get().getAuthor().name()}</span>
title="${message.get().getAuthor().name()+DiscordUtils.getDiscriminatorString(message.get().getAuthor())}">${message.get().getAuthor().name()}</span>
@if(message.get().getAuthor().mod())
<span class="badge text-bg-primary align-middle fst-normal"
style="font-size: 12px;">
Expand Down Expand Up @@ -172,15 +173,15 @@
<div class="row ">
<div class="col-2 col-md-1 text-center">
<img width="50" src="${modmailLog.getCloser().get().avatarUrl()}"
onerror="this.src='https://cdn.discordapp.com/embed/avatars/${Integer.parseInt(modmailLog.getCloser().get().discriminator()) % 5}.png';this.onerror=null"
onerror="this.src='https://cdn.discordapp.com/embed/avatars/${DiscordUtils.getAvatarId(modmailLog.getCloser().get())}.png';this.onerror=null"
class="rounded-circle" style="height: inherit;" alt="user avatar">
</div>
<div class="col">
<div class="row ">
<div class="col">
<h3>
<span data-bs-toggle="tooltip"
title="${modmailLog.getCloser().get().name()+"#"+modmailLog.getCloser().get().discriminator()}">${modmailLog.getCloser().get().name()}</span>
title="${modmailLog.getCloser().get().name()+DiscordUtils.getDiscriminatorString(modmailLog.getCloser().get())}">${modmailLog.getCloser().get().name()}</span>
<span class="fw-normal fs-5">closed the thread</span>
@if(modmailLog.getCloser().get().mod())
<span class="badge text-bg-primary align-middle fst-normal"
Expand Down
9 changes: 5 additions & 4 deletions src/main/jte/pages/homepage.jte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@import com.github.khakers.modmailviewer.auth.UserToken
@import com.github.khakers.modmailviewer.data.internal.TicketStatus
@import com.github.khakers.modmailviewer.util.DiscordUtils
@import com.github.khakers.modmailviewer.util.Pagination
@import com.github.khakers.modmailviewer.ModMailLogDB
@import com.github.khakers.modmailviewer.util.QueryParamUtil
Expand Down Expand Up @@ -107,7 +108,7 @@
<span class="user-select-all" data-bs-toggle="tooltip"
data-bs-title="${entry.get().getCreator().id()}"><span
class="fw-semibold">${entry.get().getCreator().name()}</span><span
class="text-muted">#${entry.get().getCreator().discriminator()}</span>
class="text-muted">${DiscordUtils.getDiscriminatorString(entry.get().getCreator())}</span>
</span>

<span><span class="text-muted" data-timestamp="${entry.get().getCreationTime().toString()}"
Expand All @@ -118,7 +119,7 @@
<span class="user-select-all" data-bs-toggle="tooltip"
data-bs-title="${entry.get().getCreator().id()}"><span
class="fw-semibold">${entry.get().getCreator().name()}</span><span
class="text-muted">#${entry.get().getCreator().discriminator()}</span>
class="text-muted">${DiscordUtils.getDiscriminatorString(entry.get().getCreator())}</span>
</span>
<span>was closed <span class="text-muted"
data-timestamp="${entry.get().getClosedTime().get().toString()}"
Expand All @@ -129,7 +130,7 @@
<span class="user-select-all" data-bs-toggle="tooltip"
data-bs-title="${entry.get().getCloser().get().id()}"><span
class="fw-semibold">${entry.get().getCloser().get().name()}</span><span
class="text-muted">#${entry.get().getCloser().get().discriminator()}</span>
class="text-muted">${DiscordUtils.getDiscriminatorString(entry.get().getCloser().get())}</span>
</span>
@endif

Expand All @@ -145,7 +146,7 @@
<span class="user-select-all" data-bs-toggle="tooltip"
data-bs-title="${entry.get().getMessages().get(entry.get().getMessages().size()-1).getAuthor().id()}"><span
class="fw-semibold">${entry.get().getMessages().get(entry.get().getMessages().size()-1).getAuthor().name()}</span><span
class="text-muted">#${entry.get().getMessages().get(entry.get().getMessages().size()-1).getAuthor().discriminator()}</span>
class="text-muted">${DiscordUtils.getDiscriminatorString(entry.get().getMessages().get(entry.get().getMessages().size()-1).getAuthor())}</span>
</span>
</span>
@endif
Expand Down

0 comments on commit 1cea5d4

Please sign in to comment.