A platform for community discussion. Free, open, simple.
FIX: Topic view breaks with topic timer to publish to restricted category. (#16385)
Alan Guo Xiang Tan0328757ffb39
diff --git a/app/models/topic_timer.rb b/app/models/topic_timer.rbindex 5e80b5d4edd..4fed4519767 100644--- a/app/models/topic_timer.rb+++ b/app/models/topic_timer.rb@@ -112,6 +112,10 @@ class TopicTimer < ActiveRecord::Basetrueend+ def publishing_to_category?+ self.status_type.to_i == TopicTimer.types[:publish_to_category]+ end+privatedef duration_in_range?@@ -138,10 +142,6 @@ class TopicTimer < ActiveRecord::Base))end- def publishing_to_category?- self.status_type.to_i == TopicTimer.types[:publish_to_category]- end-def schedule_auto_delete_replies_jobJobs.enqueue(TopicTimer.type_job_map[:delete_replies], topic_timer_id: id)enddiff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rbindex 268fbc78723..1207a9a5152 100644--- a/app/serializers/topic_view_serializer.rb+++ b/app/serializers/topic_view_serializer.rb@@ -203,6 +203,14 @@ class TopicViewSerializer < ApplicationSerializerenddef topic_timer+ topic_timer = object.topic.public_topic_timer++ return nil if topic_timer.blank?++ if topic_timer.publishing_to_category?+ return nil if !scope.can_see_category?(Category.find_by(id: topic_timer.category_id))+ end+TopicTimerSerializer.new(object.topic.public_topic_timer, root: false)enddiff --git a/spec/serializers/topic_view_serializer_spec.rb b/spec/serializers/topic_view_serializer_spec.rbindex 87cfcbf475d..e2577b77e9f 100644--- a/spec/serializers/topic_view_serializer_spec.rb+++ b/spec/serializers/topic_view_serializer_spec.rb@@ -544,4 +544,54 @@ describe TopicViewSerializer doexpect(json[:requested_group_name]).to eq(nil)endend++ describe '#topic_timer' do+ it 'does not include the attribute when topic does not have a topic timer' do+ json = serialize_topic(topic, user)++ expect(json[:topic_timer]).to eq(nil)+ end++ it 'includes the attribute when topic has a public topic timer' do+ topic_timer = Fabricate(:topic_timer, topic: topic, execute_at: Time.utc(2022, 4, 6, 16, 23, 56))+ json = serialize_topic(topic, user)++ expect(json[:topic_timer][:id]).to eq(topic_timer.id)+ expect(json[:topic_timer][:based_on_last_post]).to eq(false)+ expect(json[:topic_timer][:category_id]).to eq(nil)+ expect(json[:topic_timer][:duration_minutes]).to eq(nil)+ expect(json[:topic_timer][:execute_at]).to eq('2022-04-06T16:23:56.000Z')+ expect(json[:topic_timer][:status_type]).to eq("close")+ end++ it 'does not include the attribute for category topic timer where category is restricted to user' do+ category = Fabricate(:category, read_restricted: true)++ Fabricate(:topic_timer,+ topic: topic,+ category_id: category.id,+ status_type:+ TopicTimer.types[:publish_to_category]+ )++ json = serialize_topic(topic, user)++ expect(json[:topic_timer]).to eq(nil)+ end++ it 'includes the attribute for category topic timer where category is not restricted to user' do+ category = Fabricate(:category, read_restricted: false)++ topic_timer = Fabricate(:topic_timer,+ topic: topic,+ category_id: category.id,+ status_type:+ TopicTimer.types[:publish_to_category]+ )++ json = serialize_topic(topic, user)++ expect(json[:topic_timer][:id]).to eq(topic_timer.id)+ end+ endend