2020-10-06 13:20:50 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const posts = require('../../posts');
|
|
|
|
|
|
2020-10-17 15:07:04 -04:00
|
|
|
const api = require('../../api');
|
2020-10-06 13:20:50 -04:00
|
|
|
const helpers = require('../helpers');
|
2020-10-16 21:36:59 -04:00
|
|
|
const apiHelpers = require('../../api/helpers');
|
2020-10-06 13:20:50 -04:00
|
|
|
|
|
|
|
|
const Posts = module.exports;
|
|
|
|
|
|
2020-12-28 19:38:00 -05:00
|
|
|
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) => {
|
2020-10-17 15:07:04 -04:00
|
|
|
const editResult = await api.posts.edit(req, {
|
|
|
|
|
...req.body,
|
2020-10-06 13:20:50 -04:00
|
|
|
pid: req.params.pid,
|
2020-10-17 15:07:04 -04:00
|
|
|
uid: req.uid,
|
|
|
|
|
req: apiHelpers.buildReqObject(req),
|
2020-10-06 13:20:50 -04:00
|
|
|
});
|
|
|
|
|
|
2020-10-17 15:07:04 -04:00
|
|
|
helpers.formatApiResponse(200, res, editResult);
|
2020-10-06 13:20:50 -04:00
|
|
|
};
|
2020-10-06 14:12:02 -04:00
|
|
|
|
|
|
|
|
Posts.purge = async (req, res) => {
|
2020-10-17 21:24:33 -04:00
|
|
|
await api.posts.purge(req, { pid: req.params.pid });
|
2020-10-06 14:12:02 -04:00
|
|
|
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 });
|
2020-10-06 14:12:02 -04:00
|
|
|
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 });
|
2020-10-06 14:12:02 -04:00
|
|
|
helpers.formatApiResponse(200, res);
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-08 15:58:42 -04:00
|
|
|
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}` };
|
2020-10-08 15:58:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
};
|
2021-01-18 13:32:55 -05:00
|
|
|
|
|
|
|
|
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 }));
|
|
|
|
|
};
|
|
|
|
|
|