FIX: Remove last_unread_post excerpt logic for bookmarks (#17979)

The logic to determine what post excerpt to show for
a topic-level bookmark based on the last unread post
was complex and slow, so we decided to remove it and
always just use the first post excerpt.

This commit also fixes an issue where a couple of
instances of for_topic were missed when doing the
Bookmarkable refactors, so:

1. Clicking the topic bookmark link was not taking
   the user to the last unread post
2. When replying to a topic where there was a topic
   level bookmark with the auto delete preference
   of "on owner reply", we were not removing the
   bookmark from the UI correctly.

A test has been added for the former, the latter would
be quite time-consuming to test and not really worth
it considering it's quite an edge case UI bug.
This commit is contained in:
Martin Brennan
2022-08-19 09:35:25 +10:00
committed by GitHub
parent df9e546f5d
commit 49a70a37f1
6 changed files with 55 additions and 66 deletions

View File

@@ -1,12 +1,11 @@
# frozen_string_literal: true
class UserTopicBookmarkSerializer < UserPostTopicBookmarkBaseSerializer
# it does not matter what the linked post number is for topic bookmarks,
# on the client we always take the user to the last unread post in the
# topic when the bookmark URL is clicked
attributes :last_read_post_number
# NOTE: It does not matter what the linked post number is for topic bookmarks,
# on the client we always take the user to the last unread post in the
# topic when the bookmark URL is clicked
def linked_post_number
1
end
@@ -28,28 +27,7 @@ class UserTopicBookmarkSerializer < UserPostTopicBookmarkBaseSerializer
end
def cooked
@cooked ||= \
if last_read_post_number.present?
for_topic_cooked_post
else
first_post.cooked
end
end
def for_topic_cooked_post
post_number = [last_read_post_number + 1, highest_post_number].min
sorted_regular_posts = topic.posts.sort_by(&:post_number).select do |post|
post.post_type == Post.types[:regular]
end
first_unread_post = sorted_regular_posts.find do |post|
post.post_number >= post_number
end
# if first_unread_cooked is blank this likely means that the last
# read post was either deleted or is a small action post.
# in this case we should just get the last regular post and
# use that for the cooked value so we have something to show
(first_unread_post || sorted_regular_posts.last).cooked
first_post.cooked
end
def bookmarkable_user