-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlibrary.js
More file actions
143 lines (111 loc) · 3.24 KB
/
library.js
File metadata and controls
143 lines (111 loc) · 3.24 KB
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
"use strict";
var plugin = {},
winston = module.parent.require('winston'),
async = module.parent.require('async'),
user = module.parent.require('./user'),
groups = module.parent.require('./groups'),
meta = module.parent.require('./meta'),
superusers;
plugin.init = function(params, callback) {
try {
superusers = require('../nodebb-plugin-superusers/library');
} catch (e) {
winston.warn('Please install and activate the SuperUsers plugin in order for this to work.');
return callback();
}
var SocketPlugins = module.parent.require('./socket.io/plugins');
SocketPlugins.superuser = SocketPlugins.superuser || {};
SocketPlugins.superuser.squash = squash;
SocketPlugins.superuser.unsquash = unsquash;
callback();
};
plugin.getUsersTopics = function(data, callback) {
var uids = [],
topics = data.topics;
topics.forEach(function(el) {
if (uids.indexOf(el.uid) === -1) {
uids.push(el.uid);
}
});
user.getMultipleUserFields(uids, ['squashed'], function(err, usersData) {
var users = {},
filteredTopics = [];
uids.forEach(function(uid, idx) {
users[uid] = usersData[idx].squashed;
});
topics.forEach(function(topic, idx) {
if (parseInt(data.uid, 10) === parseInt(topic.uid, 10) || parseInt(topic.deleted, 10) === 1) {
filteredTopics.push(topic);
} else {
var squashed = users[topic.uid] ? parseInt(users[topic.uid], 10) : 0;
if (squashed === 0) {
filteredTopics.push(topic);
}
}
});
data.topics = filteredTopics;
callback(err, data);
});
};
plugin.getUsersPosts = function(data, callback) {
var uids = [],
posts = data.posts;
posts.forEach(function(el) {
if (el && el.uid && uids.indexOf(el.uid) === -1) {
uids.push(el.uid);
}
});
user.getMultipleUserFields(uids, ['squashed'], function(err, usersData) {
var users = {},
filteredPosts = [];
uids.forEach(function(uid, idx) {
users[uid] = usersData[idx].squashed;
});
posts.forEach(function(post, idx) {
if (post && post.uid) {
if (parseInt(data.uid, 10) === parseInt(post.uid, 10) || parseInt(post.deleted, 10) === 1) {
filteredPosts.push(post);
} else {
var squashed = users[post.uid] ? parseInt(users[post.uid], 10) : 0;
if (squashed === 0) {
filteredPosts.push(post);
}
}
}
});
data.posts = filteredPosts;
callback(err, data);
});
};
plugin.modifyUids = function(data, callback) {
user.getUserField(data.uidFrom, 'squashed', function(err, squashed) {
var squashed = squashed ? parseInt(squashed, 10) : 0;
if (squashed) {
data.uidsTo = [];
}
callback(err, data);
});
};
function squash(socket, data, callback) {
var uid = socket.uid ? socket.uid : 0;
isSuperUser(uid, function(err, isSuperUser) {
if (!isSuperUser) {
return callback(new Error('Not Allowed'));
}
user.setUserField(data.uid, 'squashed', 1, callback);
});
}
function unsquash(socket, data, callback) {
var uid = socket.uid ? socket.uid : 0;
isSuperUser(uid, function(err, isSuperUser) {
if (!isSuperUser) {
return callback(new Error('Not Allowed'));
}
user.setUserField(data.uid, 'squashed', 0, callback);
});
}
function isSuperUser(uid, callback) {
var group = meta.config['superuser:groupname'] || '';
groups.isMember(uid, group, callback);
}
module.exports = plugin;