feat(writeapi): adding missing files
parent
4c833d0bf0
commit
6096f74ab2
@ -0,0 +1,73 @@
|
||||
'use strict';
|
||||
|
||||
const topics = require('../../topics');
|
||||
const posts = require('../../posts');
|
||||
const user = require('../../user');
|
||||
const meta = require('../../meta');
|
||||
|
||||
const helpers = require('../helpers');
|
||||
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 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) => {
|
||||
var payload = {
|
||||
tid: req.params.tid,
|
||||
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) {
|
||||
return await posts.addToQueue(payload);
|
||||
}
|
||||
|
||||
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);
|
||||
};
|
@ -0,0 +1,101 @@
|
||||
'use strict';
|
||||
|
||||
const router = require('express').Router();
|
||||
const middleware = require('../../middleware');
|
||||
const controllers = require('../../controllers');
|
||||
const routeHelpers = require('../helpers');
|
||||
|
||||
const setupApiRoute = routeHelpers.setupApiRoute;
|
||||
|
||||
module.exports = function () {
|
||||
const middlewares = [middleware.authenticate];
|
||||
|
||||
setupApiRoute(router, '/', middleware, [...middlewares, middleware.checkRequired.bind(null, ['cid', 'title', 'content'])], 'post', controllers.write.topics.create);
|
||||
setupApiRoute(router, '/:tid', middleware, [...middlewares, middleware.checkRequired.bind(null, ['content']), middleware.assertTopic], 'post', controllers.write.topics.reply);
|
||||
// setupApiRoute(router, '/:cid', middleware, [...middlewares, middleware.isAdmin], 'put', controllers.write.categories.update);
|
||||
// setupApiRoute(router, '/:cid', middleware, [...middlewares, middleware.isAdmin], 'delete', controllers.write.categories.delete);
|
||||
|
||||
// app.route('/:tid')
|
||||
// .delete(apiMiddleware.requireUser, apiMiddleware.validateTid, function(req, res) {
|
||||
// Topics.purgePostsAndTopic(req.params.tid, req.params._uid, function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// })
|
||||
// .put(apiMiddleware.requireUser, function(req, res) {
|
||||
// if (!utils.checkRequired(['pid', 'content'], req, res)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// var payload = {
|
||||
// uid: req.user.uid,
|
||||
// pid: req.body.pid,
|
||||
// content: req.body.content,
|
||||
// options: {}
|
||||
// };
|
||||
// console.log(payload);
|
||||
|
||||
// // Maybe a "set if available" utils method may come in handy
|
||||
// if (req.body.handle) { payload.handle = req.body.handle; }
|
||||
// if (req.body.title) { payload.title = req.body.title; }
|
||||
// if (req.body.topic_thumb) { payload.options.topic_thumb = req.body.topic_thumb; }
|
||||
// if (req.body.tags) { payload.options.tags = req.body.tags; }
|
||||
|
||||
// Posts.edit(payload, function(err, returnData) {
|
||||
// errorHandler.handle(err, res, returnData);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.route('/:tid/state')
|
||||
// .put(apiMiddleware.requireUser, apiMiddleware.validateTid, function (req, res) {
|
||||
// Topics.restore(req.params.tid, req.params._uid, function (err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// })
|
||||
// .delete(apiMiddleware.requireUser, apiMiddleware.validateTid, function (req, res) {
|
||||
// Topics.delete(req.params.tid, req.params._uid, function (err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.route('/:tid/follow')
|
||||
// .put(apiMiddleware.requireUser, apiMiddleware.validateTid, function(req, res) {
|
||||
// Topics.follow(req.params.tid, req.user.uid, function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// })
|
||||
// .delete(apiMiddleware.requireUser, apiMiddleware.validateTid, function(req, res) {
|
||||
// Topics.unfollow(req.params.tid, req.user.uid, function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.route('/:tid/tags')
|
||||
// .put(apiMiddleware.requireUser, apiMiddleware.validateTid, function(req, res) {
|
||||
// if (!utils.checkRequired(['tags'], req, res)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// Topics.createTags(req.body.tags, req.params.tid, Date.now(), function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// })
|
||||
// .delete(apiMiddleware.requireUser, apiMiddleware.validateTid, function(req, res) {
|
||||
// Topics.deleteTopicTags(req.params.tid, function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
// app.route('/:tid/pin')
|
||||
// .put(apiMiddleware.requireUser, apiMiddleware.validateTid, function(req, res) {
|
||||
// Topics.tools.pin(req.params.tid, req.user.uid, function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// })
|
||||
// .delete(apiMiddleware.requireUser, apiMiddleware.validateTid, function(req, res) {
|
||||
// Topics.tools.unpin(req.params.tid, req.user.uid, function(err) {
|
||||
// errorHandler.handle(err, res);
|
||||
// });
|
||||
// });
|
||||
|
||||
return router;
|
||||
};
|
Loading…
Reference in New Issue