FIX: Change order of topic_tracking_state SELECT SQL (#13259)

FIX: Change order of topic_tracking_state SELECT SQL (#13259)

This allows us to do DISTINCT on the topic_id to remove duplicates (e.g. in extensions to the report SQL), and also introduces an additional_join_sql string to allow extensions to JOIN additional tables.

diff --git a/app/models/topic_tracking_state.rb b/app/models/topic_tracking_state.rb
index 1bda223..77c5766 100644
--- a/app/models/topic_tracking_state.rb
+++ b/app/models/topic_tracking_state.rb
@@ -380,7 +380,8 @@ class TopicTrackingState
     staff: false,
     admin: false,
     select: nil,
-    custom_state_filter: nil
+    custom_state_filter: nil,
+    additional_join_sql: nil
   )
     unread =
       if skip_unread
@@ -404,11 +405,11 @@ class TopicTrackingState
       end
 
     select_sql = select || "
+           DISTINCT topics.id as topic_id,
            u.id as user_id,
-           topics.id as topic_id,
            topics.created_at,
            topics.updated_at,
-           #{staff ? "highest_staff_post_number highest_post_number" : "highest_post_number"},
+           #{highest_post_number_column_select(staff)},
            last_read_post_number,
            c.id as category_id,
            tu.notification_level,
@@ -478,6 +479,7 @@ class TopicTrackingState
       LEFT JOIN topic_users tu ON tu.topic_id = topics.id AND tu.user_id = u.id
       LEFT JOIN category_users ON category_users.category_id = topics.category_id AND category_users.user_id = #{user.id}
       LEFT JOIN dismissed_topic_users ON dismissed_topic_users.topic_id = topics.id AND dismissed_topic_users.user_id = #{user.id}
+      #{additional_join_sql}
       WHERE u.id = :user_id AND
             #{filter_old_unread_sql}
             topics.archetype <> 'private_message' AND
@@ -506,6 +508,10 @@ class TopicTrackingState
     sql
   end
 
+  def self.highest_post_number_column_select(staff)
+    "#{staff ? "topics.highest_staff_post_number AS highest_post_number" : "topics.highest_post_number"}"
+  end
+
   def self.publish_private_message(topic, archive_user_id: nil,
                                           post: nil,
                                           group_archive: false)

GitHub sha: 83211cff

This commit appears in #13259 which was approved by SamSaffron. It was merged by SamSaffron.