2018-11-19 08:50:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-02-04 21:53:09 -05:00
|
|
|
# name: poll
|
2015-04-23 13:33:29 -04:00
|
|
|
# about: Official poll plugin for Discourse
|
2018-11-19 08:50:00 -05:00
|
|
|
# version: 1.0
|
2015-04-23 13:33:29 -04:00
|
|
|
# authors: Vikhyat Korrapati (vikhyat), Régis Hanol (zogstrip)
|
2015-02-06 18:08:57 -05:00
|
|
|
# url: https://github.com/discourse/discourse/tree/master/plugins/poll
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-05-07 13:49:06 -04:00
|
|
|
register_asset "stylesheets/common/poll.scss"
|
2016-06-13 06:21:14 -04:00
|
|
|
register_asset "stylesheets/common/poll-ui-builder.scss"
|
2015-05-07 13:49:06 -04:00
|
|
|
register_asset "stylesheets/desktop/poll.scss", :desktop
|
|
|
|
register_asset "stylesheets/mobile/poll.scss", :mobile
|
2019-08-13 09:49:40 -04:00
|
|
|
register_asset "stylesheets/mobile/poll-ui-builder.scss", :mobile
|
2015-05-07 13:49:06 -04:00
|
|
|
|
2018-11-26 16:49:57 -05:00
|
|
|
register_svg_icon "far fa-check-square"
|
|
|
|
|
2018-05-07 22:30:33 -04:00
|
|
|
enabled_site_setting :poll_enabled
|
2018-05-15 18:43:09 -04:00
|
|
|
hide_plugin if self.respond_to?(:hide_plugin)
|
2018-05-07 22:30:33 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
PLUGIN_NAME ||= "discourse_poll"
|
|
|
|
DATA_PREFIX ||= "data-poll-"
|
2015-09-14 13:27:54 -04:00
|
|
|
|
2014-02-04 21:53:09 -05:00
|
|
|
after_initialize do
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
[
|
|
|
|
"../app/models/poll_vote",
|
|
|
|
"../app/models/poll_option",
|
|
|
|
"../app/models/poll",
|
|
|
|
"../app/serializers/poll_option_serializer",
|
|
|
|
"../app/serializers/poll_serializer",
|
|
|
|
"../lib/polls_validator",
|
|
|
|
"../lib/polls_updater",
|
|
|
|
"../lib/post_validator",
|
|
|
|
"../jobs/regular/close_poll",
|
|
|
|
].each { |path| require File.expand_path(path, __FILE__) }
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
module ::DiscoursePoll
|
2018-11-19 08:50:00 -05:00
|
|
|
HAS_POLLS ||= "has_polls"
|
|
|
|
DEFAULT_POLL_NAME ||= "poll"
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2014-02-04 21:53:09 -05:00
|
|
|
class Engine < ::Rails::Engine
|
2015-04-23 13:33:29 -04:00
|
|
|
engine_name PLUGIN_NAME
|
|
|
|
isolate_namespace DiscoursePoll
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
class DiscoursePoll::Poll
|
|
|
|
class << self
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2016-07-29 02:02:48 -04:00
|
|
|
def vote(post_id, poll_name, options, user)
|
2018-11-19 08:50:00 -05:00
|
|
|
Poll.transaction do
|
2015-04-29 17:34:40 -04:00
|
|
|
post = Post.find_by(id: post_id)
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-06-01 16:31:47 -04:00
|
|
|
# post must not be deleted
|
|
|
|
if post.nil? || post.trashed?
|
|
|
|
raise StandardError.new I18n.t("poll.post_is_deleted")
|
|
|
|
end
|
|
|
|
|
2016-03-21 07:12:25 -04:00
|
|
|
# topic must not be archived
|
2018-11-19 08:50:00 -05:00
|
|
|
if post.topic&.archived
|
2015-04-29 17:34:40 -04:00
|
|
|
raise StandardError.new I18n.t("poll.topic_must_be_open_to_vote")
|
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2018-02-26 18:19:44 -05:00
|
|
|
# user must be allowed to post in topic
|
2020-03-20 14:29:00 -04:00
|
|
|
guardian = Guardian.new(user)
|
|
|
|
if !guardian.can_create_post?(post.topic)
|
2018-02-26 18:19:44 -05:00
|
|
|
raise StandardError.new I18n.t("poll.user_cant_post_in_topic")
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = Poll.includes(poll_options: :poll_votes).find_by(post_id: post_id, name: poll_name)
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
raise StandardError.new I18n.t("poll.no_poll_with_this_name", name: poll_name) unless poll
|
|
|
|
raise StandardError.new I18n.t("poll.poll_must_be_open_to_vote") if poll.is_closed?
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2020-01-28 07:30:04 -05:00
|
|
|
if poll.groups
|
|
|
|
poll_groups = poll.groups.split(",").map(&:downcase)
|
|
|
|
user_groups = user.groups.map { |g| g.name.downcase }
|
|
|
|
if (poll_groups & user_groups).empty?
|
2020-02-17 06:46:14 -05:00
|
|
|
raise StandardError.new I18n.t("js.poll.results.groups.title", groups: poll.groups)
|
2020-01-28 07:30:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-01 10:33:24 -04:00
|
|
|
# remove options that aren't available in the poll
|
2018-11-19 08:50:00 -05:00
|
|
|
available_options = poll.poll_options.map { |o| o.digest }.to_set
|
2015-05-01 10:33:24 -04:00
|
|
|
options.select! { |o| available_options.include?(o) }
|
|
|
|
|
|
|
|
raise StandardError.new I18n.t("poll.requires_at_least_1_valid_option") if options.empty?
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
new_option_ids = poll.poll_options.each_with_object([]) do |option, obj|
|
|
|
|
obj << option.id if options.include?(option.digest)
|
2015-11-30 05:23:38 -05:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
# remove non-selected votes
|
|
|
|
PollVote
|
|
|
|
.where(poll: poll, user: user)
|
|
|
|
.where.not(poll_option_id: new_option_ids)
|
|
|
|
.delete_all
|
2016-06-07 06:55:01 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
old_option_ids = poll.poll_options.each_with_object([]) do |option, obj|
|
|
|
|
if option.poll_votes.any? { |v| v.user_id == user.id }
|
|
|
|
obj << option.id
|
2016-06-07 06:55:01 -04:00
|
|
|
end
|
2016-04-15 17:26:18 -04:00
|
|
|
end
|
2015-12-02 06:33:34 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
# create missing votes
|
|
|
|
(new_option_ids - old_option_ids).each do |option_id|
|
|
|
|
PollVote.create!(poll: poll, user: user, poll_option_id: option_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
poll.reload
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2020-03-20 14:29:00 -04:00
|
|
|
serialized_poll = PollSerializer.new(poll, root: false, scope: guardian).as_json
|
2018-11-19 08:50:00 -05:00
|
|
|
payload = { post_id: post_id, polls: [serialized_poll] }
|
2016-06-07 06:55:01 -04:00
|
|
|
|
2018-12-05 15:27:49 -05:00
|
|
|
post.publish_message!("/polls/#{post.topic_id}", payload)
|
2015-04-29 17:34:40 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
[serialized_poll, options]
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
2015-04-29 17:34:40 -04:00
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2018-12-17 09:28:57 -05:00
|
|
|
def toggle_status(post_id, poll_name, status, user, raise_errors = true)
|
2018-11-19 08:50:00 -05:00
|
|
|
Poll.transaction do
|
2015-04-29 17:34:40 -04:00
|
|
|
post = Post.find_by(id: post_id)
|
2020-03-20 14:29:00 -04:00
|
|
|
guardian = Guardian.new(user)
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-06-01 16:31:47 -04:00
|
|
|
# post must not be deleted
|
|
|
|
if post.nil? || post.trashed?
|
2018-12-17 09:28:57 -05:00
|
|
|
raise StandardError.new I18n.t("poll.post_is_deleted") if raise_errors
|
|
|
|
return
|
2015-04-29 17:34:40 -04:00
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2016-03-21 07:12:25 -04:00
|
|
|
# topic must not be archived
|
2018-11-19 08:50:00 -05:00
|
|
|
if post.topic&.archived
|
2018-12-17 09:28:57 -05:00
|
|
|
raise StandardError.new I18n.t("poll.topic_must_be_open_to_toggle_status") if raise_errors
|
|
|
|
return
|
2015-04-29 17:34:40 -04:00
|
|
|
end
|
2014-03-24 00:05:39 -04:00
|
|
|
|
2015-06-01 16:31:47 -04:00
|
|
|
# either staff member or OP
|
2018-11-19 08:50:00 -05:00
|
|
|
unless post.user_id == user&.id || user&.staff?
|
2018-12-17 09:28:57 -05:00
|
|
|
raise StandardError.new I18n.t("poll.only_staff_or_op_can_toggle_status") if raise_errors
|
|
|
|
return
|
2015-06-01 16:31:47 -04:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = Poll.find_by(post_id: post_id, name: poll_name)
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2018-12-17 09:28:57 -05:00
|
|
|
if !poll
|
|
|
|
raise StandardError.new I18n.t("poll.no_poll_with_this_name", name: poll_name) if raise_errors
|
|
|
|
return
|
|
|
|
end
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
poll.status = status
|
|
|
|
poll.save!
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2020-03-20 14:29:00 -04:00
|
|
|
serialized_poll = PollSerializer.new(poll, root: false, scope: guardian).as_json
|
2018-11-19 08:50:00 -05:00
|
|
|
payload = { post_id: post_id, polls: [serialized_poll] }
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2018-12-05 15:27:49 -05:00
|
|
|
post.publish_message!("/polls/#{post.topic_id}", payload)
|
2015-04-29 17:34:40 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
serialized_poll
|
2014-04-12 13:23:21 -04:00
|
|
|
end
|
2015-04-29 17:34:40 -04:00
|
|
|
end
|
2015-05-02 17:44:45 -04:00
|
|
|
|
2019-02-27 11:00:21 -05:00
|
|
|
def serialized_voters(poll, opts = {})
|
2018-11-19 08:50:00 -05:00
|
|
|
limit = (opts["limit"] || 25).to_i
|
|
|
|
limit = 0 if limit < 0
|
|
|
|
limit = 50 if limit > 50
|
|
|
|
|
|
|
|
page = (opts["page"] || 1).to_i
|
|
|
|
page = 1 if page < 1
|
|
|
|
|
|
|
|
offset = (page - 1) * limit
|
|
|
|
|
|
|
|
option_digest = opts["option_id"].to_s
|
|
|
|
|
|
|
|
if poll.number?
|
|
|
|
user_ids = PollVote
|
|
|
|
.where(poll: poll)
|
|
|
|
.group(:user_id)
|
|
|
|
.order("MIN(created_at)")
|
|
|
|
.offset(offset)
|
|
|
|
.limit(limit)
|
|
|
|
.pluck(:user_id)
|
|
|
|
|
|
|
|
result = User.where(id: user_ids).map { |u| UserNameSerializer.new(u).serializable_hash }
|
|
|
|
elsif option_digest.present?
|
|
|
|
poll_option = PollOption.find_by(poll: poll, digest: option_digest)
|
|
|
|
|
2019-11-25 12:51:01 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:option_id) unless poll_option
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
user_ids = PollVote
|
|
|
|
.where(poll: poll, poll_option: poll_option)
|
|
|
|
.group(:user_id)
|
|
|
|
.order("MIN(created_at)")
|
|
|
|
.offset(offset)
|
|
|
|
.limit(limit)
|
|
|
|
.pluck(:user_id)
|
|
|
|
|
|
|
|
user_hashes = User.where(id: user_ids).map { |u| UserNameSerializer.new(u).serializable_hash }
|
|
|
|
|
|
|
|
result = { option_digest => user_hashes }
|
|
|
|
else
|
|
|
|
votes = DB.query <<~SQL
|
|
|
|
SELECT digest, user_id
|
|
|
|
FROM (
|
|
|
|
SELECT digest
|
|
|
|
, user_id
|
|
|
|
, ROW_NUMBER() OVER (PARTITION BY poll_option_id ORDER BY pv.created_at) AS row
|
|
|
|
FROM poll_votes pv
|
|
|
|
JOIN poll_options po ON pv.poll_option_id = po.id
|
|
|
|
WHERE pv.poll_id = #{poll.id}
|
|
|
|
AND po.poll_id = #{poll.id}
|
|
|
|
) v
|
|
|
|
WHERE row BETWEEN #{offset} AND #{offset + limit}
|
|
|
|
SQL
|
|
|
|
|
2019-02-27 11:00:21 -05:00
|
|
|
user_ids = votes.map(&:user_id).uniq
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
user_hashes = User
|
|
|
|
.where(id: user_ids)
|
|
|
|
.map { |u| [u.id, UserNameSerializer.new(u).serializable_hash] }
|
|
|
|
.to_h
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
votes.each do |v|
|
|
|
|
result[v.digest] ||= []
|
|
|
|
result[v.digest] << user_hashes[v.user_id]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2019-02-27 11:00:21 -05:00
|
|
|
def voters(post_id, poll_name, user, opts = {})
|
|
|
|
post = Post.find_by(id: post_id)
|
2019-11-25 12:51:01 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:post_id) unless post
|
2019-02-27 11:00:21 -05:00
|
|
|
|
|
|
|
poll = Poll.find_by(post_id: post_id, name: poll_name)
|
2019-11-25 12:51:01 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:poll_name) unless poll&.can_see_voters?(user)
|
2019-02-27 11:00:21 -05:00
|
|
|
|
|
|
|
serialized_voters(poll, opts)
|
|
|
|
end
|
|
|
|
|
2019-11-25 12:51:01 -05:00
|
|
|
def transform_for_user_field_override(custom_user_field)
|
|
|
|
existing_field = UserField.find_by(name: custom_user_field)
|
|
|
|
existing_field ? "user_field_#{existing_field.id}" : custom_user_field
|
|
|
|
end
|
|
|
|
|
|
|
|
def grouped_poll_results(post_id, poll_name, user_field_name, user)
|
|
|
|
post = Post.find_by(id: post_id)
|
|
|
|
raise Discourse::InvalidParameters.new(:post_id) unless post
|
|
|
|
|
|
|
|
poll = Poll.includes(:poll_options).includes(:poll_votes).find_by(post_id: post_id, name: poll_name)
|
|
|
|
raise Discourse::InvalidParameters.new(:poll_name) unless poll
|
|
|
|
|
|
|
|
raise Discourse::InvalidParameters.new(:user_field_name) unless SiteSetting.poll_groupable_user_fields.split('|').include?(user_field_name)
|
|
|
|
|
|
|
|
poll_votes = poll.poll_votes
|
|
|
|
|
|
|
|
poll_options = {}
|
|
|
|
poll.poll_options.each do |option|
|
|
|
|
poll_options[option.id.to_s] = { html: option.html, digest: option.digest }
|
|
|
|
end
|
|
|
|
|
|
|
|
user_ids = poll_votes.map(&:user_id).uniq
|
|
|
|
user_fields = UserCustomField.where(user_id: user_ids, name: transform_for_user_field_override(user_field_name))
|
|
|
|
|
|
|
|
user_field_map = {}
|
|
|
|
user_fields.each do |f|
|
|
|
|
# Build hash, so we can quickly look up field values for each user.
|
|
|
|
user_field_map[f.user_id] = f.value
|
|
|
|
end
|
|
|
|
|
|
|
|
votes_with_field = poll_votes.map do |vote|
|
|
|
|
v = vote.attributes
|
|
|
|
v[:field_value] = user_field_map[vote.user_id]
|
|
|
|
v
|
|
|
|
end
|
|
|
|
|
|
|
|
chart_data = []
|
|
|
|
votes_with_field.group_by { |vote| vote[:field_value] }.each do |field_answer, votes|
|
|
|
|
grouped_selected_options = {}
|
|
|
|
|
|
|
|
# Create all the options with 0 votes. This ensures all the charts will have the same order of options, and same colors per option.
|
|
|
|
poll_options.each do |id, option|
|
|
|
|
grouped_selected_options[id] = {
|
|
|
|
digest: option[:digest],
|
|
|
|
html: option[:html],
|
|
|
|
votes: 0
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Now go back and update the vote counts. Using hashes so we dont have n^2
|
|
|
|
votes.group_by { |v| v["poll_option_id"] }.each do |option_id, votes_for_option|
|
|
|
|
grouped_selected_options[option_id.to_s][:votes] = votes_for_option.length
|
|
|
|
end
|
|
|
|
|
|
|
|
group_label = field_answer ? field_answer.titleize : I18n.t("poll.user_field.no_data")
|
|
|
|
chart_data << { group: group_label, options: grouped_selected_options.values }
|
|
|
|
end
|
|
|
|
chart_data
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
def schedule_jobs(post)
|
|
|
|
Poll.where(post: post).find_each do |poll|
|
2018-12-13 03:39:36 -05:00
|
|
|
job_args = {
|
|
|
|
post_id: post.id,
|
|
|
|
poll_name: poll.name
|
|
|
|
}
|
|
|
|
|
|
|
|
Jobs.cancel_scheduled_job(:close_poll, job_args)
|
2018-05-06 23:38:04 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
if poll.open? && poll.close_at && poll.close_at > Time.zone.now
|
2018-12-13 03:39:36 -05:00
|
|
|
Jobs.enqueue_at(poll.close_at, :close_poll, job_args)
|
2018-05-02 20:12:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
def create!(post_id, poll)
|
|
|
|
close_at = begin
|
|
|
|
Time.zone.parse(poll["close"] || '')
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
|
|
|
|
|
|
|
created_poll = Poll.create!(
|
|
|
|
post_id: post_id,
|
|
|
|
name: poll["name"].presence || "poll",
|
|
|
|
close_at: close_at,
|
|
|
|
type: poll["type"].presence || "regular",
|
|
|
|
status: poll["status"].presence || "open",
|
|
|
|
visibility: poll["public"] == "true" ? "everyone" : "secret",
|
|
|
|
results: poll["results"].presence || "always",
|
|
|
|
min: poll["min"],
|
|
|
|
max: poll["max"],
|
2019-11-25 12:51:01 -05:00
|
|
|
step: poll["step"],
|
2020-01-28 07:30:04 -05:00
|
|
|
chart_type: poll["charttype"] || "bar",
|
|
|
|
groups: poll["groups"]
|
2018-11-19 08:50:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
poll["options"].each do |option|
|
|
|
|
PollOption.create!(
|
|
|
|
poll: created_poll,
|
|
|
|
digest: option["id"].presence,
|
2019-11-13 06:28:14 -05:00
|
|
|
html: option["html"].presence&.strip
|
2018-11-19 08:50:00 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-07 03:52:56 -04:00
|
|
|
def extract(raw, topic_id, user_id = nil)
|
2015-05-02 17:44:45 -04:00
|
|
|
# TODO: we should fix the callback mess so that the cooked version is available
|
|
|
|
# in the validators instead of cooking twice
|
2016-07-07 03:52:56 -04:00
|
|
|
cooked = PrettyText.cook(raw, topic_id: topic_id, user_id: user_id)
|
2015-05-02 17:44:45 -04:00
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
Nokogiri::HTML(cooked).css("div.poll").map do |p|
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = { "options" => [], "name" => DiscoursePoll::DEFAULT_POLL_NAME }
|
2015-05-02 17:44:45 -04:00
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
# attributes
|
2015-05-02 17:44:45 -04:00
|
|
|
p.attributes.values.each do |attribute|
|
|
|
|
if attribute.name.start_with?(DATA_PREFIX)
|
2017-07-25 13:38:04 -04:00
|
|
|
poll[attribute.name[DATA_PREFIX.length..-1]] = CGI.escapeHTML(attribute.value || "")
|
2015-05-02 17:44:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
# options
|
2015-05-02 17:44:45 -04:00
|
|
|
p.css("li[#{DATA_PREFIX}option-id]").each do |o|
|
2018-11-19 08:50:00 -05:00
|
|
|
option_id = o.attributes[DATA_PREFIX + "option-id"].value.to_s
|
|
|
|
poll["options"] << { "id" => option_id, "html" => o.inner_html.strip }
|
2015-05-02 17:44:45 -04:00
|
|
|
end
|
|
|
|
|
2018-05-02 20:12:19 -04:00
|
|
|
poll
|
2015-05-02 17:44:45 -04:00
|
|
|
end
|
|
|
|
end
|
2015-04-29 17:34:40 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
class DiscoursePoll::PollsController < ::ApplicationController
|
|
|
|
requires_plugin PLUGIN_NAME
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2019-11-25 12:51:01 -05:00
|
|
|
before_action :ensure_logged_in, except: [:voters, :grouped_poll_results]
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
def vote
|
|
|
|
post_id = params.require(:post_id)
|
|
|
|
poll_name = params.require(:poll_name)
|
|
|
|
options = params.require(:options)
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
begin
|
2016-07-29 02:02:48 -04:00
|
|
|
poll, options = DiscoursePoll::Poll.vote(post_id, poll_name, options, current_user)
|
2015-04-29 17:34:40 -04:00
|
|
|
render json: { poll: poll, vote: options }
|
|
|
|
rescue StandardError => e
|
|
|
|
render_json_error e.message
|
|
|
|
end
|
|
|
|
end
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
def toggle_status
|
|
|
|
post_id = params.require(:post_id)
|
|
|
|
poll_name = params.require(:poll_name)
|
|
|
|
status = params.require(:status)
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
begin
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = DiscoursePoll::Poll.toggle_status(post_id, poll_name, status, current_user)
|
2015-04-29 17:34:40 -04:00
|
|
|
render json: { poll: poll }
|
|
|
|
rescue StandardError => e
|
|
|
|
render_json_error e.message
|
2014-04-12 13:23:21 -04:00
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
2016-06-09 09:33:17 -04:00
|
|
|
|
|
|
|
def voters
|
2018-11-19 08:50:00 -05:00
|
|
|
post_id = params.require(:post_id)
|
2017-01-27 04:09:33 -05:00
|
|
|
poll_name = params.require(:poll_name)
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
opts = params.permit(:limit, :page, :option_id)
|
2017-01-27 04:09:33 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
begin
|
|
|
|
render json: { voters: DiscoursePoll::Poll.voters(post_id, poll_name, current_user, opts) }
|
|
|
|
rescue StandardError => e
|
|
|
|
render_json_error e.message
|
2016-06-09 09:33:17 -04:00
|
|
|
end
|
|
|
|
end
|
2019-11-25 12:51:01 -05:00
|
|
|
|
|
|
|
def grouped_poll_results
|
|
|
|
post_id = params.require(:post_id)
|
|
|
|
poll_name = params.require(:poll_name)
|
|
|
|
user_field_name = params.require(:user_field_name)
|
|
|
|
|
|
|
|
begin
|
|
|
|
render json: {
|
|
|
|
grouped_results: DiscoursePoll::Poll.grouped_poll_results(post_id, poll_name, user_field_name, current_user)
|
|
|
|
}
|
|
|
|
rescue StandardError => e
|
|
|
|
render_json_error e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def groupable_user_fields
|
|
|
|
render json: {
|
|
|
|
fields: SiteSetting.poll_groupable_user_fields.split('|').map do |field|
|
|
|
|
{ name: field.humanize.capitalize, value: field }
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
DiscoursePoll::Engine.routes.draw do
|
|
|
|
put "/vote" => "polls#vote"
|
|
|
|
put "/toggle_status" => "polls#toggle_status"
|
2016-06-09 09:33:17 -04:00
|
|
|
get "/voters" => 'polls#voters'
|
2019-11-25 12:51:01 -05:00
|
|
|
get "/grouped_poll_results" => 'polls#grouped_poll_results'
|
|
|
|
get "/groupable_user_fields" => 'polls#groupable_user_fields'
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Discourse::Application.routes.append do
|
2015-04-23 13:33:29 -04:00
|
|
|
mount ::DiscoursePoll::Engine, at: "/polls"
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Post.class_eval do
|
2018-11-19 08:50:00 -05:00
|
|
|
attr_accessor :extracted_polls
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
has_many :polls, dependent: :destroy
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
after_save do
|
|
|
|
polls = self.extracted_polls
|
|
|
|
next if polls.blank? || !polls.is_a?(Hash)
|
2015-04-23 13:33:29 -04:00
|
|
|
post = self
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
Poll.transaction do
|
|
|
|
polls.values.each do |poll|
|
|
|
|
DiscoursePoll::Poll.create!(post.id, poll)
|
|
|
|
end
|
|
|
|
post.custom_fields[DiscoursePoll::HAS_POLLS] = true
|
2015-04-25 18:12:19 -04:00
|
|
|
post.save_custom_fields(true)
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 05:45:53 -05:00
|
|
|
User.class_eval do
|
|
|
|
has_many :poll_votes, dependent: :delete_all
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
validate(:post, :validate_polls) do |force = nil|
|
2017-01-26 00:29:43 -05:00
|
|
|
return unless self.raw_changed? || force
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2016-06-06 11:04:56 -04:00
|
|
|
validator = DiscoursePoll::PollsValidator.new(self)
|
|
|
|
return unless (polls = validator.validate_polls)
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
if polls.present?
|
2017-12-05 12:07:21 -05:00
|
|
|
validator = DiscoursePoll::PostValidator.new(self)
|
|
|
|
return unless validator.validate_post
|
|
|
|
end
|
|
|
|
|
2015-04-25 18:12:19 -04:00
|
|
|
# are we updating a post?
|
|
|
|
if self.id.present?
|
2018-11-19 08:50:00 -05:00
|
|
|
DiscoursePoll::PollsUpdater.update(self, polls)
|
2015-04-23 13:33:29 -04:00
|
|
|
else
|
2018-11-19 08:50:00 -05:00
|
|
|
self.extracted_polls = polls
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
2015-04-25 18:12:19 -04:00
|
|
|
|
|
|
|
true
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
2019-08-29 10:56:46 -04:00
|
|
|
allow_new_queued_post_payload_attribute("is_poll")
|
2019-08-14 14:02:59 -04:00
|
|
|
|
2017-01-26 00:29:43 -05:00
|
|
|
NewPostManager.add_handler(1) do |manager|
|
|
|
|
post = Post.new(raw: manager.args[:raw])
|
|
|
|
|
|
|
|
if !DiscoursePoll::PollsValidator.new(post).validate_polls
|
|
|
|
result = NewPostResult.new(:poll, false)
|
|
|
|
|
|
|
|
post.errors.full_messages.each do |message|
|
|
|
|
result.errors[:base] << message
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
else
|
|
|
|
manager.args["is_poll"] = true
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
on(:approved_post) do |queued_post, created_post|
|
2019-01-03 12:03:01 -05:00
|
|
|
if queued_post.payload["is_poll"]
|
2017-01-26 00:29:43 -05:00
|
|
|
created_post.validate_polls(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-13 13:24:39 -04:00
|
|
|
on(:reduce_cooked) do |fragment, post|
|
|
|
|
if post.nil? || post.trashed?
|
|
|
|
fragment.css(".poll, [data-poll-name]").each(&:remove)
|
|
|
|
else
|
2018-05-02 20:12:19 -04:00
|
|
|
post_url = post.full_url
|
2016-04-13 13:24:39 -04:00
|
|
|
fragment.css(".poll, [data-poll-name]").each do |poll|
|
|
|
|
poll.replace "<p><a href='#{post_url}'>#{I18n.t("poll.email.link_to_poll")}</a></p>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-29 11:05:52 -04:00
|
|
|
on(:reduce_excerpt) do |doc, options|
|
|
|
|
post = options[:post]
|
2019-05-29 18:02:33 -04:00
|
|
|
|
2019-05-29 18:05:53 -04:00
|
|
|
replacement = post&.url.present? ?
|
2019-05-29 18:02:33 -04:00
|
|
|
"<a href='#{UrlHelper.escape_uri(post.url)}'>#{I18n.t("poll.poll")}</a>" :
|
2019-05-29 18:05:53 -04:00
|
|
|
I18n.t("poll.poll")
|
2019-05-29 18:02:33 -04:00
|
|
|
|
2019-05-29 11:05:52 -04:00
|
|
|
doc.css("div.poll").each do |poll|
|
2019-05-29 18:02:33 -04:00
|
|
|
poll.replace(replacement)
|
2019-05-29 11:05:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-20 14:29:00 -04:00
|
|
|
on(:post_created) do |post, _opts, user|
|
|
|
|
guardian = Guardian.new(user)
|
2018-05-02 20:12:19 -04:00
|
|
|
DiscoursePoll::Poll.schedule_jobs(post)
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
unless post.is_first_post?
|
2020-03-20 14:29:00 -04:00
|
|
|
polls = ActiveModel::ArraySerializer.new(post.polls, each_serializer: PollSerializer, root: false, scope: guardian).as_json
|
2018-12-05 15:27:49 -05:00
|
|
|
post.publish_message!("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
|
2018-11-19 08:50:00 -05:00
|
|
|
end
|
2015-04-27 12:59:29 -04:00
|
|
|
end
|
|
|
|
|
2018-02-28 16:21:52 -05:00
|
|
|
on(:merging_users) do |source_user, target_user|
|
2018-11-19 08:50:00 -05:00
|
|
|
PollVote.where(user_id: source_user.id).update_all(user_id: target_user.id)
|
2018-02-28 16:21:52 -05:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
register_post_custom_field_type(DiscoursePoll::HAS_POLLS, :boolean)
|
2017-01-27 04:09:33 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
topic_view_post_custom_fields_whitelister { [DiscoursePoll::HAS_POLLS] }
|
|
|
|
|
|
|
|
add_to_class(:topic_view, :polls) do
|
|
|
|
@polls ||= begin
|
|
|
|
polls = {}
|
|
|
|
|
|
|
|
post_with_polls = @post_custom_fields.each_with_object([]) do |fields, obj|
|
|
|
|
obj << fields[0] if fields[1][DiscoursePoll::HAS_POLLS]
|
|
|
|
end
|
|
|
|
|
|
|
|
if post_with_polls.present?
|
|
|
|
Poll
|
|
|
|
.includes(poll_options: :poll_votes, poll_votes: :poll_option)
|
|
|
|
.where(post_id: post_with_polls)
|
|
|
|
.each do |p|
|
|
|
|
polls[p.post_id] ||= []
|
|
|
|
polls[p.post_id] << p
|
|
|
|
end
|
2017-01-27 04:09:33 -05:00
|
|
|
end
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
polls
|
2017-01-27 04:09:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
add_to_serializer(:post, :preloaded_polls, false) do
|
|
|
|
@preloaded_polls ||= if @topic_view.present?
|
|
|
|
@topic_view.polls[object.id]
|
|
|
|
else
|
|
|
|
Poll.includes(poll_options: :poll_votes).where(post: object)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:post, :include_preloaded_polls?) do
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:post, :polls, false) do
|
2020-03-20 14:29:00 -04:00
|
|
|
preloaded_polls.map { |p| PollSerializer.new(p, root: false, scope: self.scope) }
|
2018-11-19 08:50:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:post, :include_polls?) do
|
2018-11-20 11:28:41 -05:00
|
|
|
SiteSetting.poll_enabled && preloaded_polls.present?
|
2018-11-19 08:50:00 -05:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2016-06-07 06:55:01 -04:00
|
|
|
add_to_serializer(:post, :polls_votes, false) do
|
2018-11-19 08:50:00 -05:00
|
|
|
preloaded_polls.map do |poll|
|
|
|
|
user_poll_votes = poll.poll_votes.each_with_object([]) do |vote, obj|
|
|
|
|
if vote.user_id == scope.user.id
|
|
|
|
obj << vote.poll_option.digest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[poll.name, user_poll_votes]
|
|
|
|
end.to_h
|
2016-06-07 06:55:01 -04:00
|
|
|
end
|
|
|
|
|
2015-10-16 11:18:22 -04:00
|
|
|
add_to_serializer(:post, :include_polls_votes?) do
|
2018-11-20 11:28:41 -05:00
|
|
|
SiteSetting.poll_enabled &&
|
2018-11-19 08:50:00 -05:00
|
|
|
scope.user&.id.present? &&
|
|
|
|
preloaded_polls.present? &&
|
|
|
|
preloaded_polls.any? { |p| p.has_voted?(scope.user) }
|
2015-10-16 11:18:22 -04:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|