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.
This commit is contained in:
parent
5a1a91f16a
commit
e19127f8ad
11 changed files with 431 additions and 23 deletions
|
@ -16,10 +16,11 @@ limitations under the License.
|
|||
|
||||
import React from "react";
|
||||
import { fireEvent, render, waitFor } from "@testing-library/react";
|
||||
import { MatrixClient, MsgType } from "matrix-js-sdk/src/matrix";
|
||||
import { IContent, MatrixClient, MsgType } from "matrix-js-sdk/src/matrix";
|
||||
import { mocked } from "jest-mock";
|
||||
|
||||
import SendMessageComposer, {
|
||||
attachMentions,
|
||||
createMessageContent,
|
||||
isQuickReaction,
|
||||
} from "../../../../src/components/views/rooms/SendMessageComposer";
|
||||
|
@ -38,6 +39,7 @@ import { mockPlatformPeg } from "../../../test-utils/platform";
|
|||
import { doMaybeLocalRoomAction } from "../../../../src/utils/local-room";
|
||||
import { addTextToComposer } from "../../../test-utils/composer";
|
||||
import dis from "../../../../src/dispatcher/dispatcher";
|
||||
import SettingsStore from "../../../../src/settings/SettingsStore";
|
||||
|
||||
jest.mock("../../../../src/utils/local-room", () => ({
|
||||
doMaybeLocalRoomAction: jest.fn(),
|
||||
|
@ -89,7 +91,7 @@ describe("<SendMessageComposer/>", () => {
|
|||
const documentOffset = new DocumentOffset(11, true);
|
||||
model.update("hello world", "insertText", documentOffset);
|
||||
|
||||
const content = createMessageContent(model, undefined, undefined, permalinkCreator);
|
||||
const content = createMessageContent("@alice:test", model, undefined, undefined, permalinkCreator);
|
||||
|
||||
expect(content).toEqual({
|
||||
body: "hello world",
|
||||
|
@ -102,7 +104,7 @@ describe("<SendMessageComposer/>", () => {
|
|||
const documentOffset = new DocumentOffset(13, true);
|
||||
model.update("hello *world*", "insertText", documentOffset);
|
||||
|
||||
const content = createMessageContent(model, undefined, undefined, permalinkCreator);
|
||||
const content = createMessageContent("@alice:test", model, undefined, undefined, permalinkCreator);
|
||||
|
||||
expect(content).toEqual({
|
||||
body: "hello *world*",
|
||||
|
@ -117,7 +119,7 @@ describe("<SendMessageComposer/>", () => {
|
|||
const documentOffset = new DocumentOffset(22, true);
|
||||
model.update("/me blinks __quickly__", "insertText", documentOffset);
|
||||
|
||||
const content = createMessageContent(model, undefined, undefined, permalinkCreator);
|
||||
const content = createMessageContent("@alice:test", model, undefined, undefined, permalinkCreator);
|
||||
|
||||
expect(content).toEqual({
|
||||
body: "blinks __quickly__",
|
||||
|
@ -133,7 +135,7 @@ describe("<SendMessageComposer/>", () => {
|
|||
model.update("/me ✨sparkles✨", "insertText", documentOffset);
|
||||
expect(model.parts.length).toEqual(4); // Emoji count as non-text
|
||||
|
||||
const content = createMessageContent(model, undefined, undefined, permalinkCreator);
|
||||
const content = createMessageContent("@alice:test", model, undefined, undefined, permalinkCreator);
|
||||
|
||||
expect(content).toEqual({
|
||||
body: "✨sparkles✨",
|
||||
|
@ -147,7 +149,7 @@ describe("<SendMessageComposer/>", () => {
|
|||
|
||||
model.update("//dev/null is my favourite place", "insertText", documentOffset);
|
||||
|
||||
const content = createMessageContent(model, undefined, undefined, permalinkCreator);
|
||||
const content = createMessageContent("@alice:test", model, undefined, undefined, permalinkCreator);
|
||||
|
||||
expect(content).toEqual({
|
||||
body: "/dev/null is my favourite place",
|
||||
|
@ -156,6 +158,196 @@ describe("<SendMessageComposer/>", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("attachMentions", () => {
|
||||
beforeEach(() => {
|
||||
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
||||
(settingName) => settingName === "feature_intentional_mentions",
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.spyOn(SettingsStore, "getValue").mockReset();
|
||||
});
|
||||
|
||||
const partsCreator = createPartCreator();
|
||||
|
||||
it("no mentions", () => {
|
||||
const model = new EditorModel([], partsCreator);
|
||||
const content: IContent = {};
|
||||
attachMentions("@alice:test", content, model, undefined);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
});
|
||||
});
|
||||
|
||||
it("test user mentions", () => {
|
||||
const model = new EditorModel([partsCreator.userPill("Bob", "@bob:test")], partsCreator);
|
||||
const content: IContent = {};
|
||||
attachMentions("@alice:test", content, model, undefined);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": { user_ids: ["@bob:test"] },
|
||||
});
|
||||
});
|
||||
|
||||
it("test reply", () => {
|
||||
// Replying to an event adds the sender to the list of mentioned users.
|
||||
const model = new EditorModel([], partsCreator);
|
||||
let replyToEvent = mkEvent({
|
||||
type: "m.room.message",
|
||||
user: "@bob:test",
|
||||
room: "!abc:test",
|
||||
content: { "org.matrix.msc3952.mentions": {} },
|
||||
event: true,
|
||||
});
|
||||
let content: IContent = {};
|
||||
attachMentions("@alice:test", content, model, replyToEvent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": { user_ids: ["@bob:test"] },
|
||||
});
|
||||
|
||||
// It also adds any other mentioned users, but removes yourself.
|
||||
replyToEvent = mkEvent({
|
||||
type: "m.room.message",
|
||||
user: "@bob:test",
|
||||
room: "!abc:test",
|
||||
content: { "org.matrix.msc3952.mentions": { user_ids: ["@alice:test", "@charlie:test"] } },
|
||||
event: true,
|
||||
});
|
||||
content = {};
|
||||
attachMentions("@alice:test", content, model, replyToEvent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": { user_ids: ["@bob:test", "@charlie:test"] },
|
||||
});
|
||||
});
|
||||
|
||||
it("test room mention", () => {
|
||||
const model = new EditorModel([partsCreator.atRoomPill("@room")], partsCreator);
|
||||
const content: IContent = {};
|
||||
attachMentions("@alice:test", content, model, undefined);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": { room: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("test reply to room mention", () => {
|
||||
// Replying to a room mention shouldn't automatically be a room mention.
|
||||
const model = new EditorModel([], partsCreator);
|
||||
const replyToEvent = mkEvent({
|
||||
type: "m.room.message",
|
||||
user: "@alice:test",
|
||||
room: "!abc:test",
|
||||
content: { "org.matrix.msc3952.mentions": { room: true } },
|
||||
event: true,
|
||||
});
|
||||
const content: IContent = {};
|
||||
attachMentions("@alice:test", content, model, replyToEvent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
});
|
||||
});
|
||||
|
||||
it("test broken mentions", () => {
|
||||
// Replying to a room mention shouldn't automatically be a room mention.
|
||||
const model = new EditorModel([], partsCreator);
|
||||
const replyToEvent = mkEvent({
|
||||
type: "m.room.message",
|
||||
user: "@alice:test",
|
||||
room: "!abc:test",
|
||||
// @ts-ignore - Purposefully testing invalid data.
|
||||
content: { "org.matrix.msc3952.mentions": { user_ids: "@bob:test" } },
|
||||
event: true,
|
||||
});
|
||||
const content: IContent = {};
|
||||
attachMentions("@alice:test", content, model, replyToEvent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
});
|
||||
});
|
||||
|
||||
describe("attachMentions with edit", () => {
|
||||
it("no mentions", () => {
|
||||
const model = new EditorModel([], partsCreator);
|
||||
const content: IContent = { "m.new_content": {} };
|
||||
const prevContent: IContent = {};
|
||||
attachMentions("@alice:test", content, model, undefined, prevContent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
"m.new_content": { "org.matrix.msc3952.mentions": {} },
|
||||
});
|
||||
});
|
||||
|
||||
it("mentions do not propagate", () => {
|
||||
const model = new EditorModel([], partsCreator);
|
||||
const content: IContent = { "m.new_content": {} };
|
||||
const prevContent: IContent = {
|
||||
"org.matrix.msc3952.mentions": { user_ids: ["@bob:test"], room: true },
|
||||
};
|
||||
attachMentions("@alice:test", content, model, undefined, prevContent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
"m.new_content": { "org.matrix.msc3952.mentions": {} },
|
||||
});
|
||||
});
|
||||
|
||||
it("test user mentions", () => {
|
||||
const model = new EditorModel([partsCreator.userPill("Bob", "@bob:test")], partsCreator);
|
||||
const content: IContent = { "m.new_content": {} };
|
||||
const prevContent: IContent = {};
|
||||
attachMentions("@alice:test", content, model, undefined, prevContent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": { user_ids: ["@bob:test"] },
|
||||
"m.new_content": { "org.matrix.msc3952.mentions": { user_ids: ["@bob:test"] } },
|
||||
});
|
||||
});
|
||||
|
||||
it("test prev user mentions", () => {
|
||||
const model = new EditorModel([partsCreator.userPill("Bob", "@bob:test")], partsCreator);
|
||||
const content: IContent = { "m.new_content": {} };
|
||||
const prevContent: IContent = { "org.matrix.msc3952.mentions": { user_ids: ["@bob:test"] } };
|
||||
attachMentions("@alice:test", content, model, undefined, prevContent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
"m.new_content": { "org.matrix.msc3952.mentions": { user_ids: ["@bob:test"] } },
|
||||
});
|
||||
});
|
||||
|
||||
it("test room mention", () => {
|
||||
const model = new EditorModel([partsCreator.atRoomPill("@room")], partsCreator);
|
||||
const content: IContent = { "m.new_content": {} };
|
||||
const prevContent: IContent = {};
|
||||
attachMentions("@alice:test", content, model, undefined, prevContent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": { room: true },
|
||||
"m.new_content": { "org.matrix.msc3952.mentions": { room: true } },
|
||||
});
|
||||
});
|
||||
|
||||
it("test prev room mention", () => {
|
||||
const model = new EditorModel([partsCreator.atRoomPill("@room")], partsCreator);
|
||||
const content: IContent = { "m.new_content": {} };
|
||||
const prevContent: IContent = { "org.matrix.msc3952.mentions": { room: true } };
|
||||
attachMentions("@alice:test", content, model, undefined, prevContent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
"m.new_content": { "org.matrix.msc3952.mentions": { room: true } },
|
||||
});
|
||||
});
|
||||
|
||||
it("test broken mentions", () => {
|
||||
// Replying to a room mention shouldn't automatically be a room mention.
|
||||
const model = new EditorModel([], partsCreator);
|
||||
const content: IContent = { "m.new_content": {} };
|
||||
// @ts-ignore - Purposefully testing invalid data.
|
||||
const prevContent: IContent = { "org.matrix.msc3952.mentions": { user_ids: "@bob:test" } };
|
||||
attachMentions("@alice:test", content, model, undefined, prevContent);
|
||||
expect(content).toEqual({
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
"m.new_content": { "org.matrix.msc3952.mentions": {} },
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("functions correctly mounted", () => {
|
||||
const mockClient = createTestClient();
|
||||
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
||||
|
|
|
@ -26,6 +26,8 @@ import { IUpload, VoiceMessageRecording } from "../../../../src/audio/VoiceMessa
|
|||
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
||||
import { VoiceRecordingStore } from "../../../../src/stores/VoiceRecordingStore";
|
||||
import { PlaybackClock } from "../../../../src/audio/PlaybackClock";
|
||||
import { mkEvent } from "../../../test-utils";
|
||||
import SettingsStore from "../../../../src/settings/SettingsStore";
|
||||
|
||||
jest.mock("../../../../src/utils/local-room", () => ({
|
||||
doMaybeLocalRoomAction: jest.fn(),
|
||||
|
@ -50,6 +52,7 @@ describe("<VoiceRecordComposerTile/>", () => {
|
|||
|
||||
beforeEach(() => {
|
||||
mockClient = {
|
||||
getSafeUserId: jest.fn().mockReturnValue("@alice:example.com"),
|
||||
sendMessage: jest.fn(),
|
||||
} as unknown as MatrixClient;
|
||||
MatrixClientPeg.get = () => mockClient;
|
||||
|
@ -99,6 +102,10 @@ describe("<VoiceRecordComposerTile/>", () => {
|
|||
return fn(roomId);
|
||||
},
|
||||
);
|
||||
|
||||
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
||||
(settingName) => settingName === "feature_intentional_mentions",
|
||||
);
|
||||
});
|
||||
|
||||
describe("send", () => {
|
||||
|
@ -127,6 +134,61 @@ describe("<VoiceRecordComposerTile/>", () => {
|
|||
"org.matrix.msc1767.text": "Voice message",
|
||||
"org.matrix.msc3245.voice": {},
|
||||
"url": "mxc://example.com/voice",
|
||||
"org.matrix.msc3952.mentions": {},
|
||||
});
|
||||
});
|
||||
|
||||
it("reply with voice recording", async () => {
|
||||
const room = {
|
||||
roomId,
|
||||
} as unknown as Room;
|
||||
|
||||
const replyToEvent = mkEvent({
|
||||
type: "m.room.message",
|
||||
user: "@bob:test",
|
||||
room: roomId,
|
||||
content: {},
|
||||
event: true,
|
||||
});
|
||||
|
||||
const props = {
|
||||
room,
|
||||
ref: voiceRecordComposerTile,
|
||||
permalinkCreator: new RoomPermalinkCreator(room),
|
||||
replyToEvent,
|
||||
};
|
||||
render(<VoiceRecordComposerTile {...props} />);
|
||||
|
||||
await voiceRecordComposerTile.current!.send();
|
||||
expect(mockClient.sendMessage).toHaveBeenCalledWith(roomId, {
|
||||
"body": "Voice message",
|
||||
"file": undefined,
|
||||
"info": {
|
||||
duration: 1337000,
|
||||
mimetype: "audio/ogg",
|
||||
size: undefined,
|
||||
},
|
||||
"msgtype": MsgType.Audio,
|
||||
"org.matrix.msc1767.audio": {
|
||||
duration: 1337000,
|
||||
waveform: [1434, 2560, 3686],
|
||||
},
|
||||
"org.matrix.msc1767.file": {
|
||||
file: undefined,
|
||||
mimetype: "audio/ogg",
|
||||
name: "Voice message.ogg",
|
||||
size: undefined,
|
||||
url: "mxc://example.com/voice",
|
||||
},
|
||||
"org.matrix.msc1767.text": "Voice message",
|
||||
"org.matrix.msc3245.voice": {},
|
||||
"url": "mxc://example.com/voice",
|
||||
"m.relates_to": {
|
||||
"m.in_reply_to": {
|
||||
event_id: replyToEvent.getId(),
|
||||
},
|
||||
},
|
||||
"org.matrix.msc3952.mentions": { user_ids: ["@bob:test"] },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue