-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
tools.js
117 lines (98 loc) · 3.7 KB
/
tools.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
'use strict';
const user = require('../../user');
const topics = require('../../topics');
const events = require('../../events');
const privileges = require('../../privileges');
const plugins = require('../../plugins');
const socketHelpers = require('../helpers');
const sockets = require('..');
module.exports = function (SocketTopics) {
SocketTopics.loadTopicTools = async function (socket, data) {
if (!socket.uid) {
throw new Error('[[error:no-privileges]]');
}
if (!data) {
throw new Error('[[error:invalid-data]]');
}
const [topicData, userPrivileges] = await Promise.all([
topics.getTopicData(data.tid),
privileges.topics.get(data.tid, socket.uid),
]);
if (!topicData) {
throw new Error('[[error:no-topic]]');
}
if (!userPrivileges['topics:read']) {
throw new Error('[[error:no-privileges]]');
}
topicData.privileges = userPrivileges;
const result = await plugins.fireHook('filter:topic.thread_tools', { topic: topicData, uid: socket.uid, tools: [] });
result.topic.thread_tools = result.tools;
return result.topic;
};
SocketTopics.delete = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics/state');
await SocketTopics.doTopicAction('delete', 'event:topic_deleted', socket, data);
};
SocketTopics.restore = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/state');
await SocketTopics.doTopicAction('restore', 'event:topic_restored', socket, data);
};
SocketTopics.purge = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics');
await SocketTopics.doTopicAction('purge', 'event:topic_purged', socket, data);
};
SocketTopics.lock = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/lock');
await SocketTopics.doTopicAction('lock', 'event:topic_locked', socket, data);
};
SocketTopics.unlock = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics/lock');
await SocketTopics.doTopicAction('unlock', 'event:topic_unlocked', socket, data);
};
SocketTopics.pin = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/pin');
await SocketTopics.doTopicAction('pin', 'event:topic_pinned', socket, data);
};
SocketTopics.unpin = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics/pin');
await SocketTopics.doTopicAction('unpin', 'event:topic_unpinned', socket, data);
};
SocketTopics.doTopicAction = async function (action, event, socket, data) {
if (!socket.uid) {
throw new Error('[[error:no-privileges]]');
}
if (!data || !Array.isArray(data.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(data.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.emitToUids(event, data, notifyUids);
await logTopicAction(action, socket, tid, title);
}));
};
async function logTopicAction(action, socket, tid, title) {
var actionsToLog = ['delete', 'restore', 'purge'];
if (!actionsToLog.includes(action)) {
return;
}
await events.log({
type: 'topic-' + action,
uid: socket.uid,
ip: socket.ip,
tid: tid,
title: String(title),
});
}
SocketTopics.orderPinnedTopics = async function (socket, data) {
if (!Array.isArray(data)) {
throw new Error('[[error:invalid-data]]');
}
await topics.tools.orderPinnedTopics(socket.uid, data);
};
};