198 lines
6.5 KiB
TypeScript
198 lines
6.5 KiB
TypeScript
/*
|
|
* 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 { isPluginEnabled } from "@api/PluginManager";
|
|
import { definePluginSettings } from "@api/Settings";
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
|
import NoBlockedMessagesPlugin from "@plugins/noBlockedMessages";
|
|
import { Devs } from "@utils/constants";
|
|
import { sleep } from "@utils/misc";
|
|
import { Queue } from "@utils/Queue";
|
|
import { useForceUpdater } from "@utils/react";
|
|
import definePlugin, { OptionType } from "@utils/types";
|
|
import { CustomEmoji, Message, ReactionEmoji, User } from "@vencord/discord-types";
|
|
import { ChannelStore, Constants, FluxDispatcher, React, RestAPI, useEffect, useLayoutEffect, UserStore, UserSummaryItem } from "@webpack/common";
|
|
|
|
interface ReactionCacheEntry {
|
|
fetched: boolean;
|
|
users: Map<string, User>;
|
|
}
|
|
|
|
interface ReactionProps {
|
|
message: Message;
|
|
emoji: CustomEmoji;
|
|
type: number;
|
|
}
|
|
|
|
let Scroll: any = null;
|
|
const queue = new Queue();
|
|
let reactions: Record<string, ReactionCacheEntry> = {};
|
|
|
|
function fetchReactions(msg: Message, emoji: ReactionEmoji, type: number) {
|
|
const key = emoji.name + (emoji.id ? `:${emoji.id}` : "");
|
|
return RestAPI.get({
|
|
url: Constants.Endpoints.REACTIONS(msg.channel_id, msg.id, key),
|
|
query: {
|
|
limit: 100,
|
|
type
|
|
},
|
|
oldFormErrors: true
|
|
})
|
|
.then(res => {
|
|
for (const user of res.body) {
|
|
FluxDispatcher.dispatch({
|
|
type: "USER_UPDATE",
|
|
user
|
|
});
|
|
}
|
|
|
|
FluxDispatcher.dispatch({
|
|
type: "MESSAGE_REACTION_ADD_USERS",
|
|
channelId: msg.channel_id,
|
|
messageId: msg.id,
|
|
users: res.body,
|
|
emoji,
|
|
reactionType: type
|
|
});
|
|
})
|
|
.catch(console.error)
|
|
.finally(() => sleep(250));
|
|
}
|
|
|
|
function getReactionsWithQueue(msg: Message, e: ReactionEmoji, type: number) {
|
|
const key = `${msg.id}:${e.name}:${e.id ?? ""}:${type}`;
|
|
const cache = reactions[key] ??= { fetched: false, users: new Map() };
|
|
if (!cache.fetched) {
|
|
queue.unshift(() => fetchReactions(msg, e, type));
|
|
cache.fetched = true;
|
|
}
|
|
|
|
return cache.users;
|
|
}
|
|
|
|
function handleClickAvatar(event: React.UIEvent<HTMLElement, Event>) {
|
|
event.stopPropagation();
|
|
}
|
|
|
|
function ReactionUsers({ message, users }: { message: Message, users: User[]; }) {
|
|
useLayoutEffect(() => { // bc need to prevent autoscrolling
|
|
if (Scroll?.scrollCounter > 0) {
|
|
Scroll.setAutomaticAnchor(null);
|
|
}
|
|
});
|
|
|
|
return (
|
|
<div
|
|
style={{ marginLeft: "0.5em", transform: "scale(0.9)" }}
|
|
>
|
|
<div
|
|
onClick={handleClickAvatar}
|
|
onKeyDown={handleClickAvatar}
|
|
style={settings.store.avatarClick ? {} : { pointerEvents: "none" }}
|
|
>
|
|
<UserSummaryItem
|
|
users={users}
|
|
guildId={ChannelStore.getChannel(message.channel_id)?.guild_id}
|
|
renderIcon={false}
|
|
max={5}
|
|
showDefaultAvatarsForNullUsers
|
|
showUserPopout
|
|
/>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const settings = definePluginSettings({
|
|
avatarClick: {
|
|
description: "Toggle clicking avatars in reactions",
|
|
type: OptionType.BOOLEAN,
|
|
default: false,
|
|
restartNeeded: true
|
|
}
|
|
});
|
|
|
|
export default definePlugin({
|
|
name: "WhoReacted",
|
|
description: "Renders the avatars of users who reacted to a message",
|
|
tags: ["Reactions", "Chat", "Appearance"],
|
|
authors: [Devs.Ven, Devs.KannaDev, Devs.newwares, Devs.paige],
|
|
isModified: true,
|
|
settings,
|
|
patches: [
|
|
{
|
|
find: ",reactionRef:",
|
|
replacement: {
|
|
match: /(\i)\?null:\(0,\i\.jsx\)\(\i\.\i,{className:\i\.reactionCount,.*?}\),(?<=(emoji:\i,message:\i,type:\i).+?)/,
|
|
replace: "$&$1?null:$self.renderUsers({$2}),"
|
|
}
|
|
},
|
|
{
|
|
find: '"MessageReactionsStore"',
|
|
replacement: {
|
|
match: /CONNECTION_OPEN:function\(\){(\i)={}/,
|
|
replace: "$&;$self.reactions=$1;"
|
|
}
|
|
},
|
|
{
|
|
|
|
find: "cleanAutomaticAnchor(){",
|
|
replacement: {
|
|
match: /constructor\(\i\)\{(?=.{0,100}(?:automaticAnchor|\.messages\.loadingMore))/,
|
|
replace: "$&$self.setScrollObj(this);"
|
|
}
|
|
}
|
|
],
|
|
|
|
renderUsers: ErrorBoundary.wrap(({ message, emoji, type }: ReactionProps) => {
|
|
const forceUpdate = useForceUpdater();
|
|
|
|
useEffect(() => {
|
|
const cb = (e: any) => {
|
|
if (e?.messageId === message.id)
|
|
forceUpdate();
|
|
};
|
|
FluxDispatcher.subscribe("MESSAGE_REACTION_ADD_USERS", cb);
|
|
|
|
return () => FluxDispatcher.unsubscribe("MESSAGE_REACTION_ADD_USERS", cb);
|
|
}, [message.id, forceUpdate]);
|
|
|
|
if (message.reactions.length > 10) return null;
|
|
|
|
const reactionMap = getReactionsWithQueue(message, emoji, type);
|
|
let users = Array.from(reactionMap, ([id]) => UserStore.getUser(id)).filter(Boolean);
|
|
|
|
if (isPluginEnabled(NoBlockedMessagesPlugin.name))
|
|
users = users.filter(user => {
|
|
const { blocked, ignored } = NoBlockedMessagesPlugin.getRelationshipStatus(user);
|
|
return !(blocked || (ignored && NoBlockedMessagesPlugin.settings.store.alsoHideIgnoredUsers));
|
|
});
|
|
|
|
return users.length === 0
|
|
? null
|
|
: <ReactionUsers message={message} users={users} />;
|
|
}, { noop: true }),
|
|
|
|
setScrollObj(scroll: any) {
|
|
Scroll = scroll;
|
|
},
|
|
|
|
set reactions(value: any) {
|
|
reactions = value;
|
|
}
|
|
});
|