A platform for community discussion. Free, open, simple.
FEATURE: Filter topics by bookmark creation date (#38662)
Gabriel Grubba4ec7d978328f
diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 3beeb310d9a..e233fc45513 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -2898,6 +2898,8 @@ en:exclude_tag_group: "Exclude topics with tags from a specific tag group"activity_before: "Show topics with last activity before a date (YYYY-MM-DD or days ago)"activity_after: "Show topics with last activity after a date (YYYY-MM-DD or days ago)"+ bookmarked_before: "Show topics bookmarked before a date (YYYY-MM-DD or days ago)"+ bookmarked_after: "Show topics bookmarked after a date (YYYY-MM-DD or days ago)"created_before: "Show topics created before a date (YYYY-MM-DD or days ago)"created_after: "Show topics created after a date (YYYY-MM-DD or days ago)"created_by: "Show topics created by a specific user or group"diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex 9354fac1002..47a26ef1f78 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -58,6 +58,10 @@ class TopicsFilterfilter_by_activity(before: filter_values)when "activity-after"filter_by_activity(after: filter_values)+ when "bookmarked-after"+ filter_by_bookmarked(after: filter_values)+ when "bookmarked-before"+ filter_by_bookmarked(before: filter_values)when "category"filter_categories(values: key_prefixes.zip(filter_values))when "created-after"@@ -307,6 +311,16 @@ class TopicsFilter{ name: "in:", description: I18n.t("filter.description.in"), priority: 1 },{ name: "in:pinned", description: I18n.t("filter.description.in_pinned") },{ name: "in:bookmarked", description: I18n.t("filter.description.in_bookmarked") },+ {+ name: "bookmarked-before:",+ description: I18n.t("filter.description.bookmarked_before"),+ type: "date",+ },+ {+ name: "bookmarked-after:",+ description: I18n.t("filter.description.bookmarked_after"),+ type: "date",+ },{ name: "in:watching", description: I18n.t("filter.description.in_watching") },{ name: "in:tracking", description: I18n.t("filter.description.in_tracking") },{ name: "in:muted", description: I18n.t("filter.description.in_muted") },@@ -386,8 +400,8 @@ class TopicsFilterdef extract_and_validate_value_for(filter, values)case filter- when "activity-before", "activity-after", "created-before", "created-after",- "latest-post-before", "latest-post-after"+ when "activity-before", "activity-after", "bookmarked-before", "bookmarked-after",+ "created-before", "created-after", "latest-post-before", "latest-post-after"value = values.lastif match_data = value.match(YYYY_MM_DD_REGEXP)@@ -414,10 +428,11 @@ class TopicsFilterenddef filter_by_topic_range(column_name:, min: nil, max: nil, scope: nil)- { min => ">=", max => "<=" }.each do |value, operator|- next if !value- @scope = (scope || @scope).where("#{column_name} #{operator} ?", value)- end+ return @scope if min.nil? && max.nil?++ value = min || max+ operator = min ? ">=" : "<="+ @scope = (scope || @scope).where("#{column_name} #{operator} ?", value)enddef filter_by_activity(before: nil, after: nil)@@ -428,6 +443,33 @@ class TopicsFilterfilter_by_topic_range(column_name: "topics.created_at", min: after, max: before)end+ def filter_by_bookmarked(before: nil, after: nil)+ return @scope = @scope.none if [email protected]?+ return @scope if before.nil? && after.nil?++ user_id = @guardian.user.id.to_i+ value = before || after+ date_comparison = before ? :lteq : :gteq+ bookmarks_table = Bookmark.arel_table++ topic_bookmarks =+ Bookmark+ .where(user_id: user_id, bookmarkable_type: "Topic")+ .where(bookmarks_table[:created_at].public_send(date_comparison, value))+ .select(:bookmarkable_id)++ post_bookmarks =+ Bookmark+ .joins(+ "INNER JOIN posts ON posts.id = bookmarks.bookmarkable_id AND posts.deleted_at IS NULL",+ )+ .where(user_id: user_id, bookmarkable_type: "Post")+ .where(bookmarks_table[:created_at].public_send(date_comparison, value))+ .select("posts.topic_id")++ @scope = @scope.where(id: topic_bookmarks).or(@scope.where(id: post_bookmarks))+ end+def filter_by_latest_post(before: nil, after: nil)filter_by_topic_range(column_name: "topics.last_posted_at", min: after, max: before)enddiff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex 0ed42e581b8..a3d10215acd 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -40,6 +40,8 @@ RSpec.describe TopicsFilter doin:in:pinnedin:bookmarked+ bookmarked-before:+ bookmarked-after:in:watchingin:trackingin:muted@@ -2331,6 +2333,170 @@ RSpec.describe TopicsFilter do"last posted date"end+ describe "when filtering by bookmark date of topics" do+ fab!(:topic_1, :topic)+ fab!(:topic_2, :topic)+ fab!(:topic_3, :topic)++ it "returns no topics for anonymous users" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("bookmarked-after:2023-01-01")+ .pluck(:id),+ ).to eq([])+ end++ it "returns topics bookmarked on or after the given date" do+ freeze_time Time.zone.local(2024, 1, 15)++ bookmark1 = Fabricate(:bookmark, user: user, bookmarkable: topic_1)+ bookmark1.update_column(:created_at, Time.zone.local(2023, 6, 1))++ bookmark2 = Fabricate(:bookmark, user: user, bookmarkable: topic_2)+ bookmark2.update_column(:created_at, Time.zone.local(2022, 6, 1))++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-after:2023-01-01")+ .pluck(:id),+ ).to contain_exactly(topic_1.id)+ end++ it "returns topics bookmarked on or before the given date" do+ freeze_time Time.zone.local(2024, 1, 15)++ bookmark1 = Fabricate(:bookmark, user: user, bookmarkable: topic_1)+ bookmark1.update_column(:created_at, Time.zone.local(2023, 6, 1))++ bookmark2 = Fabricate(:bookmark, user: user, bookmarkable: topic_2)+ bookmark2.update_column(:created_at, Time.zone.local(2022, 6, 1))++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-before:2023-01-01")+ .pluck(:id),+ ).to contain_exactly(topic_2.id)+ end++ it "supports integer days-ago format" do+ freeze_time do+ bookmark1 = Fabricate(:bookmark, user: user, bookmarkable: topic_1)+ bookmark1.update_column(:created_at, Time.zone.now)++ bookmark2 = Fabricate(:bookmark, user: user, bookmarkable: topic_2)+ bookmark2.update_column(:created_at, 3.days.ago)++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-after:1")+ .pluck(:id),+ ).to contain_exactly(topic_1.id)++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-before:1")+ .where(id: [topic_1.id, topic_2.id])+ .pluck(:id),+ ).to contain_exactly(topic_2.id)+ end+ end++ it "includes post bookmarks" do+ freeze_time Time.zone.local(2024, 1, 15)++ post = Fabricate(:post, topic: topic_1)+ bookmark = Fabricate(:bookmark, user: user, bookmarkable: post)+ bookmark.update_column(:created_at, Time.zone.local(2023, 6, 1))++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-after:2023-01-01")+ .pluck(:id),+ ).to contain_exactly(topic_1.id)+ end++ it "does not return deleted topics even if they are bookmarked" do+ freeze_time Time.zone.local(2024, 1, 15)++ bookmark = Fabricate(:bookmark, user: user, bookmarkable: topic_1)+ bookmark.update_column(:created_at, Time.zone.local(2023, 6, 1))+ topic_1.destroy++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-after:2023-01-01")+ .pluck(:id),+ ).to eq([])+ end++ it "does not return topics if their posts are deleted even if they are bookmarked" do+ freeze_time Time.zone.local(2024, 1, 15)++ post = Fabricate(:post, topic: topic_1)+ bookmark = Fabricate(:bookmark, user: user, bookmarkable: post)+ bookmark.update_column(:created_at, Time.zone.local(2023, 6, 1))+ post.destroy++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-after:2023-01-01")+ .pluck(:id),+ ).to eq([])+ end++ it "does not include other users' bookmarks" do+ freeze_time Time.zone.local(2024, 1, 15)++ other_user = Fabricate(:user)+ bookmark = Fabricate(:bookmark, user: other_user, bookmarkable: topic_1)+ bookmark.update_column(:created_at, Time.zone.local(2023, 6, 1))++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-after:2023-01-01")+ .pluck(:id),+ ).to eq([])+ end++ it "supports combined before and after for date range" do+ freeze_time Time.zone.local(2024, 1, 15)++ bookmark1 = Fabricate(:bookmark, user: user, bookmarkable: topic_1)+ bookmark1.update_column(:created_at, Time.zone.local(2023, 6, 1))++ bookmark2 = Fabricate(:bookmark, user: user, bookmarkable: topic_2)+ bookmark2.update_column(:created_at, Time.zone.local(2022, 6, 1))++ bookmark3 = Fabricate(:bookmark, user: user, bookmarkable: topic_3)+ bookmark3.update_column(:created_at, Time.zone.local(2024, 1, 1))++ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-after:2023-01-01 bookmarked-before:2023-12-31")+ .pluck(:id),+ ).to contain_exactly(topic_1.id)+ end++ it "ignores invalid date values" do+ expect(+ TopicsFilter+ .new(guardian: user.guardian)+ .filter_from_query_string("bookmarked-after:invalid-date")+ .pluck(:id),+ ).to include(topic_1.id, topic_2.id, topic_3.id)+ end+ end+describe "ordering topics filter" dobefore doPlugin::Instance.new.add_filter_custom_filter(