From 81b5d61fa773ea7318a6d93ba62f82967daef631 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 28 May 2018 17:06:47 +0800 Subject: [PATCH] FIX: `topic_destroyed` web hook couldn't find topic. --- lib/topic_view.rb | 21 +++++++++++++-------- spec/components/topic_view_spec.rb | 5 +++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/topic_view.rb b/lib/topic_view.rb index 3c3dbd4e84b..d2bf87de10b 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -37,15 +37,16 @@ class TopicView wpcf.flatten.uniq end - def initialize(topic_id, user = nil, options = {}) - @message_bus_last_id = MessageBus.last_id("/topic/#{topic_id}") + def initialize(topic_or_topic_id, user = nil, options = {}) + @topic = find_topic(topic_or_topic_id) @user = user @guardian = Guardian.new(@user) - @topic = find_topic(topic_id) - @print = options[:print].present? check_and_raise_exceptions + @message_bus_last_id = MessageBus.last_id("/topic/#{@topic.id}") + @print = options[:print].present? + options.each do |key, value| self.instance_variable_set("@#{key}".to_sym, value) end @@ -435,10 +436,14 @@ class TopicView @posts end - def find_topic(topic_id) - # with_deleted covered in #check_and_raise_exceptions - finder = Topic.with_deleted.where(id: topic_id).includes(:category) - finder.first + def find_topic(topic_or_topic_id) + if topic_or_topic_id.is_a?(Topic) + topic_or_topic_id + else + # with_deleted covered in #check_and_raise_exceptions + finder = Topic.with_deleted.where(id: topic_or_topic_id).includes(:category) + finder.first + end end def unfiltered_posts diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index 830b2a2c36b..06fb8099a08 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -13,6 +13,11 @@ describe TopicView do expect { TopicView.new(1231232, evil_trout) }.to raise_error(Discourse::NotFound) end + it "accepts a topic or a topic id" do + expect(TopicView.new(topic, evil_trout).topic).to eq(topic) + expect(TopicView.new(topic.id, evil_trout).topic).to eq(topic) + end + # see also spec/controllers/topics_controller_spec.rb TopicsController::show::permission errors it "raises an error if the user can't see the topic" do Guardian.any_instance.expects(:can_see?).with(topic).returns(false)