2018-11-19 07:50:00 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-02-04 20:53:09 -06:00
|
|
|
# name: poll
|
2015-04-23 12:33:29 -05:00
|
|
|
# about: Official poll plugin for Discourse
|
2018-11-19 07:50:00 -06:00
|
|
|
# version: 1.0
|
2015-04-23 12:33:29 -05:00
|
|
|
# authors: Vikhyat Korrapati (vikhyat), Régis Hanol (zogstrip)
|
2021-07-19 10:35:47 -05:00
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/poll
|
2014-02-04 20:53:09 -06:00
|
|
|
|
2015-05-07 12:49:06 -05:00
|
|
|
register_asset "stylesheets/common/poll.scss"
|
|
|
|
register_asset "stylesheets/desktop/poll.scss", :desktop
|
|
|
|
register_asset "stylesheets/mobile/poll.scss", :mobile
|
2021-10-05 03:38:49 -05: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 12:49:06 -05:00
|
|
|
|
2018-11-26 15:49:57 -06:00
|
|
|
register_svg_icon "far fa-check-square"
|
|
|
|
|
2018-05-07 21:30:33 -05:00
|
|
|
enabled_site_setting :poll_enabled
|
2021-10-05 03:38:49 -05:00
|
|
|
hide_plugin
|
2015-09-14 12:27:54 -05:00
|
|
|
|
2014-02-04 20:53:09 -06:00
|
|
|
after_initialize do
|
2015-04-23 12:33:29 -05:00
|
|
|
module ::DiscoursePoll
|
2021-10-05 03:38:49 -05:00
|
|
|
PLUGIN_NAME ||= "discourse_poll"
|
|
|
|
DATA_PREFIX ||= "data-poll-"
|
2018-11-19 07:50:00 -06:00
|
|
|
HAS_POLLS ||= "has_polls"
|
|
|
|
DEFAULT_POLL_NAME ||= "poll"
|
2016-06-06 10:04:56 -05:00
|
|
|
|
2014-02-04 20:53:09 -06:00
|
|
|
class Engine < ::Rails::Engine
|
2015-04-23 12:33:29 -05:00
|
|
|
engine_name PLUGIN_NAME
|
|
|
|
isolate_namespace DiscoursePoll
|
2014-02-04 20:53:09 -06:00
|
|
|
end
|
2015-04-23 12:33:29 -05:00
|
|
|
|
2021-10-05 03:38:49 -05:00
|
|
|
class Error < StandardError
|
|
|
|
end
|
2015-04-29 16:34:40 -05:00
|
|
|
end
|
2014-04-12 12:23:21 -05:00
|
|
|
|
2021-10-05 03:38:49 -05: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 20:53:09 -06:00
|
|
|
|
2015-04-23 12:33:29 -05:00
|
|
|
DiscoursePoll::Engine.routes.draw do
|
|
|
|
put "/vote" => "polls#vote"
|
2021-10-05 03:38:49 -05:00
|
|
|
delete "/vote" => "polls#remove_vote"
|
2015-04-23 12:33:29 -05:00
|
|
|
put "/toggle_status" => "polls#toggle_status"
|
2016-06-09 08:33:17 -05:00
|
|
|
get "/voters" => "polls#voters"
|
2019-11-25 11:51:01 -06:00
|
|
|
get "/grouped_poll_results" => "polls#grouped_poll_results"
|
2014-02-04 20:53:09 -06:00
|
|
|
end
|
|
|
|
|
2015-04-23 12:33:29 -05:00
|
|
|
Discourse::Application.routes.append { mount ::DiscoursePoll::Engine, at: "/polls" }
|
2014-02-04 20:53:09 -06:00
|
|
|
|
2021-10-05 03:38:49 -05: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 01:58:39 -05:00
|
|
|
reloadable_patch do
|
|
|
|
Post.class_eval do
|
|
|
|
attr_accessor :extracted_polls
|
2014-02-04 20:53:09 -06:00
|
|
|
|
2020-05-08 01:58:39 -05:00
|
|
|
has_many :polls, dependent: :destroy
|
2014-02-04 20:53:09 -06:00
|
|
|
|
2020-05-08 01:58:39 -05:00
|
|
|
after_save do
|
|
|
|
polls = self.extracted_polls
|
2021-05-26 17:39:51 -05:00
|
|
|
self.extracted_polls = nil
|
2020-05-08 01:58:39 -05:00
|
|
|
next if polls.blank? || !polls.is_a?(Hash)
|
|
|
|
post = self
|
2014-02-04 20:53:09 -06:00
|
|
|
|
2020-05-08 01:58:39 -05:00
|
|
|
Poll.transaction do
|
|
|
|
polls.values.each { |poll| DiscoursePoll::Poll.create!(post.id, poll) }
|
|
|
|
post.custom_fields[DiscoursePoll::HAS_POLLS] = true
|
|
|
|
post.save_custom_fields(true)
|
2018-11-19 07:50:00 -06:00
|
|
|
end
|
2015-04-23 12:33:29 -05:00
|
|
|
end
|
2014-02-04 20:53:09 -06:00
|
|
|
end
|
|
|
|
|
2020-05-08 01:58:39 -05:00
|
|
|
User.class_eval { has_many :poll_votes, dependent: :delete_all }
|
2018-11-23 04:45:53 -06:00
|
|
|
end
|
|
|
|
|
2017-01-25 23:29:43 -06:00
|
|
|
validate(:post, :validate_polls) do |force = nil|
|
|
|
|
return unless self.raw_changed? || force
|
2014-02-04 20:53:09 -06:00
|
|
|
|
2016-06-06 10:04:56 -05:00
|
|
|
validator = DiscoursePoll::PollsValidator.new(self)
|
|
|
|
return unless (polls = validator.validate_polls)
|
2014-02-04 20:53:09 -06:00
|
|
|
|
2018-11-19 07:50:00 -06:00
|
|
|
if polls.present?
|
2017-12-05 11:07:21 -06:00
|
|
|
validator = DiscoursePoll::PostValidator.new(self)
|
|
|
|
return unless validator.validate_post
|
|
|
|
end
|
|
|
|
|
2015-04-25 17:12:19 -05:00
|
|
|
# are we updating a post?
|
|
|
|
if self.id.present?
|
2018-11-19 07:50:00 -06:00
|
|
|
DiscoursePoll::PollsUpdater.update(self, polls)
|
2015-04-23 12:33:29 -05:00
|
|
|
else
|
2018-11-19 07:50:00 -06:00
|
|
|
self.extracted_polls = polls
|
2015-04-23 12:33:29 -05:00
|
|
|
end
|
2015-04-25 17:12:19 -05:00
|
|
|
|
|
|
|
true
|
2015-04-23 12:33:29 -05:00
|
|
|
end
|
|
|
|
|
2017-01-25 23:29:43 -06: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 { |message| result.add_error(message) }
|
|
|
|
|
|
|
|
result
|
|
|
|
else
|
|
|
|
manager.args["is_poll"] = true
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
on(:approved_post) do |queued_post, created_post|
|
|
|
|
created_post.validate_polls(true) if queued_post.payload["is_poll"]
|
|
|
|
end
|
|
|
|
|
2016-04-13 12:24:39 -05:00
|
|
|
on(:reduce_cooked) do |fragment, post|
|
|
|
|
if post.nil? || post.trashed?
|
|
|
|
fragment.css(".poll, [data-poll-name]").each(&:remove)
|
|
|
|
else
|
2018-05-02 19:12:19 -05:00
|
|
|
post_url = post.full_url
|
2016-04-13 12:24:39 -05: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 10:05:52 -05:00
|
|
|
on(:reduce_excerpt) do |doc, options|
|
|
|
|
post = options[:post]
|
2019-05-29 17:02:33 -05:00
|
|
|
|
2019-05-29 17:05:53 -05:00
|
|
|
replacement =
|
2023-01-06 14:42:16 -06:00
|
|
|
(
|
2019-05-29 17:05:53 -05:00
|
|
|
if post&.url.present?
|
2022-08-09 05:28:29 -05:00
|
|
|
"<a href='#{UrlHelper.normalized_encode(post.url)}'>#{I18n.t("poll.poll")}</a>"
|
2023-01-06 14:42:16 -06:00
|
|
|
else
|
2019-05-29 17:05:53 -05:00
|
|
|
I18n.t("poll.poll")
|
2023-01-06 14:42:16 -06:00
|
|
|
end
|
|
|
|
)
|
2019-05-29 17:02:33 -05:00
|
|
|
|
2019-05-29 10:05:52 -05:00
|
|
|
doc.css("div.poll").each { |poll| poll.replace(replacement) }
|
|
|
|
end
|
|
|
|
|
2020-03-20 13:29:00 -05:00
|
|
|
on(:post_created) do |post, _opts, user|
|
|
|
|
guardian = Guardian.new(user)
|
2018-05-02 19:12:19 -05:00
|
|
|
DiscoursePoll::Poll.schedule_jobs(post)
|
2018-11-19 07:50:00 -06:00
|
|
|
|
2021-11-22 05:31:53 -06: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 11:59:29 -05:00
|
|
|
end
|
|
|
|
|
2018-02-28 15:21:52 -06:00
|
|
|
on(:merging_users) do |source_user, target_user|
|
2018-11-19 07:50:00 -06:00
|
|
|
PollVote.where(user_id: source_user.id).update_all(user_id: target_user.id)
|
2018-02-28 15:21:52 -06:00
|
|
|
end
|
|
|
|
|
2018-11-19 07:50:00 -06: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
|
|
|
|
|
2018-11-20 10:28:41 -06:00
|
|
|
if post_with_polls.present?
|
2023-01-06 14:42:16 -06:00
|
|
|
Poll
|
2018-11-19 07:50:00 -06:00
|
|
|
.where(post_id: post_with_polls)
|
|
|
|
.each do |p|
|
|
|
|
polls[p.post_id] ||= []
|
|
|
|
polls[p.post_id] << p
|
2023-01-06 14:42:16 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-19 07:50:00 -06:00
|
|
|
polls
|
2017-01-27 03:09:33 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-19 07:50:00 -06:00
|
|
|
add_to_serializer(:post, :preloaded_polls, false) do
|
|
|
|
@preloaded_polls ||=
|
|
|
|
if @topic_view.present?
|
|
|
|
@topic_view.polls[object.id]
|
|
|
|
else
|
2020-09-05 10:14:45 -05:00
|
|
|
Poll.includes(:poll_options).where(post: object)
|
2018-11-19 07:50:00 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_serializer(:post, :include_preloaded_polls?) { false }
|
|
|
|
|
|
|
|
add_to_serializer(:post, :polls, false) do
|
2020-03-20 13:29:00 -05:00
|
|
|
preloaded_polls.map { |p| PollSerializer.new(p, root: false, scope: self.scope) }
|
2018-11-19 07:50:00 -06:00
|
|
|
end
|
|
|
|
|
2018-11-20 10:28:41 -06:00
|
|
|
add_to_serializer(:post, :include_polls?) { SiteSetting.poll_enabled && preloaded_polls.present? }
|
2015-04-23 12:33:29 -05:00
|
|
|
|
2016-06-07 05:55:01 -05:00
|
|
|
add_to_serializer(:post, :polls_votes, false) do
|
2018-11-19 07:50:00 -06:00
|
|
|
preloaded_polls
|
|
|
|
.map do |poll|
|
2020-09-05 10:14:45 -05:00
|
|
|
user_poll_votes =
|
|
|
|
poll
|
|
|
|
.poll_votes
|
|
|
|
.where(user_id: scope.user.id)
|
|
|
|
.joins(:poll_option)
|
|
|
|
.pluck("poll_options.digest")
|
2023-01-06 14:42:16 -06:00
|
|
|
|
2018-11-19 07:50:00 -06:00
|
|
|
[poll.name, user_poll_votes]
|
|
|
|
end
|
|
|
|
.to_h
|
2016-06-07 05:55:01 -05:00
|
|
|
end
|
|
|
|
|
2015-10-16 10:18:22 -05:00
|
|
|
add_to_serializer(:post, :include_polls_votes?) do
|
2018-11-20 10:28:41 -06:00
|
|
|
SiteSetting.poll_enabled && scope.user&.id.present? && preloaded_polls.present? &&
|
2018-11-19 07:50:00 -06:00
|
|
|
preloaded_polls.any? { |p| p.has_voted?(scope.user) }
|
2015-10-16 10:18:22 -05:00
|
|
|
end
|
2015-04-23 12:33:29 -05:00
|
|
|
end
|