From 762b0e0f836499baffa848d126815c6d4e3e4951 Mon Sep 17 00:00:00 2001 From: dolfies Date: Sat, 11 May 2024 23:04:09 -0400 Subject: [PATCH 1/5] feat(MessageLatency): Adjust for Discord kotlin clients --- src/plugins/messageLatency/index.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/plugins/messageLatency/index.tsx b/src/plugins/messageLatency/index.tsx index 0b6d750333..0909488a29 100644 --- a/src/plugins/messageLatency/index.tsx +++ b/src/plugins/messageLatency/index.tsx @@ -24,6 +24,7 @@ interface Diff { seconds: number; } +const DISCORD_KT_DELAY = 1471228.928; const HiddenVisually = findExportedComponentLazy("HiddenVisually"); export default definePlugin({ @@ -65,7 +66,12 @@ export default definePlugin({ // Message wasn't received through gateway if (!isNonNullish(nonce)) return null; - const delta = Math.round((SnowflakeUtils.extractTimestamp(id) - SnowflakeUtils.extractTimestamp(nonce)) / 1000); + let delta = Math.round((SnowflakeUtils.extractTimestamp(id) - SnowflakeUtils.extractTimestamp(nonce)) / 1000); + + // Old Discord Android clients have a delay of 1471228928 milliseconds + // This is a workaround for that + if (delta >= DISCORD_KT_DELAY - 86400) // One day of padding for good measure + delta -= DISCORD_KT_DELAY; // Thanks dziurwa (I hate you) // This is when the user's clock is ahead From 1fab608a3fd6cf48e22b7f62e083e13b6a8f305e Mon Sep 17 00:00:00 2001 From: dolfies Date: Sun, 12 May 2024 13:00:18 -0400 Subject: [PATCH 2/5] Fix and keep old android detection --- src/plugins/messageLatency/index.tsx | 37 +++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/plugins/messageLatency/index.tsx b/src/plugins/messageLatency/index.tsx index 8cceeefa44..7cabc8b284 100644 --- a/src/plugins/messageLatency/index.tsx +++ b/src/plugins/messageLatency/index.tsx @@ -36,6 +36,11 @@ export default definePlugin({ type: OptionType.NUMBER, description: "Threshold in seconds for latency indicator", default: 2 + }, + detectDiscordKotlin: { + type: OptionType.BOOLEAN, + description: "Detect old Discord Android clients", + default: true } }), patches: [ @@ -72,20 +77,24 @@ export default definePlugin({ ); }, ""); - return [ts || "0 seconds", diff.days === 17 && diff.hours === 1] as const; + return ts || "0 seconds"; }, latencyTooltipData(message: Message) { + const { latency, detectDiscordKotlin } = this.settings.store; const { id, nonce } = message; // Message wasn't received through gateway if (!isNonNullish(nonce)) return null; + let isDiscordKotlin = false; let delta = Math.round((SnowflakeUtils.extractTimestamp(id) - SnowflakeUtils.extractTimestamp(nonce)) / 1000); - // Old Discord Android clients have a delay of 1471228928 milliseconds + // Old Discord Android clients have a delay of around 17 days // This is a workaround for that - if (delta >= DISCORD_KT_DELAY - 86400) // One day of padding for good measure + if (-delta >= DISCORD_KT_DELAY - 86400) { // One day of padding for good measure + isDiscordKotlin = detectDiscordKotlin; delta -= DISCORD_KT_DELAY; + } // Thanks dziurwa (I hate you) // This is when the user's clock is ahead @@ -93,15 +102,13 @@ export default definePlugin({ const abs = Math.abs(delta); const ahead = abs !== delta; - const [stringDelta, isSuspectedKotlinDiscord] = this.stringDelta(abs); - const isKotlinDiscord = ahead && isSuspectedKotlinDiscord; + const stringDelta = abs >= latency ? this.stringDelta(abs) : null; // Also thanks dziurwa // 2 minutes const TROLL_LIMIT = 2 * 60; - const { latency } = this.settings.store; - const fill: Fill = isKotlinDiscord + const fill: Fill = isDiscordKotlin ? ["status-positive", "status-positive", "text-muted"] : delta >= TROLL_LIMIT || ahead ? ["text-muted", "text-muted", "text-muted"] @@ -109,17 +116,25 @@ export default definePlugin({ ? ["status-danger", "text-muted", "text-muted"] : ["status-warning", "status-warning", "text-muted"]; - return abs >= latency ? { delta: stringDelta, ahead, fill, isKotlinDiscord } : null; + return (abs >= latency || isDiscordKotlin) ? { delta: stringDelta, ahead, fill, isDiscordKotlin } : null; }, Tooltip() { return ErrorBoundary.wrap(({ message }: { message: Message; }) => { const d = this.latencyTooltipData(message); + console.log(d); if (!isNonNullish(d)) return null; + let text; + if (!d.delta) { + text = "User is suspected to be on an old Discord Android client"; + } else { + text = (d.ahead ? `This user's clock is ${d.delta} ahead.` : `This message was sent with a delay of ${d.delta}.`) + (d.isDiscordKotlin ? " User is suspected to be on an old Discord Android client." : ""); + } + return { @@ -133,7 +148,7 @@ export default definePlugin({ }); }, Icon({ delta, fill, props }: { - delta: string; + delta: string | null; fill: Fill, props: { onClick(): void; @@ -153,7 +168,7 @@ export default definePlugin({ role="img" fill="none" style={{ marginRight: "8px", verticalAlign: -1 }} - aria-label={delta} + aria-label={delta ?? "Old Discord Android client"} aria-hidden="false" {...props} > From b7e6f1eb76ea5fb20de3c070a83a7ca30acb24a3 Mon Sep 17 00:00:00 2001 From: dolfies Date: Sun, 12 May 2024 13:02:33 -0400 Subject: [PATCH 3/5] Remove debug log --- src/plugins/messageLatency/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/messageLatency/index.tsx b/src/plugins/messageLatency/index.tsx index 7cabc8b284..29d7bef462 100644 --- a/src/plugins/messageLatency/index.tsx +++ b/src/plugins/messageLatency/index.tsx @@ -122,7 +122,6 @@ export default definePlugin({ return ErrorBoundary.wrap(({ message }: { message: Message; }) => { const d = this.latencyTooltipData(message); - console.log(d); if (!isNonNullish(d)) return null; From 477b17c4c1151a8e5494c02086b6081c3350a728 Mon Sep 17 00:00:00 2001 From: dolfies Date: Sun, 12 May 2024 16:40:32 -0400 Subject: [PATCH 4/5] Fix delay delta --- src/plugins/messageLatency/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/messageLatency/index.tsx b/src/plugins/messageLatency/index.tsx index 29d7bef462..0557575247 100644 --- a/src/plugins/messageLatency/index.tsx +++ b/src/plugins/messageLatency/index.tsx @@ -93,7 +93,7 @@ export default definePlugin({ // This is a workaround for that if (-delta >= DISCORD_KT_DELAY - 86400) { // One day of padding for good measure isDiscordKotlin = detectDiscordKotlin; - delta -= DISCORD_KT_DELAY; + delta += DISCORD_KT_DELAY; } // Thanks dziurwa (I hate you) From 91ed133249d412e015686a6c7e8353db6a047c0b Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Sun, 12 May 2024 20:05:13 -0300 Subject: [PATCH 5/5] Fix formatting --- src/plugins/messageLatency/index.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/plugins/messageLatency/index.tsx b/src/plugins/messageLatency/index.tsx index 0557575247..301e605fb3 100644 --- a/src/plugins/messageLatency/index.tsx +++ b/src/plugins/messageLatency/index.tsx @@ -31,6 +31,7 @@ export default definePlugin({ name: "MessageLatency", description: "Displays an indicator for messages that took ≥n seconds to send", authors: [Devs.arHSM], + settings: definePluginSettings({ latency: { type: OptionType.NUMBER, @@ -43,6 +44,7 @@ export default definePlugin({ default: true } }), + patches: [ { find: "showCommunicationDisabledStyles", @@ -52,6 +54,7 @@ export default definePlugin({ } } ], + stringDelta(delta: number) { const diff: Diff = { days: Math.round(delta / (60 * 60 * 24)), @@ -79,6 +82,7 @@ export default definePlugin({ return ts || "0 seconds"; }, + latencyTooltipData(message: Message) { const { latency, detectDiscordKotlin } = this.settings.store; const { id, nonce } = message; @@ -118,14 +122,14 @@ export default definePlugin({ return (abs >= latency || isDiscordKotlin) ? { delta: stringDelta, ahead, fill, isDiscordKotlin } : null; }, + Tooltip() { return ErrorBoundary.wrap(({ message }: { message: Message; }) => { - const d = this.latencyTooltipData(message); if (!isNonNullish(d)) return null; - let text; + let text: string; if (!d.delta) { text = "User is suspected to be on an old Discord Android client"; } else { @@ -146,6 +150,7 @@ export default definePlugin({ ; }); }, + Icon({ delta, fill, props }: { delta: string | null; fill: Fill,