feat: topic reply to use api lib (also + missing file)

v1.18.x
Julian Lam 4 years ago
parent 40598b368e
commit 21974a77f8

@ -0,0 +1,77 @@
'use strict';
const user = require('../user');
const topics = require('../topics');
const posts = require('../posts');
const meta = require('../meta');
const controllerHelpers = require('../controllers/helpers');
const socketHelpers = require('../socket.io/helpers');
const topicsAPI = module.exports;
topicsAPI.create = async function (caller, data) {
if (!data) {
throw new Error('[[error:invalid-data]]');
}
const payload = { ...data };
payload.tags = payload.tags || [];
payload.uid = caller.uid;
payload.uid = caller.uid;
payload.req = controllerHelpers.buildReqObject(caller);
payload.timestamp = Date.now();
payload.fromQueue = false;
// Blacklist & Post Queue
await meta.blacklist.test(caller.ip);
const shouldQueue = await posts.shouldQueue(caller.uid, payload);
if (shouldQueue) {
const queueObj = await posts.addToQueue(payload);
return queueObj;
}
const result = await topics.post(payload);
socketHelpers.emitToUids('event:new_post', { posts: [result.postData] }, [caller.uid]);
socketHelpers.emitToUids('event:new_topic', result.topicData, [caller.uid]);
socketHelpers.notifyNew(caller.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
return result.topicData;
};
topicsAPI.reply = async function (caller, data) {
var payload = {
tid: data.tid,
uid: caller.uid,
req: controllerHelpers.buildReqObject(caller), // For IP recording
content: data.content,
timestamp: data.timestamp,
fromQueue: false,
};
if (data.toPid) { payload.toPid = data.toPid; }
// Blacklist & Post Queue
await meta.blacklist.test(caller.ip);
const shouldQueue = await posts.shouldQueue(caller.uid, payload);
if (shouldQueue) {
const queueObj = await posts.addToQueue(payload);
return queueObj;
}
const postData = await topics.reply(payload); // postData seems to be a subset of postObj, refactor?
const postObj = await posts.getPostSummaryByPids([postData.pid], caller.uid, {});
const result = {
posts: [postData],
'reputation:disabled': meta.config['reputation:disabled'] === 1,
'downvote:disabled': meta.config['downvote:disabled'] === 1,
};
user.updateOnlineUsers(caller.uid);
socketHelpers.emitToUids('event:new_post', result, [caller.uid]);
socketHelpers.notifyNew(caller.uid, 'newPost', result);
return postObj[0];
};

@ -2,9 +2,7 @@
const api = require('../../api'); const api = require('../../api');
const topics = require('../../topics'); const topics = require('../../topics');
const posts = require('../../posts');
const user = require('../../user'); const user = require('../../user');
const meta = require('../../meta');
const events = require('../../events'); const events = require('../../events');
const privileges = require('../../privileges'); const privileges = require('../../privileges');
@ -23,40 +21,8 @@ Topics.create = async (req, res) => {
}; };
Topics.reply = async (req, res) => { Topics.reply = async (req, res) => {
var payload = { const payload = await api.topics.reply(req, { ...req.body, tid: req.params.tid });
tid: req.params.tid, helpers.formatApiResponse(200, res, payload);
uid: req.user.uid,
req: helpers.buildReqObject(req), // For IP recording
content: req.body.content,
timestamp: req.body.timestamp,
fromQueue: false,
};
if (req.body.toPid) { payload.toPid = req.body.toPid; }
// Blacklist & Post Queue
await meta.blacklist.test(req.ip);
const shouldQueue = await posts.shouldQueue(req.user.uid, payload);
if (shouldQueue) {
const queueObj = await posts.addToQueue(payload);
return helpers.formatApiResponse(202, res, queueObj);
}
const postData = await topics.reply(payload); // postData seems to be a subset of postObj, refactor?
const postObj = await posts.getPostSummaryByPids([postData.pid], req.user.uid, {});
helpers.formatApiResponse(200, res, postObj[0]);
const result = {
posts: [postData],
'reputation:disabled': meta.config['reputation:disabled'] === 1,
'downvote:disabled': meta.config['downvote:disabled'] === 1,
};
// TODO
// socket.emit('event:new_post', result);
user.updateOnlineUsers(req.user.uid);
socketHelpers.notifyNew(req.user.uid, 'newPost', result);
}; };
Topics.delete = async (req, res) => { Topics.delete = async (req, res) => {

Loading…
Cancel
Save