Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix garbled replies to replies due to invalid reply formatting #1202

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/domain/session/room/timeline/tiles/BaseMessageTile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2024 Mirian Margiani <mixosaurus+ichthyo@pm.me>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -147,7 +148,7 @@ export class BaseMessageTile extends SimpleTile {
}

createReplyContent(msgtype, body) {
return this._entry.createReplyContent(msgtype, body);
return this._entry.createReplyContent(msgtype, body, this.permaLink);
}

redact(reason, log) {
Expand Down
5 changes: 3 additions & 2 deletions src/matrix/room/timeline/entries/BaseEventEntry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Copyright 2024 Mirian Margiani <mixosaurus+ichthyo@pm.me>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -185,8 +186,8 @@ export class BaseEventEntry extends BaseEntry {
return createAnnotation(this.id, key);
}

createReplyContent(msgtype, body) {
return createReplyContent(this, msgtype, body);
createReplyContent(msgtype, body, permaLink) {
return createReplyContent(this, msgtype, body, permaLink);
}

/** takes both remote event id and local txn id into account, see overriding in PendingEventEntry */
Expand Down
87 changes: 74 additions & 13 deletions src/matrix/room/timeline/entries/reply.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Copyright 2024 Mirian Margiani <mixosaurus+ichthyo@pm.me>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +37,42 @@ function fallbackPrefix(msgtype) {
return msgtype === "m.emote" ? "* " : "";
}

function _createReplyContent(targetId, msgtype, body, formattedBody) {
function _parsePlainBody(plainBody) {
// Strip any existing reply fallback and return an array of lines.

const bodyLines = plainBody.trim().split("\n");

return bodyLines
.map((elem, index, array) => {
if (index > 0 && array[index-1][0] !== '>') {
// stop stripping the fallback at the first line of non-fallback text
return elem;
} else if (elem[0] === '>' && elem[1] === ' ') {
return null;
} else {
return elem;
}
})
.filter((elem) => elem !== null)
// Join, trim, and split to remove any line breaks that were left between the
// fallback and the actual message body. Don't use trim() because that would
// also remove any other whitespace at the beginning of the message that the
// user added intentionally.
.join('\n')
.replace(/^\n+|\n+$/g, '')
.split('\n')
}

function _parseFormattedBody(formattedBody) {
// Strip any existing reply fallback and return a HTML string again.

// This is greedy and definitely not the most efficient way to do it.
// However, this function is only called when sending a reply (so: not too
// often) and it should make sure that all instances of <mx-reply> are gone.
return formattedBody.replace(/<mx-reply>[\s\S]*<\/mx-reply>/gi, '');
}

function _createReplyContent(targetId, targetSenderId, msgtype, body, formattedBody) {
return {
msgtype,
body,
Expand All @@ -46,29 +82,54 @@ function _createReplyContent(targetId, msgtype, body, formattedBody) {
"m.in_reply_to": {
"event_id": targetId
}
},
"m.mentions": {
"user_ids": [
targetSenderId,
]
}
};
}

export function createReplyContent(entry, msgtype, body) {
// TODO check for absense of sender / body / msgtype / etc?
export function createReplyContent(entry, msgtype, body, permaLink) {
// NOTE We assume sender, body, and msgtype are never invalid because they
// are required fields.
const nonTextual = fallbackForNonTextualMessage(entry.content.msgtype);
const prefix = fallbackPrefix(entry.content.msgtype);
const sender = entry.sender;
const name = entry.displayName || sender;
const repliedToId = entry.id;

const formattedBody = nonTextual || 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>`;
// TODO collect user mentions (sender and any previous mentions)
// Considerations:
// - Who should be included in the mentions? In a reply chain, should all
// previous mentions be carried over indefinitely? How to decide when to
// stop carrying mentions?
// - Don't add a mentions section when replying to own messages without
// any other mentions. As per https://spec.matrix.org/v1.12/client-server-api/#user-and-room-mentions
// "Users should not add their own Matrix ID to the m.mentions property
// as outgoing messages cannot self-notify."

// Generate new plain body with plain reply fallback
const plainBody = nonTextual || entry.content.body || "";
const bodyLines = plainBody.split("\n");
const bodyLines = _parsePlainBody(plainBody);
bodyLines[0] = `> ${prefix}<${sender}> ${bodyLines[0]}`
const plainFallback = bodyLines.join("\n> ");

const newBody = plainFallback + '\n\n' + body;
const newFormattedBody = formattedFallback + htmlEscape(body);
return _createReplyContent(entry.id, msgtype, newBody, newFormattedBody);

// Generate new formatted body with formatted reply fallback
const formattedBody = nonTextual || entry.content.formatted_body ||
(entry.content.body && htmlEscape(entry.content.body)) || "";
const cleanedFormattedBody = _parseFormattedBody(formattedBody);
const formattedFallback =
`<mx-reply>` +
`<blockquote>` +
`<a href="${permaLink}">In reply to</a>` +
`${prefix}<a href="https://matrix.to/#/${sender}">${sender}</a>` +
`<br />` +
`${cleanedFormattedBody}` +
`</blockquote>` +
`</mx-reply>`;
const newFormattedBody = formattedFallback + htmlEscape(body).replaceAll('\n', '<br/>');

return _createReplyContent(repliedToId, sender, msgtype, newBody, newFormattedBody);
}