Skip to content

Commit 39e218d

Browse files
committed
Moved quick filters into saved searches
And removed all quick filter code
1 parent 63f41af commit 39e218d

10 files changed

Lines changed: 49 additions & 279 deletions

File tree

app/controllers/topics_controller.rb

Lines changed: 2 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class 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
1080907
end

app/helpers/topics_helper.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,4 @@ def topic_title_link(topic)
234234
topic_path(topic)
235235
end
236236
end
237-
238-
def topic_filter_labels
239-
{
240-
"no_contrib_replies" => "No contributor/committer replies",
241-
"patch_no_replies" => "Patch, no replies",
242-
"reading_incomplete" => "Reading in progress",
243-
"new_for_me" => "New for me",
244-
"started_by_me" => "Started by me",
245-
"messaged_by_me" => "I posted here",
246-
"starred_by_me" => "Starred by me",
247-
"starred_by_team" => "Starred by team",
248-
"team_unread" => "Not yet read by team",
249-
"team_reading_others" => "Teammates reading",
250-
"team_reading_any" => "Team reading",
251-
"team_started" => "Started by team",
252-
"team_messaged" => "Team messages"
253-
}
254-
end
255237
end

app/views/layouts/application.html.slim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ html data-theme="light"
4141
- if user_signed_in? && current_user.username.blank?
4242
.global-warning
4343
span Please set a username in Settings.
44-
- starred_active = controller_name == "topics" && action_name == "index" && params[:filter].to_s == "starred_by_me"
44+
- starred_active = controller_name == "topics" && action_name == "search" && params[:q].to_s == "starred:me"
4545
nav.main-navigation
4646
.nav-container
4747
.nav-brand
@@ -61,7 +61,7 @@ html data-theme="light"
6161
- icon_class = starred_active ? "fa-solid fa-star" : "fa-regular fa-star"
6262
- link_classes = ["nav-link"]
6363
- link_classes << "is-active" if starred_active
64-
= link_to topics_path(filter: "starred_by_me"), class: link_classes.join(" "), title: "Starred by me", aria: { label: "Starred by me" } do
64+
= link_to search_topics_path(q: "starred:me"), class: link_classes.join(" "), title: "Starred by me", aria: { label: "Starred by me" } do
6565
i class=icon_class aria-hidden="true"
6666
span.sr-only Starred
6767
= link_to "Search", topics_path(anchor: "search"), class: "nav-link"
@@ -81,7 +81,7 @@ html data-theme="light"
8181
span.tagline PostgreSQL Hackers Archive
8282
- if user_signed_in?
8383
- unread = activity_unread_count
84-
- starred_href = starred_active ? topics_path : topics_path(filter: "starred_by_me")
84+
- starred_href = starred_active ? topics_path : search_topics_path(q: "starred:me")
8585
- starred_title = starred_active ? "All topics" : "Starred by me"
8686
= link_to starred_href, class: "nav-link nav-mobile-star#{' is-active' if starred_active}", title: starred_title, aria: { label: starred_title } do
8787
i class=(starred_active ? "fa-solid fa-star" : "fa-regular fa-star") aria-hidden="true"

app/views/topics/_sidebar.html.slim

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,42 +60,9 @@
6060
- all_team.each do |ss|
6161
li = link_to ss.name, search_topics_path(q: ss.resolve_query(team: team)), class: ("quick-filter-link is-active" if active_search == ss.resolve_query(team: team))
6262

63-
.sidebar-section
64-
h3.sidebar-heading Quick filters
65-
.sidebar-content
66-
- active_filter = params[:filter]
67-
- active_team_id = params[:team_id]&.to_i
68-
69-
.filter-subsection
70-
span.filter-subsection-label Status
71-
ul.quick-filters
72-
li = link_to "No contributor/committer replies", topics_path(filter: "no_contrib_replies"), class: ("quick-filter-link is-active" if active_filter == "no_contrib_replies")
73-
li = link_to "Patch, no replies", topics_path(filter: "patch_no_replies"), class: ("quick-filter-link is-active" if active_filter == "patch_no_replies")
74-
75-
- if user_signed_in?
76-
.filter-subsection
77-
span.filter-subsection-label My activity
78-
ul.quick-filters
79-
li = link_to "Reading in progress", topics_path(filter: "reading_incomplete"), class: ("quick-filter-link is-active" if active_filter == "reading_incomplete")
80-
li = link_to "New for me", topics_path(filter: "new_for_me"), class: ("quick-filter-link is-active" if active_filter == "new_for_me")
81-
li = link_to "Started by me", topics_path(filter: "started_by_me"), class: ("quick-filter-link is-active" if active_filter == "started_by_me")
82-
li = link_to "I posted here", topics_path(filter: "messaged_by_me"), class: ("quick-filter-link is-active" if active_filter == "messaged_by_me")
83-
li = link_to "Starred by me", topics_path(filter: "starred_by_me"), class: ("quick-filter-link is-active" if active_filter == "starred_by_me")
84-
85-
- current_user.teams.each do |team|
86-
.filter-subsection
87-
span.filter-subsection-label = team.name
88-
ul.quick-filters
89-
li = link_to "Starred by team", topics_path(filter: "starred_by_team", team_id: team.id), class: ("quick-filter-link is-active" if active_filter == "starred_by_team" && active_team_id == team.id)
90-
li = link_to "Not yet read by team", topics_path(filter: "team_unread", team_id: team.id), class: ("quick-filter-link is-active" if active_filter == "team_unread" && active_team_id == team.id)
91-
li = link_to "Teammates reading", topics_path(filter: "team_reading_others", team_id: team.id), class: ("quick-filter-link is-active" if active_filter == "team_reading_others" && active_team_id == team.id)
92-
li = link_to "Team reading", topics_path(filter: "team_reading_any", team_id: team.id), class: ("quick-filter-link is-active" if active_filter == "team_reading_any" && active_team_id == team.id)
93-
li = link_to "Started by team", topics_path(filter: "team_started", team_id: team.id), class: ("quick-filter-link is-active" if active_filter == "team_started" && active_team_id == team.id)
94-
li = link_to "Team messages", topics_path(filter: "team_messaged", team_id: team.id), class: ("quick-filter-link is-active" if active_filter == "team_messaged" && active_team_id == team.id)
95-
9663
- if user_signed_in? && @available_note_tags.present?
9764
.sidebar-section
9865
h3.sidebar-heading My tags
9966
ul.quick-filters.tags-list
10067
- @available_note_tags.each do |tag, count|
101-
li = link_to "##{tag} (#{count})", topics_path(note_tag: tag), class: ("quick-filter-link is-active" if params[:note_tag] == tag)
68+
li = link_to "##{tag} (#{count})", search_topics_path(q: "tag:#{tag}"), class: ("quick-filter-link is-active" if params[:q] == "tag:#{tag}")

app/views/topics/_topics.html.slim

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
td colspan=3
66
.topics-empty-state
77
i.fa-regular.fa-folder-open aria-hidden="true"
8-
p No topics match this filter.
9-
- if params[:filter].present? || params[:note_tag].present?
10-
= link_to "Clear filters", topics_path, class: "topics-empty-clear"
8+
p No topics found.
119

1210
- topics.each do |topic|
1311
- tp_data = topic_participants_map[topic.id] || {}

app/views/topics/index.html.slim

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
- cache_block = lambda do
2-
- if params[:filter].present?
3-
= link_to topics_path(note_tag: params[:note_tag]), class: "topics-filter-chip" do
4-
i.fa-solid.fa-filter aria-hidden="true"
5-
span = topic_filter_labels[params[:filter]] || params[:filter].humanize
6-
i.fa-solid.fa-xmark.topics-filter-chip-close aria-hidden="true"
7-
- elsif params[:note_tag].present?
8-
= link_to topics_path, class: "topics-filter-chip" do
9-
i.fa-solid.fa-tag aria-hidden="true"
10-
span = "##{params[:note_tag]}"
11-
i.fa-solid.fa-xmark.topics-filter-chip-close aria-hidden="true"
12-
13-
#new-topics-banner data-controller="new-topics-banner" data-new-topics-banner-url-value=new_topics_count_topics_path(viewing_since: @viewing_since.iso8601, filter: params[:filter], team_id: params[:team_id]) data-new-topics-banner-interval-ms-value="180000"
2+
#new-topics-banner data-controller="new-topics-banner" data-new-topics-banner-url-value=new_topics_count_topics_path(viewing_since: @viewing_since.iso8601) data-new-topics-banner-interval-ms-value="180000"
143
= render partial: "new_topics_banner", locals: { count: @new_topics_count, viewing_since: @viewing_since }
154

165
.topics-table
@@ -26,7 +15,7 @@
2615
- if @topics.size == 25
2716
- last_topic = @topics.last
2817
- cursor = "#{last_topic.last_activity.iso8601}_#{last_topic.id}"
29-
= turbo_frame_tag "pagination", src: topics_path(cursor: cursor, viewing_since: @viewing_since.iso8601, filter: params[:filter], team_id: params[:team_id], note_tag: params[:note_tag], format: :turbo_stream), loading: :lazy do
18+
= turbo_frame_tag "pagination", src: topics_path(cursor: cursor, viewing_since: @viewing_since.iso8601, format: :turbo_stream), loading: :lazy do
3019
.loading-indicator Loading more topics...
3120
- else
3221
= turbo_frame_tag "pagination"

app/views/topics/index.turbo_stream.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- last_topic = @topics.last
1111
- cursor = "#{last_topic.last_activity.iso8601}_#{last_topic.id}"
1212
= turbo_stream.replace "pagination" do
13-
= turbo_frame_tag "pagination", src: topics_path(cursor: cursor, viewing_since: @viewing_since.iso8601, filter: params[:filter], team_id: params[:team_id], note_tag: params[:note_tag], format: :turbo_stream), loading: :lazy do
13+
= turbo_frame_tag "pagination", src: topics_path(cursor: cursor, viewing_since: @viewing_since.iso8601, format: :turbo_stream), loading: :lazy do
1414
.loading-indicator Loading more topics...
1515
- elsif params[:cursor].present?
1616
= turbo_stream.remove "pagination"

0 commit comments

Comments
 (0)