mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-29 10:06:13 +01:00
refactor: topic creation to use api lib
This commit is contained in:
@@ -3,4 +3,5 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
users: require('./users'),
|
users: require('./users'),
|
||||||
groups: require('./groups'),
|
groups: require('./groups'),
|
||||||
|
topics: require('./topics'),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ const meta = require('../meta');
|
|||||||
const middleware = require('../middleware');
|
const middleware = require('../middleware');
|
||||||
const translator = require('../translator');
|
const translator = require('../translator');
|
||||||
|
|
||||||
|
const websockets = require('../socket.io');
|
||||||
|
|
||||||
const isLanguageKey = /^\[\[[\w.\-_:]+]]$/;
|
const isLanguageKey = /^\[\[[\w.\-_:]+]]$/;
|
||||||
const helpers = module.exports;
|
const helpers = module.exports;
|
||||||
|
|
||||||
@@ -341,8 +343,8 @@ helpers.getHomePageRoutes = async function (uid) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
helpers.formatApiResponse = async (statusCode, res, payload) => {
|
helpers.formatApiResponse = async (statusCode, res, payload) => {
|
||||||
if (statusCode === 200) {
|
if (String(statusCode).startsWith('2')) {
|
||||||
res.status(200).json({
|
res.status(statusCode).json({
|
||||||
status: {
|
status: {
|
||||||
code: 'ok',
|
code: 'ok',
|
||||||
message: 'OK',
|
message: 'OK',
|
||||||
@@ -426,6 +428,11 @@ helpers.generateError = (statusCode, message) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
helpers.buildReqObject = (req) => {
|
helpers.buildReqObject = (req) => {
|
||||||
|
// If a socket object is received instead, handle accordingly
|
||||||
|
if (req.id) {
|
||||||
|
return websockets.reqFromSocket(req);
|
||||||
|
}
|
||||||
|
|
||||||
var headers = req.headers;
|
var headers = req.headers;
|
||||||
var encrypted = !!req.connection.encrypted;
|
var encrypted = !!req.connection.encrypted;
|
||||||
var host = headers.host;
|
var host = headers.host;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const api = require('../../api');
|
||||||
const topics = require('../../topics');
|
const topics = require('../../topics');
|
||||||
const posts = require('../../posts');
|
const posts = require('../../posts');
|
||||||
const user = require('../../user');
|
const user = require('../../user');
|
||||||
@@ -13,29 +14,12 @@ const socketHelpers = require('../../socket.io/helpers');
|
|||||||
const Topics = module.exports;
|
const Topics = module.exports;
|
||||||
|
|
||||||
Topics.create = async (req, res) => {
|
Topics.create = async (req, res) => {
|
||||||
const payload = { ...req.body };
|
const payload = await api.topics.create(req, req.body);
|
||||||
payload.tags = payload.tags || [];
|
if (payload.queued) {
|
||||||
payload.uid = req.user.uid;
|
helpers.formatApiResponse(202, res, payload);
|
||||||
payload.uid = req.user.uid;
|
} else {
|
||||||
payload.req = req;
|
helpers.formatApiResponse(200, res, payload);
|
||||||
payload.timestamp = Date.now();
|
|
||||||
payload.fromQueue = false;
|
|
||||||
|
|
||||||
// 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 result = await topics.post(payload);
|
|
||||||
helpers.formatApiResponse(200, res, result.topicData);
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
// socket.emit('event:new_post', { posts: [result.postData] });
|
|
||||||
// socket.emit('event:new_topic', result.topicData);
|
|
||||||
socketHelpers.notifyNew(req.user.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Topics.reply = async (req, res) => {
|
Topics.reply = async (req, res) => {
|
||||||
@@ -54,7 +38,8 @@ Topics.reply = async (req, res) => {
|
|||||||
await meta.blacklist.test(req.ip);
|
await meta.blacklist.test(req.ip);
|
||||||
const shouldQueue = await posts.shouldQueue(req.user.uid, payload);
|
const shouldQueue = await posts.shouldQueue(req.user.uid, payload);
|
||||||
if (shouldQueue) {
|
if (shouldQueue) {
|
||||||
return await posts.addToQueue(payload);
|
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 postData = await topics.reply(payload); // postData seems to be a subset of postObj, refactor?
|
||||||
@@ -163,7 +148,7 @@ async function doTopicAction(action, event, socket, { tids }) {
|
|||||||
const title = await topics.getTopicField(tid, 'title');
|
const title = await topics.getTopicField(tid, 'title');
|
||||||
const data = await topics.tools[action](tid, socket.uid);
|
const data = await topics.tools[action](tid, socket.uid);
|
||||||
const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids);
|
const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids);
|
||||||
socketHelpers.emitToTopicAndCategory(event, data, notifyUids);
|
socketHelpers.emitToUids(event, data, notifyUids);
|
||||||
await logTopicAction(action, socket, tid, title);
|
await logTopicAction(action, socket, tid, title);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,7 +191,7 @@ SocketHelpers.rescindUpvoteNotification = async function (pid, fromuid) {
|
|||||||
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
|
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketHelpers.emitToTopicAndCategory = async function (event, data, uids) {
|
SocketHelpers.emitToUids = async function (event, data, uids) {
|
||||||
uids.forEach(toUid => websockets.in('uid_' + toUid).emit(event, data));
|
uids.forEach(toUid => websockets.in('uid_' + toUid).emit(event, data));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ const logger = require('../logger');
|
|||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
const ratelimit = require('../middleware/ratelimit');
|
const ratelimit = require('../middleware/ratelimit');
|
||||||
|
|
||||||
|
|
||||||
const Namespaces = {};
|
const Namespaces = {};
|
||||||
|
|
||||||
const Sockets = module.exports;
|
const Sockets = module.exports;
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
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 meta = require('../meta');
|
||||||
const apiController = require('../controllers/api');
|
const apiController = require('../controllers/api');
|
||||||
const privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
const sockets = require('.');
|
const sockets = require('.');
|
||||||
const socketHelpers = require('./helpers');
|
|
||||||
|
|
||||||
const SocketTopics = module.exports;
|
const SocketTopics = module.exports;
|
||||||
|
|
||||||
@@ -20,30 +19,9 @@ require('./topics/merge')(SocketTopics);
|
|||||||
|
|
||||||
SocketTopics.post = async function (socket, data) {
|
SocketTopics.post = async function (socket, data) {
|
||||||
sockets.warnDeprecated(socket, 'POST /api/v3/topics');
|
sockets.warnDeprecated(socket, 'POST /api/v3/topics');
|
||||||
|
return await api.topics.create(socket, data);
|
||||||
if (!data) {
|
|
||||||
throw new Error('[[error:invalid-data]]');
|
|
||||||
}
|
|
||||||
|
|
||||||
socketHelpers.setDefaultPostData(data, socket);
|
|
||||||
await meta.blacklist.test(data.req.ip);
|
|
||||||
const shouldQueue = await posts.shouldQueue(socket.uid, data);
|
|
||||||
if (shouldQueue) {
|
|
||||||
return await posts.addToQueue(data);
|
|
||||||
}
|
|
||||||
return await postTopic(socket, data);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
async function postTopic(socket, data) {
|
|
||||||
const result = await topics.post(data);
|
|
||||||
|
|
||||||
socket.emit('event:new_post', { posts: [result.postData] });
|
|
||||||
socket.emit('event:new_topic', result.topicData);
|
|
||||||
|
|
||||||
socketHelpers.notifyNew(socket.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
|
|
||||||
return result.topicData;
|
|
||||||
}
|
|
||||||
|
|
||||||
SocketTopics.postcount = async function (socket, tid) {
|
SocketTopics.postcount = async function (socket, tid) {
|
||||||
const canRead = await privileges.topics.can('topics:read', tid, socket.uid);
|
const canRead = await privileges.topics.can('topics:read', tid, socket.uid);
|
||||||
if (!canRead) {
|
if (!canRead) {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ module.exports = function (SocketTopics) {
|
|||||||
await topics.tools.move(tid, data);
|
await topics.tools.move(tid, data);
|
||||||
|
|
||||||
const notifyUids = await privileges.categories.filterUids('topics:read', topicData.cid, uids);
|
const notifyUids = await privileges.categories.filterUids('topics:read', topicData.cid, uids);
|
||||||
socketHelpers.emitToTopicAndCategory('event:topic_moved', topicData, notifyUids);
|
socketHelpers.emitToUids('event:topic_moved', topicData, notifyUids);
|
||||||
if (!topicData.deleted) {
|
if (!topicData.deleted) {
|
||||||
socketHelpers.sendNotificationToTopicOwner(tid, socket.uid, 'move', 'notifications:moved_your_topic');
|
socketHelpers.sendNotificationToTopicOwner(tid, socket.uid, 'move', 'notifications:moved_your_topic');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ module.exports = function (SocketTopics) {
|
|||||||
const title = await topics.getTopicField(tid, 'title');
|
const title = await topics.getTopicField(tid, 'title');
|
||||||
const data = await topics.tools[action](tid, socket.uid);
|
const data = await topics.tools[action](tid, socket.uid);
|
||||||
const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids);
|
const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids);
|
||||||
socketHelpers.emitToTopicAndCategory(event, data, notifyUids);
|
socketHelpers.emitToUids(event, data, notifyUids);
|
||||||
await logTopicAction(action, socket, tid, title);
|
await logTopicAction(action, socket, tid, title);
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user