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-07-03 00:45:17 +08:00
|
|
|
enabled_site_setting :poll_enabled
|
|
|
|
|
2015-05-08 01:49:06 +08:00
|
|
|
register_asset "stylesheets/common/poll.scss"
|
|
|
|
register_asset "stylesheets/desktop/poll.scss", :desktop
|
|
|
|
register_asset "stylesheets/mobile/poll.scss", :mobile
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
register_asset "javascripts/poll_dialect.js", :server_side
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
PLUGIN_NAME ||= "discourse_poll".freeze
|
|
|
|
|
|
|
|
POLLS_CUSTOM_FIELD ||= "polls".freeze
|
|
|
|
VOTES_CUSTOM_FIELD ||= "polls-votes".freeze
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-09-15 01:27:54 +08:00
|
|
|
DATA_PREFIX ||= "data-poll-".freeze
|
|
|
|
DEFAULT_POLL_NAME ||= "poll".freeze
|
|
|
|
|
2014-02-05 10:53:09 +08:00
|
|
|
after_initialize do
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-04-26 06:37:27 +08:00
|
|
|
# remove "Vote Now!" & "Show Results" links in emails
|
|
|
|
Email::Styles.register_plugin_style do |fragment|
|
|
|
|
fragment.css(".poll a.cast-votes, .poll a.toggle-results").each(&:remove)
|
|
|
|
end
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
module ::DiscoursePoll
|
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
|
|
|
|
2015-04-30 05:34:40 +08: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-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
|
|
|
|
|
2015-04-30 05:34:40 +08: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-24 01:33:29 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
polls = post.custom_fields[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"
|
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?
|
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
votes = post.custom_fields["#{VOTES_CUSTOM_FIELD}-#{user_id}"] || {}
|
|
|
|
vote = votes[poll_name] || []
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-05-01 22:33:24 +08:00
|
|
|
# increment counters only when the user hasn't casted a vote yet
|
2015-05-04 22:01:57 +08:00
|
|
|
poll["voters"] += 1 if vote.size == 0
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
poll["options"].each do |option|
|
|
|
|
option["votes"] -= 1 if vote.include?(option["id"])
|
|
|
|
option["votes"] += 1 if options.include?(option["id"])
|
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
votes[poll_name] = options
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
post.custom_fields[POLLS_CUSTOM_FIELD] = polls
|
|
|
|
post.custom_fields["#{VOTES_CUSTOM_FIELD}-#{user_id}"] = votes
|
|
|
|
post.save_custom_fields(true)
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-05-04 10:21:00 +08:00
|
|
|
MessageBus.publish("/polls/#{post_id}", { polls: polls })
|
2015-04-30 05:34:40 +08:00
|
|
|
|
|
|
|
return [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
|
|
|
|
2015-04-30 05:34:40 +08: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 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
|
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
polls = post.custom_fields[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
|
|
|
|
2015-05-04 10:21:00 +08:00
|
|
|
MessageBus.publish("/polls/#{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
|
|
|
|
|
|
|
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 22:01:57 +08:00
|
|
|
poll = { "options" => [], "voters" => 0 }
|
2015-05-03 05:44:45 +08: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-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
|
|
|
|
2015-04-30 05:34:40 +08:00
|
|
|
before_filter :ensure_logged_in
|
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)
|
|
|
|
user_id = current_user.id
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-04-30 05:34:40 +08: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-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
|
2015-04-24 01:33:29 +08:00
|
|
|
|
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"
|
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
|
|
|
|
post.custom_fields[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
|
|
|
|
|
2015-04-26 06:12:19 +08:00
|
|
|
validate(:post, :validate_polls) do
|
2015-04-24 01:33:29 +08:00
|
|
|
# only care when raw has changed!
|
|
|
|
return unless self.raw_changed?
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
polls = {}
|
2014-05-23 13:00:55 +08:00
|
|
|
|
2015-05-04 22:01:57 +08:00
|
|
|
extracted_polls = DiscoursePoll::Poll::extract(self.raw, self.topic_id)
|
|
|
|
|
2015-04-24 01:33:29 +08: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-12 02:09:17 +08:00
|
|
|
self.errors.add(:base, I18n.t("poll.multiple_polls_with_same_name", name: poll["name"]))
|
2015-04-24 01:33:29 +08:00
|
|
|
return
|
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-24 01:33:29 +08: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-12 02:09:17 +08:00
|
|
|
self.errors.add(:base, I18n.t("poll.named_poll_must_have_different_options", name: poll["name"]))
|
2015-04-24 01:33:29 +08:00
|
|
|
return
|
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-24 01:33:29 +08: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-12 02:09:17 +08:00
|
|
|
self.errors.add(:base, I18n.t("poll.named_poll_must_have_at_least_2_options", name: poll["name"]))
|
2015-04-24 01:33:29 +08:00
|
|
|
return
|
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-05-01 22:44:51 +08:00
|
|
|
# maximum # of options
|
|
|
|
if poll["options"].size > SiteSetting.poll_maximum_options
|
|
|
|
poll["name"] == DEFAULT_POLL_NAME ?
|
|
|
|
self.errors.add(:base, I18n.t("poll.default_poll_must_have_less_options", max: SiteSetting.poll_maximum_options)) :
|
2015-05-12 02:09:17 +08:00
|
|
|
self.errors.add(:base, I18n.t("poll.named_poll_must_have_less_options", name: poll["name"], max: SiteSetting.poll_maximum_options))
|
2015-05-01 22:44:51 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-06-02 01:28:05 +08: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-24 01:33:29 +08:00
|
|
|
# store the valid poll
|
|
|
|
polls[poll["name"]] = poll
|
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-26 06:12:19 +08:00
|
|
|
# are we updating a post?
|
|
|
|
if self.id.present?
|
2015-04-24 01:33:29 +08:00
|
|
|
post = self
|
|
|
|
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{post.id}") do
|
|
|
|
# load previous polls
|
|
|
|
previous_polls = post.custom_fields[POLLS_CUSTOM_FIELD] || {}
|
|
|
|
|
2015-09-15 01:27:54 +08: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-24 01:33:29 +08:00
|
|
|
# are the polls different?
|
2015-09-15 01:27:54 +08:00
|
|
|
if polls.keys != previous_polls.keys || current_options != previous_options
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2015-09-26 00:54:15 +08:00
|
|
|
has_votes = previous_polls.keys.map { |p| previous_polls[p]["voters"].to_i }.sum > 0
|
|
|
|
|
2015-05-07 23:24:35 +08:00
|
|
|
# outside of the 5-minute edit window?
|
2015-09-26 00:54:15 +08:00
|
|
|
if post.created_at < 5.minutes.ago && has_votes
|
2015-05-07 23:24:35 +08:00
|
|
|
# cannot add/remove/rename polls
|
|
|
|
if polls.keys.sort != previous_polls.keys.sort
|
2015-04-26 06:12:19 +08:00
|
|
|
post.errors.add(:base, I18n.t("poll.cannot_change_polls_after_5_minutes"))
|
|
|
|
return
|
|
|
|
end
|
2014-02-05 10:53:09 +08:00
|
|
|
|
2015-04-26 06:12:19 +08: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-26 00:54:15 +08:00
|
|
|
if polls[poll_name]["options"].size != previous_polls[poll_name]["options"].size && previous_polls[poll_name]["voters"].to_i > 0
|
2015-04-26 06:12:19 +08:00
|
|
|
post.errors.add(:base, I18n.t("poll.staff_cannot_add_or_remove_options_after_5_minutes"))
|
|
|
|
return
|
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
2015-04-26 06:12:19 +08:00
|
|
|
else
|
2015-05-07 23:24:35 +08:00
|
|
|
# OP cannot edit poll options
|
|
|
|
post.errors.add(:base, I18n.t("poll.op_cannot_edit_options_after_5_minutes"))
|
2015-04-26 06:12:19 +08:00
|
|
|
return
|
2015-04-24 01:33:29 +08:00
|
|
|
end
|
2015-04-26 06:12:19 +08:00
|
|
|
end
|
|
|
|
|
2015-05-07 00:52:09 +08:00
|
|
|
# try to merge votes
|
2015-04-26 06:12:19 +08:00
|
|
|
polls.each_key do |poll_name|
|
|
|
|
next unless previous_polls.has_key?(poll_name)
|
2015-05-07 00:52:09 +08:00
|
|
|
|
|
|
|
# when the # of options has changed, reset all the votes
|
|
|
|
if polls[poll_name]["options"].size != previous_polls[poll_name]["options"].size
|
|
|
|
PostCustomField.where(post_id: post.id)
|
|
|
|
.where("name LIKE '#{VOTES_CUSTOM_FIELD}-%'")
|
|
|
|
.destroy_all
|
|
|
|
post.clear_custom_fields
|
|
|
|
next
|
|
|
|
end
|
2015-04-26 06:12:19 +08:00
|
|
|
|
2015-05-04 22:01:57 +08:00
|
|
|
polls[poll_name]["voters"] = previous_polls[poll_name]["voters"]
|
2015-04-26 06:12:19 +08: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-24 01:33:29 +08:00
|
|
|
end
|
|
|
|
end
|
2014-04-11 14:56:55 +08:00
|
|
|
|
2015-04-26 06:12:19 +08:00
|
|
|
# immediately store the polls
|
|
|
|
post.custom_fields[POLLS_CUSTOM_FIELD] = polls
|
|
|
|
post.save_custom_fields(true)
|
|
|
|
|
2015-04-26 06:22:29 +08:00
|
|
|
# publish the changes
|
2015-05-04 10:21:00 +08:00
|
|
|
MessageBus.publish("/polls/#{post.id}", { polls: polls })
|
2015-04-26 06:12:19 +08:00
|
|
|
end
|
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
|
|
|
|
|
|
|
|
Post.register_custom_field_type(POLLS_CUSTOM_FIELD, :json)
|
|
|
|
Post.register_custom_field_type("#{VOTES_CUSTOM_FIELD}-*", :json)
|
2014-05-23 13:00:55 +08:00
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
TopicView.add_post_custom_fields_whitelister do |user|
|
|
|
|
whitelisted = [POLLS_CUSTOM_FIELD]
|
|
|
|
whitelisted << "#{VOTES_CUSTOM_FIELD}-#{user.id}" if user
|
|
|
|
whitelisted
|
|
|
|
end
|
2014-08-23 08:50:45 +08:00
|
|
|
|
2015-04-28 00:59:29 +08: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-04 10:21:00 +08:00
|
|
|
MessageBus.publish("/polls", { post_id: post.id })
|
2015-04-28 00:59:29 +08:00
|
|
|
end
|
|
|
|
|
2015-04-24 01:33:29 +08: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? }
|
|
|
|
|
|
|
|
add_to_serializer(:post, :polls_votes, false) { post_custom_fields["#{VOTES_CUSTOM_FIELD}-#{scope.user.id}"] }
|
|
|
|
add_to_serializer(:post, :include_polls_votes?) { scope.user && post_custom_fields.present? && post_custom_fields["#{VOTES_CUSTOM_FIELD}-#{scope.user.id}"].present? }
|
|
|
|
end
|