refactor(room): move reaction code to utils
This commit is contained in:
2 files changed
+31
-24
No files matched your search
@@ -51,7 +51,7 @@ import {
|
||||
import { isKeyHotkey } from 'is-hotkey';
|
||||
import { Opts as LinkifyOpts } from 'linkifyjs';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { eventWithShortcode, factoryEventSentBy, getMxIdLocalPart } from '$utils/matrix';
|
||||
import { getMxIdLocalPart, toggleReaction } from '$utils/matrix';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { ItemRange, useVirtualPaginator } from '$hooks/useVirtualPaginator';
|
||||
import { useAlive } from '$hooks/useAlive';
|
||||
@@ -85,7 +85,6 @@ import {
|
||||
getEventReactions,
|
||||
getLatestEditableEvt,
|
||||
getMemberDisplayName,
|
||||
getReactionContent,
|
||||
isMembershipChanged,
|
||||
reactionOrEditEvent,
|
||||
} from '$utils/room';
|
||||
@@ -1166,26 +1165,8 @@ export function RoomTimeline({
|
||||
);
|
||||
|
||||
const handleReactionToggle = useCallback(
|
||||
(targetEventId: string, key: string, shortcode?: string) => {
|
||||
const relations = getEventReactions(room.getUnfilteredTimelineSet(), targetEventId);
|
||||
const allReactions = relations?.getSortedAnnotationsByKey() ?? [];
|
||||
const [, reactionsSet] = allReactions.find(([k]: [string, any]) => k === key) ?? [];
|
||||
const reactions: MatrixEvent[] = reactionsSet ? Array.from(reactionsSet) : [];
|
||||
const myReaction = reactions.find(factoryEventSentBy(mx.getUserId()!));
|
||||
|
||||
if (myReaction && !!(myReaction as any)?.isRelation()) {
|
||||
mx.redactEvent(room.roomId, (myReaction as any).getId());
|
||||
return;
|
||||
}
|
||||
const rShortcode =
|
||||
shortcode ||
|
||||
((reactions.find(eventWithShortcode) as any)?.getContent().shortcode as string | undefined);
|
||||
mx.sendEvent(
|
||||
room.roomId,
|
||||
MessageEvent.Reaction as any,
|
||||
getReactionContent(targetEventId, key, rShortcode)
|
||||
);
|
||||
},
|
||||
(targetEventId: string, key: string, shortcode?: string) =>
|
||||
toggleReaction(mx, room, targetEventId, key, shortcode),
|
||||
[mx, room]
|
||||
);
|
||||
|
||||
|
||||
+28
-2
@@ -16,8 +16,8 @@ import {
|
||||
import to from 'await-to-js';
|
||||
import { IImageInfo, IThumbnailContent, IVideoInfo } from '$types/matrix/common';
|
||||
import { AccountDataEvent } from '$types/matrix/accountData';
|
||||
import { Membership, StateEvent } from '$types/matrix/room';
|
||||
import { getStateEvent } from './room';
|
||||
import { Membership, MessageEvent, StateEvent } from '$types/matrix/room';
|
||||
import { getEventReactions, getReactionContent, getStateEvent } from './room';
|
||||
|
||||
const DOMAIN_REGEX = /\b(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}\b/;
|
||||
|
||||
@@ -382,3 +382,29 @@ export const creatorsSupported = (version: string): boolean => {
|
||||
const unsupportedVersion = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'];
|
||||
return !unsupportedVersion.includes(version);
|
||||
};
|
||||
|
||||
export const toggleReaction = (
|
||||
mx: MatrixClient,
|
||||
room: Room,
|
||||
targetEventId: string,
|
||||
key: string,
|
||||
shortcode?: string
|
||||
) => {
|
||||
const relations = getEventReactions(room.getUnfilteredTimelineSet(), targetEventId);
|
||||
const allReactions = relations?.getSortedAnnotationsByKey() ?? [];
|
||||
const [, reactionsSet] = allReactions.find(([k]: [string, any]) => k === key) ?? [];
|
||||
const reactions: MatrixEvent[] = reactionsSet ? Array.from(reactionsSet) : [];
|
||||
const myReaction = reactions.find(factoryEventSentBy(mx.getUserId()!));
|
||||
|
||||
if (myReaction && !!(myReaction as any)?.isRelation()) {
|
||||
mx.redactEvent(room.roomId, (myReaction as any).getId());
|
||||
return;
|
||||
}
|
||||
const rShortcode =
|
||||
shortcode || (reactions.find(eventWithShortcode)?.getContent().shortcode as string | undefined);
|
||||
mx.sendEvent(
|
||||
room.roomId,
|
||||
MessageEvent.Reaction as any,
|
||||
getReactionContent(targetEventId, key, rShortcode)
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user