From 8cf9617630b020bd5d8a002de6090c917f172e63 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 17 May 2024 15:22:08 -0400 Subject: [PATCH] feat: passing in types to parsePost for more specific handling by plugins --- src/activitypub/mocks.js | 6 ++++++ src/posts/parse.js | 21 ++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.js index 55619e596c..9feafc6b0c 100644 --- a/src/activitypub/mocks.js +++ b/src/activitypub/mocks.js @@ -256,6 +256,12 @@ Mocks.note = async (post) => { cc.add(followersUrl); } + const content = await posts.getPostField(post.pid, 'content'); + const { postData: parsed } = await plugins.hooks.fire('filter:parse.post', { + postData: { content }, + type: 'activitypub.note', + }); + post.content = parsed.content; post.content = posts.relativeToAbsolute(post.content, posts.urlRegex); post.content = posts.relativeToAbsolute(post.content, posts.imgRegex); diff --git a/src/posts/parse.js b/src/posts/parse.js index fa89b0fdd6..62cc76b9c8 100644 --- a/src/posts/parse.js +++ b/src/posts/parse.js @@ -35,6 +35,7 @@ let sanitizeConfig = { ...sanitize.defaults.allowedClasses, }, }; +const allowedTypes = new Set(['default', 'plaintext', 'activitypub.note', 'activitypub.article']); module.exports = function (Posts) { Posts.urlRegex = { @@ -47,28 +48,38 @@ module.exports = function (Posts) { length: 5, }; - Posts.parsePost = async function (postData) { + Posts.parsePost = async function (postData, type) { if (!postData) { return postData; } + if (!type || !allowedTypes.has(type)) { + type = 'default'; + } postData.content = String(postData.sourceContent || postData.content || ''); const cache = require('./cache'); - const pid = String(postData.pid); - const cachedContent = cache.get(pid); + const cacheKey = `${String(postData.pid)}|${type}`; + const cachedContent = cache.get(cacheKey); if (postData.pid && cachedContent !== undefined) { postData.content = cachedContent; return postData; } - ({ postData } = await plugins.hooks.fire('filter:parse.post', { postData })); + ({ postData } = await plugins.hooks.fire('filter:parse.post', { postData, type })); postData.content = translator.escape(postData.content); if (postData.pid) { - cache.set(pid, postData.content); + cache.set(cacheKey, postData.content); } return postData; }; + Posts.clearCachedPost = function (pid) { + const cache = require('./cache'); + allowedTypes.forEach((type) => { + cache.del(`${String(pid)}|${type}`); + }); + }; + Posts.parseSignature = async function (userData, uid) { userData.signature = sanitizeSignature(userData.signature || ''); return await plugins.hooks.fire('filter:parse.signature', { userData: userData, uid: uid });