From 8956f6ecf414d4dcda9ad93ee0a4cc7443f6060f Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 3 Aug 2021 13:10:36 -0700 Subject: [PATCH] Fuse methods and properties related to replies --- .../room/timeline/entries/BaseEventEntry.js | 68 ++++++------------- src/matrix/room/timeline/entries/reply.js | 33 +++++++++ src/matrix/room/timeline/relations.js | 14 ---- 3 files changed, 53 insertions(+), 62 deletions(-) create mode 100644 src/matrix/room/timeline/entries/reply.js diff --git a/src/matrix/room/timeline/entries/BaseEventEntry.js b/src/matrix/room/timeline/entries/BaseEventEntry.js index 41960e75..a106af58 100644 --- a/src/matrix/room/timeline/entries/BaseEventEntry.js +++ b/src/matrix/room/timeline/entries/BaseEventEntry.js @@ -16,8 +16,9 @@ limitations under the License. import {BaseEntry} from "./BaseEntry.js"; import {REDACTION_TYPE} from "../../common.js"; -import {createAnnotation, createReply, ANNOTATION_RELATION_TYPE, getRelationFromContent} from "../relations.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, ">"); @@ -155,55 +156,26 @@ export class BaseEventEntry extends BaseEntry { return createAnnotation(this.id, key); } - _fallbackBlurb() { - switch (this.content.msgtype) { - case "m.file": - return "sent a file."; - case "m.image": - return "sent an image."; - case "m.video": - return "sent a video."; - case "m.audio": - return "sent an audio file."; - } - } - - _fallbackPrefix() { - return this.content.msgtype === "m.emote" ? "* " : ""; - } - - get _formattedBody() { - return this.content.formatted_body || (this.content.body && htmlEscape(this.content.body)); - } - - get _plainBody() { - return this.content.body; - } - - _replyFormattedFallback() { - const body = this._fallbackBlurb() || this._formattedBody || ""; - const prefix = this._fallbackPrefix(); - return ` -
- In reply to - ${prefix}${this.displayName || this.sender} -
- ${body} -
-
`; - } - - _replyPlainFallback() { - const body = this._fallbackBlurb() || this._plainBody || ""; - const bodyLines = body.split("\n"); - bodyLines[0] = `> <${this.sender}> ${bodyLines[0]}` - return bodyLines.join("\n> "); - } - reply(msgtype, body) { // TODO check for absense of sender / body / msgtype / etc? - const newBody = this._replyPlainFallback() + '\n\n' + body; - const newFormattedBody = this._replyFormattedFallback() + htmlEscape(body); + 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); } diff --git a/src/matrix/room/timeline/entries/reply.js b/src/matrix/room/timeline/entries/reply.js new file mode 100644 index 00000000..00d591e0 --- /dev/null +++ b/src/matrix/room/timeline/entries/reply.js @@ -0,0 +1,33 @@ + +export function fallbackBlurb(msgtype) { + switch (msgtype) { + case "m.file": + return "sent a file."; + case "m.image": + return "sent an image."; + case "m.video": + return "sent a video."; + case "m.audio": + return "sent an audio file."; + } + return null; +} + +export function fallbackPrefix(msgtype) { + return msgtype === "m.emote" ? "* " : ""; +} + +export function createReply(targetId, msgtype, body, formattedBody) { + return { + msgtype, + body, + "format": "org.matrix.custom.html", + "formatted_body": formattedBody, + "m.relates_to": { + "m.in_reply_to": { + "event_id": targetId + } + } + }; +} + diff --git a/src/matrix/room/timeline/relations.js b/src/matrix/room/timeline/relations.js index 17d1e9d7..4009d8c4 100644 --- a/src/matrix/room/timeline/relations.js +++ b/src/matrix/room/timeline/relations.js @@ -29,20 +29,6 @@ export function createAnnotation(targetId, key) { }; } -export function createReply(targetId, msgtype, body, formattedBody) { - return { - msgtype, - body, - "format": "org.matrix.custom.html", - "formatted_body": formattedBody, - "m.relates_to": { - "m.in_reply_to": { - "event_id": targetId - } - } - }; -} - export function getRelationTarget(relation) { return relation.event_id || relation["m.in_reply_to"]?.event_id }