Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(MessageLatency): Adjust for Discord kotlin clients #2443

Merged
merged 7 commits into from
May 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 36 additions & 11 deletions src/plugins/messageLatency/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,27 @@ interface Diff {
seconds: number;
}

const DISCORD_KT_DELAY = 1471228.928;
const HiddenVisually = findExportedComponentLazy("HiddenVisually");

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,
description: "Threshold in seconds for latency indicator",
default: 2
},
detectDiscordKotlin: {
type: OptionType.BOOLEAN,
description: "Detect old Discord Android clients",
default: true
}
}),

patches: [
{
find: "showCommunicationDisabledStyles",
Expand All @@ -46,6 +54,7 @@ export default definePlugin({
}
}
],

stringDelta(delta: number) {
const diff: Diff = {
days: Math.round(delta / (60 * 60 * 24)),
Expand All @@ -71,49 +80,64 @@ 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;

const delta = Math.round((SnowflakeUtils.extractTimestamp(id) - SnowflakeUtils.extractTimestamp(nonce)) / 1000);
let isDiscordKotlin = false;
let delta = Math.round((SnowflakeUtils.extractTimestamp(id) - SnowflakeUtils.extractTimestamp(nonce)) / 1000);

// 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
isDiscordKotlin = detectDiscordKotlin;
delta += DISCORD_KT_DELAY;
}

// Thanks dziurwa (I hate you)
// This is when the user's clock is ahead
// Can't do anything if the clock is behind
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"]
: delta >= (latency * 2)
? ["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);

if (!isNonNullish(d)) return null;

let text: string;
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 <Tooltip
text={d.ahead ? `This user's clock is ${d.delta} ahead. ${d.isKotlinDiscord ? "User is suspected to be on an old mobile client" : ""}` : `This message was sent with a delay of ${d.delta}.`}
text={text}
position="top"
>
{
Expand All @@ -126,8 +150,9 @@ export default definePlugin({
</Tooltip>;
});
},

Icon({ delta, fill, props }: {
delta: string;
delta: string | null;
fill: Fill,
props: {
onClick(): void;
Expand All @@ -147,7 +172,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}
>
Expand Down