-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
topics.js
183 lines (152 loc) · 4.99 KB
/
topics.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
const topics = require('../../topics');
const posts = require('../../posts');
const user = require('../../user');
const meta = require('../../meta');
const events = require('../../events');
const privileges = require('../../privileges');
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);
};
Topics.delete = async (req, res) => {
await doTopicAction('delete', 'event:topic_deleted', req, {
tids: [req.params.tid],
});
helpers.formatApiResponse(200, res);
};
Topics.restore = async (req, res) => {
await doTopicAction('restore', 'event:topic_restored', req, {
tids: [req.params.tid],
});
helpers.formatApiResponse(200, res);
};
Topics.purge = async (req, res) => {
await doTopicAction('purge', 'event:topic_purged', req, {
tids: [req.params.tid],
});
helpers.formatApiResponse(200, res);
};
Topics.pin = async (req, res) => {
await doTopicAction('pin', 'event:topic_pinned', req, {
tids: [req.params.tid],
});
helpers.formatApiResponse(200, res);
};
Topics.unpin = async (req, res) => {
await doTopicAction('unpin', 'event:topic_unpinned', req, {
tids: [req.params.tid],
});
helpers.formatApiResponse(200, res);
};
Topics.lock = async (req, res) => {
await doTopicAction('lock', 'event:topic_locked', req, {
tids: [req.params.tid],
});
helpers.formatApiResponse(200, res);
};
Topics.unlock = async (req, res) => {
await doTopicAction('unlock', 'event:topic_unlocked', req, {
tids: [req.params.tid],
});
helpers.formatApiResponse(200, res);
};
Topics.follow = async (req, res) => {
await topics.follow(req.params.tid, req.user.uid);
helpers.formatApiResponse(200, res);
};
Topics.ignore = async (req, res) => {
await topics.ignore(req.params.tid, req.user.uid);
helpers.formatApiResponse(200, res);
};
Topics.unfollow = async (req, res) => {
await topics.unfollow(req.params.tid, req.user.uid);
helpers.formatApiResponse(200, res);
};
Topics.addTags = async (req, res) => {
await topics.createTags(req.body.tags, req.params.tid, Date.now());
helpers.formatApiResponse(200, res);
};
Topics.deleteTags = async (req, res) => {
await topics.deleteTopicTags(req.params.tid);
helpers.formatApiResponse(200, res);
};
async function doTopicAction(action, event, socket, { tids }) {
if (!Array.isArray(tids)) {
throw new Error('[[error:invalid-tid]]');
}
if (typeof topics.tools[action] !== 'function') {
return;
}
const uids = await user.getUidsFromSet('users:online', 0, -1);
await Promise.all(tids.map(async function (tid) {
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);
await logTopicAction(action, socket, tid, title);
}));
}
async function logTopicAction(action, req, tid, title) {
var actionsToLog = ['delete', 'restore', 'purge'];
if (!actionsToLog.includes(action)) {
return;
}
await events.log({
type: 'topic-' + action,
uid: req.uid,
ip: req.ip,
tid: tid,
title: String(title),
});
}