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/poll.js CHANGED
@@ -1,23 +1,22 @@
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
- Vote = require('./vote'),
8
- Scheduler = require('./scheduler');
7
+ const Vote = require('./vote');
8
+ const Scheduler = require('./scheduler');
9
9
 
10
- (function(Poll) {
10
+ (function (Poll) {
11
+ const POLL_DATA_VERSION = 2;
11
12
 
12
- var POLL_DATA_VERSION = 2;
13
-
14
- Poll.add = function(pollData, postData, callback) {
15
- NodeBB.db.incrObjectField('global', 'nextPollId', function(err, pollId) {
13
+ Poll.add = function (pollData, postData, callback) {
14
+ NodeBB.db.incrObjectField('global', 'nextPollId', (err, pollId) => {
16
15
  if (err) {
17
16
  return callback(err);
18
17
  }
19
18
 
20
- var poll = {
19
+ const poll = {
21
20
  pollId: pollId,
22
21
  uid: postData.editor || postData.uid,
23
22
  tid: postData.tid,
@@ -25,29 +24,27 @@ var async = require('async'),
25
24
  deleted: 0,
26
25
  ended: 0,
27
26
  timestamp: postData.edited || postData.timestamp,
28
- version: POLL_DATA_VERSION
27
+ version: POLL_DATA_VERSION,
29
28
  };
30
29
 
31
- pollData.options = pollData.options.map(function(val, i) {
32
- return {
33
- id: i,
34
- title: val
35
- };
36
- });
30
+ pollData.options = pollData.options.map((val, i) => ({
31
+ id: i,
32
+ title: val,
33
+ }));
37
34
 
38
35
  // async this bitch up
39
36
  async.parallel([
40
- async.apply(async.each, pollData.options, function(option, next) {
37
+ async.apply(async.each, pollData.options, (option, next) => {
41
38
  async.series([
42
- async.apply(NodeBB.db.setObject, 'poll:' + pollId + ':options:' + option.id, option),
43
- async.apply(NodeBB.db.setAdd, 'poll:' + pollId + ':options', option.id)
39
+ async.apply(NodeBB.db.setObject, `poll:${pollId}:options:${option.id}`, option),
40
+ async.apply(NodeBB.db.setAdd, `poll:${pollId}:options`, option.id),
44
41
  ], next);
45
42
  }),
46
- async.apply(NodeBB.db.setObject, 'poll:' + pollId, poll),
47
- async.apply(NodeBB.db.setObject, 'poll:' + pollId + ':settings', pollData.settings),
43
+ async.apply(NodeBB.db.setObject, `poll:${pollId}`, poll),
44
+ async.apply(NodeBB.db.setObject, `poll:${pollId}:settings`, pollData.settings),
48
45
  async.apply(NodeBB.db.listAppend, 'polls', pollId),
49
- async.apply(NodeBB.db.setObjectField, 'topic:' + poll.tid, 'pollId', pollId)
50
- ], function(err) {
46
+ async.apply(NodeBB.db.setObjectField, `topic:${poll.tid}`, 'pollId', pollId),
47
+ ], (err) => {
51
48
  if (err) {
52
49
  return callback(err);
53
50
  }
@@ -62,18 +59,18 @@ var async = require('async'),
62
59
  });
63
60
  };
64
61
 
65
- Poll.get = function(pollId, uid, withVotes, callback) {
62
+ Poll.get = function (pollId, uid, withVotes, callback) {
66
63
  async.parallel({
67
- info: function(next) {
64
+ info: function (next) {
68
65
  Poll.getInfo(pollId, next);
69
66
  },
70
- options: function(next) {
67
+ options: function (next) {
71
68
  Poll.getOptions(pollId, withVotes, next);
72
69
  },
73
- settings: function(next) {
70
+ settings: function (next) {
74
71
  Poll.getSettings(pollId, next);
75
72
  },
76
- hasVoted: function(next) {
73
+ hasVoted: function (next) {
77
74
  if (uid) {
78
75
  Vote.hasUidVoted(uid, pollId, next);
79
76
  } else {
@@ -86,13 +83,13 @@ var async = require('async'),
86
83
  } else {
87
84
  next(null, null);
88
85
  }
89
- }
90
- }, function(err, pollData) {
86
+ },
87
+ }, (err, pollData) => {
91
88
  if (err) {
92
89
  return callback(err);
93
90
  }
94
- pollData.options.forEach(function(option) {
95
- var percentage = ((option.voteCount / pollData.info.voteCount) * 100).toFixed(0);
91
+ pollData.options.forEach((option) => {
92
+ const percentage = ((option.voteCount / pollData.info.voteCount) * 100).toFixed(0);
96
93
  option.percentage = isNaN(percentage) ? 0 : percentage;
97
94
  });
98
95
  pollData.settings.disallowVoteUpdate = parseInt(pollData.settings.disallowVoteUpdate, 10);
@@ -100,63 +97,63 @@ var async = require('async'),
100
97
  });
101
98
  };
102
99
 
103
- Poll.getPollIdByTid = function(tid, callback) {
104
- NodeBB.db.getObjectField('topic:' + tid, 'pollId', callback);
100
+ Poll.getPollIdByTid = function (tid, callback) {
101
+ NodeBB.db.getObjectField(`topic:${tid}`, 'pollId', callback);
105
102
  };
106
103
 
107
- Poll.getPollIdByPid = function(pid, callback) {
108
- NodeBB.db.getObjectField('post:' + pid, 'pollId', callback);
104
+ Poll.getPollIdByPid = function (pid, callback) {
105
+ NodeBB.db.getObjectField(`post:${pid}`, 'pollId', callback);
109
106
  };
110
107
 
111
- Poll.getInfo = function(pollId, callback) {
108
+ Poll.getInfo = function (pollId, callback) {
112
109
  async.parallel({
113
110
  poll: function (next) {
114
- NodeBB.db.getObject('poll:' + pollId, next);
111
+ NodeBB.db.getObject(`poll:${pollId}`, next);
115
112
  },
116
113
  voteCount: function (next) {
117
- Poll.getVoteCount(pollId, next)
118
- }
119
- }, function (err, results) {
114
+ Poll.getVoteCount(pollId, next);
115
+ },
116
+ }, (err, results) => {
120
117
  if (err) {
121
118
  return callback(err);
122
119
  }
123
120
  results.poll.voteCount = parseInt(results.voteCount, 10) || 0;
124
- callback(null, results.poll)
125
- })
121
+ callback(null, results.poll);
122
+ });
126
123
  };
127
124
 
128
- Poll.getOptions = function(pollId, withVotes, callback) {
125
+ Poll.getOptions = function (pollId, withVotes, callback) {
129
126
  if (typeof withVotes === 'function') {
130
127
  callback = withVotes;
131
128
  withVotes = false;
132
129
  }
133
130
 
134
- NodeBB.db.getSetMembers('poll:' + pollId + ':options', function(err, options) {
131
+ NodeBB.db.getSetMembers(`poll:${pollId}:options`, (err, options) => {
135
132
  if (err) {
136
133
  return callback(err);
137
134
  }
138
- async.map(options, function(option, next) {
135
+ async.map(options, (option, next) => {
139
136
  Poll.getOption(pollId, option, withVotes, next);
140
137
  }, callback);
141
138
  });
142
139
  };
143
140
 
144
- Poll.getOption = function(pollId, option, withVotes, callback) {
141
+ Poll.getOption = function (pollId, option, withVotes, callback) {
145
142
  async.parallel({
146
- option: function(next) {
147
- NodeBB.db.getObject('poll:' + pollId + ':options:' + option, next);
143
+ option: function (next) {
144
+ NodeBB.db.getObject(`poll:${pollId}:options:${option}`, next);
148
145
  },
149
- votes: function(next) {
146
+ votes: function (next) {
150
147
  if (withVotes) {
151
- NodeBB.db.getSortedSetRange('poll:' + pollId + ':options:' + option + ':votes', 0, -1, next);
148
+ NodeBB.db.getSortedSetRange(`poll:${pollId}:options:${option}:votes`, 0, -1, next);
152
149
  } else {
153
150
  next();
154
151
  }
155
152
  },
156
- voteCount: function(next) {
153
+ voteCount: function (next) {
157
154
  Poll.getOptionVoteCount(pollId, option, next);
158
- }
159
- }, function(err, results) {
155
+ },
156
+ }, (err, results) => {
160
157
  if (err) {
161
158
  return callback(err);
162
159
  }
@@ -170,14 +167,18 @@ var async = require('async'),
170
167
  };
171
168
 
172
169
  Poll.getVotersCount = function (pollId, callback) {
173
- NodeBB.db.sortedSetCard('poll:' + pollId + ':voters', callback);
170
+ NodeBB.db.sortedSetCard(`poll:${pollId}:voters`, callback);
174
171
  };
175
172
 
176
173
  Poll.getVoteCount = function (pollId, callback) {
177
- NodeBB.db.getSetMembers('poll:' + pollId + ':options', function(err, options) {
178
- async.map(options, function(option, next) {
174
+ NodeBB.db.getSetMembers(`poll:${pollId}:options`, (err, options) => {
175
+ if (err) {
176
+ console.error(err);
177
+ }
178
+
179
+ async.map(options, (option, next) => {
179
180
  Poll.getOptionVoteCount(pollId, option, next);
180
- }, function (err, results) {
181
+ }, (err, results) => {
181
182
  if (err) {
182
183
  return callback(err);
183
184
  }
@@ -187,112 +188,111 @@ var async = require('async'),
187
188
  };
188
189
 
189
190
  Poll.getOptionVoteCount = function (pollId, option, callback) {
190
- NodeBB.db.sortedSetCard('poll:' + pollId + ':options:' + option + ':votes', callback);
191
+ NodeBB.db.sortedSetCard(`poll:${pollId}:options:${option}:votes`, callback);
191
192
  };
192
193
 
193
- Poll.hasOption = function(pollId, option, callback) {
194
- NodeBB.db.isSetMember('poll:' + pollId + ':options', option, callback);
194
+ Poll.hasOption = function (pollId, option, callback) {
195
+ NodeBB.db.isSetMember(`poll:${pollId}:options`, option, callback);
195
196
  };
196
197
 
197
- Poll.hasOptions = function(pollId, options, callback) {
198
- NodeBB.db.isSetMembers('poll:' + pollId + ':options', options, callback);
198
+ Poll.hasOptions = function (pollId, options, callback) {
199
+ NodeBB.db.isSetMembers(`poll:${pollId}:options`, options, callback);
199
200
  };
200
201
 
201
- Poll.getSettings = function(pollId, callback) {
202
- NodeBB.db.getObject('poll:' + pollId + ':settings', callback);
202
+ Poll.getSettings = function (pollId, callback) {
203
+ NodeBB.db.getObject(`poll:${pollId}:settings`, callback);
203
204
  };
204
205
 
205
- Poll.isDeleted = function(pollId, callback) {
206
- Poll.getField(pollId, 'deleted', function(err, result) {
206
+ Poll.isDeleted = function (pollId, callback) {
207
+ Poll.getField(pollId, 'deleted', (err, result) => {
207
208
  callback(err, parseInt(result, 10) === 1);
208
209
  });
209
210
  };
210
211
 
211
- Poll.delete = function(pollId) {
212
+ Poll.delete = function (pollId) {
212
213
  Poll.setField(pollId, 'deleted', 1);
213
214
  };
214
215
 
215
- Poll.restore = function(pollId) {
216
+ Poll.restore = function (pollId) {
216
217
  Poll.setField(pollId, 'edited', 0);
217
218
  Poll.setField(pollId, 'deleted', 0);
218
219
  };
219
220
 
220
- Poll.schedule = function(pollId) {
221
+ Poll.schedule = function (pollId) {
221
222
  NodeBB.db.setAdd('polls:scheduled', pollId);
222
223
  Scheduler.add(pollId);
223
224
  };
224
225
 
225
- Poll.getScheduled = function(callback) {
226
+ Poll.getScheduled = function (callback) {
226
227
  NodeBB.db.getSetMembers('polls:scheduled', callback);
227
228
  };
228
229
 
229
- Poll.hasEnded = function(pollId, callback) {
230
- Poll.getField(pollId, 'ended', function(err, result) {
230
+ Poll.hasEnded = function (pollId, callback) {
231
+ Poll.getField(pollId, 'ended', (err, result) => {
231
232
  callback(err, parseInt(result, 10) === 1);
232
233
  });
233
234
  };
234
235
 
235
- Poll.doesDisallowVoteUpdate = function(pollId, callback) {
236
- Poll.getSettingsField(pollId, 'disallowVoteUpdate', function(err, result) {
236
+ Poll.doesDisallowVoteUpdate = function (pollId, callback) {
237
+ Poll.getSettingsField(pollId, 'disallowVoteUpdate', (err, result) => {
237
238
  callback(err, parseInt(result, 10) === 1);
238
239
  });
239
240
  };
240
241
 
241
- Poll.doesAllowVoteUpdate = function(pollId, callback) {
242
- Poll.getSettingsField(pollId, 'disallowVoteUpdate', function(err, result) {
242
+ Poll.doesAllowVoteUpdate = function (pollId, callback) {
243
+ Poll.getSettingsField(pollId, 'disallowVoteUpdate', (err, result) => {
243
244
  callback(err, parseInt(result, 10) !== 1);
244
245
  });
245
246
  };
246
247
 
247
- Poll.end = function(pollId) {
248
+ Poll.end = function (pollId) {
248
249
  NodeBB.db.setRemove('polls:scheduled', pollId);
249
250
  Poll.setField(pollId, 'ended', 1);
250
251
  };
251
252
 
252
- Poll.changePid = function(pollId, pid, callback) {
253
+ Poll.changePid = function (pollId, pid, callback) {
253
254
  async.parallel([
254
- function(next) {
255
+ function (next) {
255
256
  Poll.setField(pollId, 'pid', pid, next);
256
257
  },
257
- function(next) {
258
- NodeBB.db.setObjectField('post:' + pid, 'pollId', pollId, next);
259
- }
258
+ function (next) {
259
+ NodeBB.db.setObjectField(`post:${pid}`, 'pollId', pollId, next);
260
+ },
260
261
  ], callback);
261
262
  };
262
263
 
263
- Poll.changeTid = function(pollId, tid, callback) {
264
+ Poll.changeTid = function (pollId, tid, callback) {
264
265
  async.parallel([
265
- function(next) {
266
+ function (next) {
266
267
  Poll.setField(pollId, 'tid', tid, next);
267
268
  },
268
- function(next) {
269
- NodeBB.db.setObjectField('topic:' + tid, 'pollId', pollId, next);
270
- }
269
+ function (next) {
270
+ NodeBB.db.setObjectField(`topic:${tid}`, 'pollId', pollId, next);
271
+ },
271
272
  ], callback);
272
273
  };
273
274
 
274
- Poll.setField = function(pollId, field, value, callback) {
275
- NodeBB.db.setObjectField('poll:' + pollId, field, value, callback);
275
+ Poll.setField = function (pollId, field, value, callback) {
276
+ NodeBB.db.setObjectField(`poll:${pollId}`, field, value, callback);
276
277
  };
277
278
 
278
- Poll.setFields = function(pollId, fields, values, callback) {
279
- NodeBB.db.setObjectFields('poll:' + pollId, fields, values, callback);
279
+ Poll.setFields = function (pollId, fields, values, callback) {
280
+ NodeBB.db.setObjectFields(`poll:${pollId}`, fields, values, callback);
280
281
  };
281
282
 
282
- Poll.getField = function(pollId, field, callback) {
283
- NodeBB.db.getObjectField('poll:' + pollId, field, callback);
283
+ Poll.getField = function (pollId, field, callback) {
284
+ NodeBB.db.getObjectField(`poll:${pollId}`, field, callback);
284
285
  };
285
286
 
286
- Poll.getFields = function(pollId, fields, callback) {
287
- NodeBB.db.getObjectFields('poll:' + pollId, fields, callback);
287
+ Poll.getFields = function (pollId, fields, callback) {
288
+ NodeBB.db.getObjectFields(`poll:${pollId}`, fields, callback);
288
289
  };
289
290
 
290
- Poll.getSettingsField = function(pollId, field, callback) {
291
- NodeBB.db.getObjectField('poll:' + pollId + ':settings', field, callback);
291
+ Poll.getSettingsField = function (pollId, field, callback) {
292
+ NodeBB.db.getObjectField(`poll:${pollId}:settings`, field, callback);
292
293
  };
293
294
 
294
- Poll.setSettingsField = function(pollId, field, value, callback) {
295
- NodeBB.db.setObjectField('poll:' + pollId + ':settings', field, callback);
295
+ Poll.setSettingsField = function (pollId, field, value, callback) {
296
+ NodeBB.db.setObjectField(`poll:${pollId}:settings`, field, callback);
296
297
  };
297
-
298
- })(exports);
298
+ }(exports));
package/lib/scheduler.js CHANGED
@@ -1,55 +1,57 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- var NodeBB = require('./nodebb'),
3
+ const cron = require('cron').CronJob;
4
+ const NodeBB = require('./nodebb');
4
5
 
5
- Poll = require('./poll'),
6
- cron = require('cron').CronJob;
6
+ const Poll = require('./poll');
7
7
 
8
- (function(Scheduler) {
8
+ (function (Scheduler) {
9
+ const jobs = {};
9
10
 
10
- var jobs = {};
11
-
12
- Scheduler.start = function() {
13
- Poll.getScheduled(function(err, pollIds) {
14
- pollIds.forEach(function(pollId) {
11
+ Scheduler.start = function () {
12
+ Poll.getScheduled((err, pollIds) => {
13
+ if (err) {
14
+ console.error(err);
15
+ }
16
+ pollIds.forEach((pollId) => {
15
17
  Scheduler.add(pollId);
16
18
  });
17
19
  });
18
20
  };
19
21
 
20
- Scheduler.add = function(pollId) {
22
+ Scheduler.add = function (pollId) {
21
23
  if (Object.keys(jobs).indexOf(pollId.toString()) !== -1) {
22
24
  return;
23
25
  }
24
26
 
25
- Poll.getSettings(pollId, function(err, settings) {
27
+ Poll.getSettings(pollId, (err, settings) => {
26
28
  if (err) {
27
29
  return NodeBB.winston.error(err);
28
30
  }
29
31
 
30
32
  if (!settings) {
31
- return NodeBB.winston.error('[nodebb-plugin-poll/scheduler] Poll ID ' + pollId + ' has no settings!');
33
+ return NodeBB.winston.error(`[nodebb-plugin-poll/scheduler] Poll ID ${pollId} has no settings!`);
32
34
  }
33
35
 
34
- var now = Date.now(),
35
- end = parseInt(settings.end, 10);
36
+ const now = Date.now();
37
+ const end = parseInt(settings.end, 10);
36
38
 
37
39
  if (end < now) {
38
40
  Scheduler.end(pollId);
39
41
  } else {
40
- var date = new Date(end);
41
- NodeBB.winston.verbose('[nodebb-plugin-poll/scheduler] Starting scheduler for poll with ID ' + pollId + ' to end on ' + date);
42
- jobs[pollId] = new cron(date, function() {
42
+ const date = new Date(end);
43
+ NodeBB.winston.verbose(`[nodebb-plugin-poll/scheduler] Starting scheduler for poll with ID ${pollId} to end on ${date}`);
44
+ jobs[pollId] = new cron(date, (() => {
43
45
  Scheduler.end(pollId);
44
- }, null, true);
46
+ }), null, true);
45
47
  }
46
48
  });
47
49
  };
48
50
 
49
- Scheduler.end = function(pollId) {
50
- NodeBB.winston.verbose('[nodebb-plugin-poll/scheduler] Ending poll with ID ' + pollId);
51
+ Scheduler.end = function (pollId) {
52
+ NodeBB.winston.verbose(`[nodebb-plugin-poll/scheduler] Ending poll with ID ${pollId}`);
51
53
 
52
- var index = Object.keys(jobs).indexOf(pollId.toString());
54
+ const index = Object.keys(jobs).indexOf(pollId.toString());
53
55
 
54
56
  if (index !== -1 && jobs[pollId] !== undefined) {
55
57
  jobs[pollId].stop();
@@ -58,5 +60,4 @@ var NodeBB = require('./nodebb'),
58
60
 
59
61
  Poll.end(pollId);
60
62
  };
61
-
62
- })(exports);
63
+ }(exports));
package/lib/serializer.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- module.exports = require('../public/js/poll/serializer');
3
+ module.exports = require('../public/js/poll/serializer');