From efca196deda743b4295b90b4e5e52320c8bd3d5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Han=20Seung=20Min=20-=20=ED=95=9C=EC=8A=B9=EB=AF=BC?= Date: Wed, 8 May 2024 10:25:32 +0900 Subject: [PATCH] new plugin MessageLatency: indicator for other people's latency (#2353) --- src/plugins/messageLatency/README.md | 31 ++++++ src/plugins/messageLatency/index.tsx | 147 +++++++++++++++++++++++++++ src/utils/constants.ts | 4 + 3 files changed, 182 insertions(+) create mode 100644 src/plugins/messageLatency/README.md create mode 100644 src/plugins/messageLatency/index.tsx diff --git a/src/plugins/messageLatency/README.md b/src/plugins/messageLatency/README.md new file mode 100644 index 0000000..8d2a776 --- /dev/null +++ b/src/plugins/messageLatency/README.md @@ -0,0 +1,31 @@ +# MessageLatency + +Displays an indicator for messages that took ≥n seconds to send. + +> **NOTE** +> +> - This plugin only applies to messages received after opening the channel +> - False positives can exist if the user's system clock has drifted. +> - Grouped messages only display latency of the first message + +## Demo + +### Chat View + +![chat-view](https://github.com/Vendicated/Vencord/assets/82430093/69430881-60b3-422f-aa3d-c62953837566) + +### Clock -ve Drift + +![pissbot-on-top](https://github.com/Vendicated/Vencord/assets/82430093/d9248b66-e761-4872-8829-e8bf4fea6ec8) + +### Clock +ve Drift + +![dumb-ai](https://github.com/Vendicated/Vencord/assets/82430093/0e9783cf-51d5-4559-ae10-42399e7d4099) + +### Connection Delay + +![who-this](https://github.com/Vendicated/Vencord/assets/82430093/fd68873d-8630-42cc-a166-e9063d2718b2) + +### Icons + +![icons](https://github.com/Vendicated/Vencord/assets/82430093/17630bd9-44ee-4967-bcdf-3315eb6eca85) diff --git a/src/plugins/messageLatency/index.tsx b/src/plugins/messageLatency/index.tsx new file mode 100644 index 0000000..0b6d750 --- /dev/null +++ b/src/plugins/messageLatency/index.tsx @@ -0,0 +1,147 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2024 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { definePluginSettings } from "@api/Settings"; +import ErrorBoundary from "@components/ErrorBoundary"; +import { Devs } from "@utils/constants"; +import { isNonNullish } from "@utils/guards"; +import definePlugin, { OptionType } from "@utils/types"; +import { findExportedComponentLazy } from "@webpack"; +import { SnowflakeUtils, Tooltip } from "@webpack/common"; +import { Message } from "discord-types/general"; + +type FillValue = ("status-danger" | "status-warning" | "text-muted"); +type Fill = [FillValue, FillValue, FillValue]; +type DiffKey = keyof Diff; + +interface Diff { + days: number, + hours: number, + minutes: number, + seconds: number; +} + +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 + } + }), + patches: [ + { + find: "showCommunicationDisabledStyles", + replacement: { + match: /(message:(\i),avatar:\i,username:\(0,\i.jsxs\)\(\i.Fragment,\{children:\[)(\i&&)/, + replace: "$1$self.Tooltip()({ message: $2 }),$3" + } + } + ], + stringDelta(delta: number) { + const diff: Diff = { + days: Math.round(delta / (60 * 60 * 24)), + hours: Math.round((delta / (60 * 60)) % 24), + minutes: Math.round((delta / (60)) % 60), + seconds: Math.round(delta % 60), + }; + + const str = (k: DiffKey) => diff[k] > 0 ? `${diff[k]} ${k}` : null; + const keys = Object.keys(diff) as DiffKey[]; + + return keys.map(str).filter(isNonNullish).join(" ") || "0 seconds"; + }, + latencyTooltipData(message: Message) { + 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); + + // 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 = this.stringDelta(abs); + + // Also thanks dziurwa + // 2 minutes + const TROLL_LIMIT = 2 * 60; + const { latency } = this.settings.store; + + const fill: Fill = 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: abs !== delta, fill } : null; + }, + Tooltip() { + return ErrorBoundary.wrap(({ message }: { message: Message; }) => { + + const d = this.latencyTooltipData(message); + + if (!isNonNullish(d)) return null; + + return + { + props => <> + {} + {/* Time Out indicator uses this, I think this is for a11y */} + Delayed Message + + } + ; + }); + }, + Icon({ delta, fill, props }: { + delta: string; + fill: Fill, + props: { + onClick(): void; + onMouseEnter(): void; + onMouseLeave(): void; + onContextMenu(): void; + onFocus(): void; + onBlur(): void; + "aria-label"?: string; + }; + }) { + return + + + + ; + } +}); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index cce276e..ab6c0bb 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -266,6 +266,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({ name: "Dziurwa", id: 1001086404203389018n }, + arHSM: { + name: "arHSM", + id: 841509053422632990n + }, F53: { name: "F53", id: 280411966126948353n