Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: add support for -user and unseen filter (#34506)

Samf09020eceed2
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 54e5948b4bc..45a1a39bdd0 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2918,6 +2918,7 @@ en:
in_new: "Show topics that are new or have new replies"
in_new_replies: "Show topics with new replies"
in_new_topics: "Show new topics"
+ in_unseen: "Show topics you haven't seen"
order_activity: "Sort by last activity (newest first)"
order_activity_asc: "Sort by last activity (oldest first)"
order_category: "Sort by category name (Z-A)"
@@ -2943,6 +2944,7 @@ en:
users: "Filter topics by participants"
users_any: "Show topics with any of the specified users (comma-separated)"
users_all: "Show topics with all of the specified users (plus-separated)"
+ exclude_users: "Exclude topics with specific users"
group: "Filter topics by groups"
groups_any: "Show topics with any of the specified groups (comma-separated)"
groups_all: "Show topics with all of the specified groups (plus-separated)"
diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index 60bc78f90f0..0010c708c5a 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -430,6 +430,10 @@ class TopicQuery
end
end
+ def self.unseen_filter(list, user)
+ self.new.unseen_filter(list, user.first_seen_at || user.created_at, user.whisperer?)
+ end
+
def self.new_filter(list, treat_as_new_topic_start_date: nil, treat_as_new_topic_clause_sql: nil)
if treat_as_new_topic_start_date
list =
@@ -657,6 +661,13 @@ class TopicQuery
suggested_ordering(results, options)
end
+ def unseen_filter(list, user_first_seen_at, whisperer)
+ list = list.where("topics.bumped_at >= ?", user_first_seen_at)
+
+ col_name = whisperer ? "highest_staff_post_number" : "highest_post_number"
+ list.where("tu.last_read_post_number IS NULL OR tu.last_read_post_number < topics.#{col_name}")
+ end
+
protected
def per_page_setting
@@ -1257,13 +1268,6 @@ class TopicQuery
private
- def unseen_filter(list, user_first_seen_at, whisperer)
- list = list.where("topics.bumped_at >= ?", user_first_seen_at)
-
- col_name = whisperer ? "highest_staff_post_number" : "highest_post_number"
- list.where("tu.last_read_post_number IS NULL OR tu.last_read_post_number < topics.#{col_name}")
- end
-
def apply_max_age_limit(results, options)
if @user
# micro optimisation so we don't load up all of user stats which we do not need
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rb
index a1bc057295c..d55eb27548d 100644
--- a/lib/topics_filter.rb
+++ b/lib/topics_filter.rb
@@ -10,7 +10,12 @@ class TopicsFilter
@topic_notification_levels = Set.new
end
- FILTER_ALIASES = { "categories" => "category", "tags" => "tag", "groups" => "group" }
+ FILTER_ALIASES = {
+ "categories" => "category",
+ "tags" => "tag",
+ "groups" => "group",
+ "user" => "users",
+ }
private_constant :FILTER_ALIASES
def filter_from_query_string(query_string)
@@ -65,7 +70,7 @@ class TopicsFilter
when "order"
order_by(values: filter_values)
when "users"
- filter_users(values: filter_values)
+ filter_users(values: key_prefixes.zip(filter_values))
when "group"
filter_groups(values: filter_values)
when "posts-min"
@@ -198,6 +203,7 @@ class TopicsFilter
description: I18n.t("filter.description.users"),
type: "username",
priority: 1,
+ prefixes: [{ name: "-", description: I18n.t("filter.description.exclude_users") }],
delimiters: [
{ name: ",", description: I18n.t("filter.description.users_any") },
{ name: "+", description: I18n.t("filter.description.users_all") },
@@ -292,6 +298,7 @@ class TopicsFilter
{ name: "in:new", description: I18n.t("filter.description.in_new") },
{ name: "in:new-replies", description: I18n.t("filter.description.in_new_replies") },
{ name: "in:new-topics", description: I18n.t("filter.description.in_new_topics") },
+ { name: "in:unseen", description: I18n.t("filter.description.in_unseen") },
],
)
end
@@ -444,8 +451,10 @@ class TopicsFilter
# users:a,b => any of a or b participated in the topic
# users:a+b => both a and b participated in the topic
+ # -users:a,b => neither a nor b participated in the topic
+ # -users:a+b => at least one of a or b did not participate in the topic
def filter_users(values:)
- values.each do |value|
+ values.each do |prefix, value|
require_all, usernames = calculate_all_or_any(value)
if usernames.empty?
@@ -468,20 +477,34 @@ class TopicsFilter
# A possible alternative is to select the topics with the users with the least posts
# then expand to all of the rest of the users, this can limit the scanning
- user_ids.each_with_index { |uid, idx| @scope = @scope.where(<<~SQL) }
+ if prefix == "-"
+ @scope = @scope.where(<<~SQL, user_ids: user_ids, user_count: user_ids.length)
+ topics.id NOT IN (
+ SELECT p1.topic_id
+ FROM posts p1
+ WHERE p1.user_id IN (:user_ids) AND p1.deleted_at IS NULL
+ GROUP BY p1.topic_id
+ HAVING COUNT(DISTINCT p1.user_id) = :user_count
+ )
+ SQL
+ else
+ user_ids.each_with_index { |uid, idx| @scope = @scope.where(<<~SQL) }
EXISTS (
SELECT 1
FROM posts p#{idx}
- WHERE p#{idx}.topic_id = topics.id AND p#{idx}.user_id = #{uid}
+ WHERE p#{idx}.topic_id = topics.id AND p#{idx}.user_id = #{uid} AND p#{idx}.deleted_at IS NULL
LIMIT 1
)
SQL
+ end
else
+ not_sql = prefix == "-" ? "NOT" : ""
@scope = @scope.where(<<~SQL, user_ids: user_ids)
- topics.id IN (
+ topics.id #{not_sql} IN (
SELECT DISTINCT p.topic_id
FROM posts p
WHERE p.user_id IN (:user_ids)
+ AND p.deleted_at IS NULL
)
SQL
end
@@ -697,6 +720,11 @@ class TopicsFilter
@scope = base.merge(new_topics.or(unread_topics))
end
+ if values.delete("unseen")
+ ensure_topic_users_reference!
+ @scope = TopicQuery.unseen_filter(@scope, @guardian.user)
+ end
+
if values.delete("bookmarked")
ensure_topic_users_reference!
@scope = @scope.where("tu.bookmarked")
diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rb
index b739bcb0a46..3954fe9379b 100644
--- a/spec/lib/topics_filter_spec.rb
+++ b/spec/lib/topics_filter_spec.rb
@@ -45,6 +45,7 @@ RSpec.describe TopicsFilter do
in:muted
in:normal
in:watching_first_post
+ in:unseen
]
user_specific_options.each { |option| expect(anon_option_names).not_to include(option) }
@@ -101,9 +102,7 @@ RSpec.describe TopicsFilter do
end
fab!(:topic_by_u1) { Fabricate(:topic).tap { |t| Fabricate(:post, topic: t, user: u1) } }
-
fab!(:topic_by_u2) { Fabricate(:topic).tap { |t| Fabricate(:post, topic: t, user: u2) } }
-
fab!(:topic_by_u1_and_u2) do
Fabricate(:topic).tap do |t|
Fabricate(:post, topic: t, user: u1)
@@ -139,6 +138,34 @@ RSpec.describe TopicsFilter do
expect(ids).to contain_exactly(topic_by_u1_and_u2.id)
end
+ it "-users:alice,bob returns topics where neither alice nor bob participated" do
+ post = Fabricate(:post)
+ ids =
+ TopicsFilter
+ .new(guardian: Guardian.new)
+ .filter_from_query_string("-users:alice,bob")
+ .pluck(:id)
+ expect(ids).to contain_exactly(post.topic_id)
+ end
+
+ it "-users:alice+bob returns topics where bob and alice did not participate together" do
+ ids =
+ TopicsFilter
+ .new(guardian: Guardian.new)
+ .filter_from_query_string("-users:alice+bob")
+ .pluck(:id)
+ expect(ids).to contain_exactly(topic_by_u1.id, topic_by_u2.id)
+ end
+
+ it "-user:alice,bob (alias) returns topics where neither alice nor bob participated" do
+ ids =
+ TopicsFilter
+ .new(guardian: Guardian.new)
+ .filter_from_query_string("-user:alice,bob")
+ .pluck(:id)
+ expect(ids).to contain_exactly()
+ end
+
it "group:group1 returns topics with participants from the group or group-allowed PMs" do
ids =
TopicsFilter
@@ -289,6 +316,20 @@ RSpec.describe TopicsFilter do
expect(ids).to contain_exactly(new_topic.id, unread_topic.id)
end
+ it "in:unseen returns only unseen topics" do
+ user_for_unseen_filters = user_for_new_filters
+ seen_topic = Fabricate(:topic)
+ TopicUser.update_last_read(user_for_unseen_filters, seen_topic.id, 1, 1, 0)
+ unseen_topic = Fabricate(:topic)
+ ids =
+ TopicsFilter
+ .new(guardian: user_for_unseen_filters.guardian)
+ .filter_from_query_string("in:unseen")
+ .where(id: [seen_topic.id, unseen_topic.id])
+ .pluck(:id)
+ expect(ids).to contain_exactly(unseen_topic.id)
+ end
+
it "anonymous user with in:new returns none" do
ids =
TopicsFilter.new(guardian: Guardian.new).filter_from_query_string("in:new").pluck(:id)