mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 17:16:14 +01:00
feat: topic delete/restore/purge/(un)pin/(un)lock
This commit is contained in:
@@ -4,6 +4,8 @@ const topics = require('../../topics');
|
||||
const posts = require('../../posts');
|
||||
const user = require('../../user');
|
||||
const meta = require('../../meta');
|
||||
const events = require('../../events');
|
||||
const privileges = require('../../privileges');
|
||||
|
||||
const helpers = require('../helpers');
|
||||
const socketHelpers = require('../../socket.io/helpers');
|
||||
@@ -71,3 +73,86 @@ Topics.reply = async (req, res) => {
|
||||
user.updateOnlineUsers(req.user.uid);
|
||||
socketHelpers.notifyNew(req.user.uid, 'newPost', result);
|
||||
};
|
||||
|
||||
Topics.delete = async (req, res) => {
|
||||
await doTopicAction('delete', 'event:topic_deleted', req, {
|
||||
tids: [req.params.tid],
|
||||
});
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Topics.restore = async (req, res) => {
|
||||
await doTopicAction('restore', 'event:topic_restored', req, {
|
||||
tids: [req.params.tid],
|
||||
});
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Topics.purge = async (req, res) => {
|
||||
await doTopicAction('purge', 'event:topic_purged', req, {
|
||||
tids: [req.params.tid],
|
||||
});
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Topics.pin = async (req, res) => {
|
||||
await doTopicAction('pin', 'event:topic_pinned', req, {
|
||||
tids: [req.params.tid],
|
||||
});
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Topics.unpin = async (req, res) => {
|
||||
await doTopicAction('unpin', 'event:topic_unpinned', req, {
|
||||
tids: [req.params.tid],
|
||||
});
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Topics.lock = async (req, res) => {
|
||||
await doTopicAction('lock', 'event:topic_locked', req, {
|
||||
tids: [req.params.tid],
|
||||
});
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Topics.unlock = async (req, res) => {
|
||||
await doTopicAction('unlock', 'event:topic_unlocked', req, {
|
||||
tids: [req.params.tid],
|
||||
});
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
async function doTopicAction(action, event, socket, { tids }) {
|
||||
if (!Array.isArray(tids)) {
|
||||
throw new Error('[[error:invalid-tid]]');
|
||||
}
|
||||
|
||||
if (typeof topics.tools[action] !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
const uids = await user.getUidsFromSet('users:online', 0, -1);
|
||||
|
||||
await Promise.all(tids.map(async function (tid) {
|
||||
const title = await topics.getTopicField(tid, 'title');
|
||||
const data = await topics.tools[action](tid, socket.uid);
|
||||
const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids);
|
||||
socketHelpers.emitToTopicAndCategory(event, data, notifyUids);
|
||||
await logTopicAction(action, socket, tid, title);
|
||||
}));
|
||||
}
|
||||
|
||||
async function logTopicAction(action, req, tid, title) {
|
||||
var actionsToLog = ['delete', 'restore', 'purge'];
|
||||
if (!actionsToLog.includes(action)) {
|
||||
return;
|
||||
}
|
||||
await events.log({
|
||||
type: 'topic-' + action,
|
||||
uid: req.uid,
|
||||
ip: req.ip,
|
||||
tid: tid,
|
||||
title: String(title),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user