aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/messageLinkEmbeds.tsx
blob: d1fcbf3574df96a063bba3652861f0e3b97bf59d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Vencord, a modification for Discord's desktop app
 * Copyright (c) 2022 Vendicated and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import { addAccessory } from "@api/MessageAccessories";
import { definePluginSettings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary";
import { Devs } from "@utils/constants.js";
import { classes } from "@utils/misc";
import { Queue } from "@utils/Queue";
import { LazyComponent } from "@utils/react";
import definePlugin, { OptionType } from "@utils/types";
import { find, findByCode, findByPropsLazy } from "@webpack";
import {
    Button,
    ChannelStore,
    FluxDispatcher,
    GuildStore,
    MessageStore,
    Parser,
    PermissionStore,
    RestAPI,
    Text,
    UserStore
} from "@webpack/common";
import { Channel, Guild, Message } from "discord-types/general";

const messageCache = new Map<string, {
    message?: Message;
    fetched: boolean;
}>();

const Embed = LazyComponent(() => findByCode(".inlineMediaEmbed"));
const ChannelMessage = LazyComponent(() => find(m => m.type?.toString()?.includes('["message","compact","className",')));

const SearchResultClasses = findByPropsLazy("message", "searchResult");

let AutoModEmbed: React.ComponentType<any> = () => null;

const messageLinkRegex = /(?<!<)https?:\/\/(?:\w+\.)?discord(?:app)?\.com\/channels\/(\d{17,20}|@me)\/(\d{17,20})\/(\d{17,20})/g;
const tenorRegex = /^https:\/\/(?:www\.)?tenor\.com\//;

interface Attachment {
    height: number;
    width: number;
    url: string;
    proxyURL?: string;
}

interface MessageEmbedProps {
    message: Message;
    channel: Channel;
    guildID: string;
}

const messageFetchQueue = new Queue();

const settings = definePluginSettings({
    messageBackgroundColor: {
        description: "Background color for messages in rich embeds",
        type: OptionType.BOOLEAN
    },
    automodEmbeds: {
        description: "Use automod embeds instead of rich embeds (smaller but less info)",
        type: OptionType.SELECT,
        options: [
            {
                label: "Always use automod embeds",
                value: "always"
            },
            {
                label: "Prefer automod embeds, but use rich embeds if some content can't be shown",
                value: "prefer"
            },
            {
                label: "Never use automod embeds",
                value: "never",
                default: true
            }
        ]
    },
    clearMessageCache: {
        type: OptionType.COMPONENT,
        description: "Clear the linked message cache",
        component: () =>
            <Button onClick={() => messageCache.clear()}>
                Clear the linked message cache
            </Button>
    }
});


async function fetchMessage(channelID: string, messageID: string) {
    const cached = messageCache.get(messageID);
    if (cached) return cached.message;

    messageCache.set(messageID, { fetched: false });

    const res = await RestAPI.get({
        url: `/channels/${channelID}/messages`,
        query: {
            limit: 1,
            around: messageID
        },
        retries: 2
    }).catch(() => null);

    const msg = res?.body?.[0];
    if (!msg) return;

    const message: Message = MessageStore.getMessages(msg.channel_id).receiveMessage(msg).get(msg.id);

    messageCache.set(message.id, {
        message,
        fetched: true
    });

    return message;
}


function getImages(message: Message): Attachment[] {
    const attachments: Attachment[] = [];

    for (const { content_type, height, width, url, proxy_url } of message.attachments ?? []) {
        if (content_type?.startsWith("image/"))
            attachments.push({
                height: height!,
                width: width!,
                url: url,
                proxyURL: proxy_url!
            });
    }

    for (const { type, image, thumbnail, url } of message.embeds ?? []) {
        if (type === "image")
            attachments.push({ ...(image ?? thumbnail!) });
        else if (url && type === "gifv" && !tenorRegex.test(url))
            attachments.push({
                height: thumbnail!.height,
                width: thumbnail!.width,
                url
            });
    }

    return attachments;
}

function noContent(attachments: number, embeds: number) {
    if (!attachments && !embeds) return "";
    if (!attachments) return `[no content, ${embeds} embed${embeds !== 1 ? "s" : ""}]`;
    if (!embeds) return `[no content, ${attachments} attachment${attachments !== 1 ? "s" : ""}]`;
    return `[no content, ${attachments} attachment${attachments !== 1 ? "s" : ""} and ${embeds} embed${embeds !== 1 ? "s" : ""}]`;
}

function requiresRichEmbed(message: Message) {
    if (message.components.length) return true;
    if (message.attachments.some(a => !a.content_type?.startsWith("image/"))) return true;
    if (message.embeds.some(e => e.type !== "image" && (e.type !== "gifv" || tenorRegex.test(e.url!)))) return true;

    return false;
}

function computeWidthAndHeight(width: number, height: number) {
    const maxWidth = 400;
    const maxHeight = 300;

    if (width > height) {
        const adjustedWidth = Math.min(width, maxWidth);
        return { width: adjustedWidth, height: Math.round(height / (width / adjustedWidth)) };
    }

    const adjustedHeight = Math.min(height, maxHeight);
    return { width: Math.round(width / (height / adjustedHeight)), height: adjustedHeight };
}

function withEmbeddedBy(message: Message, embeddedBy: string[]) {
    return new Proxy(message, {
        get(_, prop) {
            if (prop === "vencordEmbeddedBy") return embeddedBy;
            // @ts-ignore ts so bad
            return Reflect.get(...arguments);
        }
    });
}


function MessageEmbedAccessory({ message }: { message: Message; }) {
    // @ts-ignore
    const embeddedBy: string[] = message.vencordEmbeddedBy ?? [];

    const accessories = [] as (JSX.Element | null)[];

    let match = null as RegExpMatchArray | null;
    while ((match = messageLinkRegex.exec(message.content!)) !== null) {
        const [_, guildID, channelID, messageID] = match;
        if (embeddedBy.includes(messageID)) {
            continue;
        }

        const linkedChannel = ChannelStore.getChannel(channelID);
        if (!linkedChannel || (guildID !== "@me" && !PermissionStore.can(1024n /* view channel */, linkedChannel))) {
            continue;
        }

        let linkedMessage = messageCache.get(messageID)?.message;
        if (!linkedMessage) {
            linkedMessage ??= MessageStore.getMessage(channelID, messageID);
            if (linkedMessage) {
                messageCache.set(messageID, { message: linkedMessage, fetched: true });
            } else {
                const msg = { ...message } as any;
                delete msg.embeds;
                messageFetchQueue.push(() => fetchMessage(channelID, messageID)
                    .then(m => m && FluxDispatcher.dispatch({
                        type: "MESSAGE_UPDATE",
                        message: msg
                    }))
                );
                continue;
            }
        }

        const messageProps: MessageEmbedProps = {
            message: withEmbeddedBy(linkedMessage, [...embeddedBy, message.id]),
            channel: linkedChannel,
            guildID
        };

        const type = settings.store.automodEmbeds;
        accessories.push(
            type === "always" || (type === "prefer" && !requiresRichEmbed(linkedMessage))
                ? <AutomodEmbedAccessory {...messageProps} />
                : <ChannelMessageEmbedAccessory {...messageProps} />
        );
    }

    return accessories.length ? <>{accessories}</> : null;
}

function ChannelMessageEmbedAccessory({ message, channel, guildID }: MessageEmbedProps): JSX.Element | null {
    const isDM = guildID === "@me";

    const guild = !isDM && GuildStore.getGuild(channel.guild_id);
    const dmReceiver = UserStore.getUser(ChannelStore.getChannel(channel.id).recipients?.[0]);


    return <Embed
        embed={{
            rawDescription: "",
            color: "var(--background-secondary)",
            author: {
                name: <Text variant="text-xs/medium" tag="span">
                    <span>{isDM ? "Direct Message - " : (guild as Guild).name + " - "}</span>
                    {isDM
                        ? Parser.parse(`<@${dmReceiver.id}>`)
                        : Parser.parse(`<#${channel.id}>`)
                    }
                </Text>,
                iconProxyURL: guild
                    ? `https://${window.GLOBAL_ENV.CDN_HOST}/icons/${guild.id}/${guild.icon}.png`
                    : `https://${window.GLOBAL_ENV.CDN_HOST}/avatars/${dmReceiver.id}/${dmReceiver.avatar}`
            }
        }}
        renderDescription={() => (
            <div key={message.id} className={classes(SearchResultClasses.message, settings.store.messageBackgroundColor && SearchResultClasses.searchResult)}>
                <ChannelMessage
                    id={`message-link-embeds-${message.id}`}
                    message={message}
                    channel={channel}
                    subscribeToComponentDispatch={false}
                />
            </div>
        )}
    />;
}

function AutomodEmbedAccessory(props: MessageEmbedProps): JSX.Element | null {
    const { message, channel, guildID } = props;

    const isDM = guildID === "@me";
    const images = getImages(message);
    const { parse } = Parser;

    return <AutoModEmbed
        channel={channel}
        childrenAccessories={
            <Text color="text-muted" variant="text-xs/medium" tag="span">
                {isDM
                    ? parse(`<@${ChannelStore.getChannel(channel.id).recipients[0]}>`)
                    : parse(`<#${channel.id}>`)
                }
                <span>{isDM ? " - Direct Message" : " - " + GuildStore.getGuild(channel.guild_id)?.name}</span>
            </Text>
        }
        compact={false}
        content={
            <>
                {message.content || message.attachments.length <= images.length
                    ? parse(message.content)
                    : [noContent(message.attachments.length, message.embeds.length)]
                }
                {images.map(a => {
                    const { width, height } = computeWidthAndHeight(a.width, a.height);
                    return (
                        <div>
                            <img src={a.url} width={width} height={height} />
                        </div>
                    );
                })}
            </>
        }
        hideTimestamp={false}
        message={message}
        _messageEmbed="automod"
    />;
}

export default definePlugin({
    name: "MessageLinkEmbeds",
    description: "Adds a preview to messages that link another message",
    authors: [Devs.TheSun, Devs.Ven],
    dependencies: ["MessageAccessoriesAPI"],
    patches: [
        {
            find: ".embedCard",
            replacement: [{
                match: /function (\i)\(\i\){var \i=\i\.message,\i=\i\.channel.{0,200}\.hideTimestamp/,
                replace: "$self.AutoModEmbed=$1;$&"
            }]
        }
    ],

    set AutoModEmbed(e: any) {
        AutoModEmbed = e;
    },

    settings,

    start() {
        addAccessory("messageLinkEmbed", props => {
            if (!messageLinkRegex.test(props.message.content))
                return null;

            // need to reset the regex because it's global
            messageLinkRegex.lastIndex = 0;

            return (
                <ErrorBoundary>
                    <MessageEmbedAccessory message={props.message} />
                </ErrorBoundary>
            );
        }, 4 /* just above rich embeds */);
    },
});