-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
helpers.js
133 lines (116 loc) · 3.6 KB
/
helpers.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
'use strict';
const url = require('url');
const user = require('../user');
const topics = require('../topics');
const posts = require('../posts');
const privileges = require('../privileges');
const plugins = require('../plugins');
const socketHelpers = require('../socket.io/helpers');
const websockets = require('../socket.io');
const events = require('../events');
// creates a slimmed down version of the request object
exports.buildReqObject = (req, payload) => {
req = req || {};
const headers = req.headers || {};
const encrypted = req.connection ? !!req.connection.encrypted : false;
let host = headers.host;
const referer = headers.referer || '';
if (!host) {
host = url.parse(referer).host || '';
}
return {
uid: req.uid,
params: req.params,
method: req.method,
body: payload || req.body,
ip: req.ip,
host: host,
protocol: encrypted ? 'https' : 'http',
secure: encrypted,
url: referer,
path: referer.substr(referer.indexOf(host) + host.length),
headers: headers,
};
};
exports.doTopicAction = async function (action, event, caller, { tids }) {
if (!Array.isArray(tids)) {
throw new Error('[[error:invalid-tid]]');
}
const exists = (await Promise.all(tids.map(async tid => await topics.exists(tid)))).every(Boolean);
if (!exists) {
throw new Error('[[error:no-topic]]');
}
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, caller.uid);
const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids);
socketHelpers.emitToUids(event, data, notifyUids);
await logTopicAction(action, caller, 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),
});
}
exports.postCommand = async function (caller, command, eventName, notification, data) {
if (!caller.uid) {
throw new Error('[[error:not-logged-in]]');
}
if (!data || !data.pid) {
throw new Error('[[error:invalid-data]]');
}
if (!data.room_id) {
throw new Error('[[error:invalid-room-id, ' + data.room_id + ' ]]');
}
const [exists, deleted] = await Promise.all([
posts.exists(data.pid),
posts.getPostField(data.pid, 'deleted'),
]);
if (!exists) {
throw new Error('[[error:invalid-pid]]');
}
if (deleted) {
throw new Error('[[error:post-deleted]]');
}
/*
hooks:
filter:post.upvote
filter:post.downvote
filter:post.unvote
filter:post.bookmark
filter:post.unbookmark
*/
const filteredData = await plugins.fireHook('filter:post.' + command, {
data: data,
uid: caller.uid,
});
return await executeCommand(caller, command, eventName, notification, filteredData.data);
};
async function executeCommand(caller, command, eventName, notification, data) {
const result = await posts[command](data.pid, caller.uid);
if (result && eventName) {
websockets.in('uid_' + caller.uid).emit('posts.' + command, result);
websockets.in(data.room_id).emit('event:' + eventName, result);
}
if (result && command === 'upvote') {
socketHelpers.upvote(result, notification);
} else if (result && notification) {
socketHelpers.sendNotificationToPostOwner(data.pid, caller.uid, command, notification);
} else if (result && command === 'unvote') {
socketHelpers.rescindUpvoteNotification(data.pid, caller.uid);
}
return result;
}