-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
posts.js
71 lines (57 loc) · 1.7 KB
/
posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
const posts = require('../../posts');
const api = require('../../api');
const helpers = require('../helpers');
const apiHelpers = require('../../api/helpers');
const Posts = module.exports;
Posts.edit = async (req, res) => {
const editResult = await api.posts.edit(req, {
...req.body,
pid: req.params.pid,
uid: req.uid,
req: apiHelpers.buildReqObject(req),
});
helpers.formatApiResponse(200, res, editResult);
};
Posts.purge = async (req, res) => {
await api.posts.purge(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
Posts.restore = async (req, res) => {
await api.posts.restore(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
Posts.delete = async (req, res) => {
await api.posts.delete(req, { pid: req.params.pid });
helpers.formatApiResponse(200, res);
};
async function mock(req) {
const tid = await posts.getPostField(req.params.pid, 'tid');
return { pid: req.params.pid, room_id: `topic_${tid}` };
}
Posts.vote = async (req, res) => {
const data = await mock(req);
if (req.body.delta > 0) {
await api.posts.upvote(req, data);
} else if (req.body.delta < 0) {
await api.posts.downvote(req, data);
} else {
await api.posts.unvote(req, data);
}
helpers.formatApiResponse(200, res);
};
Posts.unvote = async (req, res) => {
const data = await mock(req);
await api.posts.unvote(req, data);
helpers.formatApiResponse(200, res);
};
Posts.bookmark = async (req, res) => {
const data = await mock(req);
await api.posts.bookmark(req, data);
helpers.formatApiResponse(200, res);
};
Posts.unbookmark = async (req, res) => {
const data = await mock(req);
await api.posts.unbookmark(req, data);
helpers.formatApiResponse(200, res);
};