A platform for community discussion. Free, open, simple.
FIX: Prevent topic timer crashes when publishing to category (#38872)
Régis HANOL57ff455f7cee
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 4715deb65d2..e93bf4d182f 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -630,7 +630,7 @@ class TopicsController < ApplicationControllerguardian.ensure_can_delete!(topic) if TopicTimer.destructive_types.values.include?(status_type)- if status_type == TopicTimer.types[:publish_to_category] && params[:category_id].present?+ if status_type == TopicTimer.types[:publish_to_category] && params[:time].present?category = Category.find_by(id: params[:category_id])raise Discourse::NotFound if !categoryraise Discourse::InvalidAccess if !guardian.can_create_topic_on_category?(category)diff --git a/app/models/topic.rb b/app/models/topic.rbindex 967d41972bf..b4c24a0e7d3 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -1667,7 +1667,7 @@ class Topic < ActiveRecord::Basetime,by_user: nil,based_on_last_post: false,- category_id: SiteSetting.uncategorized_category_id,+ category_id: nil,duration_minutes: nil,silent: nil)diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 42b22f6c3d0..41287d64a41 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -4022,6 +4022,7 @@ en:publish_to: "Publish To:"when: "When:"time_frame_required: "Please select a time frame"+ category_required: "Please select a category"min_duration: "Duration must be greater than 0"max_duration: "Duration must be less than 20 years"duration: "Duration"diff --git a/frontend/discourse/app/components/modal/edit-topic-timer.gjs b/frontend/discourse/app/components/modal/edit-topic-timer.gjsindex b14882e0ff9..230006e2d25 100644--- a/frontend/discourse/app/components/modal/edit-topic-timer.gjs+++ b/frontend/discourse/app/components/modal/edit-topic-timer.gjs@@ -145,7 +145,7 @@ export default class EditTopicTimer extends Component {this.args.closeModal();} else {const topicTimer = this.createDefaultTimer();- this.topicTime = topicTimer;+ this.topicTimer = topicTimer;this.args.model.setTopicTimer(topicTimer);this.onChangeInput(null, null);}@@ -210,6 +210,15 @@ export default class EditTopicTimer extends Component {}let statusType = this.topicTimer.status_type;++ if (+ statusType === PUBLISH_TO_CATEGORY_STATUS_TYPE &&+ !this.topicTimer.category_id+ ) {+ this.flash = i18n("topic.topic_status_update.category_required");+ return;+ }+if (statusType === CLOSE_AFTER_LAST_POST_STATUS_TYPE) {statusType = CLOSE_STATUS_TYPE;}diff --git a/frontend/discourse/app/components/topic-timer-info.gjs b/frontend/discourse/app/components/topic-timer-info.gjsindex 368d64cbbdd..7ca93984742 100644--- a/frontend/discourse/app/components/topic-timer-info.gjs+++ b/frontend/discourse/app/components/topic-timer-info.gjs@@ -110,13 +110,15 @@ export default class TopicTimerInfo extends Component {if (categoryId) {const category = Category.findById(categoryId);- options = Object.assign(- {- categoryName: category.get("slug"),- categoryUrl: category.get("url"),- },- options- );+ if (category) {+ options = Object.assign(+ {+ categoryName: category.get("slug"),+ categoryUrl: category.get("url"),+ },+ options+ );+ }}options = Object.assign(options, this.additionalOpts());diff --git a/frontend/discourse/tests/integration/components/topic-timer-info-test.gjs b/frontend/discourse/tests/integration/components/topic-timer-info-test.gjsindex 74d99bf73fa..3627753e352 100644--- a/frontend/discourse/tests/integration/components/topic-timer-info-test.gjs+++ b/frontend/discourse/tests/integration/components/topic-timer-info-test.gjs@@ -6,6 +6,22 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test";module("Integration | Component | topic-timer-info", function (hooks) {setupRenderingTest(hooks);+ test("does not crash when category does not exist in client cache", async function (assert) {+ const executeAt = moment().add(2, "days").toISOString();++ await render(+ <template>+ <TopicTimerInfo+ @statusType="publish_to_category"+ @executeAt={{executeAt}}+ @categoryId={{99999}}+ />+ </template>+ );++ assert.dom(".topic-timer-heading").exists();+ });+test("displays delete after last post notice when basedOnLastPost is true", async function (assert) {const executeAt = moment().add(2, "days").toISOString();diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex b40e17ef032..f6f2206c3ef 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -2389,11 +2389,25 @@ RSpec.describe Topic doTopicTimer.types[:publish_to_category],72,by_user: admin,+ category_id: Fabricate(:category).id,)expect(topic_timer.execute_at).to eq_time(3.days.from_now)end+ it "raises an error for publish_to_category when category_id is not provided" do+ freeze_time+ persisted_topic = Fabricate(:topic)++ expect {+ persisted_topic.set_or_create_timer(+ TopicTimer.types[:publish_to_category],+ 72,+ by_user: admin,+ )+ }.to raise_error(ActiveRecord::RecordInvalid)+ end+it "does not update topic's topic status created_at it was already set to close" doexpect { closing_topic.set_or_create_timer(TopicTimer.types[:close], 14) }.to_not change {closing_topic.topic_timers.first.created_atdiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex b51724d6046..661d387766b 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -5908,6 +5908,32 @@ RSpec.describe TopicsController doendend+ describe "publishing topic to category without category_id" do+ it "should return an error when setting a timer" do+ post "/t/#{topic.id}/timer.json", params: { time: 24, status_type: "publish_to_category" }++ expect(response.status).to eq(404)+ end++ it "should allow removing a timer" do+ topic.set_or_create_timer(+ TopicTimer.types[:publish_to_category],+ 24,+ by_user: admin,+ category_id: topic.category_id,+ )++ post "/t/#{topic.id}/timer.json",+ params: {+ time: nil,+ status_type: "publish_to_category",+ }++ expect(response.status).to eq(200)+ expect(topic.reload.public_topic_timer).to eq(nil)+ end+ end+describe "invalid status type" doit "should raise the right error" dopost "/t/#{topic.id}/timer.json", params: { time: 10, status_type: "something" }