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 +42 -39
- package/lib/hooks.js +93 -111
- package/lib/poll.js +218 -304
- package/lib/scheduler.js +45 -56
- package/lib/sockets.js +160 -249
- package/lib/vote.js +65 -120
- package/library.js +8 -11
- package/package.json +1 -1
- package/plugin.json +0 -7
- package/public/js/poll/creator.js +1 -1
- package/public/js/poll/serializer.js +1 -1
- package/public/scss/style.scss +0 -25
- package/templates/poll/view.tpl +5 -6
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
Hooks.action = {};
|
|
11
|
+
Hooks.filter = {};
|
|
12
|
+
Hooks.action = {};
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
Hooks.filter.parseRaw = function (raw) {
|
|
15
|
+
return Serializer.removeMarkup(raw, '[Poll]');
|
|
16
|
+
};
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
27
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
123
|
-
|
|
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
|
-
|
|
127
|
-
|
|
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
|
-
|
|
130
|
-
|
|
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
|
-
|
|
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
|
+
|