FIX: don't blow up if the topic does not exists anymore

FIX: don’t blow up if the topic does not exists anymore

PERF: only select the only field we use

introduced in 7c741fa0d673f3c808cf3f77127e44e823ea40f8 (cc @romanrizzi)

diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb
index 6f05492..7f46f2c 100644
--- a/app/models/topic_tracking_state.rb
+++ b/app/models/topic_tracking_state.rb
@@ -128,14 +128,14 @@ class TopicTrackingState
   end
 
   def self.publish_read(topic_id, last_read_post_number, user_id, notification_level = nil)
-    topic = Topic.select(:highest_post_number, :archetype, :id).find_by(id: topic_id)
+    highest_post_number = DB.query_single("SELECT highest_post_number FROM topics WHERE id = ?", topic_id).first
 
     message = {
       topic_id: topic_id,
       message_type: "read",
       payload: {
         last_read_post_number: last_read_post_number,
-        highest_post_number: topic.highest_post_number,
+        highest_post_number: highest_post_number,
         topic_id: topic_id,
         notification_level: notification_level
       }

GitHub sha: e3eb4972

1 Like