Skip to content

Node.js based forum software built for the modern web

feat(activitypub): re-download stale emoji on serve

Julian Lam379f4bb72de0
diff --git a/src/activitypub/emoji.js b/src/activitypub/emoji.js
index bf7bece273..b3924d2b7b 100644
--- a/src/activitypub/emoji.js
+++ b/src/activitypub/emoji.js
@@ -200,12 +200,22 @@ async function processEmojiTag(emojiTag) {
return `<img class="not-responsive emoji" src="${icon.url}" title="${normalizeShortcode(name)}" />`;
}
+/**
+ * Delete a cached emoji entry from Redis.
+ * Does not delete the on-disk file (stale files are pruned by re-download).
+ */
+async function deleteEmoji(shortcode, hostname) {
+ const fieldKey = buildFieldKey(shortcode, hostname);
+ await db.deleteObjectField(emojiLookupKey, fieldKey);
+}
+
module.exports = {
extractHostname,
buildFieldKey,
normalizeShortcode,
getEmoji,
cacheEmoji,
+ deleteEmoji,
getProxyUrl,
processEmojiTag,
};
diff --git a/src/controllers/activitypub/emoji.js b/src/controllers/activitypub/emoji.js
index ff9762f2ec..518953dcb9 100644
--- a/src/controllers/activitypub/emoji.js
+++ b/src/controllers/activitypub/emoji.js
@@ -17,7 +17,7 @@ Controller.serve = async (req, res) => {
const { shortcode, hostname } = req.params;
try {
- const metadata = await emoji.getEmoji(shortcode, hostname);
+ let metadata = await emoji.getEmoji(shortcode, hostname);
if (!metadata || !metadata.localPath) {
return res.sendStatus(404);
}
@@ -29,12 +29,24 @@ Controller.serve = async (req, res) => {
return res.sendStatus(403);
}
- // Check file exists
+ // Check file exists; re-download from remote if stale
+ let filePath = metadata.localPath;
try {
- await fs.access(metadata.localPath);
+ await fs.access(filePath);
} catch {
- // File may have been deleted from disk but still in Redis
- return res.sendStatus(404);
+ // File missing on disk — clear stale Redis entry and re-download
+ if (metadata.remoteUrl) {
+ winston.info(`[activitypub:emoji] Re-downloading stale emoji ${shortcode} from ${metadata.remoteUrl}`);
+ await emoji.deleteEmoji(shortcode, hostname);
+ const tag = { name: shortcode, icon: { url: metadata.remoteUrl, mediaType: metadata.mediaType } };
+ metadata = await emoji.cacheEmoji(tag);
+ if (!metadata || !metadata.localPath) {
+ return res.sendStatus(404);
+ }
+ filePath = metadata.localPath;
+ } else {
+ return res.sendStatus(404);
+ }
}
const mediaType = metadata.mediaType || 'image/png';
@@ -44,7 +56,7 @@ Controller.serve = async (req, res) => {
// Stream the file
const { createReadStream } = require('fs');
- const stream = createReadStream(metadata.localPath);
+ const stream = createReadStream(filePath);
stream.pipe(res);
stream.on('error', (err) => {