Skip to content

Commit

Permalink
refactor: topic creation to use api lib
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Oct 16, 2020
1 parent b6cce75 commit 40598b3
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 54 deletions.
1 change: 1 addition & 0 deletions src/api/index.js
Expand Up @@ -3,4 +3,5 @@
module.exports = {
users: require('./users'),
groups: require('./groups'),
topics: require('./topics'),
};
11 changes: 9 additions & 2 deletions src/controllers/helpers.js
Expand Up @@ -14,6 +14,8 @@ const meta = require('../meta');
const middleware = require('../middleware');
const translator = require('../translator');

const websockets = require('../socket.io');

const isLanguageKey = /^\[\[[\w.\-_:]+]]$/;
const helpers = module.exports;

Expand Down Expand Up @@ -341,8 +343,8 @@ helpers.getHomePageRoutes = async function (uid) {
};

helpers.formatApiResponse = async (statusCode, res, payload) => {
if (statusCode === 200) {
res.status(200).json({
if (String(statusCode).startsWith('2')) {
res.status(statusCode).json({
status: {
code: 'ok',
message: 'OK',
Expand Down Expand Up @@ -426,6 +428,11 @@ helpers.generateError = (statusCode, message) => {
};

helpers.buildReqObject = (req) => {
// If a socket object is received instead, handle accordingly
if (req.id) {
return websockets.reqFromSocket(req);
}

var headers = req.headers;
var encrypted = !!req.connection.encrypted;
var host = headers.host;
Expand Down
33 changes: 9 additions & 24 deletions src/controllers/write/topics.js
@@ -1,5 +1,6 @@
'use strict';

const api = require('../../api');
const topics = require('../../topics');
const posts = require('../../posts');
const user = require('../../user');
Expand All @@ -13,29 +14,12 @@ const socketHelpers = require('../../socket.io/helpers');
const Topics = module.exports;

Topics.create = async (req, res) => {
const payload = { ...req.body };
payload.tags = payload.tags || [];
payload.uid = req.user.uid;
payload.uid = req.user.uid;
payload.req = req;
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 payload = await api.topics.create(req, req.body);
if (payload.queued) {
helpers.formatApiResponse(202, res, payload);
} else {
helpers.formatApiResponse(200, res, payload);
}

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) => {
Expand All @@ -54,7 +38,8 @@ Topics.reply = async (req, res) => {
await meta.blacklist.test(req.ip);
const shouldQueue = await posts.shouldQueue(req.user.uid, payload);
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?
Expand Down Expand Up @@ -163,7 +148,7 @@ async function doTopicAction(action, event, socket, { tids }) {
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);
socketHelpers.emitToUids(event, data, notifyUids);
await logTopicAction(action, socket, tid, title);
}));
}
Expand Down
2 changes: 1 addition & 1 deletion src/socket.io/helpers.js
Expand Up @@ -191,7 +191,7 @@ SocketHelpers.rescindUpvoteNotification = async function (pid, fromuid) {
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));
};

Expand Down
1 change: 0 additions & 1 deletion src/socket.io/index.js
Expand Up @@ -13,7 +13,6 @@ const logger = require('../logger');
const plugins = require('../plugins');
const ratelimit = require('../middleware/ratelimit');


const Namespaces = {};

const Sockets = module.exports;
Expand Down
26 changes: 2 additions & 24 deletions src/socket.io/topics.js
@@ -1,13 +1,12 @@
'use strict';

const api = require('../api');
const topics = require('../topics');
const posts = require('../posts');
const user = require('../user');
const meta = require('../meta');
const apiController = require('../controllers/api');
const privileges = require('../privileges');
const sockets = require('.');
const socketHelpers = require('./helpers');

const SocketTopics = module.exports;

Expand All @@ -20,30 +19,9 @@ require('./topics/merge')(SocketTopics);

SocketTopics.post = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v3/topics');

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);
return await api.topics.create(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) {
const canRead = await privileges.topics.can('topics:read', tid, socket.uid);
if (!canRead) {
Expand Down
2 changes: 1 addition & 1 deletion src/socket.io/topics/move.js
Expand Up @@ -30,7 +30,7 @@ module.exports = function (SocketTopics) {
await topics.tools.move(tid, data);

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) {
socketHelpers.sendNotificationToTopicOwner(tid, socket.uid, 'move', 'notifications:moved_your_topic');
}
Expand Down
2 changes: 1 addition & 1 deletion src/socket.io/topics/tools.js
Expand Up @@ -88,7 +88,7 @@ module.exports = function (SocketTopics) {
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);
socketHelpers.emitToUids(event, data, notifyUids);
await logTopicAction(action, socket, tid, title);
}));
};
Expand Down

0 comments on commit 40598b3

Please sign in to comment.