Skip to content

Commit

Permalink
Add intentional mentions to edited messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nexy7574 committed Jan 13, 2025
1 parent 39dfbb0 commit cc6b2b2
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/app/features/room/message/MessageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -122,6 +126,36 @@ export const MessageEditor = as<'div', MessageEditorProps>(
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)) {
newContent.format = 'org.matrix.custom.html';
newContent.formatted_body = customHtml;
Expand Down

0 comments on commit cc6b2b2

Please sign in to comment.