Skip to content

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

FIX: Respect permissions when counting bookmarks in user summary (#36856)

Régis Hanol06975167e526
diff --git a/app/models/user_summary.rb b/app/models/user_summary.rb
index e635a3d8670..26557159cb9 100644
--- a/app/models/user_summary.rb
+++ b/app/models/user_summary.rb
@@ -121,7 +121,7 @@ class UserSummary
end
def bookmark_count
- Bookmark.where(user: @user).count
+ BookmarkQuery.new(user: @user, guardian: @guardian).count_all
end
def recent_time_read
diff --git a/lib/bookmark_query.rb b/lib/bookmark_query.rb
index b1afee13353..18adb25ec4f 100644
--- a/lib/bookmark_query.rb
+++ b/lib/bookmark_query.rb
@@ -35,133 +35,103 @@ class BookmarkQuery
@count = 0
end
+ def count_all
+ queries = build_list_queries
+ return 0 if queries.empty?
+ Bookmark.from("(#{queries.join(" UNION ")}) AS bookmarks").count
+ end
+
def list_all(&blk)
- ts_query = @search_term.present? ? Search.ts_query(term: @search_term) : nil
- search_term_wildcard = @search_term.present? ? "%#{@search_term}%" : nil
-
- queries =
- Bookmark
- .registered_bookmarkables
- .map do |bookmarkable|
- interim_results = bookmarkable.perform_list_query(@user, @guardian)
-
- # this could occur if there is some security reason that the user cannot
- # access the bookmarkables that they have bookmarked, e.g. if they had 1 bookmark
- # on a topic and that topic was moved into a private category
- next if interim_results.blank?
-
- if @search_term.present?
- interim_results =
- bookmarkable.perform_search_query(interim_results, search_term_wildcard, ts_query)
- end
-
- # this is purely to make the query easy to read and debug, otherwise it's
- # all mashed up into a massive ball in MiniProfiler :)
- "---- #{bookmarkable.model} bookmarkable ---\n\n #{interim_results.to_sql}"
- end
- .compact
-
- # same for interim results being blank, the user might have been locked out
- # from all their various bookmarks, in which case they will see nothing and
- # no further pagination/ordering/etc is required
+ queries = build_list_queries
return [] if queries.empty?
- union_sql = queries.join("\n\nUNION\n\n")
- results = Bookmark.select("bookmarks.*").from("(\n\n#{union_sql}\n\n) as bookmarks")
results =
- results.order(
- "(CASE WHEN bookmarks.pinned THEN 0 ELSE 1 END),
- bookmarks.reminder_at ASC,
- bookmarks.updated_at DESC",
+ Bookmark.from("(#{queries.join(" UNION ")}) AS bookmarks").order(
+ Arel.sql(
+ "(CASE WHEN bookmarks.pinned THEN 0 ELSE 1 END), bookmarks.reminder_at ASC, bookmarks.updated_at DESC",
+ ),
)
@count = results.count
results = results.offset(@page * @per_page) if @page.positive?
-
- if updated_results = blk&.call(results)
- results = updated_results
- end
-
+ results = blk&.call(results) || results
results = results.limit(@per_page).to_a
BookmarkQuery.preload(results, self)
+
results
end
def unread_notifications(limit: 20)
- reminder_notifications =
- Notification
- .for_user_menu(@user.id, limit: [limit, 100].min)
- .unread
- .where(notification_type: Notification.types[:bookmark_reminder])
+ notifications = fetch_reminder_notifications(limit)
+ bookmark_ids = notifications.filter_map { |n| n.data_hash[:bookmark_id] }
- reminder_bookmark_ids = reminder_notifications.map { |n| n.data_hash[:bookmark_id] }.compact
-
- # We preload associations like we do above for the list to avoid
- # N1s in the can_see? guardian calls for each bookmark.
- bookmarks = Bookmark.where(user: @user, id: reminder_bookmark_ids)
+ bookmarks = Bookmark.where(user: @user, id: bookmark_ids)
BookmarkQuery.preload(bookmarks, self)
- # Any bookmarks that no longer exist, we need to find the associated
- # records using bookmarkable details.
- #
- # First we want to group these by type into a hash to reduce queries:
- #
- # {
- # "Post": {
- # 1234: <Post>,
- # 566: <Post>,
- # },
- # "Topic": {
- # 123: <Topic>,
- # 99: <Topic>,
- # }
- # }
- #
- # We may not need to do this most of the time. It depends mostly on
- # a user's auto_delete_preference for bookmarks.
- deleted_bookmark_ids = reminder_bookmark_ids - bookmarks.map(&:id)
- deleted_bookmarkables =
- reminder_notifications
- .select do |notif|
- deleted_bookmark_ids.include?(notif.data_hash[:bookmark_id]) &&
- notif.data_hash[:bookmarkable_type].present?
- end
- .inject({}) do |hash, notif|
- hash[notif.data_hash[:bookmarkable_type]] ||= {}
- hash[notif.data_hash[:bookmarkable_type]][notif.data_hash[:bookmarkable_id]] = nil
- hash
- end
-
- # Then, we can actually find the associated records for each type in the database.
- deleted_bookmarkables.each do |type, bookmarkable|
- records = Bookmark.registered_bookmarkable_from_type(type).model.where(id: bookmarkable.keys)
- records.each { |record| deleted_bookmarkables[type][record.id] = record }
+ bookmarks_by_id = bookmarks.index_by(&:id)
+ deleted_bookmarkables = load_deleted_bookmarkables(notifications, bookmarks_by_id)
+
+ notifications.select do |n|
+ can_see_notification_bookmark?(n, bookmarks_by_id, deleted_bookmarkables)
+ end
+ end
+
+ private
+
+ def build_list_queries
+ Bookmark.registered_bookmarkables.filter_map do |bookmarkable|
+ query = bookmarkable.perform_list_query(@user, @guardian)
+ next if query.blank?
+ query = apply_search_filter(bookmarkable, query) if @search_term.present?
+ query.to_sql
end
+ end
+
+ def apply_search_filter(bookmarkable, query)
+ ts_query = Search.ts_query(term: @search_term)
+ bookmarkable.perform_search_query(query, "%#{@search_term}%", ts_query)
+ end
+
+ def fetch_reminder_notifications(limit)
+ Notification
+ .for_user_menu(@user.id, limit: [limit, 100].min)
+ .unread
+ .where(notification_type: Notification.types[:bookmark_reminder])
+ end
- reminder_notifications.select do |notif|
- bookmark = bookmarks.find { |bm| bm.id == notif.data_hash[:bookmark_id] }
-
- # This is the happy path, it's easiest to look up using a bookmark
- # that hasn't been deleted.
- if bookmark.present?
- bookmarkable = Bookmark.registered_bookmarkable_from_type(bookmark.bookmarkable_type)
- bookmarkable.can_see?(@guardian, bookmark)
- else
- # Otherwise, we have to use our cached records from the deleted
- # bookmarks' related bookmarkable (e.g. Post, Topic) to determine
- # secure access.
- bookmarkable =
- deleted_bookmarkables.dig(
- notif.data_hash[:bookmarkable_type],
- notif.data_hash[:bookmarkable_id],
- )
- bookmarkable.present? &&
- Bookmark.registered_bookmarkable_from_type(
- notif.data_hash[:bookmarkable_type],
- ).can_see_bookmarkable?(@guardian, bookmarkable)
+ def load_deleted_bookmarkables(notifications, bookmarks_by_id)
+ notifications
+ .select do |n|
+ data = n.data_hash
+ data[:bookmark_id].present? && data[:bookmarkable_type].present? &&
+ !bookmarks_by_id[data[:bookmark_id]]
end
+ .group_by { |n| n.data_hash[:bookmarkable_type] }
+ .transform_values do |notifs|
+ ids = notifs.map { |n| n.data_hash[:bookmarkable_id] }
+ type = notifs.first.data_hash[:bookmarkable_type]
+ Bookmark.registered_bookmarkable_from_type(type).model.where(id: ids).index_by(&:id)
+ end
+ end
+
+ def can_see_notification_bookmark?(notification, bookmarks_by_id, deleted_bookmarkables)
+ data = notification.data_hash
+ return false if data[:bookmark_id].nil?
+
+ if bookmark = bookmarks_by_id[data[:bookmark_id]]
+ Bookmark.registered_bookmarkable_from_type(bookmark.bookmarkable_type).can_see?(
+ @guardian,
+ bookmark,
+ )
+ else
+ bookmarkable = deleted_bookmarkables.dig(data[:bookmarkable_type], data[:bookmarkable_id])
+ bookmarkable &&
+ Bookmark.registered_bookmarkable_from_type(data[:bookmarkable_type]).can_see_bookmarkable?(
+ @guardian,
+ bookmarkable,
+ )
end
end
end
diff --git a/spec/lib/bookmark_query_spec.rb b/spec/lib/bookmark_query_spec.rb
index ea0a6791049..7b05e087a13 100644
--- a/spec/lib/bookmark_query_spec.rb
+++ b/spec/lib/bookmark_query_spec.rb
@@ -1,276 +1,311 @@
# frozen_string_literal: true
RSpec.describe BookmarkQuery do
- before { SearchIndexer.enable }
-
fab!(:user)
- def bookmark_query(user: nil, search_term: nil, per_page: nil)
- BookmarkQuery.new(user: user || self.user, search_term:, per_page:)
+ def bookmark_query(user: nil, guardian: nil, search_term: nil, per_page: nil)
+ BookmarkQuery.new(user: user || self.user, guardian:, search_term:, per_page:)
end
- describe "#list_all" do
+ describe "#count_all" do
+ fab!(:post_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:post)) }
+ fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic)) }
+
before do
- register_test_bookmarkable
+ Fabricate(:topic_user, user:, topic: post_bookmark.bookmarkable.topic)
+ Fabricate(:topic_user, user:, topic: topic_bookmark.bookmarkable)
+ end
- Fabricate(:topic_user, user: user, topic: post_bookmark.bookmarkable.topic)
- Fabricate(:topic_user, user: user, topic: topic_bookmark.bookmarkable)
- user_bookmark
+ it "counts all accessible bookmarks" do
+ expect(bookmark_query.count_all).to eq(2)
end
- after { DiscoursePluginRegistry.reset! }
+ it "excludes deleted bookmarkables" do
+ post_bookmark.bookmarkable.trash!
+ expect(bookmark_query.count_all).to eq(1)
+ end
+
+ it "excludes bookmarks in inaccessible private categories" do
+ group = Fabricate(:group)
+ post_bookmark.bookmarkable.topic.update!(category: Fabricate(:private_category, group:))
+ expect(bookmark_query.count_all).to eq(1)
+
+ group.add(user)
+ expect(bookmark_query(guardian: Guardian.new(user.reload)).count_all).to eq(2)
+ end
+
+ it "returns 0 when all bookmarks are inaccessible" do
+ private_category = Fabricate(:private_category, group: Fabricate(:group))
+ post_bookmark.bookmarkable.topic.update!(category: private_category)
+ topic_bookmark.bookmarkable.update!(category: private_category)
+ expect(bookmark_query.count_all).to eq(0)
+ end
+ end
+
+ describe "#list_all" do
+ fab!(:post_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:post)) }
+ fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic)) }
- let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }
- let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }
let(:user_bookmark) do
- Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user, username: "bookmarkqueen"))
+ Fabricate(:bookmark, user:, bookmarkable: Fabricate(:user, username: "bookmarkqueen"))
+ end
+
+ before do
+ register_test_bookmarkable
+ Fabricate(:topic_user, user:, topic: post_bookmark.bookmarkable.topic)
+ Fabricate(:topic_user, user:, topic: topic_bookmark.bookmarkable)
+ user_bookmark
end
- it "returns all the bookmarks for a user" do
- expect(bookmark_query.list_all.count).to eq(3)
+ after { DiscoursePluginRegistry.reset! }
+
+ it "returns all bookmarks for a user" do
+ expect(bookmark_query.list_all.map(&:id)).to contain_exactly(
+ post_bookmark.id,
+ topic_bookmark.id,
+ user_bookmark.id,
+ )
end
- it "does not return deleted bookmarkables" do
+ it "excludes deleted bookmarkables" do
post_bookmark.bookmarkable.trash!
topic_bookmark.bookmarkable.trash!
- expect(bookmark_query.list_all.count).to eq(1)
+ expect(bookmark_query.list_all.map(&:id)).to contain_exactly(user_bookmark.id)
end
- it "runs the on_preload block provided passing in bookmarks" do
- preloaded_bookmarks = []
- BookmarkQuery.on_preload { |bookmarks, bq| (preloaded_bookmarks << bookmarks).flatten }
+ it "runs on_preload callbacks" do
+ preloaded = []
+ BookmarkQuery.on_preload { |bookmarks, _| preloaded.concat(bookmarks) }
bookmark_query.list_all
- expect(preloaded_bookmarks.any?).to eq(true)
+ expect(preloaded).to be_present
end
- it "returns a mixture of post, topic, and custom bookmarkable type bookmarks" do
- bookmarks = bookmark_query.list_all
- expect(bookmarks.map(&:id)).to match_array(
- [post_bookmark.id, topic_bookmark.id, user_bookmark.id],
+ it "handles nil from bookmarkable list_query" do
+ UserTestBookmarkable.expects(:list_query).returns(nil)
+ expect(bookmark_query.list_all.map(&:id)).to contain_exactly(
+ post_bookmark.id,
+ topic_bookmark.id,
)
end
- it "handles the user not having permission for all of the bookmarks of a certain bookmarkable" do
+ it "returns empty when user has no accessible bookmarks" do
+ private_category = Fabricate(:private_category, group: Fabricate(:group))
+ topic_bookmark.bookmarkable.update!(category: private_category)
+ post_bookmark.bookmarkable.topic.update!(category: private_category)
UserTestBookmarkable.expects(:list_query).returns(nil)
- bookmarks = bookmark_query.list_all
- expect(bookmarks.map(&:id)).to match_array([post_bookmark.id, topic_bookmark.id])
+ expect(bookmark_query.list_all).to be_empty
end
- it "handles the user not having permission to see any of their bookmarks" do
- topic_bookmark.bookmarkable.update(
- category: Fabricate(:private_category, group: Fabricate(:group)),
- )
- post_bookmark.bookmarkable.topic.update(category: topic_bookmark.bookmarkable.category)
- UserTestBookmarkable.expects(:list_query).returns(nil)
- bookmarks = bookmark_query.list_all
- expect(bookmarks.map(&:id)).to eq([])
- end
+ context "with search_term" do
+ before_all { SearchIndexer.enable }
- context "when search_term is provided" do
- let!(:post) do
- Fabricate(
- :post,
- raw: "Some post content here",
- topic: Fabricate(:topic, title: "Bugfix game for devs"),
- )
+ fab!(:named_bookmark) do
+ bm = Fabricate(:bookmark, user:, name: "Check later", bookmarkable: Fabricate(:post))
+ Fabricate(:topic_user, user:, topic: bm.bookmarkable.topic)
+ bm
end
- after { DiscoursePluginRegistry.reset! }
+ fab!(:content_bookmark) do
+ post =
+ Fabricate(
+ :post,
+ raw: "Special content here",
+ topic: Fabricate(:topic, title: "Unique topic title"),
+ )
+ bm = Fabricate(:bookmark, user:, bookmarkable: post)
+ Fabricate(:topic_user, user:, topic: post.topic)
+ bm
+ end
- let(:bookmark3) do
- Fabricate(:bookmark, user: user, name: "Check up later", bookmarkable: Fabricate(:post))
+ it "searches by bookmark name" do
+ expect(bookmark_query(search_term: "check").list_all.map(&:id)).to eq([named_bookmark.id])
end
- let(:bookmark4) { Fabricate(:bookmark, user: user, bookmarkable: post) }
- let!(:bookmark_with_colon) do
- Fabricate(
- :bookmark,
- user: user,
- name: "Review with:images filter",
- bookmarkable: Fabricate(:post),
+
+ it "searches by post content" do
+ expect(bookmark_query(search_term: "special").list_all.map(&:id)).to eq(
+ [content_bookmark.id],
)
end
- before do
- Fabricate(:topic_user, user: user, topic: bookmark3.bookmarkable.topic)
- Fabricate(:topic_user, user: user, topic: bookmark4.bookmarkable.topic)
- Fabricate(:topic_user, user: user, topic: bookmark_with_colon.bookmarkable.topic)
+ it "searches by topic title" do
+ expect(bookmark_query(search_term: "unique").list_all.map(&:id)).to eq(
+ [content_bookmark.id],
+ )
end
- it "can search by bookmark name" do
- bookmarks = bookmark_query(search_term: "check").list_all
- expect(bookmarks.map(&:id)).to eq([bookmark3.id])
+ it "handles colons in search term" do
+ named_bookmark.update!(name: "Review with:images")
+ expect(bookmark_query(search_term: "with:images").list_all.map(&:id)).to eq(
+ [named_bookmark.id],
+ )
end
+ end
- it "can search by post content" do
- bookmarks = bookmark_query(search_term: "content").list_all
- expect(bookmarks.map(&:id)).to eq([bookmark4.id])
- end
+ context "with whispered posts" do
+ fab!(:whisperers_group, :group)
- it "can search by topic title" do
- bookmarks = bookmark_query(search_term: "bugfix").list_all
- expect(bookmarks.map(&:id)).to eq([bookmark4.id])
+ before do
+ post_bookmark.bookmarkable.update!(post_type: Post.types[:whisper])
+ SiteSetting.whispers_allowed_groups = Group::AUTO_GROUPS[:staff].to_s
end
- it "allows searching when the search term contains a colon" do
- bookmarks = bookmark_query(search_term: "with:images").list_all
- expect(bookmarks.map(&:id)).to eq([bookmark_with_colon.id])
+ it "includes whisper for moderator" do
+ user.grant_moderation!
+ expect(bookmark_query.list_all).to include(post_bookmark)
end
- context "with custom bookmarkable fitering" do
- before { register_test_bookmarkable }
-
- after { DiscoursePluginRegistry.reset! }
-
- let!(:bookmark5) do
- Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user, username: "bookmarkking"))
- end
-
- it "allows searching bookmarkables by fields in other tables" do
- bookmarks = bookmark_query(search_term: "bookmarkk").list_all
- expect(bookmarks.map(&:id)).to eq([bookmark5.id])
- end
+ it "includes whisper for admin" do
+ user.grant_admin!
+ expect(bookmark_query.list_all).to include(post_bookmark)
end
- end
- context "for a whispered post" do
- before do
- post_bookmark.bookmarkable.update(post_type: Post.types[:whisper])
- SiteSetting.whispers_allowed_groups = "#{Group::AUTO_GROUPS[:staff]}"
+ it "includes whisper for whisperers group member" do
+ SiteSetting.whispers_allowed_groups = whisperers_group.id.to_s
+ whisperers_group.add(user)
+ expect(bookmark_query.list_all).to include(post_bookmark)
end
- fab!(:whisperers_group, :group)
- context "when the user is moderator" do
- it "does return the whispered post" do
- user.grant_moderation!
- expect(bookmark_query.list_all.count).to eq(3)
- end
- end
- context "when the user is admin" do
- it "does return the whispered post" do
- user.grant_admin!
- expect(bookmark_query.list_all.count).to eq(3)
- end
- end
- context "when the user is a member of whisperers group" do
- it "returns the whispered post" do
- SiteSetting.whispers_allowed_groups = "#{whisperers_group.id}"
- user.update!(groups: [whisperers_group])
- expect(bookmark_query.list_all.count).to eq(3)
- end
- end
- context "when the user is not staff" do
- it "does not return the whispered post" do
- expect(bookmark_query.list_all.count).to eq(2)
- end
+ it "excludes whisper for regular user" do
+ expect(bookmark_query.list_all).not_to include(post_bookmark)
end
end
- context "for a private message topic bookmark" do
- let(:pm_topic) { Fabricate(:private_message_topic) }
+ context "with private message bookmarks" do
+ fab!(:pm_topic, :private_message_topic)
+ fab!(:pm_post) { Fabricate(:post, topic: pm_topic) }
+
before do
- post_bookmark.update(bookmarkable: Fabricate(:post, topic: pm_topic))
+ post_bookmark.update!(bookmarkable: pm_post)
TopicUser.change(user.id, pm_topic.id, total_msecs_viewed: 1)
end
- context "when the user is a topic_allowed_user" do
- before { TopicAllowedUser.create(topic: pm_topic, user: user) }
- it "shows the user the bookmark in the PM" do
- expect(bookmark_query.list_all.map(&:id).count).to eq(3)
- end
+ it "includes PM bookmark for allowed user" do
+ TopicAllowedUser.create!(topic: pm_topic, user:)
+ expect(bookmark_query.list_all).to include(post_bookmark)
end
- context "when the user is in a topic_allowed_group" do
- before do
- group = Fabricate(:group)
- GroupUser.create(group: group, user: user)
- TopicAllowedGroup.create(topic: pm_topic, group: group)
- end
- it "shows the user the bookmark in the PM" do
- expect(bookmark_query.list_all.map(&:id).count).to eq(3)
- end
+ it "includes PM bookmark for allowed group member" do
+ group = Fabricate(:group)
+ group.add(user)
+ TopicAllowedGroup.create!(topic: pm_topic, group:)
+ expect(bookmark_query.list_all).to include(post_bookmark)
end
- context "when the user is not a topic_allowed_user" do
- it "does not show the user a bookmarked post in a PM where they are not an allowed user" do
- expect(bookmark_query.list_all.map(&:id).count).to eq(2)
- end
- end
-
- context "when the user is not in a topic_allowed_group" do
- it "does not show the user a bookmarked post in a PM where they are not in an allowed group" do
- expect(bookmark_query.list_all.map(&:id).count).to eq(2)
- end
+ it "excludes PM bookmark for non-allowed user" do
+ expect(bookmark_query.list_all).not_to include(post_bookmark)
end
end
- context "when the topic category is private" do
- let(:group) { Fabricate(:group) }
- before do
- post_bookmark.bookmarkable.topic.update(
- category: Fabricate(:private_category, group: group),
- )
- post_bookmark.reload
- end
- it "does not show the user a post/topic in a private category they cannot see" do
- expect(bookmark_query.list_all.map(&:id)).not_to include(post_bookmark.id)
+ context "with private categories" do
+ fab!(:group)
+ fab!(:private_category) { Fabricate(:private_category, group:) }
+
+ before { post_bookmark.bookmarkable.topic.update!(category: private_category) }
+
+ it "excludes bookmark in inaccessible category" do
+ expect(bookmark_query.list_all).not_to include(post_bookmark)
end
- it "does show the user a post/topic in a private category they can see" do
- GroupUser.create(user: user, group: group)
- expect(bookmark_query.list_all.map(&:id)).to include(post_bookmark.id)
+
+ it "includes bookmark when user gains access" do
+ group.add(user)
+ expect(bookmark_query.list_all).to include(post_bookmark)
end
end
- context "when the per_page is provided" do
- it "is respected" do
+ context "with pagination" do
+ it "respects per_page" do
expect(bookmark_query(per_page: 1).list_all.count).to eq(1)
end
end
end
describe "#list_all ordering" do
- let!(:bookmark1) { Fabricate(:bookmark, user: user, updated_at: 1.day.ago, reminder_at: nil) }
- let!(:bookmark2) { Fabricate(:bookmark, user: user, updated_at: 2.days.ago, reminder_at: nil) }
- let!(:bookmark3) { Fabricate(:bookmark, user: user, updated_at: 6.days.ago, reminder_at: nil) }
- let!(:bookmark4) { Fabricate(:bookmark, user: user, updated_at: 4.days.ago, reminder_at: nil) }
- let!(:bookmark5) { Fabricate(:bookmark, user: user, updated_at: 3.days.ago, reminder_at: nil) }
+ fab!(:bookmark1) { Fabricate(:bookmark, user:, updated_at: 1.day.ago) }
+ fab!(:bookmark2) { Fabricate(:bookmark, user:, updated_at: 2.days.ago) }
+ fab!(:bookmark3) { Fabricate(:bookmark, user:, updated_at: 3.days.ago) }
before do
- [bookmark1, bookmark2, bookmark3, bookmark4, bookmark5].each do |bm|
- Fabricate(:topic_user, topic: bm.bookmarkable.topic, user: user)
- bm.reload
+ [bookmark1, bookmark2, bookmark3].each do |bm|
+ Fabricate(:topic_user, topic: bm.bookmarkable.topic, user:)
end
end
- it "order defaults to updated_at DESC" do
- expect(bookmark_query.list_all.map(&:id)).to eq(
- [bookmark1.id, bookmark2.id, bookmark5.id, bookmark4.id, bookmark3.id],
- )
+ it "orders by updated_at DESC by default" do
+ expect(bookmark_query.list_all.map(&:id)).to eq([bookmark1.id, bookmark2.id, bookmark3.id])
end
- it "orders by reminder_at, then updated_at" do
- bookmark4.update_column(:reminder_at, 1.day.from_now)
- bookmark5.update_column(:reminder_at, 26.hours.from_now)
-
- expect(bookmark_query.list_all.map(&:id)).to eq(
- [bookmark4.id, bookmark5.id, bookmark1.id, bookmark2.id, bookmark3.id],
- )
+ it "prioritizes reminder_at over updated_at" do
+ bookmark3.update_column(:reminder_at, 1.hour.from_now)
+ expect(bookmark_query.list_all.first).to eq(bookmark3)
end
- it "shows pinned bookmarks first ordered by reminder_at ASC then updated_at DESC" do
+ it "prioritizes pinned bookmarks" do
bookmark3.update_column(:pinned, true)
- bookmark3.update_column(:reminder_at, 1.day.from_now)
+ expect(bookmark_query.list_all.first).to eq(bookmark3)
+ end
+ end
- bookmark4.update_column(:pinned, true)
- bookmark4.update_column(:reminder_at, 28.hours.from_now)
+ describe "#unread_notifications" do
+ fab!(:post)
+ fab!(:bookmark) { Fabricate(:bookmark, user:, bookmarkable: post) }
- bookmark1.update_column(:pinned, true)
- bookmark2.update_column(:pinned, true)
+ before { Fabricate(:topic_user, user:, topic: post.topic) }
- bookmark5.update_column(:reminder_at, 1.day.from_now)
+ def create_reminder_notification(bm)
+ topic =
+ case bm.bookmarkable
+ when Post
+ bm.bookmarkable.topic
+ when Topic
+ bm.bookmarkable
+ end
- expect(bookmark_query.list_all.map(&:id)).to eq(
- [bookmark3.id, bookmark4.id, bookmark1.id, bookmark2.id, bookmark5.id],
+ Fabricate(
+ :notification,
+ user:,
+ topic:,
+ notification_type: Notification.types[:bookmark_reminder],
+ data: {
+ bookmark_id: bm.id,
+ bookmarkable_type: bm.bookmarkable_type,
+ bookmarkable_id: bm.bookmarkable_id,
+ }.to_json,
)
end
+
+ it "returns unread bookmark reminder notifications" do
+ notification = create_reminder_notification(bookmark)
+ expect(bookmark_query.unread_notifications).to contain_exactly(notification)
+ end
+
+ it "excludes notifications for inaccessible bookmarks" do
+ create_reminder_notification(bookmark)
+ post.topic.update!(category: Fabricate(:private_category, group: Fabricate(:group)))
+ expect(bookmark_query.unread_notifications).to be_empty
+ end
+
+ it "handles deleted bookmarks by checking bookmarkable access" do
+ notification = create_reminder_notification(bookmark)
+ bookmark.delete # Use delete to skip callbacks and keep the notification
+
+ expect(bookmark_query.unread_notifications).to contain_exactly(notification)
+ end
+
+ it "excludes notifications for deleted bookmarks with inaccessible bookmarkables" do
+ create_reminder_notification(bookmark)
+ bookmark.delete # Use delete to skip callbacks and keep the notification
+ post.topic.update!(category: Fabricate(:private_category, group: Fabricate(:group)))
+ expect(bookmark_query.unread_notifications).to be_empty
+ end
+
+ it "respects limit parameter" do
+ 3.times do
+ bm = Fabricate(:bookmark, user:, bookmarkable: Fabricate(:post))
+ Fabricate(:topic_user, user:, topic: bm.bookmarkable.topic)
+ create_reminder_notification(bm)
+ end
+
+ expect(bookmark_query.unread_notifications(limit: 2).count).to eq(2)
+ end
end
end
diff --git a/spec/models/user_summary_spec.rb b/spec/models/user_summary_spec.rb
index 9146341fea4..343975875cc 100644
--- a/spec/models/user_summary_spec.rb
+++ b/spec/models/user_summary_spec.rb
@@ -91,6 +91,35 @@ RSpec.describe UserSummary do
expect(summary.top_categories.first[:post_count]).to eq(1)
end
+ describe "#bookmark_count" do
+ fab!(:user)
+ fab!(:post_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:post)) }
+ fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic)) }
+
+ let(:summary) { UserSummary.new(user, Guardian.new(user)) }
+
+ before do
+ Fabricate(:topic_user, user:, topic: post_bookmark.bookmarkable.topic)
+ Fabricate(:topic_user, user:, topic: topic_bookmark.bookmarkable)
+ end
+
+ it "counts accessible bookmarks" do
+ expect(summary.bookmark_count).to eq(2)
+ end
+
+ it "excludes bookmarks in inaccessible categories" do
+ post_bookmark.bookmarkable.topic.update!(
+ category: Fabricate(:private_category, group: Fabricate(:group)),
+ )
+ expect(summary.bookmark_count).to eq(1)
+ end
+
+ it "excludes bookmarks for deleted topics" do
+ post_bookmark.bookmarkable.topic.trash!
+ expect(summary.bookmark_count).to eq(1)
+ end
+ end
+
it "returns the most replied to users" do
topic1 = create_post.topic
topic1_post = create_post(topic: topic1)