Files
NodeBB/src/controllers/write/posts.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-10-06 13:20:50 -04:00
'use strict';
const posts = require('../../posts');
const api = require('../../api');
2020-10-06 13:20:50 -04:00
const helpers = require('../helpers');
const apiHelpers = require('../../api/helpers');
2020-10-06 13:20:50 -04:00
const Posts = module.exports;
Posts.get = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.get(req, { pid: req.params.pid }));
};
2020-10-06 13:20:50 -04:00
Posts.edit = async (req, res) => {
const editResult = await api.posts.edit(req, {
...req.body,
2020-10-06 13:20:50 -04:00
pid: req.params.pid,
uid: req.uid,
req: apiHelpers.buildReqObject(req),
2020-10-06 13:20:50 -04:00
});
helpers.formatApiResponse(200, res, editResult);
2020-10-06 13:20:50 -04:00
};
Posts.purge = async (req, res) => {
2020-10-17 21:24:33 -04:00
await api.posts.purge(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
Posts.restore = async (req, res) => {
2020-10-17 21:24:33 -04:00
await api.posts.restore(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
Posts.delete = async (req, res) => {
2020-10-17 21:24:33 -04:00
await api.posts.delete(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
async function mock(req) {
2020-10-06 14:47:03 -04:00
const tid = await posts.getPostField(req.params.pid, 'tid');
2020-10-17 22:59:12 -04:00
return { pid: req.params.pid, room_id: `topic_${tid}` };
}
Posts.vote = async (req, res) => {
2020-10-17 22:59:12 -04:00
const data = await mock(req);
2020-10-06 14:47:03 -04:00
if (req.body.delta > 0) {
2020-10-17 22:59:12 -04:00
await api.posts.upvote(req, data);
2020-10-06 14:47:03 -04:00
} else if (req.body.delta < 0) {
2020-10-17 22:59:12 -04:00
await api.posts.downvote(req, data);
2020-10-06 14:47:03 -04:00
} else {
2020-10-17 22:59:12 -04:00
await api.posts.unvote(req, data);
2020-10-06 14:47:03 -04:00
}
helpers.formatApiResponse(200, res);
};
Posts.unvote = async (req, res) => {
2020-10-17 22:59:12 -04:00
const data = await mock(req);
await api.posts.unvote(req, data);
2020-10-06 14:47:03 -04:00
helpers.formatApiResponse(200, res);
};
2020-10-07 10:19:27 -04:00
Posts.bookmark = async (req, res) => {
2020-10-17 22:59:12 -04:00
const data = await mock(req);
await api.posts.bookmark(req, data);
2020-10-07 10:19:27 -04:00
helpers.formatApiResponse(200, res);
};
Posts.unbookmark = async (req, res) => {
2020-10-17 22:59:12 -04:00
const data = await mock(req);
await api.posts.unbookmark(req, data);
2020-10-07 10:19:27 -04:00
helpers.formatApiResponse(200, res);
};
Posts.getDiffs = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.getDiffs(req, { ...req.params }));
};
Posts.loadDiff = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.loadDiff(req, { ...req.params }));
};
Posts.restoreDiff = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.restoreDiff(req, { ...req.params }));
};