From 29b95e60833fef1bed598897dad2deff4e875ff4 Mon Sep 17 00:00:00 2001 From: Panagiotis <27917356+panoschal@users.noreply.github.com> Date: Sat, 6 Mar 2021 16:47:29 +0200 Subject: [PATCH] fix: make edit prefill work correctly from EditHistory handle encrypted and unencrypted events get the correct event_id (the base message) when called from EditHistoryMessage keep only the `body` and `msgtype` fields when prefilling --- src/components/structures/ViewSource.js | 33 +++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/components/structures/ViewSource.js b/src/components/structures/ViewSource.js index 369a0a1ddd..4ee70ee2a7 100644 --- a/src/components/structures/ViewSource.js +++ b/src/components/structures/ViewSource.js @@ -39,6 +39,7 @@ export default class ViewSource extends React.Component { } onBack() { + // TODO: refresh the "Event ID:" modal header this.setState({ isEditing: false }); } @@ -80,15 +81,28 @@ export default class ViewSource extends React.Component { } } + // returns the id of the initial message, not the id of the previous edit + getBaseEventId() { + const mxEvent = this.props.mxEvent.replacingEvent() || this.props.mxEvent; // show the replacing event, not the original, if it is an edit + const isEncrypted = this.props.mxEvent.getType() !== this.props.mxEvent.getWireType(); + const baseMxEvent = this.props.mxEvent; + + if (isEncrypted) { + // `relates_to` field is inside the encrypted event + return mxEvent.event.content["m.relates_to"]?.event_id ?? baseMxEvent.getId(); + } else { + return mxEvent.getContent()["m.relates_to"]?.event_id ?? baseMxEvent.getId(); + } + } + // returns the SendCustomEvent component prefilled with the correct details editSourceContent() { const mxEvent = this.props.mxEvent.replacingEvent() || this.props.mxEvent; // show the replacing event, not the original, if it is an edit const isStateEvent = mxEvent.isState(); - console.log("isStateEvent", isStateEvent); const roomId = mxEvent.getRoomId(); - const eventId = mxEvent.getId(); const originalContent = mxEvent.getContent(); + if (isStateEvent) { return ( @@ -107,14 +121,19 @@ export default class ViewSource extends React.Component { ); } else { - // send an edit-message event - // prefill the "m.new_content" field + // prefill an edit-message event + // keep only the `body` and `msgtype` fields of originalContent + const bodyToStartFrom = originalContent["m.new_content"]?.body ?? originalContent.body; // prefill the last edit body, to start editing from there const newContent = { - ...originalContent, - "m.new_content": originalContent, + "body": ` * ${bodyToStartFrom}`, + "msgtype": originalContent.msgtype, + "m.new_content": { + body: bodyToStartFrom, + msgtype: originalContent.msgtype, + }, "m.relates_to": { rel_type: "m.replace", - event_id: eventId, + event_id: this.getBaseEventId(), }, }; return (