mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-06 04:21:17 +01:00
refactor: move plugin hook methods to plugin.hooks.*
This commit is contained in:
@@ -41,7 +41,7 @@ module.exports = function (Posts) {
|
||||
postData.bookmarks = await db.setCount('pid:' + pid + ':users_bookmarked');
|
||||
await Posts.setPostField(pid, 'bookmarks', postData.bookmarks);
|
||||
|
||||
plugins.fireHook('action:post.' + type, {
|
||||
plugins.hooks.fire('action:post.' + type, {
|
||||
pid: pid,
|
||||
uid: uid,
|
||||
owner: postData.uid,
|
||||
|
||||
@@ -47,7 +47,7 @@ module.exports = function (Posts) {
|
||||
postData.handle = data.handle;
|
||||
}
|
||||
|
||||
let result = await plugins.fireHook('filter:post.create', { post: postData, data: data });
|
||||
let result = await plugins.hooks.fire('filter:post.create', { post: postData, data: data });
|
||||
postData = result.post;
|
||||
await db.setObject('post:' + postData.pid, postData);
|
||||
|
||||
@@ -65,9 +65,9 @@ module.exports = function (Posts) {
|
||||
Posts.uploads.sync(postData.pid),
|
||||
]);
|
||||
|
||||
result = await plugins.fireHook('filter:post.get', { post: postData, uid: data.uid });
|
||||
result = await plugins.hooks.fire('filter:post.get', { post: postData, uid: data.uid });
|
||||
result.post.isMain = isMain;
|
||||
plugins.fireHook('action:post.save', { post: _.clone(result.post) });
|
||||
plugins.hooks.fire('action:post.save', { post: _.clone(result.post) });
|
||||
return result.post;
|
||||
};
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ module.exports = function (Posts) {
|
||||
}
|
||||
const keys = pids.map(pid => 'post:' + pid);
|
||||
const postData = await (fields.length ? db.getObjectsFields(keys, fields) : db.getObjects(keys));
|
||||
const result = await plugins.fireHook('filter:post.getFields', {
|
||||
const result = await plugins.hooks.fire('filter:post.getFields', {
|
||||
pids: pids,
|
||||
posts: postData,
|
||||
fields: fields,
|
||||
@@ -51,7 +51,7 @@ module.exports = function (Posts) {
|
||||
|
||||
Posts.setPostFields = async function (pid, data) {
|
||||
await db.setObject('post:' + pid, data);
|
||||
plugins.fireHook('action:post.setFields', { data: { ...data, pid } });
|
||||
plugins.hooks.fire('action:post.setFields', { data: { ...data, pid } });
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ module.exports = function (Posts) {
|
||||
|
||||
async function deleteOrRestore(type, pid, uid) {
|
||||
const isDeleting = type === 'delete';
|
||||
await plugins.fireHook('filter:post.' + type, { pid: pid, uid: uid });
|
||||
await plugins.hooks.fire('filter:post.' + type, { pid: pid, uid: uid });
|
||||
await Posts.setPostFields(pid, {
|
||||
deleted: isDeleting ? 1 : 0,
|
||||
deleterUid: isDeleting ? uid : 0,
|
||||
@@ -38,7 +38,7 @@ module.exports = function (Posts) {
|
||||
db.sortedSetAdd('cid:' + topicData.cid + ':pids', postData.timestamp, pid),
|
||||
]);
|
||||
await categories.updateRecentTidForCid(postData.cid);
|
||||
plugins.fireHook('action:post.' + type, { post: _.clone(postData), uid: uid });
|
||||
plugins.hooks.fire('action:post.' + type, { post: _.clone(postData), uid: uid });
|
||||
if (type === 'delete') {
|
||||
await flags.resolveFlag('post', pid, uid);
|
||||
}
|
||||
@@ -52,7 +52,7 @@ module.exports = function (Posts) {
|
||||
}
|
||||
const topicData = await topics.getTopicFields(postData.tid, ['tid', 'cid', 'pinned']);
|
||||
postData.cid = topicData.cid;
|
||||
await plugins.fireHook('filter:post.purge', { post: postData, pid: pid, uid: uid });
|
||||
await plugins.hooks.fire('filter:post.purge', { post: postData, pid: pid, uid: uid });
|
||||
await Promise.all([
|
||||
deletePostFromTopicUserNotification(postData, topicData),
|
||||
deletePostFromCategoryRecentPosts(postData),
|
||||
@@ -64,7 +64,7 @@ module.exports = function (Posts) {
|
||||
Posts.uploads.dissociateAll(pid),
|
||||
]);
|
||||
await flags.resolveFlag('post', pid, uid);
|
||||
plugins.fireHook('action:post.purge', { post: postData, uid: uid });
|
||||
plugins.hooks.fire('action:post.purge', { post: postData, uid: uid });
|
||||
await db.delete('post:' + pid);
|
||||
};
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ module.exports = function (Posts) {
|
||||
const post = await postDiffLoad(pid, since, uid);
|
||||
post.content = String(post.content || '');
|
||||
|
||||
const result = await plugins.fireHook('filter:parse.post', { postData: post });
|
||||
const result = await plugins.hooks.fire('filter:parse.post', { postData: post });
|
||||
result.postData.content = translator.escape(result.postData.content);
|
||||
return result.postData;
|
||||
};
|
||||
|
||||
@@ -40,7 +40,7 @@ module.exports = function (Posts) {
|
||||
editPostData.handle = data.handle;
|
||||
}
|
||||
|
||||
const result = await plugins.fireHook('filter:post.edit', {
|
||||
const result = await plugins.hooks.fire('filter:post.edit', {
|
||||
req: data.req,
|
||||
post: editPostData,
|
||||
data: data,
|
||||
@@ -79,7 +79,7 @@ module.exports = function (Posts) {
|
||||
nid: 'edit_post:' + data.pid + ':uid:' + data.uid,
|
||||
});
|
||||
|
||||
plugins.fireHook('action:post.edit', { post: _.clone(returnPostData), data: data, uid: data.uid });
|
||||
plugins.hooks.fire('action:post.edit', { post: _.clone(returnPostData), data: data, uid: data.uid });
|
||||
|
||||
require('./cache').del(String(postData.pid));
|
||||
pubsub.publish('post:edit', String(postData.pid));
|
||||
@@ -134,7 +134,7 @@ module.exports = function (Posts) {
|
||||
}
|
||||
await topics.validateTags(data.tags, topicData.cid);
|
||||
|
||||
const results = await plugins.fireHook('filter:topic.edit', {
|
||||
const results = await plugins.hooks.fire('filter:topic.edit', {
|
||||
req: data.req,
|
||||
topic: newTopicData,
|
||||
data: data,
|
||||
@@ -147,7 +147,7 @@ module.exports = function (Posts) {
|
||||
newTopicData.oldTitle = topicData.title;
|
||||
newTopicData.timestamp = topicData.timestamp;
|
||||
const renamed = translator.escape(validator.escape(String(title))) !== topicData.title;
|
||||
plugins.fireHook('action:topic.edit', { topic: newTopicData, uid: data.uid });
|
||||
plugins.hooks.fire('action:topic.edit', { topic: newTopicData, uid: data.uid });
|
||||
return {
|
||||
tid: tid,
|
||||
cid: newTopicData.cid,
|
||||
|
||||
@@ -48,7 +48,7 @@ Posts.getPostsByPids = async function (pids, uid) {
|
||||
let posts = await Posts.getPostsData(pids);
|
||||
posts = await Promise.all(posts.map(p => Posts.parsePost(p)));
|
||||
posts = await user.blocks.filter(uid, posts);
|
||||
const data = await plugins.fireHook('filter:post.getPosts', { posts: posts, uid: uid });
|
||||
const data = await plugins.hooks.fire('filter:post.getPosts', { posts: posts, uid: uid });
|
||||
if (!data || !Array.isArray(data.posts)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ module.exports = function (Posts) {
|
||||
return postData;
|
||||
}
|
||||
|
||||
const data = await plugins.fireHook('filter:parse.post', { postData: postData });
|
||||
const data = await plugins.hooks.fire('filter:parse.post', { postData: postData });
|
||||
data.postData.content = translator.escape(data.postData.content);
|
||||
if (data.postData.pid) {
|
||||
cache.set(pid, data.postData.content);
|
||||
@@ -70,7 +70,7 @@ module.exports = function (Posts) {
|
||||
|
||||
Posts.parseSignature = async function (userData, uid) {
|
||||
userData.signature = sanitizeSignature(userData.signature || '');
|
||||
return await plugins.fireHook('filter:parse.signature', { userData: userData, uid: uid });
|
||||
return await plugins.hooks.fire('filter:parse.signature', { userData: userData, uid: uid });
|
||||
};
|
||||
|
||||
Posts.relativeToAbsolute = function (content, regex) {
|
||||
@@ -121,11 +121,11 @@ module.exports = function (Posts) {
|
||||
});
|
||||
|
||||
// Some plugins might need to adjust or whitelist their own tags...
|
||||
sanitizeConfig = await plugins.fireHook('filter:sanitize.config', sanitizeConfig);
|
||||
sanitizeConfig = await plugins.hooks.fire('filter:sanitize.config', sanitizeConfig);
|
||||
};
|
||||
|
||||
Posts.registerHooks = () => {
|
||||
plugins.registerHook('core', {
|
||||
plugins.hooks.register('core', {
|
||||
hook: 'filter:parse.post',
|
||||
method: async (data) => {
|
||||
data.postData.content = Posts.sanitize(data.postData.content);
|
||||
@@ -133,17 +133,17 @@ module.exports = function (Posts) {
|
||||
},
|
||||
});
|
||||
|
||||
plugins.registerHook('core', {
|
||||
plugins.hooks.register('core', {
|
||||
hook: 'filter:parse.raw',
|
||||
method: async content => Posts.sanitize(content),
|
||||
});
|
||||
|
||||
plugins.registerHook('core', {
|
||||
plugins.hooks.register('core', {
|
||||
hook: 'filter:parse.aboutme',
|
||||
method: async content => Posts.sanitize(content),
|
||||
});
|
||||
|
||||
plugins.registerHook('core', {
|
||||
plugins.hooks.register('core', {
|
||||
hook: 'filter:parse.signature',
|
||||
method: async (data) => {
|
||||
data.userData.signature = Posts.sanitize(data.userData.signature);
|
||||
|
||||
@@ -18,7 +18,7 @@ module.exports = function (Posts) {
|
||||
const userData = await user.getUserFields(uid, ['uid', 'reputation', 'postcount']);
|
||||
const isMemberOfExempt = await groups.isMemberOfAny(userData.uid, meta.config.groupsExemptFromPostQueue);
|
||||
const shouldQueue = meta.config.postQueue && !isMemberOfExempt && (!userData.uid || userData.reputation < meta.config.postQueueReputationThreshold || userData.postcount <= 0);
|
||||
const result = await plugins.fireHook('filter:post.shouldQueue', {
|
||||
const result = await plugins.hooks.fire('filter:post.shouldQueue', {
|
||||
shouldQueue: !!shouldQueue,
|
||||
uid: uid,
|
||||
data: data,
|
||||
@@ -57,7 +57,7 @@ module.exports = function (Posts) {
|
||||
type: type,
|
||||
data: data,
|
||||
};
|
||||
payload = await plugins.fireHook('filter:post-queue.save', payload);
|
||||
payload = await plugins.hooks.fire('filter:post-queue.save', payload);
|
||||
payload.data = JSON.stringify(data);
|
||||
|
||||
await db.sortedSetAdd('post:queue', now, id);
|
||||
|
||||
@@ -54,7 +54,7 @@ module.exports = function (Posts) {
|
||||
posts = posts.filter(post => tidToTopic[post.tid]);
|
||||
|
||||
posts = await parsePosts(posts, options);
|
||||
const result = await plugins.fireHook('filter:post.getPostSummaryByPids', { posts: posts, uid: uid });
|
||||
const result = await plugins.hooks.fire('filter:post.getPostSummaryByPids', { posts: posts, uid: uid });
|
||||
return result.posts;
|
||||
};
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ module.exports = function (Posts) {
|
||||
const [isMemberOfGroups, signature, customProfileInfo] = await Promise.all([
|
||||
checkGroupMembership(userData.uid, userData.groupTitleArray),
|
||||
parseSignature(userData, uid, canUseSignature),
|
||||
plugins.fireHook('filter:posts.custom_profile_info', { profile: [], uid: userData.uid }),
|
||||
plugins.hooks.fire('filter:posts.custom_profile_info', { profile: [], uid: userData.uid }),
|
||||
]);
|
||||
|
||||
if (isMemberOfGroups && userData.groupTitleArray) {
|
||||
@@ -49,7 +49,7 @@ module.exports = function (Posts) {
|
||||
userData.signature = signature;
|
||||
userData.custom_profile_info = customProfileInfo.profile;
|
||||
|
||||
return await plugins.fireHook('filter:posts.modifyUserInfo', userData);
|
||||
return await plugins.hooks.fire('filter:posts.modifyUserInfo', userData);
|
||||
}));
|
||||
};
|
||||
|
||||
@@ -94,7 +94,7 @@ module.exports = function (Posts) {
|
||||
'signature', 'banned', 'banned:expire', 'status',
|
||||
'lastonline', 'groupTitle',
|
||||
];
|
||||
const result = await plugins.fireHook('filter:posts.addUserFields', {
|
||||
const result = await plugins.hooks.fire('filter:posts.addUserFields', {
|
||||
fields: fields,
|
||||
uid: uid,
|
||||
uids: uids,
|
||||
@@ -166,7 +166,7 @@ module.exports = function (Posts) {
|
||||
updateTopicPosters(postData, toUid),
|
||||
]);
|
||||
|
||||
plugins.fireHook('action:post.changeOwner', {
|
||||
plugins.hooks.fire('action:post.changeOwner', {
|
||||
posts: _.cloneDeep(postData),
|
||||
toUid: toUid,
|
||||
});
|
||||
@@ -228,7 +228,7 @@ module.exports = function (Posts) {
|
||||
]);
|
||||
|
||||
const changedTopics = mainPosts.map(p => tidToTopic[p.tid]);
|
||||
plugins.fireHook('action:topic.changeOwner', {
|
||||
plugins.hooks.fire('action:topic.changeOwner', {
|
||||
topics: _.cloneDeep(changedTopics),
|
||||
toUid: toUid,
|
||||
});
|
||||
|
||||
@@ -144,7 +144,7 @@ module.exports = function (Posts) {
|
||||
current = 'unvote';
|
||||
}
|
||||
|
||||
plugins.fireHook('action:post.' + hook, {
|
||||
plugins.hooks.fire('action:post.' + hook, {
|
||||
pid: pid,
|
||||
uid: uid,
|
||||
owner: owner,
|
||||
@@ -251,7 +251,7 @@ module.exports = function (Posts) {
|
||||
downvotes: postData.downvotes,
|
||||
}),
|
||||
]);
|
||||
plugins.fireHook('action:post.updatePostVoteCount', { post: postData });
|
||||
plugins.hooks.fire('action:post.updatePostVoteCount', { post: postData });
|
||||
};
|
||||
|
||||
async function updateTopicVoteCount(postData) {
|
||||
|
||||
Reference in New Issue
Block a user