Move replying code into reply.js and add license

This commit is contained in:
Danila Fedorin 2021-08-04 09:26:26 -07:00
parent 1a0e305212
commit 611c6e9717
2 changed files with 46 additions and 28 deletions

View file

@ -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, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
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 = `<mx-reply><blockquote>In reply to ${prefix}` +
`<a href="https://matrix.to/#/${sender}">${name}</a><br />` +
`${formattedBody}</blockquote></mx-reply>`;
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 */

View file

@ -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, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
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 = `<mx-reply><blockquote>In reply to ${prefix}` +
`<a href="https://matrix.to/#/${sender}">${name}</a><br />` +
`${formattedBody}</blockquote></mx-reply>`;
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);
}