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,7 +16,7 @@ limitations under the License.
|
|||
|
||||
import React, { ClipboardEvent, createRef, KeyboardEvent } from "react";
|
||||
import EMOJI_REGEX from "emojibase-regex";
|
||||
import { IContent, MatrixEvent, IEventRelation } from "matrix-js-sdk/src/models/event";
|
||||
import { IContent, MatrixEvent, IEventRelation, IMentions } from "matrix-js-sdk/src/models/event";
|
||||
import { DebouncedFunc, throttle } from "lodash";
|
||||
import { EventType, RelationType } from "matrix-js-sdk/src/@types/event";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
@ -36,7 +36,7 @@ import {
|
|||
unescapeMessage,
|
||||
} from "../../../editor/serialize";
|
||||
import BasicMessageComposer, { REGEX_EMOTICON } from "./BasicMessageComposer";
|
||||
import { CommandPartCreator, Part, PartCreator, SerializedPart } from "../../../editor/parts";
|
||||
import { CommandPartCreator, Part, PartCreator, SerializedPart, Type } from "../../../editor/parts";
|
||||
import { findEditableEvent } from "../../../utils/EventUtils";
|
||||
import SendHistoryManager from "../../../SendHistoryManager";
|
||||
import { CommandCategories } from "../../../SlashCommands";
|
||||
|
@ -60,6 +60,102 @@ import { PosthogAnalytics } from "../../../PosthogAnalytics";
|
|||
import { addReplyToMessageContent } from "../../../utils/Reply";
|
||||
import { doMaybeLocalRoomAction } from "../../../utils/local-room";
|
||||
|
||||
/**
|
||||
* Build the mentions information based on the editor model (and any related events):
|
||||
*
|
||||
* 1. Search the model parts for room or user pills and fill in the mentions object.
|
||||
* 2. If this is a reply to another event, include any user mentions from that
|
||||
* (but do not include a room mention).
|
||||
*
|
||||
* @param sender - The Matrix ID of the user sending the event.
|
||||
* @param content - The event content.
|
||||
* @param model - The editor model to search for mentions, null if there is no editor.
|
||||
* @param replyToEvent - The event being replied to or undefined if it is not a reply.
|
||||
* @param editedContent - The content of the parent event being edited.
|
||||
*/
|
||||
export function attachMentions(
|
||||
sender: string,
|
||||
content: IContent,
|
||||
model: EditorModel | null,
|
||||
replyToEvent: MatrixEvent | undefined,
|
||||
editedContent: IContent | null = null,
|
||||
): void {
|
||||
// If this feature is disabled, do nothing.
|
||||
if (!SettingsStore.getValue("feature_intentional_mentions")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The mentions property *always* gets included to disable legacy push rules.
|
||||
const mentions: IMentions = (content["org.matrix.msc3952.mentions"] = {});
|
||||
|
||||
const userMentions = new Set<string>();
|
||||
let roomMention = false;
|
||||
|
||||
// If there's a reply, initialize the mentioned users as the sender of that
|
||||
// event + any mentioned users in that event.
|
||||
if (replyToEvent) {
|
||||
userMentions.add(replyToEvent.sender!.userId);
|
||||
// TODO What do we do if the reply event *doeesn't* have this property?
|
||||
// Try to fish out replies from the contents?
|
||||
const userIds = replyToEvent.getContent()["org.matrix.msc3952.mentions"]?.user_ids;
|
||||
if (Array.isArray(userIds)) {
|
||||
userIds.forEach((userId) => userMentions.add(userId));
|
||||
}
|
||||
}
|
||||
|
||||
// If user provided content is available, check to see if any users are mentioned.
|
||||
if (model) {
|
||||
// Add any mentioned users in the current content.
|
||||
for (const part of model.parts) {
|
||||
if (part.type === Type.UserPill) {
|
||||
userMentions.add(part.resourceId);
|
||||
} else if (part.type === Type.AtRoomPill) {
|
||||
roomMention = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the *current* user isn't listed in the mentioned users.
|
||||
userMentions.delete(sender);
|
||||
|
||||
// Finally, if this event is editing a previous event, only include users who
|
||||
// were not previously mentioned and a room mention if the previous event was
|
||||
// not a room mention.
|
||||
if (editedContent) {
|
||||
// First, the new event content gets the *full* set of users.
|
||||
const newContent = content["m.new_content"];
|
||||
const newMentions: IMentions = (newContent["org.matrix.msc3952.mentions"] = {});
|
||||
|
||||
// Only include the users/room if there is any content.
|
||||
if (userMentions.size) {
|
||||
newMentions.user_ids = [...userMentions];
|
||||
}
|
||||
if (roomMention) {
|
||||
newMentions.room = true;
|
||||
}
|
||||
|
||||
// Fetch the mentions from the original event and remove any previously
|
||||
// mentioned users.
|
||||
const prevMentions = editedContent["org.matrix.msc3952.mentions"];
|
||||
if (Array.isArray(prevMentions?.user_ids)) {
|
||||
prevMentions!.user_ids.forEach((userId) => userMentions.delete(userId));
|
||||
}
|
||||
|
||||
// If the original event mentioned the room, nothing to do here.
|
||||
if (prevMentions?.room) {
|
||||
roomMention = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Only include the users/room if there is any content.
|
||||
if (userMentions.size) {
|
||||
mentions.user_ids = [...userMentions];
|
||||
}
|
||||
if (roomMention) {
|
||||
mentions.room = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Merges favouring the given relation
|
||||
export function attachRelation(content: IContent, relation?: IEventRelation): void {
|
||||
if (relation) {
|
||||
|
@ -72,6 +168,7 @@ export function attachRelation(content: IContent, relation?: IEventRelation): vo
|
|||
|
||||
// exported for tests
|
||||
export function createMessageContent(
|
||||
sender: string,
|
||||
model: EditorModel,
|
||||
replyToEvent: MatrixEvent | undefined,
|
||||
relation: IEventRelation | undefined,
|
||||
|
@ -102,6 +199,9 @@ export function createMessageContent(
|
|||
content.formatted_body = formattedBody;
|
||||
}
|
||||
|
||||
// Build the mentions property and add it to the event content.
|
||||
attachMentions(sender, content, model, replyToEvent);
|
||||
|
||||
attachRelation(content, relation);
|
||||
if (replyToEvent) {
|
||||
addReplyToMessageContent(content, replyToEvent, {
|
||||
|
@ -381,6 +481,8 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
|
|||
}
|
||||
|
||||
if (cmd.category === CommandCategories.messages || cmd.category === CommandCategories.effects) {
|
||||
// Attach any mentions which might be contained in the command content.
|
||||
attachMentions(this.props.mxClient.getSafeUserId(), content, model, replyToEvent);
|
||||
attachRelation(content, this.props.relation);
|
||||
if (replyToEvent) {
|
||||
addReplyToMessageContent(content, replyToEvent, {
|
||||
|
@ -413,6 +515,7 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
|
|||
const { roomId } = this.props.room;
|
||||
if (!content) {
|
||||
content = createMessageContent(
|
||||
this.props.mxClient.getSafeUserId(),
|
||||
model,
|
||||
replyToEvent,
|
||||
this.props.relation,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue