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)
|
2021-07-19 11:35:47 -04:00
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/poll
|
2021-11-13 06:51:53 -05:00
|
|
|
# transpile_js: true
|
2014-02-04 21:53:09 -05:00
|
|
|
|
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
|
2021-10-05 04:38:49 -04:00
|
|
|
register_asset "stylesheets/common/poll-ui-builder.scss"
|
|
|
|
register_asset "stylesheets/desktop/poll-ui-builder.scss", :desktop
|
|
|
|
register_asset "stylesheets/common/poll-breakdown.scss"
|
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
|
2021-10-05 04:38:49 -04:00
|
|
|
hide_plugin
|
2015-09-14 13:27:54 -04:00
|
|
|
|
2014-02-04 21:53:09 -05:00
|
|
|
after_initialize do
|
2015-04-23 13:33:29 -04:00
|
|
|
module ::DiscoursePoll
|
2021-10-05 04:38:49 -04:00
|
|
|
PLUGIN_NAME ||= "discourse_poll"
|
|
|
|
DATA_PREFIX ||= "data-poll-"
|
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
|
|
|
|
2021-10-05 04:38:49 -04:00
|
|
|
class Error < StandardError; end
|
2015-04-29 17:34:40 -04:00
|
|
|
end
|
2014-04-12 13:23:21 -04:00
|
|
|
|
2021-10-05 04:38:49 -04:00
|
|
|
require_relative "app/controllers/polls_controller.rb"
|
|
|
|
require_relative "app/models/poll_option.rb"
|
|
|
|
require_relative "app/models/poll_vote.rb"
|
|
|
|
require_relative "app/models/poll.rb"
|
|
|
|
require_relative "app/serializers/poll_option_serializer.rb"
|
|
|
|
require_relative "app/serializers/poll_serializer.rb"
|
|
|
|
require_relative "jobs/regular/close_poll.rb"
|
|
|
|
require_relative "lib/poll.rb"
|
|
|
|
require_relative "lib/polls_updater.rb"
|
|
|
|
require_relative "lib/polls_validator.rb"
|
|
|
|
require_relative "lib/post_validator.rb"
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
DiscoursePoll::Engine.routes.draw do
|
|
|
|
put "/vote" => "polls#vote"
|
2021-10-05 04:38:49 -04:00
|
|
|
delete "/vote" => "polls#remove_vote"
|
2015-04-23 13:33:29 -04:00
|
|
|
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'
|
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
|
|
|
|
|
2021-10-05 04:38:49 -04:00
|
|
|
allow_new_queued_post_payload_attribute("is_poll")
|
|
|
|
register_post_custom_field_type(DiscoursePoll::HAS_POLLS, :boolean)
|
|
|
|
topic_view_post_custom_fields_allowlister { [DiscoursePoll::HAS_POLLS] }
|
|
|
|
|
2020-05-08 02:58:39 -04:00
|
|
|
reloadable_patch do
|
|
|
|
Post.class_eval do
|
|
|
|
attr_accessor :extracted_polls
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2020-05-08 02:58:39 -04:00
|
|
|
has_many :polls, dependent: :destroy
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2020-05-08 02:58:39 -04:00
|
|
|
after_save do
|
|
|
|
polls = self.extracted_polls
|
2021-05-26 18:39:51 -04:00
|
|
|
self.extracted_polls = nil
|
2020-05-08 02:58:39 -04:00
|
|
|
next if polls.blank? || !polls.is_a?(Hash)
|
|
|
|
post = self
|
2014-02-04 21:53:09 -05:00
|
|
|
|
2020-05-08 02:58:39 -04:00
|
|
|
Poll.transaction do
|
|
|
|
polls.values.each do |poll|
|
|
|
|
DiscoursePoll::Poll.create!(post.id, poll)
|
|
|
|
end
|
|
|
|
post.custom_fields[DiscoursePoll::HAS_POLLS] = true
|
|
|
|
post.save_custom_fields(true)
|
2018-11-19 08:50:00 -05:00
|
|
|
end
|
2015-04-23 13:33:29 -04:00
|
|
|
end
|
2014-02-04 21:53:09 -05:00
|
|
|
end
|
|
|
|
|
2020-05-08 02:58:39 -04:00
|
|
|
User.class_eval do
|
|
|
|
has_many :poll_votes, dependent: :delete_all
|
|
|
|
end
|
2018-11-23 05:45:53 -05:00
|
|
|
end
|
|
|
|
|
2017-01-26 00:29:43 -05:00
|
|
|
validate(:post, :validate_polls) do |force = nil|
|
|
|
|
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
|
|
|
|
|
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|
|
2021-06-05 05:37:15 -04:00
|
|
|
result.add_error(message)
|
2017-01-26 00:29:43 -05:00
|
|
|
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
|
|
|
|
2021-11-22 06:31:53 -05:00
|
|
|
next if post.is_first_post?
|
|
|
|
next if post.custom_fields[DiscoursePoll::HAS_POLLS].blank?
|
|
|
|
|
|
|
|
polls = ActiveModel::ArraySerializer.new(post.polls, each_serializer: PollSerializer, root: false, scope: guardian).as_json
|
|
|
|
post.publish_message!("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
|
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
|
|
|
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
|
|
|
|
.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
|
2020-09-05 11:14:45 -04:00
|
|
|
Poll.includes(:poll_options).where(post: object)
|
2018-11-19 08:50:00 -05:00
|
|
|
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|
|
2020-09-05 11:14:45 -04:00
|
|
|
user_poll_votes =
|
|
|
|
poll
|
|
|
|
.poll_votes
|
|
|
|
.where(user_id: scope.user.id)
|
|
|
|
.joins(:poll_option)
|
|
|
|
.pluck("poll_options.digest")
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
[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
|