fix: replace attachment generation logic in notes.public

Previously, the logic retrieved the list of uploads, checked if they
were thumbs, and set attachment (and noteAttachment) depending on object
type. It was complicated and didn't really work so well, so I simplified
it.

Now thumbs.get is called, and attachment is appended with all thumbs and
uploads. Sizing is not provided. Maybe later. Image is also now set,
which is the first image in attachment.
This commit is contained in:
Julian Lam
2026-01-19 21:37:23 -05:00
parent dce82aaeca
commit 428b6e730a

View File

@@ -734,20 +734,12 @@ Mocks.notes.public = async (post) => {
// Special handling for main posts (as:Article w/ as:Note preview) // Special handling for main posts (as:Article w/ as:Note preview)
const plaintext = posts.sanitizePlaintext(content); const plaintext = posts.sanitizePlaintext(content);
const isArticle = post.pid === post.topic.mainPid && plaintext.length > 500; const isArticle = post.pid === post.topic.mainPid && plaintext.length > 500;
const noteAttachment = isArticle ? [...attachment] : null;
const [uploads, thumbs] = await Promise.all([
posts.uploads.listWithSizes(post.pid),
topics.getTopicField(post.tid, 'thumbs'),
]);
const isThumb = uploads.map(u => Array.isArray(thumbs) ? thumbs.includes(u.name) : false);
uploads.forEach(({ name, width, height }, idx) => { const thumbs = await topics.thumbs.get(post.tid);
thumbs.forEach(({ name, path }) => {
const mediaType = mime.getType(name); const mediaType = mime.getType(name);
const url = `${nconf.get('url') + nconf.get('upload_url')}/${name}`; const url = `${nconf.get('url') + nconf.get('upload_url')}${path}`;
(noteAttachment || attachment).push({ mediaType, url, width, height }); attachment.push({ mediaType, url });
if (isThumb[idx] && noteAttachment) {
attachment.push({ mediaType, url, width, height });
}
}); });
// Inspect post content for external imagery as well // Inspect post content for external imagery as well
@@ -757,13 +749,14 @@ Mocks.notes.public = async (post) => {
const { hostname, pathname, href: url } = new URL(match[1]); const { hostname, pathname, href: url } = new URL(match[1]);
if (hostname !== nconf.get('url_parsed').hostname) { if (hostname !== nconf.get('url_parsed').hostname) {
const mediaType = mime.getType(pathname); const mediaType = mime.getType(pathname);
(noteAttachment || attachment).push({ mediaType, url }); attachment.push({ mediaType, url });
} }
} }
match = posts.imgRegex.exec(post.content); match = posts.imgRegex.exec(post.content);
} }
attachment = normalizeAttachment(attachment); attachment = normalizeAttachment(attachment);
const image = attachment.filter(entry => entry.type === 'Image')?.shift();
let preview; let preview;
let summary = null; let summary = null;
if (isArticle) { if (isArticle) {
@@ -772,7 +765,7 @@ Mocks.notes.public = async (post) => {
attributedTo: `${nconf.get('url')}/uid/${post.user.uid}`, attributedTo: `${nconf.get('url')}/uid/${post.user.uid}`,
content: post.content, content: post.content,
published, published,
attachment: normalizeAttachment(noteAttachment), attachment,
}; };
const sentences = tokenizer.sentences(post.content, { newline_boundaries: true }); const sentences = tokenizer.sentences(post.content, { newline_boundaries: true });
@@ -833,6 +826,7 @@ Mocks.notes.public = async (post) => {
source, source,
tag, tag,
attachment, attachment,
image,
replies: `${id}/replies`, replies: `${id}/replies`,
}; };