How to improve popular posts response time?
-
Yeah that custom code could explain the slowdown of popular. Best way to check is to disable it temporarily and see if the pages load fast.
Can simply comment out the code and return immediately and restart nodebb to test.myPlugin.filterTopicsGet = async function (hookData) { // const groupData = await Promise.all(hookData.topics.map(async (t) => { // const uids = await db.getSortedSetMembers('tid:' + t.tid + ':posters'); // const userGroups = await Groups.getUserGroupMembership('groups:createtime', uids); // return userGroups.flat().includes('My developer group'); // })); // hookData.topics.forEach((t, i) => { // t.postedInByDeveloper = groupData[i]; // }); return hookData; };
If pages are fast then gotto find a more efficient way to show the group badge. That code also explains why the hit rate was low for groups since its calling getUserGroupMembership alot.
-
Yes another way to set
postedInByDeveloper
would be to do it when someone posts in a topic. In the hook,action:topic.post
You can execute that function you have in the hook and setpostedByInDeveloper
that way it is not called 20 times for each topic when loading topic lists. -
It would look something like this
myPlugin.actionTopicReply = async function (hookData) { const userGroups = await Groups.getUserGroupMembership('groups:visible:createtime', [hookData.data.uid]); const isInDevGroup = userGroups.flat().includes('My developer group'); if (isInDevGroup) { await topics.setTopicField(hookData.topic.tid, 'postedInByDeveloper', 1); } };
So essentially you save a field into the topic object if a developer posts in it. This way you don't need to calculate it each time the topic list is loaded.
-
You might want to use both
action:topic.reply
andaction:topic.post
one is triggered on reply and one is triggered when a new topic is posted. Also if the developer group is visible then you can useconst userGroups = await Groups.getUserGroupMembership('groups:visible:createtime', [hookData.data.uid]);
and it will be even faster. -
-
I just realized there is a much simpler way to do this, since you only seem to care about if the user is in a special
dev group
you don't need to load the user's groups, you can just check if they are in that special group.myPlugin.actionTopicReply = async function (hookData) { const isInDevGroup = await Groups.isMember(hookData.data.uid, 'My developer group'); if (isInDevGroup) { await topics.setTopicField(hookData.topic.tid, 'postedInByDeveloper', 1); } };