discourse/spec/serializers/user_bookmark_serializer_spec.rb
Martin Brennan 27699648ef
FEATURE: Go to last unread for topic-level bookmark links (#14396)
Instead of going to the OP of the topic for topic-level bookmarks
(which are bookmarks where for_topic is true) when clicking on the
bookmark in the quick access menu or on the user bookmark list,
this commit takes the user to the last unread post in
the topic instead. This should be generally more useful than landing
on the unchanging OP.

To make this work nicely, I needed to add the last_read_post_number to
the BookmarkQuery based on the TopicUser association. It should not add
too much extra weight to the query, because it is limited to the user
that we are fetching bookmarks for.

Also fixed an issue where the bookmark serializer highest_post_number was
not taking into account whether the user was staff, which is when we
should use highest_staff_post_number instead.
2021-09-21 13:49:56 +10:00

49 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe UserBookmarkSerializer do
let(:user) { Fabricate(:user) }
let(:post) { Fabricate(:post, user: user) }
let!(:bookmark) { Fabricate(:bookmark, name: 'Test', user: user, post: post) }
it "serializes all properties correctly" do
s = UserBookmarkSerializer.new(bookmark, scope: Guardian.new(user))
expect(s.id).to eq(bookmark.id)
expect(s.created_at).to eq_time(bookmark.created_at)
expect(s.topic_id).to eq(bookmark.topic_id)
expect(s.linked_post_number).to eq(bookmark.post.post_number)
expect(s.post_id).to eq(bookmark.post_id)
expect(s.name).to eq(bookmark.name)
expect(s.reminder_at).to eq_time(bookmark.reminder_at)
expect(s.title).to eq(bookmark.topic.title)
expect(s.deleted).to eq(false)
expect(s.hidden).to eq(false)
expect(s.closed).to eq(false)
expect(s.archived).to eq(false)
expect(s.category_id).to eq(bookmark.topic.category_id)
expect(s.archetype).to eq(bookmark.topic.archetype)
expect(s.highest_post_number).to eq(1)
expect(s.bumped_at).to eq_time(bookmark.topic.bumped_at)
expect(s.slug).to eq(bookmark.topic.slug)
expect(s.post_user_username).to eq(bookmark.post.user.username)
expect(s.post_user_name).to eq(bookmark.post.user.name)
expect(s.post_user_avatar_template).not_to eq(nil)
end
it "uses the correct highest_post_number column based on whether the user is staff" do
Fabricate(:post, topic: bookmark.topic)
Fabricate(:post, topic: bookmark.topic)
Fabricate(:whisper, topic: bookmark.topic)
bookmark.reload
serializer = UserBookmarkSerializer.new(bookmark, scope: Guardian.new(user))
expect(serializer.highest_post_number).to eq(3)
user.update!(admin: true)
expect(serializer.highest_post_number).to eq(4)
end
end