feat: passing in types to parsePost for more specific handling by plugins

This commit is contained in:
Julian Lam
2024-05-17 15:22:08 -04:00
parent 78e11d6eba
commit 8cf9617630
2 changed files with 22 additions and 5 deletions

View File

@@ -256,6 +256,12 @@ Mocks.note = async (post) => {
cc.add(followersUrl); 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.urlRegex);
post.content = posts.relativeToAbsolute(post.content, posts.imgRegex); post.content = posts.relativeToAbsolute(post.content, posts.imgRegex);

View File

@@ -35,6 +35,7 @@ let sanitizeConfig = {
...sanitize.defaults.allowedClasses, ...sanitize.defaults.allowedClasses,
}, },
}; };
const allowedTypes = new Set(['default', 'plaintext', 'activitypub.note', 'activitypub.article']);
module.exports = function (Posts) { module.exports = function (Posts) {
Posts.urlRegex = { Posts.urlRegex = {
@@ -47,28 +48,38 @@ module.exports = function (Posts) {
length: 5, length: 5,
}; };
Posts.parsePost = async function (postData) { Posts.parsePost = async function (postData, type) {
if (!postData) { if (!postData) {
return postData; return postData;
} }
if (!type || !allowedTypes.has(type)) {
type = 'default';
}
postData.content = String(postData.sourceContent || postData.content || ''); postData.content = String(postData.sourceContent || postData.content || '');
const cache = require('./cache'); const cache = require('./cache');
const pid = String(postData.pid); const cacheKey = `${String(postData.pid)}|${type}`;
const cachedContent = cache.get(pid); const cachedContent = cache.get(cacheKey);
if (postData.pid && cachedContent !== undefined) { if (postData.pid && cachedContent !== undefined) {
postData.content = cachedContent; postData.content = cachedContent;
return postData; 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); postData.content = translator.escape(postData.content);
if (postData.pid) { if (postData.pid) {
cache.set(pid, postData.content); cache.set(cacheKey, postData.content);
} }
return postData; return postData;
}; };
Posts.clearCachedPost = function (pid) {
const cache = require('./cache');
allowedTypes.forEach((type) => {
cache.del(`${String(pid)}|${type}`);
});
};
Posts.parseSignature = async function (userData, uid) { Posts.parseSignature = async function (userData, uid) {
userData.signature = sanitizeSignature(userData.signature || ''); userData.signature = sanitizeSignature(userData.signature || '');
return await plugins.hooks.fire('filter:parse.signature', { userData: userData, uid: uid }); return await plugins.hooks.fire('filter:parse.signature', { userData: userData, uid: uid });