mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 11:11:13 +08:00
FEATURE: automatically close a poll on a given date and time
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
module DiscoursePoll
|
||||
class PollsUpdater
|
||||
VALID_POLLS_CONFIGS = %w{type min max public}.map(&:freeze)
|
||||
VALID_POLLS_CONFIGS = %w{type min max public close}.map(&:freeze)
|
||||
|
||||
def self.update(post, polls)
|
||||
# load previous polls
|
||||
@ -18,28 +18,18 @@ module DiscoursePoll
|
||||
poll_edit_window_mins = SiteSetting.poll_edit_window_mins
|
||||
|
||||
if post.created_at < poll_edit_window_mins.minutes.ago && has_votes
|
||||
is_staff = User.staff.where(id: post.last_editor_id).exists?
|
||||
|
||||
# deal with option changes
|
||||
if is_staff
|
||||
if User.staff.where(id: post.last_editor_id).exists?
|
||||
# staff can edit options
|
||||
polls.each_key do |poll_name|
|
||||
if polls.dig(poll_name, "options")&.size != previous_polls.dig(poll_name, "options")&.size && previous_polls.dig(poll_name, "voters").to_i > 0
|
||||
post.errors.add(:base, I18n.t(
|
||||
"poll.edit_window_expired.staff_cannot_add_or_remove_options",
|
||||
minutes: poll_edit_window_mins
|
||||
))
|
||||
|
||||
post.errors.add(:base, I18n.t("poll.edit_window_expired.staff_cannot_add_or_remove_options", minutes: poll_edit_window_mins))
|
||||
return
|
||||
end
|
||||
end
|
||||
else
|
||||
# OP cannot edit poll options
|
||||
post.errors.add(:base, I18n.t(
|
||||
"poll.edit_window_expired.op_cannot_edit_options",
|
||||
minutes: poll_edit_window_mins
|
||||
))
|
||||
|
||||
post.errors.add(:base, I18n.t("poll.edit_window_expired.op_cannot_edit_options", minutes: poll_edit_window_mins))
|
||||
return
|
||||
end
|
||||
end
|
||||
@ -93,6 +83,9 @@ module DiscoursePoll
|
||||
post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD] = polls
|
||||
post.save_custom_fields(true)
|
||||
|
||||
# re-schedule jobs
|
||||
DiscoursePoll::Poll.schedule_jobs(post)
|
||||
|
||||
# publish the changes
|
||||
MessageBus.publish("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
|
||||
end
|
||||
@ -102,9 +95,7 @@ module DiscoursePoll
|
||||
return true if (current_polls.keys.sort != previous_polls.keys.sort)
|
||||
|
||||
current_polls.each_key do |poll_name|
|
||||
if !previous_polls[poll_name] ||
|
||||
(current_polls[poll_name].values_at(*VALID_POLLS_CONFIGS) != previous_polls[poll_name].values_at(*VALID_POLLS_CONFIGS))
|
||||
|
||||
if !previous_polls[poll_name] || (current_polls[poll_name].values_at(*VALID_POLLS_CONFIGS) != previous_polls[poll_name].values_at(*VALID_POLLS_CONFIGS))
|
||||
return true
|
||||
end
|
||||
end
|
||||
@ -127,12 +118,9 @@ module DiscoursePoll
|
||||
current_poll = current_polls[poll_name]
|
||||
|
||||
if previous_polls["public"].nil? && current_poll["public"] == "true"
|
||||
error =
|
||||
if poll_name == DiscoursePoll::DEFAULT_POLL_NAME
|
||||
I18n.t("poll.default_cannot_be_made_public")
|
||||
else
|
||||
I18n.t("poll.named_cannot_be_made_public", name: poll_name)
|
||||
end
|
||||
error = poll_name == DiscoursePoll::DEFAULT_POLL_NAME ?
|
||||
I18n.t("poll.default_cannot_be_made_public") :
|
||||
I18n.t("poll.named_cannot_be_made_public", name: poll_name)
|
||||
|
||||
post.errors.add(:base, error)
|
||||
return true
|
||||
|
@ -6,17 +6,13 @@ module DiscoursePoll
|
||||
|
||||
def validate_post
|
||||
min_trust_level = SiteSetting.poll_minimum_trust_level_to_create
|
||||
trusted = @post&.user&.staff? ||
|
||||
@post&.user&.trust_level >= TrustLevel[min_trust_level]
|
||||
|
||||
if !trusted
|
||||
message = I18n.t("poll.insufficient_rights_to_create")
|
||||
|
||||
@post.errors.add(:base, message)
|
||||
return false
|
||||
if @post&.user&.staff? || @post&.user&.trust_level >= TrustLevel[min_trust_level]
|
||||
true
|
||||
else
|
||||
@post.errors.add(:base, I18n.t("poll.insufficient_rights_to_create"))
|
||||
false
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user