nodebb-plugin-poll 4.0.12 → 4.0.14
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/hooks.js +14 -0
- package/lib/sockets.js +7 -4
- package/package.json +1 -1
- package/public/js/poll/creator.js +4 -3
- package/public/js/poll/view.js +4 -4
- package/templates/poll/view/details.tpl +1 -1
package/lib/hooks.js
CHANGED
|
@@ -44,6 +44,7 @@ Hooks.filter.postCreate = async function (hookData) {
|
|
|
44
44
|
// post is going to be saved to db, data is what is submitted by user
|
|
45
45
|
const { post, data } = hookData;
|
|
46
46
|
if (Array.isArray(data?.polls) && data.polls.length) {
|
|
47
|
+
await checkMaxOptions(data.polls);
|
|
47
48
|
const savedPolls = await Poll.add(post, data.polls);
|
|
48
49
|
if (savedPolls.length) {
|
|
49
50
|
post.pollIds = JSON.stringify(savedPolls.map(p => String(p.pollId)));
|
|
@@ -73,6 +74,7 @@ Hooks.filter.postEdit = async function (hookData) {
|
|
|
73
74
|
return hookData;
|
|
74
75
|
}
|
|
75
76
|
const pollData = hookData.data.polls || [];
|
|
77
|
+
await checkMaxOptions(pollData);
|
|
76
78
|
const toAdd = pollData.filter(p => !currentPollIds.includes(String(p.pollId)));
|
|
77
79
|
|
|
78
80
|
if (toAdd.length) {
|
|
@@ -134,3 +136,15 @@ async function canCreate(cid, uid) {
|
|
|
134
136
|
}
|
|
135
137
|
}
|
|
136
138
|
|
|
139
|
+
async function checkMaxOptions(polls) {
|
|
140
|
+
if (!Array.isArray(polls) || !polls.length) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const { maxOptions } = await Config.getSettings();
|
|
144
|
+
polls.forEach((poll) => {
|
|
145
|
+
if (poll && poll.options.length > maxOptions) {
|
|
146
|
+
throw new Error(`[[poll:error.max_options, ${maxOptions}]]`);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
package/lib/sockets.js
CHANGED
|
@@ -27,7 +27,8 @@ Sockets.get = async function (socket, data) {
|
|
|
27
27
|
return [];
|
|
28
28
|
}
|
|
29
29
|
return await Promise.all(data.pollIds.map(async (pollId) => {
|
|
30
|
-
const
|
|
30
|
+
const withVotes = allowAnon || socket.uid > 0;
|
|
31
|
+
const pollData = await Poll.get(pollId, socket.uid, withVotes);
|
|
31
32
|
if (!pollData) {
|
|
32
33
|
return null;
|
|
33
34
|
}
|
|
@@ -144,18 +145,20 @@ Sockets.removeVote = async function (socket, data) {
|
|
|
144
145
|
};
|
|
145
146
|
|
|
146
147
|
Sockets.getOptionDetails = async function (socket, data) {
|
|
147
|
-
if (!
|
|
148
|
+
if (!data || isNaN(parseInt(data.pollId, 10)) || isNaN(parseInt(data.optionId, 10))) {
|
|
148
149
|
throw new Error('Invalid request');
|
|
149
150
|
}
|
|
150
|
-
const [poll, option] = await Promise.all([
|
|
151
|
+
const [poll, option, settings] = await Promise.all([
|
|
151
152
|
Poll.getInfo(data.pollId),
|
|
152
153
|
Poll.getOption(data.pollId, data.optionId, true),
|
|
154
|
+
Config.getSettings(),
|
|
153
155
|
]);
|
|
154
156
|
if (!poll) {
|
|
155
157
|
throw new Error('[[error:invalid-data]]');
|
|
156
158
|
}
|
|
159
|
+
const allowAnon = settings.allowGuestsToViewResults === 'on';
|
|
157
160
|
const canRead = await privileges.posts.can('topics:read', poll.pid, socket.uid);
|
|
158
|
-
if (!canRead) {
|
|
161
|
+
if (!canRead || (!allowAnon && socket.uid <= 0)) {
|
|
159
162
|
throw new Error('[[error:no-privileges]]');
|
|
160
163
|
}
|
|
161
164
|
if (!option || !option.votes || !option.votes.length) {
|
package/package.json
CHANGED
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
// used to identify the poll in the manage modal after creation
|
|
65
65
|
// so we can edit/delete
|
|
66
66
|
pollId: Date.now(),
|
|
67
|
+
title: config.poll.defaultTitle,
|
|
67
68
|
maximumVotesPerUser: config.poll.maximumVotesPerUser,
|
|
68
69
|
}, config.poll).then((newPoll) => {
|
|
69
70
|
if (newPoll) {
|
|
@@ -194,9 +195,9 @@
|
|
|
194
195
|
// Add option adder
|
|
195
196
|
modal.find('#pollAddOption')
|
|
196
197
|
.off('click')
|
|
197
|
-
.on('click', async function (
|
|
198
|
-
const
|
|
199
|
-
if (config.maxOptions <=
|
|
198
|
+
.on('click', async function () {
|
|
199
|
+
const optionEls = modal.find('[component="post/poll/option/item"]');
|
|
200
|
+
if (config.maxOptions <= optionEls.length) {
|
|
200
201
|
clearErrors();
|
|
201
202
|
error(`[[poll:error.max_options, ${config.maxOptions}]]`);
|
|
202
203
|
return false;
|
package/public/js/poll/view.js
CHANGED
|
@@ -258,9 +258,9 @@
|
|
|
258
258
|
};
|
|
259
259
|
|
|
260
260
|
View.prototype.showOptionDetails = function (details) {
|
|
261
|
-
require(['
|
|
262
|
-
|
|
263
|
-
|
|
261
|
+
require(['benchpress', 'modals'], function (benchpress, modals) {
|
|
262
|
+
benchpress.render('poll/view/details', details).then(function (html) {
|
|
263
|
+
modals.dialog({
|
|
264
264
|
title: `[[poll:x-users-voted-for-this-option, ${details.voteCount}]]`,
|
|
265
265
|
message: html,
|
|
266
266
|
backdrop: true,
|
|
@@ -301,7 +301,7 @@
|
|
|
301
301
|
}
|
|
302
302
|
} else {
|
|
303
303
|
this.showVoteButton();
|
|
304
|
-
if (this.pollData
|
|
304
|
+
if (this.pollData?.info?.allowAnonVoting) {
|
|
305
305
|
this.showVoteAnonButton();
|
|
306
306
|
}
|
|
307
307
|
}
|