Skip to content

Commit

Permalink
fix: missing doTopicAction, fix wrong api params
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 17, 2020
1 parent bc880ee commit e78c498
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 63 deletions.
45 changes: 45 additions & 0 deletions src/api/helpers.js
@@ -1,6 +1,11 @@
'use strict';

const url = require('url');
const user = require('../user');
const topics = require('../topics');
const privileges = require('../privileges');
const socketHelpers = require('../socket.io/helpers');
const events = require('../events');

// creates a slimmed down version of the request object
exports.buildReqObject = (req, payload) => {
Expand Down Expand Up @@ -28,3 +33,43 @@ exports.buildReqObject = (req, payload) => {
headers: headers,
};
};


exports.doTopicAction = async function (action, event, caller, { tids }) {
if (!Array.isArray(tids)) {
throw new Error('[[error:invalid-tid]]');
}

const exists = (await Promise.all(tids.map(async tid => await topics.exists(tid)))).every(Boolean);
if (!exists) {
throw new Error('[[error:no-topic]]');
}

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, caller.uid);
const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids);
socketHelpers.emitToUids(event, data, notifyUids);
await logTopicAction(action, caller, 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),
});
}
57 changes: 9 additions & 48 deletions src/api/topics.js
Expand Up @@ -4,10 +4,10 @@ const user = require('../user');
const topics = require('../topics');
const posts = require('../posts');
const meta = require('../meta');
const events = require('../events');
const privileges = require('../privileges');

const apiHelpers = require('./helpers');
const doTopicAction = apiHelpers.doTopicAction;

const socketHelpers = require('../socket.io/helpers');

const topicsAPI = module.exports;
Expand Down Expand Up @@ -80,43 +80,43 @@ topicsAPI.reply = async function (caller, data) {

topicsAPI.delete = async function (caller, data) {
await doTopicAction('delete', 'event:topic_deleted', caller, {
tids: [data.tid],
tids: data.tids,
});
};

topicsAPI.restore = async function (caller, data) {
await doTopicAction('restore', 'event:topic_restored', caller, {
tids: [data.tid],
tids: data.tids,
});
};

topicsAPI.purge = async function (caller, data) {
await doTopicAction('purge', 'event:topic_purged', caller, {
tids: [data.tid],
tids: data.tids,
});
};

topicsAPI.pin = async function (caller, data) {
await doTopicAction('pin', 'event:topic_pinned', caller, {
tids: [data.tid],
tids: data.tids,
});
};

topicsAPI.unpin = async function (caller, data) {
await doTopicAction('unpin', 'event:topic_unpinned', caller, {
tids: [data.tid],
tids: data.tids,
});
};

topicsAPI.lock = async function (caller, data) {
await doTopicAction('lock', 'event:topic_locked', caller, {
tids: [data.tid],
tids: data.tids,
});
};

topicsAPI.unlock = async function (caller, data) {
await doTopicAction('unlock', 'event:topic_unlocked', caller, {
tids: [data.tid],
tids: data.tids,
});
};

Expand All @@ -131,42 +131,3 @@ topicsAPI.ignore = async function (caller, data) {
topicsAPI.unfollow = async function (caller, data) {
await topics.unfollow(data.tid, caller.uid);
};

async function doTopicAction(action, event, caller, { tids }) {
if (!Array.isArray(tids)) {
throw new Error('[[error:invalid-tid]]');
}

const exists = (await Promise.all(tids.map(async tid => await topics.exists(tid)))).every(Boolean);
if (!exists) {
throw new Error('[[error:no-topic]]');
}

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, caller.uid);
const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids);
socketHelpers.emitToUids(event, data, notifyUids);
await logTopicAction(action, caller, 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),
});
}
8 changes: 4 additions & 4 deletions src/controllers/write/posts.js
Expand Up @@ -13,8 +13,8 @@ const utils = require('../../utils');

const helpers = require('../helpers');
const sockets = require('../../socket.io');
const apiHelpers = require('../../api/helpers');
const socketPostHelpers = require('../../socket.io/posts/helpers'); // eehhh...
const socketTopics = require('../../socket.io/topics'); // eehhh...

const Posts = module.exports;

Expand Down Expand Up @@ -111,7 +111,7 @@ Posts.purge = async (req, res) => {
});

if (isMainAndLast) {
await socketTopics.doTopicAction('purge', 'event:topic_purged', {
await apiHelpers.doTopicAction('purge', 'event:topic_purged', {
ip: req.ip,
uid: req.user.uid,
}, { tids: [postData.tid], cid: topicData.cid });
Expand Down Expand Up @@ -199,12 +199,12 @@ async function isMainAndLastPost(pid) {
async function deleteOrRestoreTopicOf(command, pid, req) {
const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
if (command === 'delete' && !topic.deleted) {
await socketTopics.doTopicAction('delete', 'event:topic_deleted', {
await apiHelpers.doTopicAction('delete', 'event:topic_deleted', {
uid: req.user.uid,
ip: req.ip,
}, { tids: [topic.tid], cid: topic.cid });
} else if (command === 'restore' && topic.deleted) {
await socketTopics.doTopicAction('restore', 'event:topic_restored', {
await apiHelpers.doTopicAction('restore', 'event:topic_restored', {
uid: req.user.uid,
ip: req.ip,
}, { tids: [topic.tid], cid: topic.cid });
Expand Down
14 changes: 7 additions & 7 deletions src/controllers/write/topics.js
Expand Up @@ -22,37 +22,37 @@ Topics.reply = async (req, res) => {
};

Topics.delete = async (req, res) => {
await api.topics.delete(req, req.params);
await api.topics.delete(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};

Topics.restore = async (req, res) => {
await api.topics.restore(req, req.params);
await api.topics.restore(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};

Topics.purge = async (req, res) => {
await api.topics.purge(req, req.params);
await api.topics.purge(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};

Topics.pin = async (req, res) => {
await api.topics.pin(req, req.params);
await api.topics.pin(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};

Topics.unpin = async (req, res) => {
await api.topics.unpin(req, req.params);
await api.topics.unpin(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};

Topics.lock = async (req, res) => {
await api.topics.lock(req, req.params);
await api.topics.lock(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};

Topics.unlock = async (req, res) => {
await api.topics.unlock(req, req.params);
await api.topics.unlock(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};

Expand Down
8 changes: 4 additions & 4 deletions src/socket.io/posts/tools.js
Expand Up @@ -5,7 +5,7 @@ const topics = require('../../topics');
const flags = require('../../flags');
const events = require('../../events');
const websockets = require('../index');
const socketTopics = require('../topics');
const apiHelpers = require('../../api/helpers');
const privileges = require('../../privileges');
const plugins = require('../../plugins');
const social = require('../../social');
Expand Down Expand Up @@ -158,16 +158,16 @@ module.exports = function (SocketPosts) {
});

if (isMainAndLast) {
await socketTopics.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid });
await apiHelpers.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid });
}
};

async function deleteOrRestoreTopicOf(command, pid, socket) {
const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
if (command === 'delete' && !topic.deleted) {
await socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid });
await apiHelpers.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid });
} else if (command === 'restore' && topic.deleted) {
await socketTopics.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid });
await apiHelpers.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid });
}
}

Expand Down

0 comments on commit e78c498

Please sign in to comment.