2014-02-04 21:53:09 -05:00
|
|
|
# name: poll
|
2015-04-23 13:33:29 -04:00
|
|
|
# about: Official poll plugin for Discourse
|
|
|
|
# version: 0.9
|
|
|
|
# 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-07-02 12:45:17 -04:00
|
|
|
enabled_site_setting :poll_enabled
|
|
|
|
|
2015-05-07 13:49:06 -04:00
|
|
|
register_asset "stylesheets/common/poll.scss"
|
|
|
|
register_asset "stylesheets/desktop/poll.scss", :desktop
|
|
|
|
register_asset "stylesheets/mobile/poll.scss", :mobile
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
register_asset "javascripts/poll_dialect.js", :server_side
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
PLUGIN_NAME ||= "discourse_poll".freeze
|
|
|
|
|
|
|
|
POLLS_CUSTOM_FIELD ||= "polls".freeze
|
|
|
|
VOTES_CUSTOM_FIELD ||= "polls-votes".freeze
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-09-14 13:27:54 -04:00
|
|
|
DATA_PREFIX ||= "data-poll-".freeze
|
|
|
|
DEFAULT_POLL_NAME ||= "poll".freeze
|
|
|
|
|
2014-02-04 21:53:09 -05:00
|
|
|
after_initialize do
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-10-22 13:10:07 -04:00
|
|
|
# turn polls into a link in emails
|
|
|
|
Email::Styles.register_plugin_style do |fragment, opts|
|
|
|
|
post = Post.find_by(id: opts[:post_id]) rescue nil
|
|
|
|
if post.nil? || post.trashed?
|
|
|
|
fragment.css(".poll").each(&:remove)
|
|
|
|
else
|
|
|
|
post_url = "#{Discourse.base_url}#{post.url}"
|
|
|
|
fragment.css(".poll").each do |poll|
|
|
|
|
poll.replace "<p><a href='#{post_url}'>#{I18n.t("poll.email.link_to_poll")}</a></p>"
|
|
|
|
end
|
|
|
|
end
|
2015-04-25 18:37:27 -04:00
|
|
|
end
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
module ::DiscoursePoll
|
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
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
def vote(post_id, poll_name, options, user_id)
|
|
|
|
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post_id}") do
|
|
|
|
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
|
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
# topic must be open
|
|
|
|
if post.topic.try(:closed) || post.topic.try(:archived)
|
|
|
|
raise StandardError.new I18n.t("poll.topic_must_be_open_to_vote")
|
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
polls = post.custom_fields[POLLS_CUSTOM_FIELD]
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
raise StandardError.new I18n.t("poll.no_polls_associated_with_this_post") if polls.blank?
|
|
|
|
|
|
|
|
poll = polls[poll_name]
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-29 17:34:40 -04: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"
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-05-01 10:33:24 -04: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?
|
|
|
|
|
2015-11-30 05:23:38 -05:00
|
|
|
poll["voters"] = 0
|
2015-10-16 11:18:22 -04:00
|
|
|
all_options = Hash.new(0)
|
2015-11-30 05:23:38 -05:00
|
|
|
|
2015-12-02 06:33:34 -05:00
|
|
|
post.custom_fields[VOTES_CUSTOM_FIELD] ||= {}
|
|
|
|
post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"] ||= {}
|
|
|
|
post.custom_fields[VOTES_CUSTOM_FIELD]["#{user_id}"][poll_name] = options
|
2015-11-30 05:23:38 -05:00
|
|
|
|
2015-12-02 06:33:34 -05:00
|
|
|
post.custom_fields[VOTES_CUSTOM_FIELD].each do |user_id, user_votes|
|
|
|
|
next unless votes = user_votes[poll_name]
|
|
|
|
votes.each { |option| all_options[option] += 1 }
|
2015-12-02 11:25:01 -05:00
|
|
|
poll["voters"] += 1 if (available_options & votes.to_set).size > 0
|
2015-11-30 05:23:38 -05:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-12-02 06:33:34 -05:00
|
|
|
poll["options"].each { |o| o["votes"] = all_options[o["id"]] }
|
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
post.custom_fields[POLLS_CUSTOM_FIELD] = polls
|
|
|
|
post.save_custom_fields(true)
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-05-03 22:21:00 -04:00
|
|
|
MessageBus.publish("/polls/#{post_id}", { polls: polls })
|
2015-04-29 17:34:40 -04:00
|
|
|
|
|
|
|
return [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
|
|
|
|
2015-04-29 17:34:40 -04: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-23 13:33:29 -04: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")
|
2015-04-29 17:34:40 -04:00
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
# topic must be open
|
|
|
|
if post.topic.try(:closed) || post.topic.try(:archived)
|
|
|
|
raise StandardError.new I18n.t("poll.topic_must_be_open_to_toggle_status")
|
|
|
|
end
|
2014-03-24 00:05:39 -04:00
|
|
|
|
2015-06-01 16:31:47 -04: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
|
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
polls = post.custom_fields[POLLS_CUSTOM_FIELD]
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2015-04-29 17:34:40 -04: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-12 13:23:21 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
polls[poll_name]["status"] = status
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
post.save_custom_fields(true)
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2015-05-03 22:21:00 -04:00
|
|
|
MessageBus.publish("/polls/#{post_id}", { polls: polls })
|
2015-04-29 17:34:40 -04:00
|
|
|
|
|
|
|
polls[poll_name]
|
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
|
|
|
|
|
|
|
def extract(raw, topic_id)
|
|
|
|
# TODO: we should fix the callback mess so that the cooked version is available
|
|
|
|
# in the validators instead of cooking twice
|
|
|
|
cooked = PrettyText.cook(raw, topic_id: topic_id)
|
|
|
|
parsed = Nokogiri::HTML(cooked)
|
|
|
|
|
|
|
|
extracted_polls = []
|
|
|
|
|
|
|
|
# extract polls
|
|
|
|
parsed.css("div.poll").each do |p|
|
2015-05-04 10:01:57 -04:00
|
|
|
poll = { "options" => [], "voters" => 0 }
|
2015-05-02 17:44:45 -04:00
|
|
|
|
|
|
|
# extract attributes
|
|
|
|
p.attributes.values.each do |attribute|
|
|
|
|
if attribute.name.start_with?(DATA_PREFIX)
|
|
|
|
poll[attribute.name[DATA_PREFIX.length..-1]] = attribute.value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# extract options
|
|
|
|
p.css("li[#{DATA_PREFIX}option-id]").each do |o|
|
|
|
|
option_id = o.attributes[DATA_PREFIX + "option-id"].value
|
|
|
|
poll["options"] << { "id" => option_id, "html" => o.inner_html, "votes" => 0 }
|
|
|
|
end
|
|
|
|
|
|
|
|
# add the poll
|
|
|
|
extracted_polls << poll
|
|
|
|
end
|
|
|
|
|
|
|
|
extracted_polls
|
|
|
|
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
|
|
|
require_dependency "application_controller"
|
2015-09-14 13:27:54 -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
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
before_filter :ensure_logged_in
|
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)
|
|
|
|
user_id = current_user.id
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-04-29 17:34:40 -04:00
|
|
|
begin
|
|
|
|
poll, options = DiscoursePoll::Poll.vote(post_id, poll_name, options, user_id)
|
|
|
|
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)
|
|
|
|
user_id = current_user.id
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2015-04-29 17:34:40 -04: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-12 13:23:21 -04:00
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
|
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"
|
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
|
2015-04-23 13:33:29 -04:00
|
|
|
attr_accessor :polls
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
after_save do
|
|
|
|
next if self.polls.blank? || !self.polls.is_a?(Hash)
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
post = self
|
|
|
|
polls = self.polls
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post.id}") do
|
|
|
|
post.custom_fields[POLLS_CUSTOM_FIELD] = polls
|
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
|
|
|
|
|
2015-04-25 18:12:19 -04:00
|
|
|
validate(:post, :validate_polls) do
|
2015-04-23 13:33:29 -04:00
|
|
|
# only care when raw has changed!
|
|
|
|
return unless self.raw_changed?
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
polls = {}
|
2014-05-23 01:00:55 -04:00
|
|
|
|
2015-05-04 10:01:57 -04:00
|
|
|
extracted_polls = DiscoursePoll::Poll::extract(self.raw, self.topic_id)
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
extracted_polls.each do |poll|
|
|
|
|
# polls should have a unique name
|
|
|
|
if polls.has_key?(poll["name"])
|
|
|
|
poll["name"] == DEFAULT_POLL_NAME ?
|
|
|
|
self.errors.add(:base, I18n.t("poll.multiple_polls_without_name")) :
|
2015-05-11 14:09:17 -04:00
|
|
|
self.errors.add(:base, I18n.t("poll.multiple_polls_with_same_name", name: poll["name"]))
|
2015-04-23 13:33:29 -04:00
|
|
|
return
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
# options must be unique
|
|
|
|
if poll["options"].map { |o| o["id"] }.uniq.size != poll["options"].size
|
|
|
|
poll["name"] == DEFAULT_POLL_NAME ?
|
|
|
|
self.errors.add(:base, I18n.t("poll.default_poll_must_have_different_options")) :
|
2015-05-11 14:09:17 -04:00
|
|
|
self.errors.add(:base, I18n.t("poll.named_poll_must_have_different_options", name: poll["name"]))
|
2015-04-23 13:33:29 -04:00
|
|
|
return
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
# at least 2 options
|
|
|
|
if poll["options"].size < 2
|
|
|
|
poll["name"] == DEFAULT_POLL_NAME ?
|
|
|
|
self.errors.add(:base, I18n.t("poll.default_poll_must_have_at_least_2_options")) :
|
2015-05-11 14:09:17 -04:00
|
|
|
self.errors.add(:base, I18n.t("poll.named_poll_must_have_at_least_2_options", name: poll["name"]))
|
2015-04-23 13:33:29 -04:00
|
|
|
return
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-05-01 10:44:51 -04:00
|
|
|
# maximum # of options
|
|
|
|
if poll["options"].size > SiteSetting.poll_maximum_options
|
|
|
|
poll["name"] == DEFAULT_POLL_NAME ?
|
2015-09-27 15:36:57 -04:00
|
|
|
self.errors.add(:base, I18n.t("poll.default_poll_must_have_less_options", count: SiteSetting.poll_maximum_options)) :
|
|
|
|
self.errors.add(:base, I18n.t("poll.named_poll_must_have_less_options", name: poll["name"], count: SiteSetting.poll_maximum_options))
|
2015-05-01 10:44:51 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-06-01 13:28:05 -04:00
|
|
|
# poll with multiple choices
|
|
|
|
if poll["type"] == "multiple"
|
|
|
|
min = (poll["min"].presence || 1).to_i
|
|
|
|
max = (poll["max"].presence || poll["options"].size).to_i
|
|
|
|
|
|
|
|
if min > max || max <= 0 || max > poll["options"].size || min >= poll["options"].size
|
|
|
|
poll["name"] == DEFAULT_POLL_NAME ?
|
|
|
|
self.errors.add(:base, I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")) :
|
|
|
|
self.errors.add(:base, I18n.t("poll.named_poll_with_multiple_choices_has_invalid_parameters", name: poll["name"]))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
# store the valid poll
|
|
|
|
polls[poll["name"]] = poll
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-25 18:12:19 -04:00
|
|
|
# are we updating a post?
|
|
|
|
if self.id.present?
|
2015-04-23 13:33:29 -04:00
|
|
|
post = self
|
|
|
|
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post.id}") do
|
|
|
|
# load previous polls
|
|
|
|
previous_polls = post.custom_fields[POLLS_CUSTOM_FIELD] || {}
|
|
|
|
|
2015-09-14 13:27:54 -04:00
|
|
|
# extract options
|
|
|
|
current_options = polls.values.map { |p| p["options"].map { |o| o["id"] } }.flatten.sort
|
|
|
|
previous_options = previous_polls.values.map { |p| p["options"].map { |o| o["id"] } }.flatten.sort
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
# are the polls different?
|
2015-09-14 13:27:54 -04:00
|
|
|
if polls.keys != previous_polls.keys || current_options != previous_options
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-09-25 12:54:15 -04:00
|
|
|
has_votes = previous_polls.keys.map { |p| previous_polls[p]["voters"].to_i }.sum > 0
|
|
|
|
|
2015-05-07 11:24:35 -04:00
|
|
|
# outside of the 5-minute edit window?
|
2015-09-25 12:54:15 -04:00
|
|
|
if post.created_at < 5.minutes.ago && has_votes
|
2015-05-07 11:24:35 -04:00
|
|
|
# cannot add/remove/rename polls
|
|
|
|
if polls.keys.sort != previous_polls.keys.sort
|
2015-04-25 18:12:19 -04:00
|
|
|
post.errors.add(:base, I18n.t("poll.cannot_change_polls_after_5_minutes"))
|
|
|
|
return
|
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-25 18:12:19 -04:00
|
|
|
# deal with option changes
|
|
|
|
if User.staff.pluck(:id).include?(post.last_editor_id)
|
|
|
|
# staff can only edit options
|
|
|
|
polls.each_key do |poll_name|
|
2015-09-25 12:54:15 -04:00
|
|
|
if polls[poll_name]["options"].size != previous_polls[poll_name]["options"].size && previous_polls[poll_name]["voters"].to_i > 0
|
2015-04-25 18:12:19 -04:00
|
|
|
post.errors.add(:base, I18n.t("poll.staff_cannot_add_or_remove_options_after_5_minutes"))
|
|
|
|
return
|
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
2015-04-25 18:12:19 -04:00
|
|
|
else
|
2015-05-07 11:24:35 -04:00
|
|
|
# OP cannot edit poll options
|
|
|
|
post.errors.add(:base, I18n.t("poll.op_cannot_edit_options_after_5_minutes"))
|
2015-04-25 18:12:19 -04:00
|
|
|
return
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
2015-04-25 18:12:19 -04:00
|
|
|
end
|
|
|
|
|
2015-05-06 12:52:09 -04:00
|
|
|
# try to merge votes
|
2015-04-25 18:12:19 -04:00
|
|
|
polls.each_key do |poll_name|
|
|
|
|
next unless previous_polls.has_key?(poll_name)
|
2015-05-06 12:52:09 -04:00
|
|
|
|
|
|
|
# when the # of options has changed, reset all the votes
|
|
|
|
if polls[poll_name]["options"].size != previous_polls[poll_name]["options"].size
|
2015-10-16 11:18:22 -04:00
|
|
|
PostCustomField.where(post_id: post.id, name: VOTES_CUSTOM_FIELD).destroy_all
|
2015-05-06 12:52:09 -04:00
|
|
|
post.clear_custom_fields
|
|
|
|
next
|
|
|
|
end
|
2015-04-25 18:12:19 -04:00
|
|
|
|
2015-05-04 10:01:57 -04:00
|
|
|
polls[poll_name]["voters"] = previous_polls[poll_name]["voters"]
|
2015-04-25 18:12:19 -04:00
|
|
|
for o in 0...polls[poll_name]["options"].size
|
|
|
|
polls[poll_name]["options"][o]["votes"] = previous_polls[poll_name]["options"][o]["votes"]
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-11 02:56:55 -04:00
|
|
|
|
2015-04-25 18:12:19 -04:00
|
|
|
# immediately store the polls
|
|
|
|
post.custom_fields[POLLS_CUSTOM_FIELD] = polls
|
|
|
|
post.save_custom_fields(true)
|
|
|
|
|
2015-04-25 18:22:29 -04:00
|
|
|
# publish the changes
|
2015-05-03 22:21:00 -04:00
|
|
|
MessageBus.publish("/polls/#{post.id}", { polls: polls })
|
2015-04-25 18:12:19 -04:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
self.polls = polls
|
|
|
|
end
|
2015-04-25 18:12:19 -04:00
|
|
|
|
|
|
|
true
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Post.register_custom_field_type(POLLS_CUSTOM_FIELD, :json)
|
2015-10-16 11:18:22 -04:00
|
|
|
Post.register_custom_field_type(VOTES_CUSTOM_FIELD, :json)
|
2014-05-23 01:00:55 -04:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
TopicView.add_post_custom_fields_whitelister do |user|
|
2015-10-16 11:18:22 -04:00
|
|
|
user ? [POLLS_CUSTOM_FIELD, VOTES_CUSTOM_FIELD] : [POLLS_CUSTOM_FIELD]
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
2014-08-22 20:50:45 -04:00
|
|
|
|
2015-04-27 12:59:29 -04:00
|
|
|
# tells the front-end we have a poll for that post
|
|
|
|
on(:post_created) do |post|
|
|
|
|
next if post.is_first_post? || post.custom_fields[POLLS_CUSTOM_FIELD].blank?
|
2015-05-03 22:21:00 -04:00
|
|
|
MessageBus.publish("/polls", { post_id: post.id })
|
2015-04-27 12:59:29 -04:00
|
|
|
end
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
add_to_serializer(:post, :polls, false) { post_custom_fields[POLLS_CUSTOM_FIELD] }
|
|
|
|
add_to_serializer(:post, :include_polls?) { post_custom_fields.present? && post_custom_fields[POLLS_CUSTOM_FIELD].present? }
|
|
|
|
|
2015-10-16 11:18:22 -04:00
|
|
|
add_to_serializer(:post, :polls_votes, false) { post_custom_fields[VOTES_CUSTOM_FIELD]["#{scope.user.id}"] }
|
|
|
|
add_to_serializer(:post, :include_polls_votes?) do
|
|
|
|
return unless scope.user
|
|
|
|
return unless post_custom_fields.present?
|
|
|
|
return unless post_custom_fields[VOTES_CUSTOM_FIELD].present?
|
|
|
|
post_custom_fields[VOTES_CUSTOM_FIELD].has_key?("#{scope.user.id}")
|
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|