-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
queue.js
205 lines (185 loc) · 5.62 KB
/
queue.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
'use strict';
const _ = require('lodash');
const db = require('../database');
const user = require('../user');
const meta = require('../meta');
const groups = require('../groups');
const topics = require('../topics');
const categories = require('../categories');
const notifications = require('../notifications');
const privileges = require('../privileges');
const plugins = require('../plugins');
const socketHelpers = require('../socket.io/helpers');
module.exports = function (Posts) {
Posts.shouldQueue = async function (uid, data) {
const userData = await user.getUserFields(uid, ['uid', 'reputation', 'postcount']);
const isMemberOfExempt = await groups.isMemberOfAny(userData.uid, meta.config.groupsExemptFromPostQueue);
const shouldQueue = meta.config.postQueue && !isMemberOfExempt && (!userData.uid || userData.reputation < meta.config.postQueueReputationThreshold || userData.postcount <= 0);
const result = await plugins.fireHook('filter:post.shouldQueue', {
shouldQueue: !!shouldQueue,
uid: uid,
data: data,
});
return result.shouldQueue;
};
async function removeQueueNotification(id) {
await notifications.rescind('post-queue-' + id);
const data = await getParsedObject(id);
if (!data) {
return;
}
const cid = await getCid(data.type, data);
const uids = await getNotificationUids(cid);
uids.forEach(uid => user.notifications.pushCount(uid));
}
async function getNotificationUids(cid) {
const results = await Promise.all([
groups.getMembersOfGroups(['administrators', 'Global Moderators']),
categories.getModeratorUids([cid]),
]);
return _.uniq(_.flattenDeep(results));
}
Posts.addToQueue = async function (data) {
const type = data.title ? 'topic' : 'reply';
const now = Date.now();
const id = type + '-' + now;
await canPost(type, data);
await db.sortedSetAdd('post:queue', now, id);
await db.setObject('post:queue:' + id, {
id: id,
uid: data.uid,
type: type,
data: JSON.stringify(data),
});
await user.setUserField(data.uid, 'lastqueuetime', now);
const cid = await getCid(type, data);
const uids = await getNotificationUids(cid);
const notifObj = await notifications.create({
type: 'post-queue',
nid: 'post-queue-' + id,
mergeId: 'post-queue',
bodyShort: '[[notifications:post_awaiting_review]]',
bodyLong: data.content,
path: '/post-queue',
});
await notifications.push(notifObj, uids);
return {
id: id,
type: type,
queued: true,
message: '[[success:post-queued]]',
};
};
async function getCid(type, data) {
if (type === 'topic') {
return data.cid;
} else if (type === 'reply') {
return await topics.getTopicField(data.tid, 'cid');
}
return null;
}
async function canPost(type, data) {
const cid = await getCid(type, data);
const typeToPrivilege = {
topic: 'topics:create',
reply: 'topics:reply',
};
topics.checkContent(data.content);
if (type === 'topic') {
topics.checkTitle(data.title);
if (data.tags) {
await topics.validateTags(data.tags);
}
}
const [canPost] = await Promise.all([
privileges.categories.can(typeToPrivilege[type], cid, data.uid),
user.isReadyToQueue(data.uid, cid),
]);
if (!canPost) {
throw new Error('[[error:no-privileges]]');
}
}
Posts.removeFromQueue = async function (id) {
await removeQueueNotification(id);
await db.sortedSetRemove('post:queue', id);
await db.delete('post:queue:' + id);
};
Posts.submitFromQueue = async function (id) {
const data = await getParsedObject(id);
if (!data) {
return;
}
if (data.type === 'topic') {
await createTopic(data.data);
} else if (data.type === 'reply') {
await createReply(data.data);
}
await Posts.removeFromQueue(id);
};
async function getParsedObject(id) {
const data = await db.getObject('post:queue:' + id);
if (!data) {
return null;
}
data.data = JSON.parse(data.data);
data.data.fromQueue = true;
return data;
}
async function createTopic(data) {
const result = await topics.post(data);
socketHelpers.notifyNew(data.uid, 'newTopic', { posts: [result.postData], topic: result.topicData });
}
async function createReply(data) {
const postData = await topics.reply(data);
const result = {
posts: [postData],
'reputation:disabled': !!meta.config['reputation:disabled'],
'downvote:disabled': !!meta.config['downvote:disabled'],
};
socketHelpers.notifyNew(data.uid, 'newPost', result);
}
Posts.editQueuedContent = async function (uid, editData) {
const canEditQueue = await Posts.canEditQueue(uid, editData);
if (!canEditQueue) {
throw new Error('[[error:no-privileges]]');
}
const data = await getParsedObject(editData.id);
if (!data) {
return;
}
if (editData.content !== undefined) {
data.data.content = editData.content;
}
if (editData.title !== undefined) {
data.data.title = editData.title;
}
if (editData.cid !== undefined) {
data.data.cid = editData.cid;
}
await db.setObjectField('post:queue:' + editData.id, 'data', JSON.stringify(data.data));
};
Posts.canEditQueue = async function (uid, editData) {
const [isAdminOrGlobalMod, data] = await Promise.all([
user.isAdminOrGlobalMod(uid),
getParsedObject(editData.id),
]);
if (!data) {
return false;
}
if (isAdminOrGlobalMod) {
return true;
}
let cid;
if (data.type === 'topic') {
cid = data.data.cid;
} else if (data.type === 'reply') {
cid = await topics.getTopicField(data.data.tid, 'cid');
}
const isModerator = await user.isModerator(uid, cid);
let isModeratorOfTargetCid = true;
if (editData.cid) {
isModeratorOfTargetCid = await user.isModerator(uid, editData.cid);
}
return isModerator && isModeratorOfTargetCid;
};
};