2018-11-19 08:50:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-06 11:04:56 -04:00
|
|
|
module DiscoursePoll
|
|
|
|
class PollsUpdater
|
2018-11-19 08:50:00 -05:00
|
|
|
|
2020-10-02 03:21:24 -04:00
|
|
|
POLL_ATTRIBUTES ||= %w{close_at max min results status step type visibility title groups}
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
def self.update(post, polls)
|
2018-11-19 08:50:00 -05:00
|
|
|
::Poll.transaction do
|
|
|
|
has_changed = false
|
|
|
|
edit_window = SiteSetting.poll_edit_window_mins
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
old_poll_names = ::Poll.where(post: post).pluck(:name)
|
|
|
|
new_poll_names = polls.keys
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
deleted_poll_names = old_poll_names - new_poll_names
|
|
|
|
created_poll_names = new_poll_names - old_poll_names
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
# delete polls
|
|
|
|
if deleted_poll_names.present?
|
|
|
|
::Poll.where(post: post, name: deleted_poll_names).destroy_all
|
|
|
|
end
|
2017-01-16 09:51:57 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
# create polls
|
|
|
|
if created_poll_names.present?
|
|
|
|
has_changed = true
|
|
|
|
polls.slice(*created_poll_names).values.each do |poll|
|
|
|
|
Poll.create!(post.id, poll)
|
2017-01-16 09:51:57 -05:00
|
|
|
end
|
2018-11-19 08:50:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# update polls
|
|
|
|
::Poll.includes(:poll_votes, :poll_options).where(post: post).find_each do |old_poll|
|
|
|
|
new_poll = polls[old_poll.name]
|
|
|
|
new_poll_options = new_poll["options"]
|
|
|
|
|
|
|
|
attributes = new_poll.slice(*POLL_ATTRIBUTES)
|
|
|
|
attributes["visibility"] = new_poll["public"] == "true" ? "everyone" : "secret"
|
|
|
|
attributes["close_at"] = Time.zone.parse(new_poll["close"]) rescue nil
|
2018-12-19 05:29:13 -05:00
|
|
|
attributes["status"] = old_poll["status"]
|
2020-01-28 07:30:04 -05:00
|
|
|
attributes["groups"] = new_poll["groups"]
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = ::Poll.new(attributes)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
if is_different?(old_poll, poll, new_poll_options)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
# only prevent changes when there's at least 1 vote
|
|
|
|
if old_poll.poll_votes.size > 0
|
|
|
|
# can't change after edit window (when enabled)
|
|
|
|
if edit_window > 0 && old_poll.created_at < edit_window.minutes.ago
|
|
|
|
error = poll.name == DiscoursePoll::DEFAULT_POLL_NAME ?
|
|
|
|
I18n.t("poll.edit_window_expired.cannot_edit_default_poll_with_votes", minutes: edit_window) :
|
|
|
|
I18n.t("poll.edit_window_expired.cannot_edit_named_poll_with_votes", minutes: edit_window, name: poll.name)
|
2017-01-16 09:51:57 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
post.errors.add(:base, error)
|
|
|
|
return
|
2017-01-16 09:51:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
# update poll
|
|
|
|
POLL_ATTRIBUTES.each do |attr|
|
2019-05-06 21:27:05 -04:00
|
|
|
old_poll.public_send("#{attr}=", poll.public_send(attr))
|
2017-01-16 09:51:57 -05:00
|
|
|
end
|
2019-05-06 21:27:05 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
old_poll.save!
|
|
|
|
|
|
|
|
# keep track of anonymous votes
|
|
|
|
anonymous_votes = old_poll.poll_options.map { |pv| [pv.digest, pv.anonymous_votes] }.to_h
|
|
|
|
|
|
|
|
# destroy existing options & votes
|
|
|
|
::PollOption.where(poll: old_poll).destroy_all
|
|
|
|
|
|
|
|
# create new options
|
|
|
|
new_poll_options.each do |option|
|
|
|
|
::PollOption.create!(
|
|
|
|
poll: old_poll,
|
|
|
|
digest: option["id"],
|
|
|
|
html: option["html"].strip,
|
|
|
|
anonymous_votes: anonymous_votes[option["id"]],
|
|
|
|
)
|
2016-06-07 06:55:01 -04:00
|
|
|
end
|
2018-11-19 08:50:00 -05:00
|
|
|
|
|
|
|
has_changed = true
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
if ::Poll.exists?(post: post)
|
|
|
|
post.custom_fields[HAS_POLLS] = true
|
|
|
|
else
|
|
|
|
post.custom_fields.delete(HAS_POLLS)
|
|
|
|
end
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
post.save_custom_fields(true)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
if has_changed
|
|
|
|
polls = ::Poll.includes(poll_options: :poll_votes).where(post: post)
|
2020-03-20 14:29:00 -04:00
|
|
|
polls = ActiveModel::ArraySerializer.new(polls, each_serializer: PollSerializer, root: false, scope: Guardian.new(nil)).as_json
|
2018-12-05 15:27:49 -05:00
|
|
|
post.publish_message!("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-09 00:10:27 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
def self.is_different?(old_poll, new_poll, new_options)
|
|
|
|
# an attribute was changed?
|
|
|
|
POLL_ATTRIBUTES.each do |attr|
|
2019-05-06 21:27:05 -04:00
|
|
|
return true if old_poll.public_send(attr) != new_poll.public_send(attr)
|
2016-06-09 00:10:27 -04:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
# an option was changed?
|
|
|
|
return true if old_poll.poll_options.map { |o| o.digest }.sort != new_options.map { |o| o["id"] }.sort
|
|
|
|
|
|
|
|
# it's the same!
|
2016-06-09 00:10:27 -04:00
|
|
|
false
|
|
|
|
end
|
2018-11-19 08:50:00 -05:00
|
|
|
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
|
|
|
end
|