2018-11-19 21:50:00 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-06 23:04:56 +08:00
|
|
|
module DiscoursePoll
|
|
|
|
class PollsUpdater
|
2020-10-02 15:21:24 +08:00
|
|
|
POLL_ATTRIBUTES ||= %w[close_at max min results status step type visibility title groups]
|
2016-06-06 23:04:56 +08:00
|
|
|
|
|
|
|
def self.update(post, polls)
|
2018-11-19 21:50:00 +08:00
|
|
|
::Poll.transaction do
|
|
|
|
has_changed = false
|
|
|
|
edit_window = SiteSetting.poll_edit_window_mins
|
2016-06-06 23:04:56 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
old_poll_names = ::Poll.where(post: post).pluck(:name)
|
|
|
|
new_poll_names = polls.keys
|
2016-06-06 23:04:56 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
deleted_poll_names = old_poll_names - new_poll_names
|
|
|
|
created_poll_names = new_poll_names - old_poll_names
|
2016-06-06 23:04:56 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
# delete polls
|
|
|
|
if deleted_poll_names.present?
|
|
|
|
::Poll.where(post: post, name: deleted_poll_names).destroy_all
|
|
|
|
end
|
2017-01-16 22:51:57 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
# create polls
|
|
|
|
if created_poll_names.present?
|
|
|
|
has_changed = true
|
|
|
|
polls.slice(*created_poll_names).values.each { |poll| Poll.create!(post.id, poll) }
|
|
|
|
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"]
|
2023-01-07 04:42:16 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
attributes = new_poll.slice(*POLL_ATTRIBUTES)
|
|
|
|
attributes["visibility"] = new_poll["public"] == "true" ? "everyone" : "secret"
|
2018-12-19 18:29:13 +08:00
|
|
|
attributes["close_at"] = begin
|
2018-11-19 21:50:00 +08:00
|
|
|
Time.zone.parse(new_poll["close"])
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2017-01-16 22:51:57 +08:00
|
|
|
end
|
2020-01-28 20:30:04 +08:00
|
|
|
attributes["status"] = old_poll["status"]
|
|
|
|
attributes["groups"] = new_poll["groups"]
|
2018-11-19 21:50:00 +08:00
|
|
|
poll = ::Poll.new(attributes)
|
2023-01-07 04:42:16 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
if is_different?(old_poll, poll, new_poll_options)
|
|
|
|
# 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
|
2023-01-07 04:42:16 +08:00
|
|
|
error =
|
|
|
|
(
|
2018-11-19 21:50:00 +08:00
|
|
|
if poll.name == DiscoursePoll::DEFAULT_POLL_NAME
|
2023-01-07 04:42:16 +08:00
|
|
|
I18n.t(
|
2018-11-19 21:50:00 +08:00
|
|
|
"poll.edit_window_expired.cannot_edit_default_poll_with_votes",
|
|
|
|
minutes: edit_window,
|
2023-01-07 04:42:16 +08:00
|
|
|
)
|
|
|
|
else
|
|
|
|
I18n.t(
|
2018-11-19 21:50:00 +08:00
|
|
|
"poll.edit_window_expired.cannot_edit_named_poll_with_votes",
|
|
|
|
minutes: edit_window,
|
|
|
|
name: poll.name,
|
2023-01-07 04:42:16 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
post.errors.add(:base, error)
|
2023-01-07 04:42:16 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2017-01-16 22:51:57 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
# update poll
|
|
|
|
POLL_ATTRIBUTES.each do |attr|
|
2019-05-07 09:27:05 +08:00
|
|
|
old_poll.public_send("#{attr}=", poll.public_send(attr))
|
2017-01-16 22:51:57 +08:00
|
|
|
end
|
2019-05-07 09:27:05 +08:00
|
|
|
|
2018-11-19 21:50:00 +08: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 18:55:01 +08:00
|
|
|
end
|
2018-11-19 21:50:00 +08:00
|
|
|
|
|
|
|
has_changed = true
|
2023-01-07 04:42:16 +08:00
|
|
|
end
|
2016-06-06 23:04:56 +08:00
|
|
|
end
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
if ::Poll.exists?(post: post)
|
|
|
|
post.custom_fields[HAS_POLLS] = true
|
|
|
|
else
|
|
|
|
post.custom_fields.delete(HAS_POLLS)
|
|
|
|
end
|
2016-06-06 23:04:56 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
post.save_custom_fields(true)
|
2016-06-06 23:04:56 +08:00
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
if has_changed
|
|
|
|
polls = ::Poll.includes(poll_options: :poll_votes).where(post: post)
|
2020-03-21 02:29:00 +08:00
|
|
|
polls =
|
|
|
|
ActiveModel::ArraySerializer.new(
|
|
|
|
polls,
|
|
|
|
each_serializer: PollSerializer,
|
|
|
|
root: false,
|
2023-12-06 14:37:32 +08:00
|
|
|
scope: Guardian.new(nil),
|
2020-03-21 02:29:00 +08:00
|
|
|
).as_json
|
2018-12-06 04:27:49 +08:00
|
|
|
post.publish_message!("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
|
2016-06-06 23:04:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-09 12:10:27 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
def self.is_different?(old_poll, new_poll, new_options)
|
|
|
|
# an attribute was changed?
|
|
|
|
POLL_ATTRIBUTES.each do |attr|
|
2019-05-07 09:27:05 +08:00
|
|
|
return true if old_poll.public_send(attr) != new_poll.public_send(attr)
|
2016-06-09 12:10:27 +08:00
|
|
|
end
|
|
|
|
|
2018-11-19 21:50:00 +08:00
|
|
|
# an option was changed?
|
|
|
|
if old_poll.poll_options.map { |o| o.digest }.sort != new_options.map { |o| o["id"] }.sort
|
|
|
|
return true
|
2023-01-07 04:42:16 +08:00
|
|
|
end
|
2018-11-19 21:50:00 +08:00
|
|
|
|
|
|
|
# it's the same!
|
2016-06-09 12:10:27 +08:00
|
|
|
false
|
|
|
|
end
|
2016-06-06 23:04:56 +08:00
|
|
|
end
|
|
|
|
end
|