Fuse methods and properties related to replies
This commit is contained in:
parent
9bd7d1397c
commit
8956f6ecf4
3 changed files with 53 additions and 62 deletions
|
@ -16,8 +16,9 @@ limitations under the License.
|
||||||
|
|
||||||
import {BaseEntry} from "./BaseEntry.js";
|
import {BaseEntry} from "./BaseEntry.js";
|
||||||
import {REDACTION_TYPE} from "../../common.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 {PendingAnnotation} from "../PendingAnnotation.js";
|
||||||
|
import {createReply, fallbackBlurb, fallbackPrefix} from "./reply.js"
|
||||||
|
|
||||||
function htmlEscape(string) {
|
function htmlEscape(string) {
|
||||||
return string.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
return string.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
||||||
|
@ -155,55 +156,26 @@ export class BaseEventEntry extends BaseEntry {
|
||||||
return createAnnotation(this.id, key);
|
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 `<mx-reply>
|
|
||||||
<blockquote>
|
|
||||||
In reply to
|
|
||||||
${prefix}<a href="https://matrix.to/#/${this.sender}">${this.displayName || this.sender}</a>
|
|
||||||
<br />
|
|
||||||
${body}
|
|
||||||
</blockquote>
|
|
||||||
</mx-reply>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
_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) {
|
reply(msgtype, body) {
|
||||||
// TODO check for absense of sender / body / msgtype / etc?
|
// TODO check for absense of sender / body / msgtype / etc?
|
||||||
const newBody = this._replyPlainFallback() + '\n\n' + body;
|
let blurb = fallbackBlurb(this.content.msgtype);
|
||||||
const newFormattedBody = this._replyFormattedFallback() + htmlEscape(body);
|
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 createReply(this.id, msgtype, newBody, newFormattedBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
33
src/matrix/room/timeline/entries/reply.js
Normal file
33
src/matrix/room/timeline/entries/reply.js
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -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) {
|
export function getRelationTarget(relation) {
|
||||||
return relation.event_id || relation["m.in_reply_to"]?.event_id
|
return relation.event_id || relation["m.in_reply_to"]?.event_id
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue