11class TopicsController < ApplicationController
22 before_action :set_topic , only : [ :show , :aware , :read_all , :star , :unstar , :latest_patchset ]
33 before_action :require_authentication , only : [ :aware , :aware_bulk , :aware_all , :read_all , :star , :unstar ]
4- before_action :require_team_membership , only : [ :index , :new_topics_count ]
54
65 def index
76 @search_query = nil
8- base_query = apply_filters ( Topic . includes ( :creator , creator_person : :default_alias , last_sender_person : :default_alias ) )
7+ base_query = Topic . includes ( :creator , creator_person : :default_alias , last_sender_person : :default_alias )
98
109 apply_cursor_pagination ( base_query )
1110 preload_topic_participants
@@ -696,161 +695,8 @@ def preload_team_reader_states(topic_ids, last_ids)
696695 result
697696 end
698697
699- def apply_filters ( base_query )
700- filter = params [ :filter ] . to_s
701- team_id = params [ :team_id ] . presence &.to_i
702- current_user_id = current_user &.id
703- tag_filter = params [ :note_tag ] . to_s . strip . downcase
704-
705- if tag_filter . present? && user_signed_in?
706- visible_notes = Note . active . visible_to ( current_user )
707- tagged_notes = visible_notes . joins ( :note_tags ) . where ( note_tags : { tag : tag_filter } )
708- note_topics = tagged_notes . select ( :topic_id ) . distinct
709-
710- base_query = base_query . joins ( "INNER JOIN (#{ note_topics . to_sql } ) tagged_notes ON tagged_notes.topic_id = topics.id" )
711- @active_note_tag = tag_filter
712- end
713-
714- case filter
715- when "no_contrib_replies"
716- base_query = base_query . joins ( :messages )
717- . left_joins ( messages : { sender : { person : :contributor_memberships } } )
718- . group ( "topics.id" )
719- . having ( "COUNT(DISTINCT contributor_memberships.person_id) = 0" )
720- when "patch_no_replies"
721- base_query = base_query . joins ( messages : :attachments )
722- . group ( "topics.id" )
723- . having ( "COUNT(messages.id) = 1" )
724- . where ( "attachments.file_name ILIKE ? OR attachments.file_name ILIKE ?" , "%.patch" , "%.diff" )
725- when "reading_incomplete"
726- if current_user_id
727- base_query = base_query . joins ( :messages )
728- . joins ( "LEFT JOIN message_read_ranges mrr ON mrr.topic_id = topics.id AND mrr.user_id = #{ current_user_id } " )
729- . group ( "topics.id" )
730- . having ( "COALESCE(MAX(mrr.range_end_message_id), 0) > 0" )
731- . having ( "COALESCE(MAX(mrr.range_end_message_id), 0) < MAX(messages.id)" )
732- end
733- when "new_for_me"
734- if current_user_id
735- aware_before = current_user &.aware_before || Time . at ( 0 )
736- base_query = base_query . joins ( :messages )
737- . joins ( "LEFT JOIN message_read_ranges mrr ON mrr.topic_id = topics.id AND mrr.user_id = #{ current_user_id } " )
738- . group ( "topics.id" )
739- . having ( "MAX(messages.created_at) > ?" , aware_before )
740- . having ( "COALESCE(MAX(mrr.range_end_message_id), 0) < MAX(messages.id)" )
741- end
742- when "team_unread"
743- if team_id && current_user_id
744- member_ids = TeamMember . where ( team_id : team_id ) . select ( :user_id )
745- base_query = base_query . joins ( :messages )
746- . where . not ( id : MessageReadRange . where ( user_id : member_ids ) . select ( :topic_id ) )
747- . group ( "topics.id" )
748- end
749- when "team_reading_others"
750- if team_id && current_user_id
751- teammate_ids = TeamMember . where ( team_id : team_id ) . where . not ( user_id : current_user_id ) . select ( :user_id )
752- base_query = base_query . joins ( :messages )
753- . joins ( "LEFT JOIN message_read_ranges mrr_self ON mrr_self.topic_id = topics.id AND mrr_self.user_id = #{ current_user_id } " )
754- . joins ( "INNER JOIN message_read_ranges mrr_team ON mrr_team.topic_id = topics.id AND mrr_team.user_id IN (#{ teammate_ids . to_sql } )" )
755- . group ( "topics.id" )
756- . having ( "COALESCE(MAX(mrr_self.range_end_message_id), 0) < MAX(messages.id)" )
757- end
758- when "team_reading_any"
759- if team_id && current_user_id
760- member_ids = TeamMember . where ( team_id : team_id ) . select ( :user_id )
761- base_query = base_query . joins ( :messages )
762- . joins ( "INNER JOIN message_read_ranges mrr_team ON mrr_team.topic_id = topics.id AND mrr_team.user_id IN (#{ member_ids . to_sql } )" )
763- . group ( "topics.id" )
764- end
765- when "started_by_me"
766- if current_user_id
767- base_query = base_query . where ( creator_person_id : current_user . person_id )
768- end
769- when "messaged_by_me"
770- if current_user_id
771- base_query = base_query . joins ( :messages ) . where ( messages : { sender_person_id : current_user . person_id } ) . distinct
772- end
773- when "team_started"
774- if team_id
775- member_person_ids = User . joins ( :team_members ) . where ( team_members : { team_id : team_id } ) . select ( :person_id )
776- base_query = base_query . where ( creator_person_id : member_person_ids )
777- end
778- when "team_messaged"
779- if team_id
780- member_person_ids = User . joins ( :team_members ) . where ( team_members : { team_id : team_id } ) . select ( :person_id )
781- base_query = base_query . where ( id : Message . where ( sender_person_id : member_person_ids ) . select ( :topic_id ) )
782- end
783- when "starred_by_me"
784- if current_user_id
785- base_query = base_query . joins ( :topic_stars )
786- . where ( topic_stars : { user_id : current_user_id } )
787- end
788- when "starred_by_team"
789- if team_id && current_user_id
790- member_ids = TeamMember . where ( team_id : team_id ) . select ( :user_id )
791- base_query = base_query . joins ( :topic_stars )
792- . where ( topic_stars : { user_id : member_ids } )
793- . distinct
794- end
795- end
796- base_query
797- end
798-
799- def apply_state_filters
800- filter = params [ :filter ] . to_s
801- return if filter . blank?
802- return unless @topic_states
803- team_id = params [ :team_id ] . presence &.to_i
804-
805- case filter
806- when "reading_incomplete"
807- @topics = @topics . select { |t | @topic_states . dig ( t . id , :status ) == :reading }
808- when "new_for_me"
809- aware_before = current_user . aware_before
810- @topics = @topics . select do |t |
811- state = @topic_states [ t . id ] || { }
812- last_time = state [ :last_time ]
813- last_id = state [ :last_id ]
814- aware_until = state [ :aware_until ]
815- status = state [ :status ]
816-
817- newer_than_global = aware_before . nil? || ( last_time && last_time > aware_before )
818- missing_latest = last_id && ( !aware_until || aware_until < last_id )
819- not_read = status != :read
820-
821- newer_than_global && missing_latest && not_read
822- end
823- when "team_unread"
824- @topics = @topics . select do |t |
825- team_readers = filter_team_readers ( t , team_id : team_id )
826- team_readers . empty?
827- end
828- when "team_reading_others"
829- @topics = @topics . select do |t |
830- state = @topic_states [ t . id ] || { }
831- my_status = state [ :status ]
832- team_readers = filter_team_readers ( t , team_id : team_id )
833- team_readers . any? && my_status != :read && my_status != :reading
834- end
835- when "team_reading_any"
836- @topics = @topics . select do |t |
837- team_readers = filter_team_readers ( t , team_id : team_id )
838- team_readers . any?
839- end
840- end
841-
842- topic_ids = @topics . map ( &:id )
843- @topic_states . slice! ( *topic_ids ) if topic_ids . any?
844- end
845-
846- def filter_team_readers ( topic , team_id :)
847- readers = @topic_states . dig ( topic . id , :team_readers ) || [ ]
848- return readers unless team_id
849- readers . select { |r | r [ :team_id ] == team_id }
850- end
851-
852698 def topics_base_query ( search_query : nil )
853- return apply_filters ( Topic . includes ( :creator , creator_person : :default_alias , last_sender_person : :default_alias ) ) if search_query . nil?
699+ return Topic . includes ( :creator , creator_person : :default_alias , last_sender_person : :default_alias ) if search_query . nil?
854700
855701 cleaned_query = search_query . to_s . strip
856702 return Topic . none if cleaned_query . blank?
@@ -1030,8 +876,6 @@ def hydrate_topics_from_entries(entries)
1030876
1031877 def topics_page_cache_key
1032878 return nil unless @topics &.first
1033- return nil if params [ :filter ] . present? || params [ :team_id ] . present?
1034- return nil if params [ :note_tag ] . present?
1035879
1036880 latest_topic = @topics . first
1037881 watermark = "#{ latest_topic . last_activity . to_i } _#{ latest_topic . id } "
@@ -1041,8 +885,6 @@ def topics_page_cache_key
1041885 def topics_turbo_stream_cache_key
1042886 [
1043887 "topics-index-turbo" ,
1044- params [ :filter ] ,
1045- params [ :team_id ] ,
1046888 params [ :cursor ] . presence || "root" ,
1047889 topic_link_pref_cache_key
1048890 ]
@@ -1053,8 +895,6 @@ def topics_turbo_stream_cache_read
1053895 end
1054896
1055897 def topics_turbo_stream_cache_fetch
1056- return yield if params [ :filter ] . present? || params [ :team_id ] . present? || params [ :note_tag ] . present?
1057-
1058898 Rails . cache . fetch ( topics_turbo_stream_cache_key , expires_in : 10 . minutes ) { yield }
1059899 end
1060900
@@ -1064,17 +904,4 @@ def topic_link_pref_cache_key
1064904 current_user . open_threads_at_first_unread? ? "first-unread" : "top"
1065905 end
1066906
1067- def require_team_membership
1068- team_id = params [ :team_id ] . presence &.to_i
1069- return unless team_id
1070-
1071- unless user_signed_in?
1072- redirect_to new_session_path , alert : "Please sign in"
1073- return
1074- end
1075-
1076- team = Team . find_by ( id : team_id )
1077- render_404 and return unless team
1078- render_404 unless team . member? ( current_user )
1079- end
1080907end
0 commit comments