Add intentional mentions to edited messages

This commit is contained in:
nexy7574 2025-01-13 17:08:36 +00:00
parent 39dfbb0713
commit cc6b2b2934

View file

@ -19,9 +19,9 @@ import {
as, as,
config, config,
} from 'folds'; } from 'folds';
import { Editor, Transforms } from 'slate'; import {Descendant, Editor, Transforms} from 'slate';
import { ReactEditor } from 'slate-react'; 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 { isKeyHotkey } from 'is-hotkey';
import { import {
AUTOCOMPLETE_PREFIXES, AUTOCOMPLETE_PREFIXES,
@ -42,7 +42,7 @@ import {
toMatrixCustomHTML, toMatrixCustomHTML,
toPlainText, toPlainText,
trimCustomHtml, trimCustomHtml,
useEditor, useEditor, BlockType,
} from '../../../components/editor'; } from '../../../components/editor';
import { useSetting } from '../../../state/hooks/settings'; import { useSetting } from '../../../state/hooks/settings';
import { settingsAtom } from '../../../state/settings'; import { settingsAtom } from '../../../state/settings';
@ -52,6 +52,10 @@ import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { useMatrixClient } from '../../../hooks/useMatrixClient';
import { getEditedEvent, trimReplyFromFormattedBody } from '../../../utils/room'; import { getEditedEvent, trimReplyFromFormattedBody } from '../../../utils/room';
import { mobileOrTablet } from '../../../utils/user-agent'; 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 = { type MessageEditorProps = {
roomId: string; roomId: string;
@ -122,6 +126,36 @@ export const MessageEditor = as<'div', MessageEditorProps>(
body: plainText, body: plainText,
}; };
const userIdMentions = new Set<string>();
// 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)) { if (!customHtmlEqualsPlainText(customHtml, plainText)) {
newContent.format = 'org.matrix.custom.html'; newContent.format = 'org.matrix.custom.html';
newContent.formatted_body = customHtml; newContent.formatted_body = customHtml;