-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
posts.js
233 lines (194 loc) · 6.95 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
const validator = require('validator');
const _ = require('lodash');
const meta = require('../../meta');
const privileges = require('../../privileges');
const groups = require('../../groups');
const posts = require('../../posts');
const topics = require('../../topics');
const events = require('../../events');
const utils = require('../../utils');
const helpers = require('../helpers');
const sockets = require('../../socket.io');
const socketPostHelpers = require('../../socket.io/posts/helpers'); // eehhh...
const socketTopics = require('../../socket.io/topics'); // eehhh...
const Posts = module.exports;
Posts.edit = async (req, res) => {
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
var contentLen = utils.stripHTMLTags(req.body.content).trim().length;
if (req.body.title && req.body.title.length < meta.config.minimumTitleLength) {
throw new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]');
} else if (req.body.title && req.body.title.length > meta.config.maximumTitleLength) {
throw new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]');
} else if (meta.config.minimumPostLength !== 0 && contentLen < meta.config.minimumPostLength) {
throw new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]');
} else if (contentLen > meta.config.maximumPostLength) {
throw new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]');
}
// Payload construction
var payload = {
req,
uid: req.user.uid,
pid: req.params.pid,
content: req.body.content,
options: {},
};
['handle', 'title'].forEach((prop) => {
if (req.body.hasOwnProperty(prop)) {
payload[prop] = req.body[prop];
}
});
['topic_thumb', 'tags'].forEach((prop) => {
if (req.body.hasOwnProperty(prop)) {
payload.options[prop] = req.body[prop];
}
});
const editResult = await posts.edit(payload);
helpers.formatApiResponse(200, res, await posts.getPostSummaryByPids([editResult.pid], req.user.uid, {}));
if (editResult.topic.renamed) {
await events.log({
type: 'topic-rename',
uid: req.user.uid,
ip: req.ip,
tid: editResult.topic.tid,
oldTitle: validator.escape(String(editResult.topic.oldTitle)),
newTitle: validator.escape(String(editResult.topic.title)),
});
}
if (!editResult.post.deleted) {
sockets.in('topic_' + editResult.topic.tid).emit('event:post_edited', editResult);
}
const memberData = await groups.getMembersOfGroups([
'administrators',
'Global Moderators',
'cid:' + editResult.topic.cid + ':privileges:moderate',
'cid:' + editResult.topic.cid + ':privileges:groups:moderate',
]);
const uids = _.uniq(_.flatten(memberData).concat(req.user.uid.toString()));
uids.forEach(uid => sockets.in('uid_' + uid).emit('event:post_edited', editResult));
};
Posts.purge = async (req, res) => {
const results = await isMainAndLastPost(req.params.pid);
if (results.isMain && !results.isLast) {
throw new Error('[[error:cant-purge-main-post]]');
}
const isMainAndLast = results.isMain && results.isLast;
const postData = await posts.getPostFields(req.params.pid, ['pid', 'toPid', 'tid']);
const canPurge = await privileges.posts.canPurge(req.params.pid, req.user.uid);
if (!canPurge) {
throw new Error('[[error:no-privileges]]');
}
require('../../posts/cache').del(req.params.pid);
await posts.purge(req.params.pid, req.user.uid);
helpers.formatApiResponse(200, res);
sockets.in('topic_' + postData.tid).emit('event:post_purged', postData);
const topicData = await topics.getTopicFields(postData.tid, ['title', 'cid']);
await events.log({
type: 'post-purge',
uid: req.user.uid,
pid: req.params.pid,
ip: req.ip,
tid: postData.tid,
title: String(topicData.title),
});
if (isMainAndLast) {
await socketTopics.doTopicAction('purge', 'event:topic_purged', {
ip: req.ip,
uid: req.user.uid,
}, { tids: [postData.tid], cid: topicData.cid });
}
};
Posts.restore = async (req, res) => {
await deleteOrRestore(req, {
pid: req.params.pid,
}, {
command: 'restore',
event: 'event:post_restored',
type: 'post-restore',
});
helpers.formatApiResponse(200, res);
};
Posts.delete = async (req, res) => {
await deleteOrRestore(req, {
pid: req.params.pid,
}, {
command: 'delete',
event: 'event:post_deleted',
type: 'post-delete',
});
helpers.formatApiResponse(200, res);
};
async function mock(req) {
const tid = await posts.getPostField(req.params.pid, 'tid');
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
const socketMock = { uid: req.user.uid };
return { data, socketMock };
}
Posts.vote = async (req, res) => {
const { data, socketMock } = await mock(req);
if (req.body.delta > 0) {
await socketPostHelpers.postCommand(socketMock, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
} else if (req.body.delta < 0) {
await socketPostHelpers.postCommand(socketMock, 'downvote', 'voted', '', data);
} else {
await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data);
}
helpers.formatApiResponse(200, res);
};
Posts.unvote = async (req, res) => {
const { data, socketMock } = await mock(req);
await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data);
helpers.formatApiResponse(200, res);
};
Posts.bookmark = async (req, res) => {
const { data, socketMock } = await mock(req);
await socketPostHelpers.postCommand(socketMock, 'bookmark', 'bookmarked', '', data);
helpers.formatApiResponse(200, res);
};
Posts.unbookmark = async (req, res) => {
const { data, socketMock } = await mock(req);
await socketPostHelpers.postCommand(socketMock, 'unbookmark', 'bookmarked', '', data);
helpers.formatApiResponse(200, res);
};
async function isMainAndLastPost(pid) {
const [isMain, topicData] = await Promise.all([
posts.isMain(pid),
posts.getTopicFields(pid, ['postcount']),
]);
return {
isMain: isMain,
isLast: topicData && topicData.postcount === 1,
};
}
async function deleteOrRestoreTopicOf(command, pid, req) {
const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
if (command === 'delete' && !topic.deleted) {
await socketTopics.doTopicAction('delete', 'event:topic_deleted', {
uid: req.user.uid,
ip: req.ip,
}, { tids: [topic.tid], cid: topic.cid });
} else if (command === 'restore' && topic.deleted) {
await socketTopics.doTopicAction('restore', 'event:topic_restored', {
uid: req.user.uid,
ip: req.ip,
}, { tids: [topic.tid], cid: topic.cid });
}
}
async function deleteOrRestore(req, data, params) {
if (!data || !data.pid) {
throw new Error('[[error:invalid-data]]');
}
const postData = await posts.tools[params.command](req.user.uid, data.pid);
const results = await isMainAndLastPost(data.pid);
if (results.isMain && results.isLast) {
await deleteOrRestoreTopicOf(params.command, data.pid, req);
}
sockets.in('topic_' + postData.tid).emit(params.event, postData);
await events.log({
type: params.type,
uid: req.user.uid,
pid: data.pid,
tid: postData.tid,
ip: req.ip,
});
}