Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit e19127f

Browse files
authored
Implement MSC3952: intentional mentions (#9983)
Implements the intentional mentions feature of MSC3952 (behind a labs flag). If enabled, this will send an org.matrix.msc3952.mentions property on events that will contain the user IDs and/or whether the room is being mentioned. These mentions also gets propagated via some custom behaviour for replies and edits.
1 parent 5a1a91f commit e19127f

File tree

11 files changed

+431
-23
lines changed

11 files changed

+431
-23
lines changed

src/ContentMessages.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import ErrorDialog from "./components/views/dialogs/ErrorDialog";
4848
import UploadFailureDialog from "./components/views/dialogs/UploadFailureDialog";
4949
import UploadConfirmDialog from "./components/views/dialogs/UploadConfirmDialog";
5050
import { createThumbnail } from "./utils/image-media";
51-
import { attachRelation } from "./components/views/rooms/SendMessageComposer";
51+
import { attachMentions, attachRelation } from "./components/views/rooms/SendMessageComposer";
5252
import { doMaybeLocalRoomAction } from "./utils/local-room";
5353
import { SdkContextClass } from "./contexts/SDKContext";
5454

@@ -492,6 +492,8 @@ export default class ContentMessages {
492492
msgtype: MsgType.File, // set more specifically later
493493
};
494494

495+
// Attach mentions, which really only applies if there's a replyToEvent.
496+
attachMentions(matrixClient.getSafeUserId(), content, null, replyToEvent);
495497
attachRelation(content, relation);
496498
if (replyToEvent) {
497499
addReplyToMessageContent(content, replyToEvent, {

src/MatrixClientPeg.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ class MatrixClientPegClass implements IMatrixClientPeg {
235235
SlidingSyncManager.instance.startSpidering(100, 50); // 100 rooms at a time, 50ms apart
236236
}
237237

238+
opts.intentionalMentions = SettingsStore.getValue("feature_intentional_mentions");
239+
238240
// Connect the matrix client to the dispatcher and setting handlers
239241
MatrixActionCreators.start(this.matrixClient);
240242
MatrixClientBackedSettingsHandler.matrixClient = this.matrixClient;

src/components/views/rooms/EditMessageComposer.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
4848
import { PosthogAnalytics } from "../../../PosthogAnalytics";
4949
import { editorRoomKey, editorStateKey } from "../../../Editing";
5050
import DocumentOffset from "../../../editor/offset";
51+
import { attachMentions, attachRelation } from "./SendMessageComposer";
5152

5253
function getHtmlReplyFallback(mxEvent: MatrixEvent): string {
5354
const html = mxEvent.getContent().formatted_body;
@@ -90,8 +91,9 @@ export function createEditContent(model: EditorModel, editedEvent: MatrixEvent):
9091
body: body,
9192
};
9293
const contentBody: IContent = {
93-
msgtype: newContent.msgtype,
94-
body: `${plainPrefix} * ${body}`,
94+
"msgtype": newContent.msgtype,
95+
"body": `${plainPrefix} * ${body}`,
96+
"m.new_content": newContent,
9597
};
9698

9799
const formattedBody = htmlSerializeIfNeeded(model, {
@@ -105,16 +107,15 @@ export function createEditContent(model: EditorModel, editedEvent: MatrixEvent):
105107
contentBody.formatted_body = `${htmlPrefix} * ${formattedBody}`;
106108
}
107109

108-
return Object.assign(
109-
{
110-
"m.new_content": newContent,
111-
"m.relates_to": {
112-
rel_type: "m.replace",
113-
event_id: editedEvent.getId(),
114-
},
115-
},
116-
contentBody,
117-
);
110+
// Build the mentions properties for both the content and new_content.
111+
//
112+
// TODO If this is a reply we need to include all the users from it.
113+
if (SettingsStore.getValue("feature_intentional_mentions")) {
114+
attachMentions(editedEvent.sender!.userId, contentBody, model, undefined, editedEvent.getContent());
115+
}
116+
attachRelation(contentBody, { rel_type: "m.replace", event_id: editedEvent.getId() });
117+
118+
return contentBody;
118119
}
119120

120121
interface IEditMessageComposerProps extends MatrixClientProps {

src/components/views/rooms/SendMessageComposer.tsx

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
import React, { ClipboardEvent, createRef, KeyboardEvent } from "react";
1818
import EMOJI_REGEX from "emojibase-regex";
19-
import { IContent, MatrixEvent, IEventRelation } from "matrix-js-sdk/src/models/event";
19+
import { IContent, MatrixEvent, IEventRelation, IMentions } from "matrix-js-sdk/src/models/event";
2020
import { DebouncedFunc, throttle } from "lodash";
2121
import { EventType, RelationType } from "matrix-js-sdk/src/@types/event";
2222
import { logger } from "matrix-js-sdk/src/logger";
@@ -36,7 +36,7 @@ import {
3636
unescapeMessage,
3737
} from "../../../editor/serialize";
3838
import BasicMessageComposer, { REGEX_EMOTICON } from "./BasicMessageComposer";
39-
import { CommandPartCreator, Part, PartCreator, SerializedPart } from "../../../editor/parts";
39+
import { CommandPartCreator, Part, PartCreator, SerializedPart, Type } from "../../../editor/parts";
4040
import { findEditableEvent } from "../../../utils/EventUtils";
4141
import SendHistoryManager from "../../../SendHistoryManager";
4242
import { CommandCategories } from "../../../SlashCommands";
@@ -60,6 +60,102 @@ import { PosthogAnalytics } from "../../../PosthogAnalytics";
6060
import { addReplyToMessageContent } from "../../../utils/Reply";
6161
import { doMaybeLocalRoomAction } from "../../../utils/local-room";
6262

63+
/**
64+
* Build the mentions information based on the editor model (and any related events):
65+
*
66+
* 1. Search the model parts for room or user pills and fill in the mentions object.
67+
* 2. If this is a reply to another event, include any user mentions from that
68+
* (but do not include a room mention).
69+
*
70+
* @param sender - The Matrix ID of the user sending the event.
71+
* @param content - The event content.
72+
* @param model - The editor model to search for mentions, null if there is no editor.
73+
* @param replyToEvent - The event being replied to or undefined if it is not a reply.
74+
* @param editedContent - The content of the parent event being edited.
75+
*/
76+
export function attachMentions(
77+
sender: string,
78+
content: IContent,
79+
model: EditorModel | null,
80+
replyToEvent: MatrixEvent | undefined,
81+
editedContent: IContent | null = null,
82+
): void {
83+
// If this feature is disabled, do nothing.
84+
if (!SettingsStore.getValue("feature_intentional_mentions")) {
85+
return;
86+
}
87+
88+
// The mentions property *always* gets included to disable legacy push rules.
89+
const mentions: IMentions = (content["org.matrix.msc3952.mentions"] = {});
90+
91+
const userMentions = new Set<string>();
92+
let roomMention = false;
93+
94+
// If there's a reply, initialize the mentioned users as the sender of that
95+
// event + any mentioned users in that event.
96+
if (replyToEvent) {
97+
userMentions.add(replyToEvent.sender!.userId);
98+
// TODO What do we do if the reply event *doeesn't* have this property?
99+
// Try to fish out replies from the contents?
100+
const userIds = replyToEvent.getContent()["org.matrix.msc3952.mentions"]?.user_ids;
101+
if (Array.isArray(userIds)) {
102+
userIds.forEach((userId) => userMentions.add(userId));
103+
}
104+
}
105+
106+
// If user provided content is available, check to see if any users are mentioned.
107+
if (model) {
108+
// Add any mentioned users in the current content.
109+
for (const part of model.parts) {
110+
if (part.type === Type.UserPill) {
111+
userMentions.add(part.resourceId);
112+
} else if (part.type === Type.AtRoomPill) {
113+
roomMention = true;
114+
}
115+
}
116+
}
117+
118+
// Ensure the *current* user isn't listed in the mentioned users.
119+
userMentions.delete(sender);
120+
121+
// Finally, if this event is editing a previous event, only include users who
122+
// were not previously mentioned and a room mention if the previous event was
123+
// not a room mention.
124+
if (editedContent) {
125+
// First, the new event content gets the *full* set of users.
126+
const newContent = content["m.new_content"];
127+
const newMentions: IMentions = (newContent["org.matrix.msc3952.mentions"] = {});
128+
129+
// Only include the users/room if there is any content.
130+
if (userMentions.size) {
131+
newMentions.user_ids = [...userMentions];
132+
}
133+
if (roomMention) {
134+
newMentions.room = true;
135+
}
136+
137+
// Fetch the mentions from the original event and remove any previously
138+
// mentioned users.
139+
const prevMentions = editedContent["org.matrix.msc3952.mentions"];
140+
if (Array.isArray(prevMentions?.user_ids)) {
141+
prevMentions!.user_ids.forEach((userId) => userMentions.delete(userId));
142+
}
143+
144+
// If the original event mentioned the room, nothing to do here.
145+
if (prevMentions?.room) {
146+
roomMention = false;
147+
}
148+
}
149+
150+
// Only include the users/room if there is any content.
151+
if (userMentions.size) {
152+
mentions.user_ids = [...userMentions];
153+
}
154+
if (roomMention) {
155+
mentions.room = true;
156+
}
157+
}
158+
63159
// Merges favouring the given relation
64160
export function attachRelation(content: IContent, relation?: IEventRelation): void {
65161
if (relation) {
@@ -72,6 +168,7 @@ export function attachRelation(content: IContent, relation?: IEventRelation): vo
72168

73169
// exported for tests
74170
export function createMessageContent(
171+
sender: string,
75172
model: EditorModel,
76173
replyToEvent: MatrixEvent | undefined,
77174
relation: IEventRelation | undefined,
@@ -102,6 +199,9 @@ export function createMessageContent(
102199
content.formatted_body = formattedBody;
103200
}
104201

202+
// Build the mentions property and add it to the event content.
203+
attachMentions(sender, content, model, replyToEvent);
204+
105205
attachRelation(content, relation);
106206
if (replyToEvent) {
107207
addReplyToMessageContent(content, replyToEvent, {
@@ -381,6 +481,8 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
381481
}
382482

383483
if (cmd.category === CommandCategories.messages || cmd.category === CommandCategories.effects) {
484+
// Attach any mentions which might be contained in the command content.
485+
attachMentions(this.props.mxClient.getSafeUserId(), content, model, replyToEvent);
384486
attachRelation(content, this.props.relation);
385487
if (replyToEvent) {
386488
addReplyToMessageContent(content, replyToEvent, {
@@ -413,6 +515,7 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
413515
const { roomId } = this.props.room;
414516
if (!content) {
415517
content = createMessageContent(
518+
this.props.mxClient.getSafeUserId(),
416519
model,
417520
replyToEvent,
418521
this.props.relation,

src/components/views/rooms/VoiceRecordComposerTile.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import InlineSpinner from "../elements/InlineSpinner";
3939
import { PlaybackManager } from "../../../audio/PlaybackManager";
4040
import { doMaybeLocalRoomAction } from "../../../utils/local-room";
4141
import defaultDispatcher from "../../../dispatcher/dispatcher";
42-
import { attachRelation } from "./SendMessageComposer";
42+
import { attachMentions, attachRelation } from "./SendMessageComposer";
4343
import { addReplyToMessageContent } from "../../../utils/Reply";
4444
import { RoomPermalinkCreator } from "../../../utils/permalinks/Permalinks";
4545
import RoomContext from "../../../contexts/RoomContext";
@@ -129,6 +129,8 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
129129
this.state.recorder.getPlayback().thumbnailWaveform.map((v) => Math.round(v * 1024)),
130130
);
131131

132+
// Attach mentions, which really only applies if there's a replyToEvent.
133+
attachMentions(MatrixClientPeg.get().getSafeUserId(), content, null, replyToEvent);
132134
attachRelation(content, relation);
133135
if (replyToEvent) {
134136
addReplyToMessageContent(content, replyToEvent, {

src/components/views/rooms/wysiwyg_composer/utils/createMessageContent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ export async function createMessageContent(
125125

126126
const newRelation = isEditing ? { ...relation, rel_type: "m.replace", event_id: editedEvent.getId() } : relation;
127127

128+
// TODO Do we need to attach mentions here?
129+
// TODO Handle editing?
128130
attachRelation(content, newRelation);
129131

130132
if (!isEditing && replyToEvent && permalinkCreator) {

src/i18n/strings/en_EN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@
987987
"Show polls button": "Show polls button",
988988
"Insert a trailing colon after user mentions at the start of a message": "Insert a trailing colon after user mentions at the start of a message",
989989
"Hide notification dot (only display counters badges)": "Hide notification dot (only display counters badges)",
990+
"Enable intentional mentions": "Enable intentional mentions",
990991
"Use a more compact 'Modern' layout": "Use a more compact 'Modern' layout",
991992
"Show a placeholder for removed messages": "Show a placeholder for removed messages",
992993
"Show join/leave messages (invites/removes/bans unaffected)": "Show join/leave messages (invites/removes/bans unaffected)",

src/settings/Settings.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,17 @@ export const SETTINGS: { [setting: string]: ISetting } = {
540540
labsGroup: LabGroup.Rooms,
541541
default: false,
542542
},
543+
// MSC3952 intentional mentions support.
544+
"feature_intentional_mentions": {
545+
isFeature: true,
546+
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS_WITH_CONFIG,
547+
displayName: _td("Enable intentional mentions"),
548+
labsGroup: LabGroup.Rooms,
549+
default: false,
550+
controller: new ServerSupportUnstableFeatureController("feature_intentional_mentions", defaultWatchManager, [
551+
["org.matrix.msc3952_intentional_mentions"],
552+
]),
553+
},
543554
"useCompactLayout": {
544555
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS,
545556
displayName: _td("Use a more compact 'Modern' layout"),

test/ContentMessages-test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import encrypt, { IEncryptedFile } from "matrix-encrypt-attachment";
2121

2222
import ContentMessages, { UploadCanceledError, uploadFile } from "../src/ContentMessages";
2323
import { doMaybeLocalRoomAction } from "../src/utils/local-room";
24-
import { createTestClient } from "./test-utils";
24+
import { createTestClient, mkEvent } from "./test-utils";
2525
import { BlurhashEncoder } from "../src/BlurhashEncoder";
26+
import SettingsStore from "../src/settings/SettingsStore";
2627

2728
jest.mock("matrix-encrypt-attachment", () => ({ encryptAttachment: jest.fn().mockResolvedValue({}) }));
2829

@@ -51,6 +52,7 @@ describe("ContentMessages", () => {
5152

5253
beforeEach(() => {
5354
client = {
55+
getSafeUserId: jest.fn().mockReturnValue("@alice:test"),
5456
sendStickerMessage: jest.fn(),
5557
sendMessage: jest.fn(),
5658
isRoomEncrypted: jest.fn().mockReturnValue(false),
@@ -221,6 +223,34 @@ describe("ContentMessages", () => {
221223
expect(upload.total).toBe(1234);
222224
await prom;
223225
});
226+
227+
it("properly handles replies", async () => {
228+
jest.spyOn(SettingsStore, "getValue").mockImplementation(
229+
(settingName) => settingName === "feature_intentional_mentions",
230+
);
231+
232+
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" });
233+
const file = new File([], "fileName", { type: "image/jpeg" });
234+
const replyToEvent = mkEvent({
235+
type: "m.room.message",
236+
user: "@bob:test",
237+
room: roomId,
238+
content: {},
239+
event: true,
240+
});
241+
await contentMessages.sendContentToRoom(file, roomId, undefined, client, replyToEvent);
242+
expect(client.sendMessage).toHaveBeenCalledWith(
243+
roomId,
244+
null,
245+
expect.objectContaining({
246+
"url": "mxc://server/file",
247+
"msgtype": "m.image",
248+
"org.matrix.msc3952.mentions": {
249+
user_ids: ["@bob:test"],
250+
},
251+
}),
252+
);
253+
});
224254
});
225255

226256
describe("getCurrentUploads", () => {

0 commit comments

Comments
 (0)