2014-02-05 10:53:09 +08:00
|
|
|
# name: poll
|
2015-04-24 01:33:29 +08:00
|
|
|
# about: Official poll plugin for Discourse
|
|
|
|
# version: 0.9
|
|
|
|
# authors: Vikhyat Korrapati (vikhyat), Régis Hanol (zogstrip)
|
2015-02-07 07:08:57 +08:00
|
|
|
# url: https://github.com/discourse/discourse/tree/master/plugins/poll
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-05-08 01:49:06 +08:00
|
|
|
register_asset "stylesheets/common/poll.scss"
|
2016-06-13 18:21:14 +08:00
|
|
|
register_asset "stylesheets/common/poll-ui-builder.scss"
|
2015-05-08 01:49:06 +08:00
|
|
|
register_asset "stylesheets/desktop/poll.scss", :desktop
|
|
|
|
register_asset "stylesheets/mobile/poll.scss", :mobile
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
PLUGIN_NAME ||= "discourse_poll".freeze
|
2015-09-15 01:27:54 +08:00
|
|
|
DATA_PREFIX ||= "data-poll-".freeze
|
|
|
|
|
2014-02-05 10:53:09 +08:00
|
|
|
after_initialize do
|
2018-05-03 08:12:19 +08:00
|
|
|
|
|
|
|
require File.expand_path("../jobs/regular/close_poll", __FILE__)
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
module ::DiscoursePoll
|
2018-05-03 08:12:19 +08:00
|
|
|
DEFAULT_POLL_NAME ||= "poll".freeze
|
2016-06-06 23:04:56 +08:00
|
|
|
POLLS_CUSTOM_FIELD ||= "polls".freeze
|
|
|
|
VOTES_CUSTOM_FIELD ||= "polls-votes".freeze
|
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
autoload :PostValidator, "#{Rails.root}/plugins/poll/lib/post_validator"
|
2016-06-06 23:04:56 +08:00
|
|
|
autoload :PollsValidator, "#{Rails.root}/plugins/poll/lib/polls_validator"
|
2018-05-03 08:12:19 +08:00
|
|
|
autoload :PollsUpdater, "#{Rails.root}/plugins/poll/lib/polls_updater"
|
|
|
|
autoload :VotesUpdater, "#{Rails.root}/plugins/poll/lib/votes_updater"
|
2016-06-06 23:04:56 +08:00
|
|
|
|
2014-02-05 10:53:09 +08:00
|
|
|
class Engine < ::Rails::Engine
|
2015-04-24 01:33:29 +08:00
|
|
|
engine_name PLUGIN_NAME
|
|
|
|
isolate_namespace DiscoursePoll
|
2014-02-05 10:53:09 +08:00
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
class DiscoursePoll::Poll
|
|
|
|
class << self
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2016-07-29 14:02:48 +08:00
|
|
|
def vote(post_id, poll_name, options, user)
|
2015-04-30 05:34:40 +08:00
|
|
|
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post_id}") do
|
2016-07-29 14:02:48 +08:00
|
|
|
user_id = user.id
|
2015-04-30 05:34:40 +08:00
|
|
|
post = Post.find_by(id: post_id)
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-06-02 04:31:47 +08:00
|
|
|
# post must not be deleted
|
|
|
|
if post.nil? || post.trashed?
|
|
|
|
raise StandardError.new I18n.t("poll.post_is_deleted")
|
|
|
|
end
|
|
|
|
|
2016-03-21 19:12:25 +08:00
|
|
|
# topic must not be archived
|
|
|
|
if post.topic.try(:archived)
|
2015-04-30 05:34:40 +08:00
|
|
|
raise StandardError.new I18n.t("poll.topic_must_be_open_to_vote")
|
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2018-02-27 07:19:44 +08:00
|
|
|
# user must be allowed to post in topic
|
|
|
|
unless Guardian.new(user).can_create_post?(post.topic)
|
|
|
|
raise StandardError.new I18n.t("poll.user_cant_post_in_topic")
|
|
|
|
end
|
|
|
|
|
2016-06-06 23:04:56 +08:00
|
|
|
polls = post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD]
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
raise StandardError.new I18n.t("poll.no_polls_associated_with_this_post") if polls.blank?
|
|
|
|
|
|
|
|
poll = polls[poll_name]
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
raise StandardError.new I18n.t("poll.no_poll_with_this_name", name: poll_name) if poll.blank?
|
|
|
|
raise StandardError.new I18n.t("poll.poll_must_be_open_to_vote") if poll["status"] != "open"
|
2018-05-03 08:12:19 +08:00
|
|
|
|
|
|
|
# ensure no race condition when poll is automatically closed
|
|
|
|
if poll["close"].present?
|
|
|
|
close_date = Time.zone.parse(poll["close"]) rescue nil
|
|
|
|
raise StandardError.new I18n.t("poll.poll_must_be_open_to_vote") if close_date && close_date <= Time.zone.now
|
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-05-01 22:33:24 +08:00
|
|
|
# remove options that aren't available in the poll
|
|
|
|
available_options = poll["options"].map { |o| o["id"] }.to_set
|
|
|
|
options.select! { |o| available_options.include?(o) }
|
|
|
|
|
|
|
|
raise StandardError.new I18n.t("poll.requires_at_least_1_valid_option") if options.empty?
|
|
|
|
|
2016-04-16 05:26:18 +08:00
|
|
|
poll["voters"] = poll["anonymous_voters"] || 0
|
2015-10-16 23:18:22 +08:00
|
|
|
all_options = Hash.new(0)
|
2015-11-30 18:23:38 +08:00
|
|
|
|
2016-07-29 14:28:09 +08:00
|
|
|
post.custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD] ||= {}
|
|
|
|
post.custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD]["#{user_id}"] ||= {}
|
|
|
|
post.custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD]["#{user_id}"][poll_name] = options
|
2015-11-30 18:23:38 +08:00
|
|
|
|
2016-07-29 14:28:09 +08:00
|
|
|
post.custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD].each do |_, user_votes|
|
2015-12-02 19:33:34 +08:00
|
|
|
next unless votes = user_votes[poll_name]
|
|
|
|
votes.each { |option| all_options[option] += 1 }
|
2015-12-03 00:25:01 +08:00
|
|
|
poll["voters"] += 1 if (available_options & votes.to_set).size > 0
|
2015-11-30 18:23:38 +08:00
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
public_poll = (poll["public"] == "true")
|
|
|
|
|
2016-04-16 05:26:18 +08:00
|
|
|
poll["options"].each do |option|
|
|
|
|
anonymous_votes = option["anonymous_votes"] || 0
|
|
|
|
option["votes"] = all_options[option["id"]] + anonymous_votes
|
2016-06-07 18:55:01 +08:00
|
|
|
|
|
|
|
if public_poll
|
|
|
|
option["voter_ids"] ||= []
|
|
|
|
|
|
|
|
if options.include?(option["id"])
|
|
|
|
option["voter_ids"] << user_id if !option["voter_ids"].include?(user_id)
|
|
|
|
else
|
|
|
|
option["voter_ids"].delete(user_id)
|
|
|
|
end
|
|
|
|
end
|
2016-04-16 05:26:18 +08:00
|
|
|
end
|
2015-12-02 19:33:34 +08:00
|
|
|
|
2016-06-06 23:04:56 +08:00
|
|
|
post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD] = polls
|
2015-04-30 05:34:40 +08:00
|
|
|
post.save_custom_fields(true)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-06-07 18:55:01 +08:00
|
|
|
payload = { post_id: post_id, polls: polls }
|
2018-05-03 08:12:19 +08:00
|
|
|
payload.merge!(user: UserNameSerializer.new(user).serializable_hash) if public_poll
|
2016-06-07 18:55:01 +08:00
|
|
|
|
|
|
|
MessageBus.publish("/polls/#{post.topic_id}", payload)
|
2015-04-30 05:34:40 +08:00
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
[poll, options]
|
2014-02-05 10:53:09 +08:00
|
|
|
end
|
2015-04-30 05:34:40 +08:00
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
def toggle_status(post_id, poll_name, status, user_id)
|
|
|
|
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post_id}") do
|
|
|
|
post = Post.find_by(id: post_id)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-06-02 04:31:47 +08:00
|
|
|
# post must not be deleted
|
|
|
|
if post.nil? || post.trashed?
|
|
|
|
raise StandardError.new I18n.t("poll.post_is_deleted")
|
2015-04-30 05:34:40 +08:00
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2016-03-21 19:12:25 +08:00
|
|
|
# topic must not be archived
|
|
|
|
if post.topic.try(:archived)
|
2015-04-30 05:34:40 +08:00
|
|
|
raise StandardError.new I18n.t("poll.topic_must_be_open_to_toggle_status")
|
|
|
|
end
|
2014-03-24 12:05:39 +08:00
|
|
|
|
2015-06-02 04:31:47 +08:00
|
|
|
user = User.find_by(id: user_id)
|
|
|
|
|
|
|
|
# either staff member or OP
|
|
|
|
unless user_id == post.user_id || user.try(:staff?)
|
|
|
|
raise StandardError.new I18n.t("poll.only_staff_or_op_can_toggle_status")
|
|
|
|
end
|
|
|
|
|
2016-06-06 23:04:56 +08:00
|
|
|
polls = post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD]
|
2014-04-13 01:23:21 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
raise StandardError.new I18n.t("poll.no_polls_associated_with_this_post") if polls.blank?
|
|
|
|
raise StandardError.new I18n.t("poll.no_poll_with_this_name", name: poll_name) if polls[poll_name].blank?
|
2014-04-13 01:23:21 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
polls[poll_name]["status"] = status
|
2014-04-13 01:23:21 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
post.save_custom_fields(true)
|
2014-04-13 01:23:21 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
MessageBus.publish("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
|
2015-04-30 05:34:40 +08:00
|
|
|
|
|
|
|
polls[poll_name]
|
2014-04-13 01:23:21 +08:00
|
|
|
end
|
2015-04-30 05:34:40 +08:00
|
|
|
end
|
2015-05-03 05:44:45 +08:00
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
def schedule_jobs(post)
|
|
|
|
post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD].each do |name, poll|
|
|
|
|
Jobs.cancel_scheduled_job(:close_poll, post_id: post.id, poll_name: name)
|
|
|
|
|
|
|
|
if poll["status"] == "open" && poll["close"].present?
|
|
|
|
close_date = Time.zone.parse(poll["close"]) rescue nil
|
|
|
|
Jobs.enqueue_at(close_date, :close_poll, post_id: post.id, poll_name: name) if close_date && close_date > Time.zone.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-07 15:52:56 +08:00
|
|
|
def extract(raw, topic_id, user_id = nil)
|
2015-05-03 05:44:45 +08: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 15:52:56 +08:00
|
|
|
cooked = PrettyText.cook(raw, topic_id: topic_id, user_id: user_id)
|
2015-05-03 05:44:45 +08:00
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
Nokogiri::HTML(cooked).css("div.poll").map do |p|
|
2015-05-04 22:01:57 +08:00
|
|
|
poll = { "options" => [], "voters" => 0 }
|
2015-05-03 05:44:45 +08:00
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
# attributes
|
2015-05-03 05:44:45 +08:00
|
|
|
p.attributes.values.each do |attribute|
|
|
|
|
if attribute.name.start_with?(DATA_PREFIX)
|
2017-07-26 01:38:04 +08:00
|
|
|
poll[attribute.name[DATA_PREFIX.length..-1]] = CGI.escapeHTML(attribute.value || "")
|
2015-05-03 05:44:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
# options
|
2015-05-03 05:44:45 +08:00
|
|
|
p.css("li[#{DATA_PREFIX}option-id]").each do |o|
|
2017-07-26 01:38:04 +08:00
|
|
|
option_id = o.attributes[DATA_PREFIX + "option-id"].value || ""
|
|
|
|
poll["options"] << { "id" => option_id, "html" => o.inner_html, "votes" => 0 }
|
2015-05-03 05:44:45 +08:00
|
|
|
end
|
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
poll
|
2015-05-03 05:44:45 +08:00
|
|
|
end
|
|
|
|
end
|
2015-04-30 05:34:40 +08:00
|
|
|
end
|
|
|
|
end
|
2014-04-13 01:23:21 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
require_dependency "application_controller"
|
2015-09-15 01:27:54 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
class DiscoursePoll::PollsController < ::ApplicationController
|
|
|
|
requires_plugin PLUGIN_NAME
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
before_action :ensure_logged_in, except: [:voters]
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
def vote
|
|
|
|
post_id = params.require(:post_id)
|
|
|
|
poll_name = params.require(:poll_name)
|
|
|
|
options = params.require(:options)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
begin
|
2016-07-29 14:02:48 +08:00
|
|
|
poll, options = DiscoursePoll::Poll.vote(post_id, poll_name, options, current_user)
|
2015-04-30 05:34:40 +08:00
|
|
|
render json: { poll: poll, vote: options }
|
|
|
|
rescue StandardError => e
|
|
|
|
render_json_error e.message
|
|
|
|
end
|
|
|
|
end
|
2014-04-13 01:23:21 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
def toggle_status
|
|
|
|
post_id = params.require(:post_id)
|
|
|
|
poll_name = params.require(:poll_name)
|
|
|
|
status = params.require(:status)
|
|
|
|
user_id = current_user.id
|
2014-04-13 01:23:21 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
begin
|
|
|
|
poll = DiscoursePoll::Poll.toggle_status(post_id, poll_name, status, user_id)
|
|
|
|
render json: { poll: poll }
|
|
|
|
rescue StandardError => e
|
|
|
|
render_json_error e.message
|
2014-04-13 01:23:21 +08:00
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
end
|
2016-06-09 21:33:17 +08:00
|
|
|
|
|
|
|
def voters
|
2017-01-27 17:09:33 +08:00
|
|
|
post_id = params.require(:post_id)
|
|
|
|
poll_name = params.require(:poll_name)
|
|
|
|
|
|
|
|
post = Post.find_by(id: post_id)
|
|
|
|
raise Discourse::InvalidParameters.new("post_id is invalid") if !post
|
|
|
|
|
|
|
|
poll = post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD][poll_name]
|
|
|
|
raise Discourse::InvalidParameters.new("poll_name is invalid") if !poll
|
|
|
|
|
2017-08-15 03:33:47 +08:00
|
|
|
voter_limit = (params[:voter_limit] || 25).to_i
|
2018-05-03 08:12:19 +08:00
|
|
|
voter_limit = 0 if voter_limit < 0
|
2017-08-15 03:33:47 +08:00
|
|
|
voter_limit = 50 if voter_limit > 50
|
|
|
|
|
2017-01-27 17:09:33 +08:00
|
|
|
user_ids = []
|
|
|
|
options = poll["options"]
|
|
|
|
|
|
|
|
if poll["type"] != "number"
|
2017-08-15 03:33:47 +08:00
|
|
|
per_option_voters = {}
|
|
|
|
|
2017-01-27 17:09:33 +08:00
|
|
|
options.each do |option|
|
|
|
|
if (params[:option_id])
|
|
|
|
next unless option["id"] == params[:option_id].to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
next unless option["voter_ids"]
|
2017-08-15 03:33:47 +08:00
|
|
|
voters = option["voter_ids"].slice((params[:offset].to_i || 0) * voter_limit, voter_limit)
|
|
|
|
per_option_voters[option["id"]] = Set.new(voters)
|
|
|
|
user_ids << voters
|
2017-01-27 17:09:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
user_ids.flatten!
|
|
|
|
user_ids.uniq!
|
|
|
|
|
|
|
|
poll_votes = post.custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD]
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
2018-05-03 08:12:19 +08:00
|
|
|
User.where(id: user_ids).each do |user|
|
2017-01-27 17:09:33 +08:00
|
|
|
user_hash = UserNameSerializer.new(user).serializable_hash
|
2016-06-09 21:33:17 +08:00
|
|
|
|
2017-01-27 17:09:33 +08:00
|
|
|
poll_votes[user.id.to_s][poll_name].each do |option_id|
|
|
|
|
if (params[:option_id])
|
|
|
|
next unless option_id == params[:option_id].to_s
|
|
|
|
end
|
|
|
|
|
2017-08-15 03:33:47 +08:00
|
|
|
voters = per_option_voters[option_id]
|
|
|
|
# we may have a user from a different vote
|
|
|
|
next unless voters.include?(user.id)
|
|
|
|
|
2017-01-27 17:09:33 +08:00
|
|
|
result[option_id] ||= []
|
|
|
|
result[option_id] << user_hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
user_ids = options.map { |option| option["voter_ids"] }.sort!
|
|
|
|
user_ids.flatten!
|
|
|
|
user_ids.uniq!
|
2017-08-15 03:33:47 +08:00
|
|
|
user_ids = user_ids.slice((params[:offset].to_i || 0) * voter_limit, voter_limit)
|
2018-05-03 08:12:19 +08:00
|
|
|
result = User.where(id: user_ids).map { |user| UserNameSerializer.new(user).serializable_hash }
|
2016-06-09 21:33:17 +08:00
|
|
|
end
|
|
|
|
|
2017-01-27 17:09:33 +08:00
|
|
|
render json: { poll_name => result }
|
2016-06-09 21:33:17 +08:00
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
end
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
DiscoursePoll::Engine.routes.draw do
|
|
|
|
put "/vote" => "polls#vote"
|
|
|
|
put "/toggle_status" => "polls#toggle_status"
|
2016-06-09 21:33:17 +08:00
|
|
|
get "/voters" => 'polls#voters'
|
2014-02-05 10:53:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
Discourse::Application.routes.append do
|
2015-04-24 01:33:29 +08:00
|
|
|
mount ::DiscoursePoll::Engine, at: "/polls"
|
2014-02-05 10:53:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
Post.class_eval do
|
2015-04-24 01:33:29 +08:00
|
|
|
attr_accessor :polls
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
after_save do
|
|
|
|
next if self.polls.blank? || !self.polls.is_a?(Hash)
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
post = self
|
|
|
|
polls = self.polls
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post.id}") do
|
2016-06-06 23:04:56 +08:00
|
|
|
post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD] = polls
|
2015-04-26 06:12:19 +08:00
|
|
|
post.save_custom_fields(true)
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
validate(:post, :validate_polls) do |force = nil|
|
2015-04-24 01:33:29 +08:00
|
|
|
# only care when raw has changed!
|
2017-01-26 13:29:43 +08:00
|
|
|
return unless self.raw_changed? || force
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2016-06-06 23:04:56 +08:00
|
|
|
validator = DiscoursePoll::PollsValidator.new(self)
|
|
|
|
return unless (polls = validator.validate_polls)
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2017-12-06 01:07:21 +08:00
|
|
|
if !polls.empty?
|
|
|
|
validator = DiscoursePoll::PostValidator.new(self)
|
|
|
|
return unless validator.validate_post
|
|
|
|
end
|
|
|
|
|
2015-04-26 06:12:19 +08:00
|
|
|
# are we updating a post?
|
|
|
|
if self.id.present?
|
2016-06-06 23:04:56 +08:00
|
|
|
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{self.id}") do
|
|
|
|
DiscoursePoll::PollsUpdater.update(self, polls)
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
self.polls = polls
|
|
|
|
end
|
2015-04-26 06:12:19 +08:00
|
|
|
|
|
|
|
true
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
|
2017-01-26 13:29:43 +08: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|
|
|
|
|
if queued_post.post_options["is_poll"]
|
|
|
|
created_post.validate_polls(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-12 10:22:44 +08:00
|
|
|
register_post_custom_field_type(DiscoursePoll::POLLS_CUSTOM_FIELD, :json)
|
|
|
|
register_post_custom_field_type(DiscoursePoll::VOTES_CUSTOM_FIELD, :json)
|
2014-05-23 13:00:55 +08:00
|
|
|
|
2017-08-12 10:22:44 +08:00
|
|
|
topic_view_post_custom_fields_whitelister do |user|
|
2016-06-06 23:04:56 +08:00
|
|
|
user ? [DiscoursePoll::POLLS_CUSTOM_FIELD, DiscoursePoll::VOTES_CUSTOM_FIELD] : [DiscoursePoll::POLLS_CUSTOM_FIELD]
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
2014-08-23 08:50:45 +08:00
|
|
|
|
2016-04-14 01:24:39 +08:00
|
|
|
on(:reduce_cooked) do |fragment, post|
|
|
|
|
if post.nil? || post.trashed?
|
|
|
|
fragment.css(".poll, [data-poll-name]").each(&:remove)
|
|
|
|
else
|
2018-05-03 08:12:19 +08:00
|
|
|
post_url = post.full_url
|
2016-04-14 01:24:39 +08: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
|
|
|
|
|
2015-04-28 00:59:29 +08:00
|
|
|
on(:post_created) do |post|
|
2016-06-06 23:04:56 +08:00
|
|
|
next if post.is_first_post? || post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD].blank?
|
2018-05-03 08:12:19 +08:00
|
|
|
# signals the front-end we have polls for that post
|
|
|
|
MessageBus.publish("/polls/#{post.topic_id}", post_id: post.id, polls: post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD])
|
|
|
|
# schedule automatic close jobs
|
|
|
|
DiscoursePoll::Poll.schedule_jobs(post)
|
2015-04-28 00:59:29 +08:00
|
|
|
end
|
|
|
|
|
2018-03-01 05:21:52 +08:00
|
|
|
on(:merging_users) do |source_user, target_user|
|
|
|
|
DiscoursePoll::VotesUpdater.merge_users!(source_user, target_user)
|
|
|
|
end
|
|
|
|
|
2017-01-27 17:09:33 +08:00
|
|
|
add_to_serializer(:post, :polls, false) do
|
|
|
|
polls = post_custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD].dup
|
|
|
|
|
|
|
|
polls.each do |_, poll|
|
|
|
|
poll["options"].each do |option|
|
|
|
|
option.delete("voter_ids")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-06 23:04:56 +08:00
|
|
|
add_to_serializer(:post, :include_polls?) { post_custom_fields.present? && post_custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD].present? }
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2016-06-07 18:55:01 +08:00
|
|
|
add_to_serializer(:post, :polls_votes, false) do
|
|
|
|
post_custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD]["#{scope.user.id}"]
|
|
|
|
end
|
|
|
|
|
2015-10-16 23:18:22 +08:00
|
|
|
add_to_serializer(:post, :include_polls_votes?) do
|
|
|
|
return unless scope.user
|
|
|
|
return unless post_custom_fields.present?
|
2016-06-06 23:04:56 +08:00
|
|
|
return unless post_custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD].present?
|
|
|
|
post_custom_fields[DiscoursePoll::VOTES_CUSTOM_FIELD].has_key?("#{scope.user.id}")
|
2015-10-16 23:18:22 +08:00
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|