Skip to content

Commit

Permalink
feat(topic-events): support for uids in topic event payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Jan 27, 2021
1 parent 425eca1 commit 611d1f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
2 changes: 2 additions & 0 deletions public/language/en-GB/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
"guest": "Guest",
"guests": "Guests",
"former_user": "A Former User",
"system-user": "System",
"unknown-user": "Unknown user",

"updated.title": "Forum Updated",
"updated.message": "This forum has just been updated to the latest version. Click here to refresh the page.",
Expand Down
11 changes: 7 additions & 4 deletions public/language/en-GB/topic.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,30 @@
"login-to-view": "🔒 Log in to view",
"edit": "Edit",
"delete": "Delete",
"deleted": "Deleted",
"purge": "Purge",
"restore": "Restore",
"restored": "Restored",
"move": "Move",
"change-owner": "Change Owner",
"fork": "Fork",
"link": "Link",
"share": "Share",
"tools": "Tools",
"locked": "Locked",
"unlocked": "Unlocked",
"pinned": "Pinned",
"pinned-with-expiry": "Pinned until %1",
"unpinned": "Unpinned",
"moved": "Moved",
"moved-from": "Moved from %1",
"copy-ip": "Copy IP",
"ban-ip": "Ban IP",
"view-history": "Edit History",

"locked-by": "Locked by",
"unlocked-by": "Unlocked by",
"pinned-by": "Pinned by",
"unpinned-by": "Unpinned by",
"deleted-by": "Deleted by",
"restored-by": "Restored by",

"bookmark_instructions" : "Click here to return to the last read post in this thread.",

"flag-post": "Flag this post",
Expand Down
48 changes: 37 additions & 11 deletions src/topics/events.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
'use strict';

const db = require('../database');
const user = require('../user');
const plugins = require('../plugins');

const Events = module.exports;

/**
* Note: Plugins!
*
* You are able to define additional topic event types here.
* Register to hook `filter:topicEvents.init` and append your custom type to the `types` object.
* You can then log a custom topic event by calling `topics.events.log(tid, { type, uid });`
* `uid` is optional; if you pass in a valid uid in the payload, the user avatar/username will be rendered as part of the event text
*
*/
Events._types = {
pin: {
icon: 'fa-thumb-tack',
text: '[[topic:pinned]]',
},
pin_expiry: {
icon: 'fa-thumb-tack',
text: '[[topic:pinned-with-expiry]]',
text: '[[topic:pinned-by]]',
},
unpin: {
icon: 'fa-thumb-tack',
text: '[[topic:unpinned]]',
text: '[[topic:unpinned-by]]',
},
lock: {
icon: 'fa-lock',
text: '[[topic:locked]]',
text: '[[topic:locked-by]]',
},
unlock: {
icon: 'fa-unlock',
text: '[[topic:unlocked]]',
text: '[[topic:unlocked-by]]',
},
delete: {
icon: 'fa-trash',
text: '[[topic:deleted]]',
text: '[[topic:deleted-by]]',
},
restore: {
icon: 'fa-trash-o',
text: '[[topic:restored]]',
text: '[[topic:restored-by]]',
},
};
Events._ready = false;
Expand All @@ -57,18 +63,38 @@ Events.get = async (tid) => {
const eventIds = await db.getSortedSetRangeWithScores(`topic:${tid}:events`, 0, -1);
const keys = eventIds.map(obj => `topicEvent:${obj.value}`);
const timestamps = eventIds.map(obj => obj.score);
const events = await db.getObjects(keys);
let events = await db.getObjects(keys);
const users = await getUserInfo(events.map(event => event.uid).filter(Boolean));

// Remove events whose types no longer exist (e.g. plugin uninstalled)
events = events.filter(event => Events._types.hasOwnProperty(event.type));

// Add user & metadata
events.forEach((event, idx) => {
event.id = parseInt(eventIds[idx].value, 10);
event.timestamp = timestamps[idx];
event.timestampISO = new Date(timestamps[idx]).toISOString();
if (event.hasOwnProperty('uid')) {
event.user = users.get(event.uid === 'system' ? 'system' : parseInt(event.uid, 10));
}

Object.assign(event, Events._types[event.type]);
});

return events;
};

async function getUserInfo(uids) {
uids = uids.filter((uid, idx) => !isNaN(parseInt(uid, 10)) && uids.indexOf(uid) === idx);
const userData = await user.getUsersFields(uids, ['picture', 'username', 'userslug']);
const userMap = userData.reduce((memo, cur) => memo.set(cur.uid, cur), new Map());
userMap.set('system', {
system: true,
});

return userMap;
}

Events.log = async (tid, payload) => {
await Events.init();
const topics = require('.');
Expand Down

0 comments on commit 611d1f8

Please sign in to comment.