nodebb-plugin-poll
Advanced tools
Comparing version 0.3.7 to 1.1.0
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -10,2 +10,3 @@ { | ||
"max_votes": "Максимальное количество голосов в опросе", | ||
"disallow_vote_update": "Запретить переголосование", | ||
"info_choices": "число, большее 1 позволит с несколькими вариантами опросы.", | ||
@@ -30,5 +31,8 @@ "settings": "Настройки", | ||
"error.not_main": "Опрос доступен при создании темы", | ||
"error.privilege.create": "У вас недостаточно прав для создания опроса", | ||
"vote": "Голосовать", | ||
"update_vote": "Переголосовать", | ||
"remove_vote": "Удалить голос", | ||
"to_voting": "Для голосования", | ||
@@ -38,5 +42,6 @@ "to_results": "Результаты", | ||
"voting_unavailable_message": "Этот опрос завершен или был помечен как удаленный. Вы все еще можете просмотреть результаты.", | ||
"voting_update_disallowed_message": "Переголосование недоступно", | ||
"vote_is_final": "Опрос уже завершен", | ||
"vote_count": "пользователей проголосовали за этот вариант", | ||
"votes": "голосов" | ||
} |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -7,2 +7,4 @@ var NodeBB = require('./nodebb'), | ||
const util = require('util'); | ||
(function(Hooks) { | ||
@@ -17,46 +19,49 @@ | ||
Hooks.filter.postCreate = function(obj, callback) { | ||
Hooks.filter.postCreate = async function (obj) { | ||
if (Serializer.hasMarkup(obj.post.content) && obj.data.isMain) { | ||
savePoll(obj, callback); | ||
return await savePoll(obj); | ||
} else { | ||
return callback(null, obj); | ||
return obj; | ||
} | ||
}; | ||
Hooks.filter.postEdit = function(obj, callback) { | ||
if (obj.post.hasOwnProperty('pollId') || !Serializer.hasMarkup(obj.post.content)) { | ||
return callback(null, obj); | ||
Hooks.filter.postEdit = async function(obj) { | ||
const { tid, pollId } = await NodeBB.Posts.getPostFields(obj.data.pid, ['tid', 'pollId']); | ||
if (pollId || !Serializer.hasMarkup(obj.post.content)) { | ||
return obj; | ||
} | ||
NodeBB.Topics.getTopicFields(obj.post.tid, ['mainPid', 'cid'], function(err, result) { | ||
if (parseInt(result.mainPid, 10) !== parseInt(obj.post.pid, 10)) { | ||
return callback(null, obj); | ||
} | ||
const result = await NodeBB.Topics.getTopicFields(tid, ['mainPid', 'cid']); | ||
if (parseInt(result.mainPid, 10) !== parseInt(obj.data.pid, 10)) { | ||
return obj; | ||
} | ||
canCreate(result.cid, obj.post.editor, function (err, canCreate) { | ||
if (err) { | ||
return callback(err, obj); | ||
} | ||
await canCreate(result.cid, obj.post.editor); | ||
savePoll(obj.post, function (err, postData) { | ||
if (err || !postData.pollId) { | ||
return callback(err, obj); | ||
} | ||
const postData = await savePoll({ | ||
...obj.post, | ||
uid: obj.data.uid, | ||
pid: obj.data.pid, | ||
tid: tid, | ||
}); | ||
delete postData.uid; | ||
delete postData.pid; | ||
delete postData.tid; | ||
obj.post = postData; | ||
// NodeBB only updates the edited, editor and content fields, so we add the pollId field manually. | ||
NodeBB.Posts.setPostField(obj.post.pid, 'pollId', postData.pollId, function () { | ||
callback(null, obj); | ||
}); | ||
}); | ||
}); | ||
}); | ||
if (!postData.pollId) { | ||
return obj; | ||
} | ||
// NodeBB only updates the edited, editor and content fields, so we add the pollId field manually. | ||
await NodeBB.Posts.setPostField(obj.data.pid, 'pollId', postData.pollId); | ||
return obj; | ||
}; | ||
Hooks.filter.topicPost = function(data, callback) { | ||
Hooks.filter.topicPost = async function (data, callback) { | ||
if (Serializer.hasMarkup(data.content)) { | ||
canCreate(data.cid, data.uid, function(err, can) { | ||
return callback(err, data); | ||
}); | ||
await canCreate(data.cid, data.uid); | ||
return data; | ||
} else { | ||
return callback(null, data); | ||
return data; | ||
} | ||
@@ -97,13 +102,10 @@ }; | ||
function canCreate(cid, uid, callback) { | ||
NodeBB.Privileges.categories.can('poll:create', cid, uid, function (err, can) { | ||
if (err || !can) { | ||
return callback(new Error('[[poll:error.privilege.create]]'), can); | ||
} else { | ||
return callback(null, can); | ||
} | ||
}); | ||
async function canCreate(cid, uid) { | ||
const can = await NodeBB.Privileges.categories.can('poll:create', cid, uid); | ||
if (!can) { | ||
throw new Error('[[poll:error.privilege.create]]'); | ||
} | ||
} | ||
function savePoll(obj, callback) { | ||
async function savePoll(obj) { | ||
var postobj = obj.post ? obj.post : obj; | ||
@@ -113,16 +115,13 @@ var pollData = Serializer.serialize(postobj.content, Config.settings.get()); | ||
if (!pollData || !pollData.options.length) { | ||
return callback(null, obj); | ||
return obj; | ||
} | ||
Poll.add(pollData, postobj, function(err, pollId) { | ||
if (err) { | ||
return callback(err, obj); | ||
} | ||
const addPoll = util.promisify(Poll.add); | ||
const pollId = await addPoll(pollData, postobj); | ||
postobj.pollId = pollId; | ||
postobj.content = Serializer.removeMarkup(postobj.content); | ||
postobj.pollId = pollId; | ||
postobj.content = Serializer.removeMarkup(postobj.content); | ||
callback(null, obj); | ||
}); | ||
return obj; | ||
} | ||
})(exports); |
@@ -0,0 +0,0 @@ |
@@ -98,2 +98,3 @@ "use strict"; | ||
}); | ||
pollData.settings.disallowVoteUpdate = parseInt(pollData.settings.disallowVoteUpdate, 10); | ||
callback(null, pollData); | ||
@@ -100,0 +101,0 @@ }); |
@@ -0,0 +0,0 @@ "use strict"; |
"use strict"; | ||
module.exports = require('../public/js/poll/serializer'); |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ "use strict"; |
{ | ||
"name": "nodebb-plugin-poll", | ||
"version": "0.3.7", | ||
"version": "1.1.0", | ||
"description": "NodeBB Poll Plugin", | ||
@@ -29,4 +29,4 @@ "main": "library.js", | ||
"nbbpm": { | ||
"compatibility": "^1.7.0" | ||
"compatibility": "^1.15.0" | ||
} | ||
} |
@@ -51,4 +51,5 @@ { | ||
"upgrades/give_categories_poll_create_privs_to_reg_users.js", | ||
"upgrades/convert_poll_id_voters_and_options_votes_to_sorted_sets.js" | ||
"upgrades/convert_poll_id_voters_and_options_votes_to_sorted_sets.js", | ||
"upgrades/fix_incorrect_poll_privilege.js" | ||
] | ||
} |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -66,43 +66,48 @@ "use strict"; | ||
function composerBtnHandle(composer, textarea) { | ||
var post = composer.posts[composer.active]; | ||
if (!post || !post.isMain || (isNaN(parseInt(post.cid, 10)) && isNaN(parseInt(post.pid, 10)))) { | ||
return app.alertError('[[poll:error.not_main]]'); | ||
} | ||
if (parseInt(post.cid, 10) === 0) { | ||
return app.alertError("[[error:category-not-selected]]"); | ||
} | ||
Poll.sockets.canCreate({cid: post.cid, pid: post.pid}, function(err, canCreate) { | ||
if (err || !canCreate) { | ||
return app.alertError(err.message); | ||
require(['composer/controls'], function(controls) { | ||
var post = composer.posts[composer.active]; | ||
if (!post || !post.isMain || (isNaN(parseInt(post.cid, 10)) && isNaN(parseInt(post.pid, 10)))) { | ||
return app.alertError('[[poll:error.not_main]]'); | ||
} | ||
if (parseInt(post.cid, 10) === 0) { | ||
return app.alertError("[[error:category-not-selected]]"); | ||
} | ||
Poll.sockets.getConfig(null, function(err, config) { | ||
var poll = {}; | ||
Poll.sockets.canCreate({cid: post.cid, pid: post.pid}, function(err, canCreate) { | ||
if (err || !canCreate) { | ||
return app.alertError(err.message); | ||
} | ||
// If there's already a poll in the post, serialize it for editing | ||
if (Poll.serializer.canSerialize(textarea.value)) { | ||
poll = Poll.serializer.serialize(textarea.value, config); | ||
Poll.sockets.getConfig(null, function(err, config) { | ||
var poll = {}; | ||
if (poll.settings.end === 0) { | ||
delete poll.settings.end; | ||
} else { | ||
poll.settings.end = parseInt(poll.settings.end, 10); | ||
// If there's already a poll in the post, serialize it for editing | ||
if (Poll.serializer.canSerialize(textarea.value)) { | ||
poll = Poll.serializer.serialize(textarea.value, config); | ||
if (poll.settings.end === 0) { | ||
delete poll.settings.end; | ||
} else { | ||
poll.settings.end = parseInt(poll.settings.end, 10); | ||
} | ||
} | ||
} | ||
Creator.show(poll, config, function(data) { | ||
// Anything invalid will be discarded by the serializer | ||
var markup = Poll.serializer.deserialize(data, config); | ||
Creator.show(poll, config, function(data) { | ||
// Anything invalid will be discarded by the serializer | ||
var markup = Poll.serializer.deserialize(data, config); | ||
// Remove any existing poll markup | ||
textarea.value = Poll.serializer.removeMarkup(textarea.value); | ||
// Remove any existing poll markup | ||
textarea.value = Poll.serializer.removeMarkup(textarea.value); | ||
// Insert the poll markup at the bottom | ||
if (textarea.value.charAt(textarea.value.length - 1) !== '\n') { | ||
markup = '\n' + markup; | ||
} | ||
// Insert the poll markup at the bottom | ||
if (textarea.value.charAt(textarea.value.length - 1) !== '\n') { | ||
markup = '\n' + markup; | ||
} | ||
if ($.Redactor) textarea.redactor(textarea.value + '<p>' + markup + '</p>'); | ||
else textarea.value += markup | ||
if ($.Redactor) { | ||
textarea.redactor(textarea.value + '<p>' + markup + '</p>'); | ||
} else { | ||
controls.insertIntoTextarea(textarea, markup); | ||
} | ||
}); | ||
}); | ||
@@ -109,0 +114,0 @@ }); |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -6,15 +6,13 @@ "use strict"; | ||
var XRegExp, utils; | ||
var utils; | ||
var Serializer = {}; | ||
if ('undefined' === typeof window) { | ||
XRegExp = require('xregexp'); | ||
utils = require.main.require('./src/utils'); | ||
} else { | ||
XRegExp = window.XRegExp; | ||
utils = window.utils; | ||
} | ||
var pollRegex = XRegExp('(?:(?:\\[poll(?<settings>.*?)\\])\n(?<content>(?:-.+?\n)+)(?:\\[\/poll\\]))', 'g'); | ||
var settingsRegex = XRegExp('(?<key>.+?)=(?:"|")(?<value>.+?)(?:"|")', 'g'); | ||
var pollRegex = /(?:(?:\[poll(?<settings>.*?)\])(?:\\n|\n|<br \/>)(?<content>(?:-.+?(?:\\n|\n|<br \/>))+)(?:\[\/poll\]))/g; | ||
var settingsRegex = /(?<key>.+?)=(?:"|")(?<value>.+?)(?:"|")/g; | ||
var settingsValidators = { | ||
@@ -56,15 +54,17 @@ title: { | ||
Serializer.canSerialize = function(post) { | ||
return XRegExp.exec(post, pollRegex) !== null; | ||
return pollRegex.test(post) !== null; | ||
}; | ||
Serializer.removeMarkup = function(content, replace) { | ||
return XRegExp.replace(content, pollRegex, replace || ''); | ||
return content.replace(pollRegex, replace || ''); | ||
}; | ||
Serializer.hasMarkup = function(content) { | ||
return XRegExp.exec(content, pollRegex) !== null; | ||
const has = pollRegex.test(content) !== null; | ||
return has; | ||
}; | ||
Serializer.serialize = function(post, config) { | ||
var match = XRegExp.exec(post, pollRegex); | ||
pollRegex.lastIndex = 0; | ||
var match = pollRegex.exec(post); | ||
@@ -76,4 +76,4 @@ if (match === null) { | ||
return { | ||
options: serializeOptions(match.content, config), | ||
settings: serializeSettings(match.settings, config) | ||
options: serializeOptions(match.groups.content, config), | ||
settings: serializeSettings(match.groups.settings, config) | ||
}; | ||
@@ -90,4 +90,6 @@ }; | ||
function serializeOptions(raw, config) { | ||
// Depending on composer, the line breaks can either be \n or <br /> so handle both | ||
var pollOptions = []; | ||
var rawOptions = utils.stripHTMLTags(raw).split('\n'); | ||
var rawOptions = raw.split(/(?:\\n|\n|<br \/>)/); | ||
rawOptions.map(raw => utils.stripHTMLTags(raw)); | ||
var maxOptions = parseInt(config.limits.maxOptions, 10); | ||
@@ -135,3 +137,5 @@ | ||
XRegExp.forEach(utils.stripHTMLTags(raw), settingsRegex, function(match) { | ||
const stripped = utils.stripHTMLTags(raw); | ||
let match; | ||
while ((match = settingsRegex.exec(stripped)) !== null) { | ||
var key = match.key.trim(); | ||
@@ -145,3 +149,3 @@ var value = match.value.trim(); | ||
} | ||
}); | ||
} | ||
@@ -148,0 +152,0 @@ return settings; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -344,6 +344,4 @@ "use strict"; | ||
handle: function(view) { | ||
console.log(view.pollData); | ||
Poll.sockets.getConfig(null, function(err, config) { | ||
Poll.creator.show(view.pollData, config, function(data) { | ||
console.log(data); | ||
}); | ||
@@ -350,0 +348,0 @@ }); |
@@ -0,0 +0,0 @@ /*! version : 4.17.37 |
@@ -0,0 +0,0 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ //! moment.js |
@@ -0,0 +0,0 @@ # NodeBB Poll plugin |
@@ -28,3 +28,3 @@ | ||
async.each(cids, function(cid, next) { | ||
NodeBB.Privileges.categories.give(['poll:create'], cid, 'registered-users', next); | ||
NodeBB.Privileges.categories.give(['groups:poll:create'], cid, 'registered-users', next); | ||
}, callback); | ||
@@ -31,0 +31,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
50
3841
3
233029
6