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

Commit

Permalink
Handle message edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Mar 8, 2023
1 parent 077e774 commit edc0edb
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/components/views/rooms/EditMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import React, { createRef, KeyboardEvent } from "react";
import classNames from "classnames";
import { EventStatus, IContent, MatrixEvent } from "matrix-js-sdk/src/models/event";
import { EventStatus, IContent, IMentions, MatrixEvent } from "matrix-js-sdk/src/models/event";
import { MsgType } from "matrix-js-sdk/src/@types/event";
import { Room } from "matrix-js-sdk/src/models/room";
import { logger } from "matrix-js-sdk/src/logger";
Expand All @@ -29,7 +29,7 @@ import { getCaretOffsetAndText } from "../../../editor/dom";
import { htmlSerializeIfNeeded, textSerialize, containsEmote, stripEmoteCommand } from "../../../editor/serialize";
import { findEditableEvent } from "../../../utils/EventUtils";
import { parseEvent } from "../../../editor/deserialize";
import { CommandPartCreator, Part, PartCreator, SerializedPart } from "../../../editor/parts";
import { CommandPartCreator, Part, PartCreator, SerializedPart, Type } from "../../../editor/parts";
import EditorStateTransfer from "../../../utils/EditorStateTransfer";
import BasicMessageComposer, { REGEX_EMOTICON } from "./BasicMessageComposer";
import { CommandCategories } from "../../../SlashCommands";
Expand All @@ -47,6 +47,7 @@ import { getSlashCommand, isSlashCommand, runSlashCommand, shouldSendAnyway } fr
import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
import { PosthogAnalytics } from "../../../PosthogAnalytics";
import { editorRoomKey, editorStateKey } from "../../../Editing";
import { attachMentions } from "./SendMessageComposer";

function getHtmlReplyFallback(mxEvent: MatrixEvent): string {
const html = mxEvent.getContent().formatted_body;
Expand Down Expand Up @@ -103,12 +104,44 @@ function createEditContent(model: EditorModel, editedEvent: MatrixEvent): IConte
contentBody.formatted_body = `${htmlPrefix} * ${formattedBody}`;
}

// Build the mentions property for the *new* content (as if there was no edit).
//
// TODO If this is a reply we need to include all the users from it.
attachMentions(editedEvent.sender.userId, newContent, model, undefined);

// Attach the *differential* mentions of any *newly* mentioned users / room.
const mentions: IMentions = {};
// TODO What if the edit doesn't have the mentions property?
const prevMentions = editedEvent.getContent()["org.matrix.msc3952.mentions"];
let prevUserMentions = prevMentions?.user_ids;
if (!Array.isArray(prevUserMentions)) {
prevUserMentions = [];
}
const newUserMentions = new Set<string>();
for (const part of model.parts) {
// Only include the user if they were not previously mentioned and a room
// mention if the previous version did not include one.
if (part.type === Type.UserPill && !prevUserMentions.includes(part.resourceId)) {
newUserMentions.add(part.resourceId);
} else if (part.type === Type.AtRoomPill && !prevMentions?.room) {
mentions.room = true;
}
}

// Ensure the *current* user isn't listed in the mentioned users.
newUserMentions.delete(editedEvent.sender.userId);

if (newUserMentions.size) {
mentions.user_ids = [...newUserMentions];
}

const relation = {
"m.new_content": newContent,
"m.relates_to": {
rel_type: "m.replace",
event_id: editedEvent.getId(),
},
"org.matrix.msc3952.mentions": mentions,
};

return Object.assign(relation, contentBody);
Expand Down

0 comments on commit edc0edb

Please sign in to comment.