nodebb-plugin-poll 4.0.13 → 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 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-poll",
3
- "version": "4.0.13",
3
+ "version": "4.0.14",
4
4
  "description": "NodeBB Poll Plugin",
5
5
  "main": "library.js",
6
6
  "scripts": {
@@ -195,9 +195,9 @@
195
195
  // Add option adder
196
196
  modal.find('#pollAddOption')
197
197
  .off('click')
198
- .on('click', async function (e) {
199
- const el = $(e.currentTarget);
200
- if (config.maxOptions <= el.prevAll('input').length) {
198
+ .on('click', async function () {
199
+ const optionEls = modal.find('[component="post/poll/option/item"]');
200
+ if (config.maxOptions <= optionEls.length) {
201
201
  clearErrors();
202
202
  error(`[[poll:error.max_options, ${config.maxOptions}]]`);
203
203
  return false;