Fix WhoReacted (#487)

Co-authored-by: Ven <vendicated@riseup.net>
This commit is contained in:
Nuckyz 2023-02-12 14:58:44 -03:00 committed by GitHub
parent d95be1acba
commit 2489bc6831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -32,12 +32,13 @@ const ReactionStore = findByPropsLazy("getReactions");
const queue = new Queue(); const queue = new Queue();
function fetchReactions(msg: Message, emoji: ReactionEmoji) { function fetchReactions(msg: Message, emoji: ReactionEmoji, type: number) {
const key = emoji.name + (emoji.id ? `:${emoji.id}` : ""); const key = emoji.name + (emoji.id ? `:${emoji.id}` : "");
return RestAPI.get({ return RestAPI.get({
url: `/channels/${msg.channel_id}/messages/${msg.id}/reactions/${key}`, url: `/channels/${msg.channel_id}/messages/${msg.id}/reactions/${key}`,
query: { query: {
limit: 100 limit: 100,
type
}, },
oldFormErrors: true oldFormErrors: true
}) })
@ -46,18 +47,19 @@ function fetchReactions(msg: Message, emoji: ReactionEmoji) {
channelId: msg.channel_id, channelId: msg.channel_id,
messageId: msg.id, messageId: msg.id,
users: res.body, users: res.body,
emoji emoji,
reactionType: type
})) }))
.catch(console.error) .catch(console.error)
.finally(() => sleep(250)); .finally(() => sleep(250));
} }
function getReactionsWithQueue(msg: Message, e: ReactionEmoji) { function getReactionsWithQueue(msg: Message, e: ReactionEmoji, type: number) {
const key = `${msg.id}:${e.name}:${e.id ?? ""}`; const key = `${msg.id}:${e.name}:${e.id ?? ""}:${type}`;
const cache = ReactionStore.__getLocalVars().reactions[key] ??= { fetched: false, users: {} }; const cache = ReactionStore.__getLocalVars().reactions[key] ??= { fetched: false, users: {} };
if (!cache.fetched) { if (!cache.fetched) {
queue.unshift(() => queue.unshift(() =>
fetchReactions(msg, e) fetchReactions(msg, e, type)
); );
cache.fetched = true; cache.fetched = true;
} }
@ -104,7 +106,7 @@ export default definePlugin({
); );
}, },
_renderUsers({ message, emoji }: RootObject) { _renderUsers({ message, emoji, type }: RootObject) {
const forceUpdate = useForceUpdater(); const forceUpdate = useForceUpdater();
React.useEffect(() => { React.useEffect(() => {
const cb = (e: any) => { const cb = (e: any) => {
@ -116,7 +118,7 @@ export default definePlugin({
return () => FluxDispatcher.unsubscribe("MESSAGE_REACTION_ADD_USERS", cb); return () => FluxDispatcher.unsubscribe("MESSAGE_REACTION_ADD_USERS", cb);
}, [message.id]); }, [message.id]);
const reactions = getReactionsWithQueue(message, emoji); const reactions = getReactionsWithQueue(message, emoji, type);
const users = Object.values(reactions).filter(Boolean) as User[]; const users = Object.values(reactions).filter(Boolean) as User[];
return ( return (