2022-11-18 22:31:53 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-28 12:37:55 +00:00
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
|
|
|
import { Devs } from "@utils/constants";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { sleep } from "@utils/misc";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { Queue } from "@utils/Queue";
|
2023-11-22 05:49:08 +00:00
|
|
|
import { findComponentByCodeLazy, useForceUpdater } from "@utils/react";
|
2022-11-28 12:37:55 +00:00
|
|
|
import definePlugin from "@utils/types";
|
2023-11-22 05:49:08 +00:00
|
|
|
import { findByPropsLazy } from "@webpack";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { ChannelStore, FluxDispatcher, React, RestAPI, Tooltip } from "@webpack/common";
|
2023-10-03 00:26:38 +00:00
|
|
|
import { CustomEmoji } from "@webpack/types";
|
|
|
|
import { Message, ReactionEmoji, User } from "discord-types/general";
|
2022-11-18 22:31:53 +00:00
|
|
|
|
2023-11-22 05:49:08 +00:00
|
|
|
const UserSummaryItem = findComponentByCodeLazy("defaultRenderUser", "showDefaultAvatarsForNullUsers");
|
2022-11-28 12:37:55 +00:00
|
|
|
const AvatarStyles = findByPropsLazy("moreUsers", "emptyUser", "avatarContainer", "clickableAvatar");
|
2022-11-18 22:31:53 +00:00
|
|
|
|
2022-11-25 17:07:29 +00:00
|
|
|
const queue = new Queue();
|
2023-10-03 00:26:38 +00:00
|
|
|
let reactions: Record<string, ReactionCacheEntry>;
|
2022-11-25 17:07:29 +00:00
|
|
|
|
2023-02-12 17:58:44 +00:00
|
|
|
function fetchReactions(msg: Message, emoji: ReactionEmoji, type: number) {
|
2022-11-25 17:07:29 +00:00
|
|
|
const key = emoji.name + (emoji.id ? `:${emoji.id}` : "");
|
|
|
|
return RestAPI.get({
|
|
|
|
url: `/channels/${msg.channel_id}/messages/${msg.id}/reactions/${key}`,
|
|
|
|
query: {
|
2023-02-12 17:58:44 +00:00
|
|
|
limit: 100,
|
|
|
|
type
|
2022-11-25 17:07:29 +00:00
|
|
|
},
|
|
|
|
oldFormErrors: true
|
|
|
|
})
|
|
|
|
.then(res => FluxDispatcher.dispatch({
|
|
|
|
type: "MESSAGE_REACTION_ADD_USERS",
|
|
|
|
channelId: msg.channel_id,
|
|
|
|
messageId: msg.id,
|
|
|
|
users: res.body,
|
2023-02-12 17:58:44 +00:00
|
|
|
emoji,
|
|
|
|
reactionType: type
|
2022-11-25 17:07:29 +00:00
|
|
|
}))
|
|
|
|
.catch(console.error)
|
|
|
|
.finally(() => sleep(250));
|
|
|
|
}
|
|
|
|
|
2023-02-12 17:58:44 +00:00
|
|
|
function getReactionsWithQueue(msg: Message, e: ReactionEmoji, type: number) {
|
|
|
|
const key = `${msg.id}:${e.name}:${e.id ?? ""}:${type}`;
|
2023-10-03 00:26:38 +00:00
|
|
|
const cache = reactions[key] ??= { fetched: false, users: {} };
|
2022-11-25 17:07:29 +00:00
|
|
|
if (!cache.fetched) {
|
2023-10-03 00:26:38 +00:00
|
|
|
queue.unshift(() => fetchReactions(msg, e, type));
|
2022-11-25 17:07:29 +00:00
|
|
|
cache.fetched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache.users;
|
|
|
|
}
|
|
|
|
|
2022-11-18 22:31:53 +00:00
|
|
|
function makeRenderMoreUsers(users: User[]) {
|
|
|
|
return function renderMoreUsers(_label: string, _count: number) {
|
|
|
|
return (
|
|
|
|
<Tooltip text={users.slice(5).map(u => u.username).join(", ")} >
|
|
|
|
{({ onMouseEnter, onMouseLeave }) => (
|
|
|
|
<div
|
|
|
|
className={AvatarStyles.moreUsers}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
>
|
|
|
|
+{users.length - 5}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Tooltip >
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-23 23:42:38 +00:00
|
|
|
function handleClickAvatar(event: React.MouseEvent<HTMLElement, MouseEvent>) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2022-11-18 22:31:53 +00:00
|
|
|
export default definePlugin({
|
|
|
|
name: "WhoReacted",
|
2023-10-03 00:26:38 +00:00
|
|
|
description: "Renders the avatars of users who reacted to a message",
|
2023-04-23 23:42:38 +00:00
|
|
|
authors: [Devs.Ven, Devs.KannaDev],
|
2022-11-18 22:31:53 +00:00
|
|
|
|
|
|
|
patches: [{
|
|
|
|
find: ",reactionRef:",
|
|
|
|
replacement: {
|
2023-10-25 17:09:37 +00:00
|
|
|
match: /(\i)\?null:\(0,\i\.jsx\)\(\i\.\i,{className:\i\.reactionCount,.*?}\),/,
|
|
|
|
replace: "$&$1?null:$self.renderUsers(this.props),"
|
2022-11-18 22:31:53 +00:00
|
|
|
}
|
2023-10-03 00:26:38 +00:00
|
|
|
}, {
|
|
|
|
find: '.displayName="MessageReactionsStore";',
|
|
|
|
replacement: {
|
|
|
|
match: /(?<=CONNECTION_OPEN:function\(\){)(\i)={}/,
|
|
|
|
replace: "$&;$self.reactions=$1"
|
|
|
|
}
|
2022-11-18 22:31:53 +00:00
|
|
|
}],
|
|
|
|
|
|
|
|
renderUsers(props: RootObject) {
|
2022-11-25 17:07:29 +00:00
|
|
|
return props.message.reactions.length > 10 ? null : (
|
2022-11-18 22:31:53 +00:00
|
|
|
<ErrorBoundary noop>
|
|
|
|
<this._renderUsers {...props} />
|
|
|
|
</ErrorBoundary>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2023-02-12 17:58:44 +00:00
|
|
|
_renderUsers({ message, emoji, type }: RootObject) {
|
2022-11-25 17:07:29 +00:00
|
|
|
const forceUpdate = useForceUpdater();
|
|
|
|
React.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]);
|
|
|
|
|
2023-02-12 17:58:44 +00:00
|
|
|
const reactions = getReactionsWithQueue(message, emoji, type);
|
2022-11-25 17:07:29 +00:00
|
|
|
const users = Object.values(reactions).filter(Boolean) as User[];
|
2022-11-18 22:31:53 +00:00
|
|
|
|
2023-02-16 01:00:09 +00:00
|
|
|
for (const user of users) {
|
|
|
|
FluxDispatcher.dispatch({
|
|
|
|
type: "USER_UPDATE",
|
|
|
|
user
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-18 22:31:53 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
style={{ marginLeft: "0.5em", transform: "scale(0.9)" }}
|
|
|
|
>
|
2023-04-23 23:42:38 +00:00
|
|
|
<div onClick={handleClickAvatar}>
|
|
|
|
<UserSummaryItem
|
|
|
|
users={users}
|
|
|
|
guildId={ChannelStore.getChannel(message.channel_id)?.guild_id}
|
|
|
|
renderIcon={false}
|
|
|
|
max={5}
|
|
|
|
showDefaultAvatarsForNullUsers
|
|
|
|
showUserPopout
|
|
|
|
renderMoreUsers={makeRenderMoreUsers(users)}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-11-18 22:31:53 +00:00
|
|
|
</div>
|
|
|
|
);
|
2023-10-03 00:26:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set reactions(value: any) {
|
|
|
|
reactions = value;
|
2022-11-18 22:31:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-10-03 00:26:38 +00:00
|
|
|
interface ReactionCacheEntry {
|
|
|
|
fetched: boolean;
|
|
|
|
users: Record<string, User>;
|
2022-11-18 22:31:53 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 00:26:38 +00:00
|
|
|
interface RootObject {
|
2022-11-18 22:31:53 +00:00
|
|
|
message: Message;
|
|
|
|
readOnly: boolean;
|
|
|
|
isLurking: boolean;
|
|
|
|
isPendingMember: boolean;
|
|
|
|
useChatFontScaling: boolean;
|
2023-10-03 00:26:38 +00:00
|
|
|
emoji: CustomEmoji;
|
2022-11-18 22:31:53 +00:00
|
|
|
count: number;
|
|
|
|
burst_user_ids: any[];
|
|
|
|
burst_count: number;
|
|
|
|
burst_colors: any[];
|
|
|
|
burst_me: boolean;
|
|
|
|
me: boolean;
|
|
|
|
type: number;
|
|
|
|
hideEmoji: boolean;
|
|
|
|
remainingBurstCurrency: number;
|
|
|
|
}
|