nodebb-plugin-poll 3.2.8 → 4.0.0

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/vote.js CHANGED
@@ -1,23 +1,25 @@
1
1
  'use strict';
2
2
 
3
- const NodeBB = require('./nodebb');
3
+ const db = require.main.require('./src/database');
4
+ const privileges = require.main.require('./src/privileges');
5
+
4
6
  const Poll = require('./poll');
5
7
 
6
8
  const Vote = exports;
7
9
 
8
10
  Vote.add = async function (voteData) {
9
- const { pollId, options, uid } = voteData;
10
- const timestamp = +new Date();
11
+ const { pollId, options, uid, voteAnon } = voteData;
12
+ const timestamp = Date.now();
11
13
 
12
14
  const promises = [
13
- NodeBB.db.sortedSetAdd(`poll:${pollId}:voters`, timestamp, uid),
14
- NodeBB.db.sortedSetAddBulk(
15
+ db.sortedSetAdd(`poll:${pollId}:voters`, timestamp, uid),
16
+ db.sortedSetAddBulk(
15
17
  options.map(option => ([`poll:${pollId}:options:${option}:votes`, timestamp, uid]))
16
18
  ),
17
19
  ];
18
- if (voteData.voteAnon) {
20
+ if (voteAnon) {
19
21
  promises.push(
20
- NodeBB.db.sortedSetAdd(`poll:${pollId}:anon:voters`, timestamp, uid)
22
+ db.sortedSetAdd(`poll:${pollId}:anon:voters`, timestamp, uid)
21
23
  );
22
24
  }
23
25
 
@@ -36,11 +38,11 @@ Vote.removeUidVote = async function (uid, pollId) {
36
38
  const vote = await Vote.getUidVote(uid, pollId);
37
39
  const options = vote.options || [];
38
40
  await Promise.all([
39
- NodeBB.db.sortedSetsRemove([
41
+ db.sortedSetsRemove([
40
42
  `poll:${pollId}:voters`,
41
43
  `poll:${pollId}:anon:voters`,
42
44
  ], uid),
43
- NodeBB.db.sortedSetsRemove(
45
+ db.sortedSetsRemove(
44
46
  options.map(option => `poll:${pollId}:options:${option}:votes`),
45
47
  uid,
46
48
  ),
@@ -60,37 +62,48 @@ Vote.getUidVote = async function (uid, pollId) {
60
62
 
61
63
  Vote.update = async function (voteData) {
62
64
  const { uid, pollId } = voteData;
63
- const isVotedAnon = await NodeBB.db.isSortedSetMember(`poll:${pollId}:anon:voters`, uid);
65
+ const isVotedAnon = await db.isSortedSetMember(`poll:${pollId}:anon:voters`, uid);
64
66
  await Vote.remove(voteData);
65
67
  voteData.voteAnon = isVotedAnon;
66
68
  await Vote.add(voteData);
67
69
  };
68
70
 
69
71
  Vote.canUpdateVote = async function (uid, pollId) {
70
- const [ended, deleted, updateDisabled] = await Promise.all([
72
+ const pollInfo = await Poll.getInfo(pollId, false);
73
+ if (!pollInfo) {
74
+ throw new Error('[[error:invalid-data]]');
75
+ }
76
+ const [canRead, ended, deleted, updateDisabled] = await Promise.all([
77
+ privileges.posts.can('topics:read', pollInfo.pid, uid),
71
78
  Poll.hasEnded(pollId),
72
79
  Poll.isDeleted(pollId),
73
80
  Poll.doesDisallowVoteUpdate(pollId),
74
81
  ]);
75
- return !ended && !deleted && !updateDisabled;
82
+ return !canRead && !ended && !deleted && !updateDisabled;
76
83
  };
77
84
 
78
85
  Vote.canVote = async function (uid, pollId) {
79
- const [ended, deleted, hasVoted] = await Promise.all([
86
+ const pollInfo = await Poll.getInfo(pollId, false);
87
+ if (!pollInfo) {
88
+ throw new Error('[[error:invalid-data]]');
89
+ }
90
+
91
+ const [canRead, ended, deleted, hasVoted] = await Promise.all([
92
+ privileges.posts.can('topics:read', pollInfo.pid, uid),
80
93
  Poll.hasEnded(pollId),
81
94
  Poll.isDeleted(pollId),
82
95
  Vote.hasUidVoted(uid, pollId),
83
96
  ]);
84
- return !ended && !deleted && !hasVoted;
97
+ return !canRead && !ended && !deleted && !hasVoted;
85
98
  };
86
99
 
87
100
  Vote.hasUidVoted = async function (uid, pollId) {
88
- const score = await NodeBB.db.sortedSetScore(`poll:${pollId}:voters`, uid);
101
+ const score = await db.sortedSetScore(`poll:${pollId}:voters`, uid);
89
102
  return !!score;
90
103
  };
91
104
 
92
105
  Vote.hasUidVotedOnOption = async function (uid, pollId, option) {
93
- const score = await NodeBB.db.sortedSetScore(`poll:${pollId}:options:${option}:votes`, uid);
106
+ const score = await db.sortedSetScore(`poll:${pollId}:options:${option}:votes`, uid);
94
107
  return !!score;
95
108
  };
96
109
 
package/library.js CHANGED
@@ -1,12 +1,13 @@
1
1
  'use strict';
2
2
 
3
- const nconf = require.main.require('nconf');
4
-
5
3
  const NodeBB = require('./lib/nodebb');
6
4
  const Config = require('./lib/config');
7
5
  const Sockets = require('./lib/sockets');
8
6
  const Hooks = require('./lib/hooks');
9
- const Scheduler = require('./lib/scheduler');
7
+ const Poll = require('./lib/poll');
8
+ const db = require.main.require('./src/database');
9
+ const pagination = require.main.require('./src/pagination');
10
+ const utils = require.main.require('./src/utils');
10
11
 
11
12
  const Plugin = module.exports;
12
13
 
@@ -16,22 +17,36 @@ Plugin.load = async function (params) {
16
17
  const routeHelpers = require.main.require('./src/routes/helpers');
17
18
  const { router } = params;
18
19
 
19
- routeHelpers.setupAdminPageRoute(router, `/admin/plugins/${Config.plugin.id}`, (req, res) => {
20
+ routeHelpers.setupAdminPageRoute(router, `/admin/plugins/${Config.plugin.id}`, async (req, res) => {
21
+ const page = Math.max(parseInt(req.query.page, 10) || 1, 1);
22
+ const itemsPerPage = 20;
23
+ const start = Math.max(0, (page - 1) * itemsPerPage);
24
+ const stop = start + itemsPerPage - 1;
25
+ const [pollIds, count] = await Promise.all([
26
+ db.getSortedSetRevRange('polls:createtime', start, stop),
27
+ db.sortedSetCard('polls:createtime'),
28
+ ]);
29
+
30
+ const pollData = await db.getObjects(pollIds.map(pollId => `poll:${pollId}`));
31
+ const voteCounts = await Promise.all(pollIds.map(Poll.getVoteCount));
32
+ pollData.forEach((poll, index) => {
33
+ if (poll) {
34
+ poll.timestampISO = utils.toISOString(poll.timestamp);
35
+ poll.endISO = utils.toISOString(poll.end);
36
+ poll.voteCount = voteCounts[index];
37
+ }
38
+ });
39
+ const pageCount = Math.ceil(count / itemsPerPage);
20
40
  res.render(`admin/plugins/${Config.plugin.id}`, {
21
41
  title: 'Poll',
42
+ polls: pollData,
43
+ pagination: pagination.create(page, pageCount, req.query),
22
44
  });
23
45
  });
24
46
 
25
47
  NodeBB.PluginSockets[Config.plugin.id] = Sockets;
26
- NodeBB.AdminSockets[Config.plugin.id] = Config.adminSockets;
27
48
 
28
49
  NodeBB.app = params.app;
29
-
30
- if (nconf.get('runJobs')) {
31
- Scheduler.start();
32
- }
33
-
34
- await Config.init();
35
50
  };
36
51
 
37
52
  Plugin.addAdminNavigation = function (adminHeader) {
@@ -43,15 +58,6 @@ Plugin.addAdminNavigation = function (adminHeader) {
43
58
  return adminHeader;
44
59
  };
45
60
 
46
- Plugin.registerFormatting = function (payload) {
47
- payload.options.push({
48
- name: 'poll',
49
- className: `fa ${Config.plugin.icon}`,
50
- title: '[[poll:creator_title]]',
51
- });
52
- return payload;
53
- };
54
-
55
61
  Plugin.addPrivilege = function (hookData) {
56
62
  hookData.privileges.set(
57
63
  'poll:create', { label: '[[poll:admin.create-poll]]' },
@@ -82,12 +88,8 @@ Plugin.defineWidgets = async function (widgets) {
82
88
  };
83
89
 
84
90
  Plugin.renderPollWidget = async function (widget) {
85
- const { tid } = widget.data;
91
+ const { pollId } = widget.data;
86
92
  const Poll = require('./lib/poll');
87
- const pollId = await Poll.getPollIdByTid(tid);
88
- if (!pollId) {
89
- return null;
90
- }
91
93
  const pollData = await Poll.get(pollId, widget.uid, widget.req.loggedIn);
92
94
  if (!pollData) {
93
95
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-poll",
3
- "version": "3.2.8",
3
+ "version": "4.0.0",
4
4
  "description": "NodeBB Poll Plugin",
5
5
  "main": "library.js",
6
6
  "scripts": {
@@ -26,10 +26,10 @@
26
26
  "cron": "^1.8.2"
27
27
  },
28
28
  "nbbpm": {
29
- "compatibility": "^4.0.0"
29
+ "compatibility": "^4.9.2"
30
30
  },
31
31
  "devDependencies": {
32
- "eslint": "9.28.0",
33
- "eslint-config-nodebb": "1.1.7"
32
+ "eslint": "10.0.3",
33
+ "eslint-config-nodebb": "^2.0.0"
34
34
  }
35
35
  }
package/plugin.json CHANGED
@@ -6,19 +6,21 @@
6
6
  "library": "library.js",
7
7
  "hooks": [
8
8
  { "hook": "static:app.load", "method": "load" },
9
+ { "hook": "filter:config.get", "method": "hooks.filter.configGet" },
9
10
  { "hook": "filter:admin.header.build", "method": "addAdminNavigation" },
10
- { "hook": "filter:composer.formatting", "method": "registerFormatting" },
11
+ { "hook": "filter:composer.formatting", "method": "hooks.filter.registerFormatting" },
12
+ { "hook": "filter:composer.push", "method": "hooks.filter.composerPush" },
11
13
  { "hook": "static:privileges.categories.init", "method": "addPrivilege" },
12
14
  { "hook": "filter:categories.copyPrivilegesFrom", "method": "copyPrivilegesFrom" },
13
15
  { "hook": "filter:topic.post", "method": "hooks.filter.topicPost" },
14
- { "hook": "filter:parse.raw", "method": "hooks.filter.parseRaw", "priority": 1 },
16
+ { "hook": "filter:topic.reply", "method": "hooks.filter.topicReply" },
15
17
  { "hook": "filter:post.create", "method": "hooks.filter.postCreate" },
18
+ { "hook": "filter:post.getFields", "method": "hooks.filter.postGetFields" },
16
19
  { "hook": "filter:post.edit", "method": "hooks.filter.postEdit" },
17
20
 
18
21
  { "hook": "action:post.delete", "method": "hooks.action.postDelete" },
19
22
  { "hook": "action:post.restore", "method": "hooks.action.postRestore" },
20
- { "hook": "action:topic.delete", "method": "hooks.action.topicDelete" },
21
- { "hook": "action:topic.restore", "method": "hooks.action.topicRestore" },
23
+ { "hook": "action:posts.purge", "method": "hooks.action.postsPurge" },
22
24
 
23
25
  { "hook": "filter:widgets.getWidgets", "method": "defineWidgets" },
24
26
  { "hook": "filter:widget.render:poll", "method": "renderPollWidget" }
@@ -40,6 +42,7 @@
40
42
  "upgrades": [
41
43
  "upgrades/give_categories_poll_create_privs_to_reg_users.js",
42
44
  "upgrades/convert_poll_id_voters_and_options_votes_to_sorted_sets.js",
43
- "upgrades/fix_incorrect_poll_privilege.js"
45
+ "upgrades/fix_incorrect_poll_privilege.js",
46
+ "upgrades/poll-4.0-upgrade.js"
44
47
  ]
45
48
  }
@@ -1,44 +1,15 @@
1
1
  'use strict';
2
2
 
3
3
  define('admin/plugins/poll', ['settings'], function (Settings) {
4
- var wrapper;
5
-
6
- var ACP = {};
4
+ const ACP = {};
7
5
 
8
6
  ACP.init = function () {
9
- wrapper = $('.poll-settings');
10
-
11
- Settings.sync('poll', wrapper);
7
+ Settings.load('poll', $('.poll-settings'));
12
8
 
13
9
  $('#save').on('click', function () {
14
- save();
15
- });
16
-
17
- $('#reset').click(function () {
18
- reset();
10
+ Settings.save('poll', $('.poll-settings'));
19
11
  });
20
12
  };
21
13
 
22
- function save() {
23
- Settings.persist('poll', wrapper, function () {
24
- socket.emit('admin.plugins.poll.sync');
25
- });
26
- }
27
-
28
- function reset() {
29
- bootbox.confirm('Are you sure you wish to reset the settings?', function (sure) {
30
- if (sure) {
31
- socket.emit('admin.plugins.poll.getDefaults', null, function (err, data) {
32
- if (err) {
33
- console.error(err);
34
- }
35
- Settings.set('poll', data, wrapper, function () {
36
- socket.emit('admin.plugins.poll.sync');
37
- });
38
- });
39
- }
40
- });
41
- }
42
-
43
14
  return ACP;
44
15
  });