From 46215b3c51adbd62d133637def38652d1b171821 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 20 Jul 2021 12:53:31 -0700 Subject: [PATCH] Add the ability to reply --- src/domain/session/room/RoomViewModel.js | 10 +++++++--- src/matrix/room/timeline/entries/BaseEventEntry.js | 6 +++++- src/matrix/room/timeline/relations.js | 8 ++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/domain/session/room/RoomViewModel.js b/src/domain/session/room/RoomViewModel.js index 0bd56f8e..164712df 100644 --- a/src/domain/session/room/RoomViewModel.js +++ b/src/domain/session/room/RoomViewModel.js @@ -155,7 +155,7 @@ export class RoomViewModel extends ViewModel { this._room.join(); } - async _sendMessage(message) { + async _sendMessage(message, replyTo) { if (!this._room.isArchived && message) { try { let msgtype = "m.text"; @@ -163,7 +163,11 @@ export class RoomViewModel extends ViewModel { message = message.substr(4).trim(); msgtype = "m.emote"; } - await this._room.sendEvent("m.room.message", {msgtype, body: message}); + const content = {msgtype, body: message}; + if (replyTo) { + content["m.relates_to"] = replyTo.reply(); + } + await this._room.sendEvent("m.room.message", content); } catch (err) { console.error(`room.sendMessage(): ${err.message}:\n${err.stack}`); this._sendError = err; @@ -327,7 +331,7 @@ class ComposerViewModel extends ViewModel { } sendMessage(message) { - const success = this._roomVM._sendMessage(message); + const success = this._roomVM._sendMessage(message, this._replyTo); if (success) { this._isEmpty = true; this.emitChange("canSend"); diff --git a/src/matrix/room/timeline/entries/BaseEventEntry.js b/src/matrix/room/timeline/entries/BaseEventEntry.js index 2e681104..68f38357 100644 --- a/src/matrix/room/timeline/entries/BaseEventEntry.js +++ b/src/matrix/room/timeline/entries/BaseEventEntry.js @@ -16,7 +16,7 @@ limitations under the License. import {BaseEntry} from "./BaseEntry.js"; import {REDACTION_TYPE} from "../../common.js"; -import {createAnnotation, ANNOTATION_RELATION_TYPE, getRelationFromContent} from "../relations.js"; +import {createAnnotation, createReply, ANNOTATION_RELATION_TYPE, getRelationFromContent} from "../relations.js"; import {PendingAnnotation} from "../PendingAnnotation.js"; /** Deals mainly with local echo for relations and redactions, @@ -151,6 +151,10 @@ export class BaseEventEntry extends BaseEntry { return createAnnotation(this.id, key); } + reply() { + return createReply(this.id); + } + /** takes both remote event id and local txn id into account, see overriding in PendingEventEntry */ isRelatedToId(id) { return id && this.relatedEventId === id; diff --git a/src/matrix/room/timeline/relations.js b/src/matrix/room/timeline/relations.js index 4009d8c4..0f861d0e 100644 --- a/src/matrix/room/timeline/relations.js +++ b/src/matrix/room/timeline/relations.js @@ -29,6 +29,14 @@ export function createAnnotation(targetId, key) { }; } +export function createReply(targetId) { + return { + "m.in_reply_to": { + "event_id": targetId + } + }; +} + export function getRelationTarget(relation) { return relation.event_id || relation["m.in_reply_to"]?.event_id }