From 611c6e9717abaa2cf70eb8572533d01eb06986e0 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 4 Aug 2021 09:26:26 -0700 Subject: [PATCH] Move replying code into reply.js and add license --- .../room/timeline/entries/BaseEventEntry.js | 27 +---------- src/matrix/room/timeline/entries/reply.js | 47 +++++++++++++++++-- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/src/matrix/room/timeline/entries/BaseEventEntry.js b/src/matrix/room/timeline/entries/BaseEventEntry.js index a106af58..6893b890 100644 --- a/src/matrix/room/timeline/entries/BaseEventEntry.js +++ b/src/matrix/room/timeline/entries/BaseEventEntry.js @@ -18,11 +18,7 @@ import {BaseEntry} from "./BaseEntry.js"; import {REDACTION_TYPE} from "../../common.js"; import {createAnnotation, ANNOTATION_RELATION_TYPE, getRelationFromContent} from "../relations.js"; import {PendingAnnotation} from "../PendingAnnotation.js"; -import {createReply, fallbackBlurb, fallbackPrefix} from "./reply.js" - -function htmlEscape(string) { - return string.replace(/&/g, "&").replace(//g, ">"); -} +import {reply} from "./reply.js" /** Deals mainly with local echo for relations and redactions, * so it is shared between PendingEventEntry and EventEntry */ @@ -157,26 +153,7 @@ export class BaseEventEntry extends BaseEntry { } reply(msgtype, body) { - // TODO check for absense of sender / body / msgtype / etc? - let blurb = fallbackBlurb(this.content.msgtype); - const prefix = fallbackPrefix(this.content.msgtype); - const sender = this.sender; - const name = this.displayName || sender; - - const formattedBody = blurb || this.content.formatted_body || - (this.content.body && htmlEscape(this.content.body)) || ""; - const formattedFallback = `
In reply to ${prefix}` + - `${name}
` + - `${formattedBody}
`; - - const plainBody = blurb || this.content.body || ""; - const bodyLines = plainBody.split("\n"); - bodyLines[0] = `> ${prefix}<${sender}> ${bodyLines[0]}` - const plainFallback = bodyLines.join("\n> "); - - const newBody = plainFallback + '\n\n' + body; - const newFormattedBody = formattedFallback + htmlEscape(body); - return createReply(this.id, msgtype, newBody, newFormattedBody); + return reply(this, msgtype, body); } /** takes both remote event id and local txn id into account, see overriding in PendingEventEntry */ diff --git a/src/matrix/room/timeline/entries/reply.js b/src/matrix/room/timeline/entries/reply.js index 00d591e0..7afae036 100644 --- a/src/matrix/room/timeline/entries/reply.js +++ b/src/matrix/room/timeline/entries/reply.js @@ -1,5 +1,24 @@ +/* +Copyright 2021 The Matrix.org Foundation C.I.C. -export function fallbackBlurb(msgtype) { +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +function htmlEscape(string) { + return string.replace(/&/g, "&").replace(//g, ">"); +} + +function fallbackBlurb(msgtype) { switch (msgtype) { case "m.file": return "sent a file."; @@ -13,11 +32,11 @@ export function fallbackBlurb(msgtype) { return null; } -export function fallbackPrefix(msgtype) { +function fallbackPrefix(msgtype) { return msgtype === "m.emote" ? "* " : ""; } -export function createReply(targetId, msgtype, body, formattedBody) { +function createReply(targetId, msgtype, body, formattedBody) { return { msgtype, body, @@ -31,3 +50,25 @@ export function createReply(targetId, msgtype, body, formattedBody) { }; } +export function reply(entry, msgtype, body) { + // TODO check for absense of sender / body / msgtype / etc? + let blurb = fallbackBlurb(entry.content.msgtype); + const prefix = fallbackPrefix(entry.content.msgtype); + const sender = entry.sender; + const name = entry.displayName || sender; + + const formattedBody = blurb || entry.content.formatted_body || + (entry.content.body && htmlEscape(entry.content.body)) || ""; + const formattedFallback = `
In reply to ${prefix}` + + `${name}
` + + `${formattedBody}
`; + + const plainBody = blurb || entry.content.body || ""; + const bodyLines = plainBody.split("\n"); + bodyLines[0] = `> ${prefix}<${sender}> ${bodyLines[0]}` + const plainFallback = bodyLines.join("\n> "); + + const newBody = plainFallback + '\n\n' + body; + const newFormattedBody = formattedFallback + htmlEscape(body); + return createReply(entry.id, msgtype, newBody, newFormattedBody); +}