A platform for community discussion. Free, open, simple.
FEATURE: Automatically disable slow mode. (#11461)
Roman Rizzic7b9f044a4c4
diff --git a/app/assets/javascripts/discourse/app/components/future-date-input.js b/app/assets/javascripts/discourse/app/components/future-date-input.jsindex ab0f447601c..f1d7752fb1b 100644--- a/app/assets/javascripts/discourse/app/components/future-date-input.js+++ b/app/assets/javascripts/discourse/app/components/future-date-input.js@@ -17,6 +17,7 @@ export default Component.extend({isBasedOnLastPost: equal("selection", "set_based_on_last_post"),displayDateAndTimePicker: and("includeDateTime", "isCustom"),displayLabel: null,+ labelClasses: null,displayNumberInput: or("isBasedOnLastPost", "isBasedOnDuration"),init() {diff --git a/app/assets/javascripts/discourse/app/controllers/edit-slow-mode.js b/app/assets/javascripts/discourse/app/controllers/edit-slow-mode.jsindex e76573617f9..84f990eeb48 100644--- a/app/assets/javascripts/discourse/app/controllers/edit-slow-mode.js+++ b/app/assets/javascripts/discourse/app/controllers/edit-slow-mode.js@@ -14,6 +14,7 @@ export default Controller.extend(ModalFunctionality, {minutes: null,seconds: null,saveDisabled: false,+ enabledUntil: null,showCustomSelect: equal("selectedSlowMode", "custom"),init() {@@ -99,7 +100,7 @@ export default Controller.extend(ModalFunctionality, {this._parseValue(this.seconds));- Topic.setSlowMode(this.model.id, seconds)+ Topic.setSlowMode(this.model.id, seconds, this.enabledUntil).catch(popupAjaxError).then(() => {this.set("model.slow_mode_seconds", seconds);diff --git a/app/assets/javascripts/discourse/app/models/topic.js b/app/assets/javascripts/discourse/app/models/topic.jsindex 819f8c8ff04..c350c5e7470 100644--- a/app/assets/javascripts/discourse/app/models/topic.js+++ b/app/assets/javascripts/discourse/app/models/topic.js@@ -837,8 +837,10 @@ Topic.reopenClass({return ajax(`/t/id_for/${slug}`);},- setSlowMode(topicId, seconds) {+ setSlowMode(topicId, seconds, enabledUntil) {const data = { seconds };+ data.enabled_until = enabledUntil;+return ajax(`/t/${topicId}/slow_mode`, { type: "PUT", data });},});diff --git a/app/assets/javascripts/discourse/app/templates/components/future-date-input.hbs b/app/assets/javascripts/discourse/app/templates/components/future-date-input.hbsindex 43c7e421e00..aa92210bc75 100644--- a/app/assets/javascripts/discourse/app/templates/components/future-date-input.hbs+++ b/app/assets/javascripts/discourse/app/templates/components/future-date-input.hbs@@ -1,7 +1,7 @@<div class="future-date-input">{{#unless isBasedOnDuration}}<div class="control-group">- <label>{{displayLabel}}</label>+ <label class={{labelClasses}}>{{displayLabel}}</label>{{future-date-input-selectorminimumResultsForSearch=-1statusType=statusTypediff --git a/app/assets/javascripts/discourse/app/templates/modal/edit-slow-mode.hbs b/app/assets/javascripts/discourse/app/templates/modal/edit-slow-mode.hbsindex 956db77d769..fdf231d1308 100644--- a/app/assets/javascripts/discourse/app/templates/modal/edit-slow-mode.hbs+++ b/app/assets/javascripts/discourse/app/templates/modal/edit-slow-mode.hbs@@ -25,6 +25,18 @@{{input value=seconds type="number" class="input-small"}}</div>{{/if}}++ <div class="control-group">+ {{future-date-input+ class="enabled-until"+ label="topic.slow_mode_update.enabled_until"+ labelClasses="slow-mode-label"+ includeFarFuture=false+ clearable=true+ input=enabledUntil+ onChangeInput=(action (mut enabledUntil))+ }}+ </div>{{/d-modal-body}}<div class="modal-footer">diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex d1b5ca2b7c6..e5a2f9387a9 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -938,9 +938,20 @@ class TopicsController < ApplicationControllerdef set_slow_modetopic = Topic.find(params[:topic_id])+ slow_mode_type = TopicTimer.types[:clear_slow_mode]+ timer = TopicTimer.find_by(topic: topic, status_type: slow_mode_type)guardian.ensure_can_moderate!(topic)topic.update!(slow_mode_seconds: params[:seconds])+ enabled = params[:seconds].to_i > 0++ time = enabled && params[:enabled_until].present? ? params[:enabled_until] : nil++ topic.set_or_create_timer(+ slow_mode_type,+ time,+ by_user: timer&.user+ )head :okenddiff --git a/app/jobs/regular/clear_slow_mode.rb b/app/jobs/regular/clear_slow_mode.rbnew file mode 100644index 00000000000..0c45421d18d--- /dev/null+++ b/app/jobs/regular/clear_slow_mode.rb@@ -0,0 +1,24 @@+# frozen_string_literal: true++module Jobs+ class ClearSlowMode < ::Jobs::Base++ def execute(args)+ topic_timer = TopicTimer.find_by(id: args[:topic_timer_id] || args[:topic_status_update_id])++ if topic_timer.nil? || topic_timer.execute_at > Time.zone.now+ return+ end++ topic = topic_timer&.topic++ if topic.nil?+ topic_timer.destroy!+ return+ end++ topic.update!(slow_mode_seconds: 0)+ topic_timer.trash!(Discourse.system_user)+ end+ end+enddiff --git a/app/models/topic_timer.rb b/app/models/topic_timer.rbindex 441e631aa1c..0d7be787f26 100644--- a/app/models/topic_timer.rb+++ b/app/models/topic_timer.rb@@ -51,16 +51,17 @@ class TopicTimer < ActiveRecord::Basereminder: 5,bump: 6,delete_replies: 7,- silent_close: 8+ silent_close: 8,+ clear_slow_mode: 9)enddef self.public_types- @_public_types ||= types.except(:reminder)+ @_public_types ||= types.except(:reminder, :clear_slow_mode)enddef self.private_types- @_private_types ||= types.only(:reminder)+ @_private_types ||= types.only(:reminder, :clear_slow_mode)enddef self.ensure_consistency!@@ -122,6 +123,10 @@ class TopicTimer < ActiveRecord::BaseJobs.cancel_scheduled_job(:delete_replies, topic_timer_id: id)end+ def cancel_auto_clear_slow_mode_job+ Jobs.cancel_scheduled_job(:clear_slow_mode, topic_timer_id: id)+ end+def schedule_auto_delete_replies_job(time)Jobs.enqueue_at(time, :delete_replies, topic_timer_id: id)end@@ -173,6 +178,10 @@ class TopicTimer < ActiveRecord::Basedef schedule_auto_reminder_job(time)# noop, TODO(martin 2021-03-11): Remove this after timers migrated and outstanding jobs cancelledend++ def schedule_auto_clear_slow_mode_job(time)+ Jobs.enqueue_at(time, :clear_slow_mode, topic_timer_id: id)+ endend# == Schema Informationdiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 39d1b298696..98d7ebdb299 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -2333,6 +2333,7 @@ en:select: "Users may only post in this topic once every:"description: "To promote thoughtful discussion in fast moving or contentious discussions, users must wait before posting again in this topic."save: "Enable"+ enabled_until: "(Optional) Enabled until:"remove: "Disable"hours: "Hours:"minutes: "Minutes:"diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 32e6ee1a516..36ab0ee0021 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -3227,6 +3227,54 @@ RSpec.describe TopicsController doexpect(topic.slow_mode_seconds).to eq(3600)endend++ context 'auto-disable slow mode' do+ before { sign_in(admin) }++ let(:timestamp) { 1.week.from_now.to_formatted_s(:iso8601) }++ it 'sets a topic timer to clear the slow mode automatically' do+ put "/t/#{topic.id}/slow_mode.json", params: {+ seconds: '3600', enabled_until: timestamp+ }++ created_timer = TopicTimer.find_by(topic: topic)+ execute_at = created_timer.execute_at.to_formatted_s(:iso8601)++ expect(execute_at).to eq(timestamp)+ end++ it 'deletes the topic timer' do+ put "/t/#{topic.id}/slow_mode.json", params: {+ seconds: '3600', enabled_until: timestamp+ }++ put "/t/#{topic.id}/slow_mode.json", params: {+ seconds: '0', enabled_until: timestamp+ }++ created_timer = TopicTimer.find_by(topic: topic)++ expect(created_timer).to be_nil+ end++ it 'updates the existing timer' do+ put "/t/#{topic.id}/slow_mode.json", params: {+ seconds: '3600', enabled_until: timestamp+ }++ updated_timestamp = 1.hour.from_now.to_formatted_s(:iso8601)++ put "/t/#{topic.id}/slow_mode.json", params: {+ seconds: '3600', enabled_until: updated_timestamp+ }++ created_timer = TopicTimer.find_by(topic: topic)+ execute_at = created_timer.execute_at.to_formatted_s(:iso8601)++ expect(execute_at).to eq(updated_timestamp)+ end+ endenddescribe '#invite' do