From 283095386217bafbc09f1180b0176d7499425560 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 17 Oct 2023 00:08:11 +0200 Subject: [PATCH] [mastodon-client] Rework quote rendering --- .../src/server/api/mastodon/converters/note.ts | 18 ++++++++++-------- .../src/server/api/mastodon/helpers/mfm.ts | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/converters/note.ts b/packages/backend/src/server/api/mastodon/converters/note.ts index bea166dc4..0bc454599 100644 --- a/packages/backend/src/server/api/mastodon/converters/note.ts +++ b/packages/backend/src/server/api/mastodon/converters/note.ts @@ -87,14 +87,16 @@ export class NoteConverter { .catch(() => null))) .then(p => p.filter(m => m)) as Promise; - const text = Promise.resolve(renote).then(renote => { - if (!renote || note.text === null) return note.text - const uri = renote.uri ? renote.uri : `${config.url}/notes/${renote.id}`; - return note.text.includes(uri) - ? note.text - : note.text + `\n\nRE: ${uri}`; + const quoteUri = Promise.resolve(renote).then(renote => { + if (!renote || note.text === null) return null; + return renote.uri ? renote.uri : `${config.url}/notes/${renote.id}`; }); + const content = note.text !== null + ? quoteUri.then(quoteUri => MfmHelpers.toHtml(mfm.parse(note.text!), JSON.parse(note.mentionedRemoteUsers), note.userHost, false, quoteUri) + .then(p => p ?? escapeMFM(note.text!))) + : ""; + const isPinned = user && note.userId === user.id ? UserNotePinings.exist({ where: { userId: user.id, noteId: note.id } }) : undefined; @@ -117,9 +119,9 @@ export class NoteConverter { in_reply_to_id: note.replyId, in_reply_to_account_id: note.replyUserId, reblog: reblog.then(reblog => note.text === null ? reblog : null), - content: text.then(async text => text !== null ? MfmHelpers.toHtml(mfm.parse(text), JSON.parse(note.mentionedRemoteUsers), note.userHost).then(p => p ?? escapeMFM(text)) : ""), + content: content, content_type: 'text/x.misskeymarkdown', - text: text, + text: note.text, created_at: note.createdAt.toISOString(), emojis: noteEmoji, replies_count: note.repliesCount, diff --git a/packages/backend/src/server/api/mastodon/helpers/mfm.ts b/packages/backend/src/server/api/mastodon/helpers/mfm.ts index e78d862db..39c20e5d4 100644 --- a/packages/backend/src/server/api/mastodon/helpers/mfm.ts +++ b/packages/backend/src/server/api/mastodon/helpers/mfm.ts @@ -10,7 +10,8 @@ export class MfmHelpers { nodes: mfm.MfmNode[] | null, mentionedRemoteUsers: IMentionedRemoteUsers = [], objectHost: string | null, - inline: boolean = false + inline: boolean = false, + quoteUri: string | null = null ) { if (nodes == null) { return null; @@ -194,6 +195,21 @@ export class MfmHelpers { await appendChildren(nodes, doc.body); + if (quoteUri !== null) { + const a = doc.createElement("a"); + a.href = quoteUri; + a.textContent = quoteUri.replace(/^https?:\/\//, ''); + + const quote = doc.createElement("span"); + quote.setAttribute("class", "quote-inline"); + quote.appendChild(doc.createElement("br")); + quote.appendChild(doc.createElement("br")); + quote.innerHTML += 'RE: '; + quote.appendChild(a); + + doc.body.appendChild(quote); + } + return inline ? doc.body.innerHTML : `

${doc.body.innerHTML}

`; } }