nodebb-plugin-poll 1.1.7 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/sockets.js CHANGED
@@ -1,21 +1,20 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- var async = require('async'),
3
+ const async = require('async');
4
4
 
5
- NodeBB = require('./nodebb'),
5
+ const NodeBB = require('./nodebb');
6
6
 
7
- Config = require('./config'),
8
- Poll = require('./poll'),
9
- Vote = require('./vote');
7
+ const Config = require('./config');
8
+ const Poll = require('./poll');
9
+ const Vote = require('./vote');
10
10
 
11
- (function(Sockets) {
12
-
13
- Sockets.getConfig = function(socket, data, callback) {
11
+ (function (Sockets) {
12
+ Sockets.getConfig = function (socket, data, callback) {
14
13
  callback(null, Config.settings.get());
15
14
  };
16
15
 
17
- Sockets.get = function(socket, data, callback) {
18
- var allowAnon = Config.settings.get('toggles.allowAnon');
16
+ Sockets.get = function (socket, data, callback) {
17
+ const allowAnon = Config.settings.get('toggles.allowAnon');
19
18
 
20
19
  if (!data) {
21
20
  return callback(new Error('Invalid request, request data is not defined'));
@@ -27,8 +26,11 @@ var async = require('async'),
27
26
  return callback(new Error('Invalid request, anonymous access is not allowed'));
28
27
  }
29
28
 
30
- var pollId = parseInt(data.pollId, 10);
31
- Poll.get(pollId, socket.uid, !!socket.uid, function(err, pollData) {
29
+ const pollId = parseInt(data.pollId, 10);
30
+ Poll.get(pollId, socket.uid, !!socket.uid, (err, pollData) => {
31
+ if (err) {
32
+ console.error(err);
33
+ }
32
34
  if (!pollData.info.version) {
33
35
  return callback(new Error('Legacy polls are not supported'));
34
36
  }
@@ -39,7 +41,7 @@ var async = require('async'),
39
41
  });
40
42
  };
41
43
 
42
- Sockets.vote = function(socket, data, callback) {
44
+ Sockets.vote = function (socket, data, callback) {
43
45
  if (!socket.uid) {
44
46
  return callback(new Error('You need to be logged in to vote'));
45
47
  }
@@ -50,36 +52,38 @@ var async = require('async'),
50
52
  data.uid = socket.uid;
51
53
 
52
54
  async.parallel({
53
- canVote: function(next) {
55
+ canVote: function (next) {
54
56
  Vote.canVote(data.uid, data.pollId, next);
55
57
  },
56
- optionsFilter: function(next) {
58
+ optionsFilter: function (next) {
57
59
  Poll.hasOptions(data.pollId, data.options, next);
58
60
  },
59
- settings: function(next) {
61
+ settings: function (next) {
60
62
  Poll.getSettings(data.pollId, next);
61
- }
62
- }, function(err, result) {
63
+ },
64
+ }, (err, result) => {
63
65
  // Filter the options on their existence
64
- data.options = data.options.filter(function(el, index) {
65
- return result.optionsFilter[index];
66
- });
66
+ data.options = data.options.filter((el, index) => result.optionsFilter[index]);
67
67
 
68
68
  // Give an error if there are too many votes
69
69
  if (data.options.length > parseInt(result.settings.maxvotes, 10)) {
70
- return callback(new Error('You can only vote for ' + result.settings.maxvotes + ' options on this poll.'));
70
+ return callback(new Error(`You can only vote for ${result.settings.maxvotes} options on this poll.`));
71
71
  }
72
72
 
73
73
  if (err || !result.canVote || !data.options.length) {
74
74
  return callback(new Error('Already voted or invalid option'));
75
75
  }
76
76
 
77
- Vote.add(data, function(err) {
77
+ Vote.add(data, (err) => {
78
78
  if (err) {
79
79
  return callback(new Error('Error during voting'));
80
80
  }
81
81
 
82
- Poll.get(data.pollId, socket.uid, false, function(err, pollData) {
82
+ Poll.get(data.pollId, socket.uid, false, (err, pollData) => {
83
+ if (err) {
84
+ console.error(err);
85
+ }
86
+
83
87
  NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', pollData);
84
88
 
85
89
  callback();
@@ -88,7 +92,7 @@ var async = require('async'),
88
92
  });
89
93
  };
90
94
 
91
- Sockets.updateVote = function(socket, data, callback) {
95
+ Sockets.updateVote = function (socket, data, callback) {
92
96
  if (!socket.uid) {
93
97
  return callback(new Error('You need to be logged in to make changes'));
94
98
  }
@@ -98,39 +102,43 @@ var async = require('async'),
98
102
  data.uid = socket.uid;
99
103
 
100
104
  async.parallel({
101
- canUpdateVote: function(next) {
105
+ canUpdateVote: function (next) {
102
106
  Vote.canUpdateVote(data.uid, data.pollId, next);
103
107
  },
104
- optionsFilter: function(next) {
108
+ optionsFilter: function (next) {
105
109
  Poll.hasOptions(data.pollId, data.options, next);
106
110
  },
107
- settings: function(next) {
111
+ settings: function (next) {
108
112
  Poll.getSettings(data.pollId, next);
109
- }
110
- }, function(err, result) {
113
+ },
114
+ }, (err, result) => {
111
115
  // Filter the options on their existence
112
- data.options = data.options.filter(function(el, index) {
113
- return result.optionsFilter[index];
114
- });
116
+ data.options = data.options.filter((el, index) => result.optionsFilter[index]);
115
117
 
116
118
  // Give an error if there are too many votes
117
119
  if (data.options.length > parseInt(result.settings.maxvotes, 10)) {
118
- return callback(new Error('You can only vote for ' + result.settings.maxvotes + ' options on this poll.'));
120
+ return callback(new Error(`You can only vote for ${result.settings.maxvotes} options on this poll.`));
119
121
  }
120
122
  if (!result.canUpdateVote) {
121
123
  return callback(new Error('Can\'t update vote'));
122
124
  }
123
125
  if (err || !data.options.length) {
124
- err && console.error(err);
126
+ if (err) {
127
+ console.error(err);
128
+ }
125
129
  return callback(new Error('Invalid option'));
126
130
  }
127
131
 
128
- Vote.update(data, function(err) {
132
+ Vote.update(data, (err) => {
129
133
  if (err) {
130
134
  return callback(new Error('Error during updating vote'));
131
135
  }
132
136
 
133
- Poll.get(data.pollId, socket.uid, false, function(err, pollData) {
137
+ Poll.get(data.pollId, socket.uid, false, (err, pollData) => {
138
+ if (err) {
139
+ console.error(err);
140
+ }
141
+
134
142
  NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', pollData);
135
143
 
136
144
  callback();
@@ -139,7 +147,7 @@ var async = require('async'),
139
147
  });
140
148
  };
141
149
 
142
- Sockets.removeVote = function(socket, data, callback) {
150
+ Sockets.removeVote = function (socket, data, callback) {
143
151
  if (!socket.uid) {
144
152
  return callback(new Error('You need to be logged in to make changes'));
145
153
  }
@@ -149,10 +157,10 @@ var async = require('async'),
149
157
  data.uid = socket.uid;
150
158
 
151
159
  async.parallel({
152
- canUpdateVote: function(next) {
160
+ canUpdateVote: function (next) {
153
161
  Vote.canUpdateVote(data.uid, data.pollId, next);
154
- }
155
- }, function(err, result) {
162
+ },
163
+ }, (err, result) => {
156
164
  if (!result.canUpdateVote) {
157
165
  return callback(new Error('Can\'t remove vote'));
158
166
  }
@@ -160,11 +168,15 @@ var async = require('async'),
160
168
  console.error(err);
161
169
  return callback(new Error('Something went wrong'));
162
170
  }
163
- Vote.remove(data, function(err) {
171
+ Vote.remove(data, (err) => {
164
172
  if (err) {
165
173
  return callback(new Error('Error during removing vote'));
166
174
  }
167
- Poll.get(data.pollId, socket.uid, false, function(err, pollData) {
175
+ Poll.get(data.pollId, socket.uid, false, (err, pollData) => {
176
+ if (err) {
177
+ console.error(err);
178
+ }
179
+
168
180
  NodeBB.SocketIndex.server.sockets.emit('event:poll.voteChange', pollData);
169
181
  callback();
170
182
  });
@@ -172,12 +184,12 @@ var async = require('async'),
172
184
  });
173
185
  };
174
186
 
175
- Sockets.getOptionDetails = function(socket, data, callback) {
187
+ Sockets.getOptionDetails = function (socket, data, callback) {
176
188
  if (!socket.uid || !data || isNaN(parseInt(data.pollId, 10)) || isNaN(parseInt(data.optionId, 10))) {
177
189
  return callback(new Error('Invalid request'));
178
190
  }
179
191
 
180
- Poll.getOption(data.pollId, data.optionId, true, function(err, result) {
192
+ Poll.getOption(data.pollId, data.optionId, true, (err, result) => {
181
193
  if (err) {
182
194
  return callback(new Error('Something went wrong!'));
183
195
  }
@@ -186,14 +198,17 @@ var async = require('async'),
186
198
  return callback(null, result);
187
199
  }
188
200
 
189
- NodeBB.User.getUsersFields(result.votes, ['uid', 'username', 'userslug', 'picture'], function(err, userData) {
201
+ NodeBB.User.getUsersFields(result.votes, ['uid', 'username', 'userslug', 'picture'], (err, userData) => {
202
+ if (err) {
203
+ console.error(err);
204
+ }
190
205
  result.votes = userData;
191
206
  callback(null, result);
192
- })
207
+ });
193
208
  });
194
209
  };
195
210
 
196
- Sockets.canCreate = function(socket, data, callback) {
211
+ Sockets.canCreate = function (socket, data, callback) {
197
212
  if (!socket.uid || !data || (isNaN(parseInt(data.cid, 10)) && isNaN(parseInt(data.pid, 10)))) {
198
213
  return callback(new Error('Invalid request'));
199
214
  }
@@ -204,8 +219,7 @@ var async = require('async'),
204
219
  NodeBB.Posts.getPostField(data.pid, 'tid', next);
205
220
  },
206
221
  function (tid, next) {
207
-
208
- NodeBB.Topics.getTopicField(tid, 'cid', function(err, cid) {
222
+ NodeBB.Topics.getTopicField(tid, 'cid', (err, cid) => {
209
223
  if (err) {
210
224
  console.error(err);
211
225
  return callback(new Error('Invalid request'));
@@ -213,24 +227,21 @@ var async = require('async'),
213
227
  next(null, cid);
214
228
  });
215
229
  },
216
- function (cid, next) {
230
+ function (cid) {
217
231
  checkPrivs(cid, socket.uid, callback);
218
- }
232
+ },
219
233
  ]);
220
- }
221
- else {
234
+ } else {
222
235
  checkPrivs(data.cid, socket.uid, callback);
223
236
  }
224
237
  };
225
238
 
226
239
  function checkPrivs(cid, socketUid, callback) {
227
- NodeBB.Privileges.categories.can('poll:create', cid, socketUid, function (err, can) {
240
+ NodeBB.Privileges.categories.can('poll:create', cid, socketUid, (err, can) => {
228
241
  if (err || !can) {
229
242
  return callback(new Error('[[poll:error.privilege.create]]'), can);
230
- } else {
231
- return callback(null, can);
232
243
  }
244
+ return callback(null, can);
233
245
  });
234
246
  }
235
-
236
- })(exports);
247
+ }(exports));
package/lib/vote.js CHANGED
@@ -1,28 +1,27 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- var async = require('async'),
4
- NodeBB = require('./nodebb'),
5
- Poll = require('./poll');
3
+ const async = require('async');
4
+ const NodeBB = require('./nodebb');
5
+ const Poll = require('./poll');
6
6
 
7
- (function(Vote) {
8
-
9
- Vote.add = function(voteData, callback) {
10
- var pollId = voteData.pollId,
11
- options = voteData.options,
12
- uid = voteData.uid,
13
- timestamp = +new Date;
7
+ (function (Vote) {
8
+ Vote.add = function (voteData, callback) {
9
+ const { pollId } = voteData;
10
+ const { options } = voteData;
11
+ const { uid } = voteData;
12
+ const timestamp = +new Date();
14
13
 
15
14
  async.parallel([
16
- function(next) {
17
- async.each(options, function(option, next) {
15
+ function (next) {
16
+ async.each(options, (option, next) => {
18
17
  // Add uid to list of votes
19
- NodeBB.db.sortedSetAdd('poll:' + pollId + ':options:' + option + ':votes', timestamp, uid, next);
18
+ NodeBB.db.sortedSetAdd(`poll:${pollId}:options:${option}:votes`, timestamp, uid, next);
20
19
  }, next);
21
20
  },
22
- function(next) {
23
- NodeBB.db.sortedSetAdd('poll:' + pollId + ':voters', timestamp, uid, next);
24
- }
25
- ], function(err) {
21
+ function (next) {
22
+ NodeBB.db.sortedSetAdd(`poll:${pollId}:voters`, timestamp, uid, next);
23
+ },
24
+ ], (err) => {
26
25
  if (err) {
27
26
  return callback(err, false);
28
27
  }
@@ -31,7 +30,7 @@ var async = require('async'),
31
30
  });
32
31
  };
33
32
 
34
- Vote.remove = function(voteData, callback) {
33
+ Vote.remove = function (voteData, callback) {
35
34
  return Vote.removeUidVote(voteData.uid, voteData.pollId, callback);
36
35
  };
37
36
 
@@ -39,18 +38,22 @@ var async = require('async'),
39
38
  return Vote.getUidVote(voteData.uid, voteData.pollId, callback);
40
39
  };
41
40
 
42
- Vote.removeUidVote = function(uid, pollId, callback) {
43
- Vote.getUidVote(uid, pollId, function (err, vote) {
41
+ Vote.removeUidVote = function (uid, pollId, callback) {
42
+ Vote.getUidVote(uid, pollId, (err, vote) => {
43
+ if (err) {
44
+ console.error(err);
45
+ }
46
+
44
47
  async.parallel([
45
- function(next) {
46
- async.each(vote.options || [], function(option, next) {
47
- NodeBB.db.sortedSetRemove('poll:' + pollId + ':options:' + option + ':votes', uid, next);
48
+ function (next) {
49
+ async.each(vote.options || [], (option, next) => {
50
+ NodeBB.db.sortedSetRemove(`poll:${pollId}:options:${option}:votes`, uid, next);
48
51
  }, next);
49
52
  },
50
- function(next) {
51
- NodeBB.db.sortedSetRemove('poll:' + pollId + ':voters', uid, next);
52
- }
53
- ], function(err) {
53
+ function (next) {
54
+ NodeBB.db.sortedSetRemove(`poll:${pollId}:voters`, uid, next);
55
+ },
56
+ ], (err) => {
54
57
  if (err) {
55
58
  return callback(err, false);
56
59
  }
@@ -60,14 +63,14 @@ var async = require('async'),
60
63
  });
61
64
  };
62
65
 
63
- Vote.getUidVote = function(uid, pollId, callback) {
64
- Poll.getOptions(pollId, true, function (err, options) {
66
+ Vote.getUidVote = function (uid, pollId, callback) {
67
+ Poll.getOptions(pollId, true, (err, options) => {
65
68
  callback(err, {
66
69
  pollId: pollId,
67
70
  uid: uid,
68
71
  options: options
69
- .filter(option => option.votes.some(_uid => _uid == uid))
70
- .map(option => option.id)
72
+ .filter(option => option.votes.some(_uid => parseInt(_uid, 10) === parseInt(uid, 10)))
73
+ .map(option => option.id),
71
74
  });
72
75
  });
73
76
  };
@@ -79,58 +82,57 @@ var async = require('async'),
79
82
  },
80
83
  function (next) {
81
84
  Vote.add(voteData, next);
82
- }
85
+ },
83
86
  ], callback);
84
87
  };
85
88
 
86
- Vote.canUpdateVote = function(uid, pollId, callback) {
89
+ Vote.canUpdateVote = function (uid, pollId, callback) {
87
90
  async.parallel([
88
- function(next) {
91
+ function (next) {
89
92
  // Ended?
90
93
  Poll.hasEnded(pollId, next);
91
94
  },
92
- function(next) {
95
+ function (next) {
93
96
  // Deleted?
94
97
  Poll.isDeleted(pollId, next);
95
98
  },
96
- function(next) {
99
+ function (next) {
97
100
  // voteUpdate?
98
101
  Poll.doesDisallowVoteUpdate(pollId, next);
99
- }
100
- ], function(err, result) {
102
+ },
103
+ ], (err, result) => {
101
104
  callback(err, result.indexOf(true) === -1);
102
105
  });
103
106
  };
104
107
 
105
- Vote.canVote = function(uid, pollId, callback) {
108
+ Vote.canVote = function (uid, pollId, callback) {
106
109
  async.parallel([
107
- function(next) {
110
+ function (next) {
108
111
  // Ended?
109
112
  Poll.hasEnded(pollId, next);
110
113
  },
111
- function(next) {
114
+ function (next) {
112
115
  // Deleted?
113
116
  Poll.isDeleted(pollId, next);
114
117
  },
115
- function(next) {
118
+ function (next) {
116
119
  // Already voted?
117
120
  Vote.hasUidVoted(uid, pollId, next);
118
- }
119
- ], function(err, result) {
121
+ },
122
+ ], (err, result) => {
120
123
  callback(err, result.indexOf(true) === -1);
121
124
  });
122
125
  };
123
126
 
124
- Vote.hasUidVoted = function(uid, pollId, callback) {
125
- NodeBB.db.sortedSetScore('poll:' + pollId + ':voters', uid, function (err, score) {
126
- callback(err, !!score)
127
+ Vote.hasUidVoted = function (uid, pollId, callback) {
128
+ NodeBB.db.sortedSetScore(`poll:${pollId}:voters`, uid, (err, score) => {
129
+ callback(err, !!score);
127
130
  });
128
131
  };
129
132
 
130
- Vote.hasUidVotedOnOption = function(uid, pollId, option, callback) {
131
- NodeBB.db.sortedSetScore('poll:' + pollId + ':options:' + option + ':votes', uid, function (err, score) {
132
- callback(err, !!score)
133
+ Vote.hasUidVotedOnOption = function (uid, pollId, option, callback) {
134
+ NodeBB.db.sortedSetScore(`poll:${pollId}:options:${option}:votes`, uid, (err, score) => {
135
+ callback(err, !!score);
133
136
  });
134
137
  };
135
-
136
- })(exports);
138
+ }(exports));
package/library.js CHANGED
@@ -1,22 +1,21 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- var NodeBB = require('./lib/nodebb'),
4
- Config = require('./lib/config'),
5
- Sockets = require('./lib/sockets'),
6
- Hooks = require('./lib/hooks'),
7
- Scheduler = require('./lib/scheduler');
8
-
9
- (function(Plugin) {
3
+ const NodeBB = require('./lib/nodebb');
4
+ const Config = require('./lib/config');
5
+ const Sockets = require('./lib/sockets');
6
+ const Hooks = require('./lib/hooks');
7
+ const Scheduler = require('./lib/scheduler');
10
8
 
9
+ (function (Plugin) {
11
10
  Plugin.hooks = Hooks;
12
11
 
13
- Plugin.load = function(params, callback) {
14
- function renderAdmin(req, res, next) {
15
- res.render('admin/plugins/' + Config.plugin.id, {});
12
+ Plugin.load = function (params, callback) {
13
+ function renderAdmin(req, res) {
14
+ res.render(`admin/plugins/${Config.plugin.id}`, {});
16
15
  }
17
16
 
18
- params.router.get('/admin/plugins/' + Config.plugin.id, params.middleware.admin.buildHeader, renderAdmin);
19
- params.router.get('/api/admin/plugins/' + Config.plugin.id, renderAdmin);
17
+ params.router.get(`/admin/plugins/${Config.plugin.id}`, params.middleware.admin.buildHeader, renderAdmin);
18
+ params.router.get(`/api/admin/plugins/${Config.plugin.id}`, renderAdmin);
20
19
 
21
20
  NodeBB.PluginSockets[Config.plugin.id] = Sockets;
22
21
  NodeBB.AdminSockets[Config.plugin.id] = Config.adminSockets;
@@ -27,50 +26,49 @@ var NodeBB = require('./lib/nodebb'),
27
26
  Config.init(callback);
28
27
  };
29
28
 
30
- Plugin.addAdminNavigation = function(adminHeader, callback) {
29
+ Plugin.addAdminNavigation = function (adminHeader, callback) {
31
30
  adminHeader.plugins.push({
32
- route: '/plugins/' + Config.plugin.id,
31
+ route: `/plugins/${Config.plugin.id}`,
33
32
  icon: Config.plugin.icon,
34
- name: Config.plugin.name
33
+ name: Config.plugin.name,
35
34
  });
36
35
 
37
36
  callback(null, adminHeader);
38
37
  };
39
38
 
40
- Plugin.registerFormatting = function(payload, callback) {
39
+ Plugin.registerFormatting = function (payload, callback) {
41
40
  payload.options.push({
42
41
  name: 'poll',
43
- className: 'fa ' + Config.plugin.icon,
44
- title: '[[poll:creator_title]]'
42
+ className: `fa ${Config.plugin.icon}`,
43
+ title: '[[poll:creator_title]]',
45
44
  });
46
45
 
47
46
  callback(null, payload);
48
47
  };
49
48
 
50
- Plugin.addUserPrivilege = function(privileges, callback) {
49
+ Plugin.addUserPrivilege = function (privileges, callback) {
51
50
  privileges.push('poll:create');
52
51
  callback(null, privileges);
53
52
  };
54
53
 
55
- Plugin.addPrivilegeLabels = function(labels, callback) {
56
- labels.push({name: 'Create Poll'});
54
+ Plugin.addPrivilegeLabels = function (labels, callback) {
55
+ labels.push({ name: 'Create Poll' });
57
56
  callback(null, labels);
58
57
  };
59
58
 
60
- Plugin.addGroupPrivilege = function(privileges, callback) {
59
+ Plugin.addGroupPrivilege = function (privileges, callback) {
61
60
  privileges.push('groups:poll:create');
62
61
  callback(null, privileges);
63
62
  };
64
63
 
65
64
  Plugin.copyPrivilegesFrom = function (data, callback) {
66
- if (data.privileges.indexOf('poll:create') == -1) {
65
+ if (data.privileges.indexOf('poll:create') === -1) {
67
66
  data.privileges.push('poll:create');
68
67
  }
69
68
 
70
- if (data.privileges.indexOf('groups:poll:create') == -1) {
69
+ if (data.privileges.indexOf('groups:poll:create') === -1) {
71
70
  data.privileges.push('groups:poll:create');
72
71
  }
73
72
  callback(null, data);
74
73
  };
75
-
76
- })(exports);
74
+ }(exports));