nodebb-plugin-poll 3.1.8 → 3.2.1

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/config.js CHANGED
@@ -7,42 +7,45 @@ const pluginInfo = require('../plugin.json');
7
7
 
8
8
  const pluginId = pluginInfo.id.replace('nodebb-plugin-', '');
9
9
 
10
- (function (Config) {
11
- Config.plugin = {
12
- name: pluginInfo.name,
13
- id: pluginId,
14
- version: packageInfo.version,
15
- description: packageInfo.description,
16
- icon: 'fa-bar-chart-o',
17
- };
18
-
19
- Config.defaults = {
20
- toggles: {
21
- allowAnon: false,
22
- },
23
- limits: {
24
- maxOptions: 10,
25
- },
26
- defaults: {
27
- title: 'Poll',
28
- maxvotes: 1,
29
- disallowVoteUpdate: 0,
30
- end: 0,
31
- },
32
- };
33
-
34
- Config.settings = {};
35
-
36
- Config.init = function (callback) {
37
- Config.settings = new NodeBB.Settings(Config.plugin.id, Config.plugin.version, Config.defaults, callback);
38
- };
39
-
40
- Config.adminSockets = {
41
- sync: function () {
42
- Config.settings.sync();
43
- },
44
- getDefaults: function (socket, data, callback) {
45
- callback(null, Config.settings.createDefaultWrapper());
46
- },
47
- };
48
- }(exports));
10
+ const Config = exports;
11
+
12
+ Config.plugin = {
13
+ name: pluginInfo.name,
14
+ id: pluginId,
15
+ version: packageInfo.version,
16
+ description: packageInfo.description,
17
+ icon: 'fa-bar-chart-o',
18
+ };
19
+
20
+ Config.defaults = {
21
+ toggles: {
22
+ allowAnon: false,
23
+ },
24
+ limits: {
25
+ maxOptions: 10,
26
+ },
27
+ defaults: {
28
+ title: 'Poll',
29
+ maxvotes: 1,
30
+ disallowVoteUpdate: 0,
31
+ end: 0,
32
+ },
33
+ };
34
+
35
+ Config.settings = {};
36
+
37
+ Config.init = function () {
38
+ return new Promise((resolve) => {
39
+ Config.settings = new NodeBB.Settings(Config.plugin.id, Config.plugin.version, Config.defaults, resolve);
40
+ });
41
+ };
42
+
43
+ Config.adminSockets = {
44
+ sync: function () {
45
+ Config.settings.sync();
46
+ },
47
+ getDefaults: function (socket, data, callback) {
48
+ callback(null, Config.settings.createDefaultWrapper());
49
+ },
50
+ };
51
+
package/lib/hooks.js CHANGED
@@ -1,134 +1,116 @@
1
1
  'use strict';
2
2
 
3
- const util = require('util');
4
3
  const NodeBB = require('./nodebb');
5
4
 
6
5
  const Config = require('./config');
7
6
  const Poll = require('./poll');
8
7
  const Serializer = require('./serializer');
9
8
 
9
+ const Hooks = exports;
10
10
 
11
- (function (Hooks) {
12
- Hooks.filter = {};
13
- Hooks.action = {};
11
+ Hooks.filter = {};
12
+ Hooks.action = {};
14
13
 
15
- Hooks.filter.parseRaw = function (raw, callback) {
16
- callback(null, Serializer.removeMarkup(raw, '[Poll]'));
17
- };
14
+ Hooks.filter.parseRaw = function (raw) {
15
+ return Serializer.removeMarkup(raw, '[Poll]');
16
+ };
18
17
 
19
- Hooks.filter.postCreate = async function (obj) {
20
- if (Serializer.hasMarkup(obj.post.content) && obj.data.isMain) {
21
- return await savePoll(obj);
22
- }
18
+ Hooks.filter.postCreate = async function (obj) {
19
+ if (Serializer.hasMarkup(obj.post.content) && obj.data.isMain) {
20
+ return await savePoll(obj);
21
+ }
22
+ return obj;
23
+ };
24
+
25
+ Hooks.filter.postEdit = async function (obj) {
26
+ const { tid, pollId } = await NodeBB.Posts.getPostFields(obj.data.pid, ['tid', 'pollId']);
27
+ if (pollId || !Serializer.hasMarkup(obj.post.content)) {
23
28
  return obj;
24
- };
25
-
26
- Hooks.filter.postEdit = async function (obj) {
27
- const { tid, pollId } = await NodeBB.Posts.getPostFields(obj.data.pid, ['tid', 'pollId']);
28
- if (pollId || !Serializer.hasMarkup(obj.post.content)) {
29
- return obj;
30
- }
31
-
32
- const result = await NodeBB.Topics.getTopicFields(tid, ['mainPid', 'cid']);
33
- if (parseInt(result.mainPid, 10) !== parseInt(obj.data.pid, 10)) {
34
- return obj;
35
- }
36
-
37
- await canCreate(result.cid, obj.post.editor);
38
-
39
- const postData = await savePoll({
40
- ...obj.post,
41
- uid: obj.data.uid,
42
- pid: obj.data.pid,
43
- tid: tid,
44
- });
45
- delete postData.uid;
46
- delete postData.pid;
47
- delete postData.tid;
48
- obj.post = postData;
49
-
50
- if (!postData.pollId) {
51
- return obj;
52
- }
53
-
54
- // NodeBB only updates the edited, editor and content fields, so we add the pollId field manually.
55
- await NodeBB.Posts.setPostField(obj.data.pid, 'pollId', postData.pollId);
29
+ }
30
+
31
+ const result = await NodeBB.Topics.getTopicFields(tid, ['mainPid', 'cid']);
32
+ if (parseInt(result.mainPid, 10) !== parseInt(obj.data.pid, 10)) {
56
33
  return obj;
57
- };
34
+ }
35
+
36
+ await canCreate(result.cid, obj.post.editor);
37
+
38
+ const postData = await savePoll({
39
+ ...obj.post,
40
+ uid: obj.data.uid,
41
+ pid: obj.data.pid,
42
+ tid: tid,
43
+ });
44
+ delete postData.uid;
45
+ delete postData.pid;
46
+ delete postData.tid;
47
+ obj.post = postData;
48
+
49
+ if (!postData.pollId) {
50
+ return obj;
51
+ }
52
+
53
+ // NodeBB only updates the edited, editor and content fields, so we add the pollId field manually.
54
+ await NodeBB.Posts.setPostField(obj.data.pid, 'pollId', postData.pollId);
55
+ return obj;
56
+ };
58
57
 
59
- Hooks.filter.topicPost = async function (data) {
60
- if (Serializer.hasMarkup(data.content)) {
61
- await canCreate(data.cid, data.uid);
62
- return data;
63
- }
58
+ Hooks.filter.topicPost = async function (data) {
59
+ if (Serializer.hasMarkup(data.content)) {
60
+ await canCreate(data.cid, data.uid);
64
61
  return data;
65
- };
66
-
67
- Hooks.action.postDelete = function (data) {
68
- Poll.getPollIdByPid(data.post.pid, (err, pollId) => {
69
- if (err) {
70
- console.error(err);
71
- }
72
- if (pollId) {
73
- Poll.delete(pollId);
74
- }
75
- });
76
- };
77
-
78
- Hooks.action.postRestore = function (data) {
79
- Poll.getPollIdByPid(data.post.pid, (err, pollId) => {
80
- if (err) {
81
- console.error(err);
82
- }
83
- if (pollId) {
84
- Poll.restore(pollId);
85
- }
86
- });
87
- };
88
-
89
- Hooks.action.topicDelete = function (data) {
90
- Poll.getPollIdByTid(data.topic.tid, (err, pollId) => {
91
- if (err) {
92
- console.error(err);
93
- }
94
- if (pollId) {
95
- Poll.delete(pollId);
96
- }
97
- });
98
- };
99
-
100
- Hooks.action.topicRestore = function (data) {
101
- Poll.getPollIdByTid(data.topic.tid, (err, pollId) => {
102
- if (err) {
103
- console.error(err);
104
- }
105
- if (pollId) {
106
- Poll.restore(pollId);
107
- }
108
- });
109
- };
110
-
111
- async function canCreate(cid, uid) {
112
- const can = await NodeBB.Privileges.categories.can('poll:create', cid, uid);
113
- if (!can) {
114
- throw new Error('[[poll:error.privilege.create]]');
115
- }
116
62
  }
63
+ return data;
64
+ };
117
65
 
118
- async function savePoll(obj) {
119
- const postobj = obj.post ? obj.post : obj;
120
- const pollData = Serializer.serialize(postobj.content, Config.settings.get());
66
+ Hooks.action.postDelete = async function (data) {
67
+ const pollId = await Poll.getPollIdByPid(data.post.pid);
68
+ if (pollId) {
69
+ await Poll.delete(pollId);
70
+ }
71
+ };
72
+
73
+ Hooks.action.postRestore = async function (data) {
74
+ const pollId = await Poll.getPollIdByPid(data.post.pid);
75
+ if (pollId) {
76
+ await Poll.restore(pollId);
77
+ }
78
+ };
121
79
 
122
- if (!pollData || !pollData.options.length) {
123
- return obj;
124
- }
80
+ Hooks.action.topicDelete = async function (data) {
81
+ const pollId = await Poll.getPollIdByTid(data.topic.tid);
82
+ if (pollId) {
83
+ Poll.delete(pollId);
84
+ }
85
+ };
125
86
 
126
- const addPoll = util.promisify(Poll.add);
127
- const pollId = await addPoll(pollData, postobj);
87
+ Hooks.action.topicRestore = async function (data) {
88
+ const pollId = await Poll.getPollIdByTid(data.topic.tid);
89
+ if (pollId) {
90
+ Poll.restore(pollId);
91
+ }
92
+ };
128
93
 
129
- postobj.pollId = pollId;
130
- postobj.content = Serializer.removeMarkup(postobj.content);
94
+ async function canCreate(cid, uid) {
95
+ const can = await NodeBB.Privileges.categories.can('poll:create', cid, uid);
96
+ if (!can) {
97
+ throw new Error('[[poll:error.privilege.create]]');
98
+ }
99
+ }
131
100
 
101
+ async function savePoll(obj) {
102
+ const postobj = obj.post ? obj.post : obj;
103
+ const pollData = Serializer.serialize(postobj.content, Config.settings.get());
104
+
105
+ if (!pollData || !pollData.options.length) {
132
106
  return obj;
133
107
  }
134
- }(exports));
108
+
109
+ const pollId = await Poll.add(pollData, postobj);
110
+
111
+ postobj.pollId = pollId;
112
+ postobj.content = Serializer.removeMarkup(postobj.content);
113
+
114
+ return obj;
115
+ }
116
+