From cc6b2b2934634d94d1e2fc1452533ee0f076ef6e Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Mon, 13 Jan 2025 17:08:36 +0000 Subject: [PATCH] Add intentional mentions to edited messages --- .../features/room/message/MessageEditor.tsx | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/app/features/room/message/MessageEditor.tsx b/src/app/features/room/message/MessageEditor.tsx index 0c995030..f35f2f41 100644 --- a/src/app/features/room/message/MessageEditor.tsx +++ b/src/app/features/room/message/MessageEditor.tsx @@ -19,9 +19,9 @@ import { as, config, } from 'folds'; -import { Editor, Transforms } from 'slate'; +import {Descendant, Editor, Transforms} from 'slate'; import { ReactEditor } from 'slate-react'; -import { IContent, MatrixEvent, RelationType, Room } from 'matrix-js-sdk'; +import {IContent, IMentions, MatrixEvent, RelationType, Room} from 'matrix-js-sdk'; import { isKeyHotkey } from 'is-hotkey'; import { AUTOCOMPLETE_PREFIXES, @@ -42,7 +42,7 @@ import { toMatrixCustomHTML, toPlainText, trimCustomHtml, - useEditor, + useEditor, BlockType, } from '../../../components/editor'; import { useSetting } from '../../../state/hooks/settings'; import { settingsAtom } from '../../../state/settings'; @@ -52,6 +52,10 @@ import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { getEditedEvent, trimReplyFromFormattedBody } from '../../../utils/room'; import { mobileOrTablet } from '../../../utils/user-agent'; +import {InlineElement} from "../../../components/editor/slate"; +import {getCanonicalAliasOrRoomId, isUserId} from "../../../utils/matrix"; +import {useAtom} from "jotai/index"; +import {roomIdToReplyDraftAtomFamily} from "../../../state/room/roomInputDrafts"; type MessageEditorProps = { roomId: string; @@ -122,6 +126,36 @@ export const MessageEditor = as<'div', MessageEditorProps>( body: plainText, }; + const userIdMentions = new Set(); + // if (replyDraft && replyDraft.userId !== mx.getUserId()) { + // userIdMentions.add(replyDraft.userId); + // } + // TODO: Get the original message's reply to pick up the mention + let mentionsRoom = false; + editor.children.forEach((node: Descendant): void => { + if ("type" in node && node.type === BlockType.Paragraph) { + node.children?.forEach((child: InlineElement): void => { + if ("type" in child && child.type === BlockType.Mention) { + const mention = child; + if (mention.id === getCanonicalAliasOrRoomId(mx, roomId)) { + mentionsRoom = true + } else if (isUserId(mention.id) && mention.id !== mx.getUserId()) { + userIdMentions.add(mention.id) + } + } + }) + } + }) + const mMentions: IMentions = {} + if (userIdMentions.size > 0) { + mMentions.user_ids = Array.from(userIdMentions) + } + if(mentionsRoom) { + mMentions.room = true + } + + newContent["m.mentions"] = mMentions + if (!customHtmlEqualsPlainText(customHtml, plainText)) { newContent.format = 'org.matrix.custom.html'; newContent.formatted_body = customHtml;