2020-03-12 13:20:56 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative 'post_item_excerpt'
|
|
|
|
|
|
|
|
class UserBookmarkSerializer < ApplicationSerializer
|
|
|
|
include PostItemExcerpt
|
|
|
|
include TopicTagsMixin
|
|
|
|
|
|
|
|
attributes :id,
|
|
|
|
:created_at,
|
2020-05-01 14:14:20 +08:00
|
|
|
:updated_at,
|
2020-03-12 13:20:56 +08:00
|
|
|
:topic_id,
|
|
|
|
:linked_post_number,
|
|
|
|
:post_id,
|
|
|
|
:name,
|
|
|
|
:reminder_at,
|
2021-03-22 07:50:22 +08:00
|
|
|
:pinned,
|
2020-03-12 13:20:56 +08:00
|
|
|
:title,
|
2021-06-07 22:49:57 +08:00
|
|
|
:fancy_title,
|
2020-03-12 13:20:56 +08:00
|
|
|
:deleted,
|
|
|
|
:hidden,
|
|
|
|
:category_id,
|
|
|
|
:closed,
|
|
|
|
:archived,
|
|
|
|
:archetype,
|
|
|
|
:highest_post_number,
|
2021-09-21 11:49:56 +08:00
|
|
|
:last_read_post_number,
|
2020-03-12 13:20:56 +08:00
|
|
|
:bumped_at,
|
|
|
|
:slug,
|
2020-05-13 12:03:24 +08:00
|
|
|
:post_user_username,
|
|
|
|
:post_user_avatar_template,
|
2021-09-21 06:45:47 +08:00
|
|
|
:post_user_name,
|
|
|
|
:for_topic
|
2020-03-12 13:20:56 +08:00
|
|
|
|
2021-09-15 08:16:54 +08:00
|
|
|
def topic_id
|
|
|
|
post.topic_id
|
|
|
|
end
|
|
|
|
|
2020-03-13 08:44:39 +08:00
|
|
|
def topic
|
2021-09-15 08:16:54 +08:00
|
|
|
@topic ||= object.topic
|
2020-03-13 08:44:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def post
|
2021-09-15 08:16:54 +08:00
|
|
|
@post ||= object.post
|
2020-03-13 08:44:39 +08:00
|
|
|
end
|
|
|
|
|
2020-03-12 13:20:56 +08:00
|
|
|
def closed
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.closed
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def archived
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.archived
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def linked_post_number
|
2020-03-13 08:44:39 +08:00
|
|
|
post.post_number
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.title
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
2021-06-07 22:49:57 +08:00
|
|
|
def fancy_title
|
|
|
|
topic.fancy_title
|
|
|
|
end
|
|
|
|
|
2020-03-12 13:20:56 +08:00
|
|
|
def deleted
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.deleted_at.present? || post.deleted_at.present?
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def hidden
|
2020-03-13 08:44:39 +08:00
|
|
|
post.hidden
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def category_id
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.category_id
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def archetype
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.archetype
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def archived
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.archived
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def closed
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.closed
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def highest_post_number
|
2021-09-21 11:49:56 +08:00
|
|
|
scope.is_staff? ? topic.highest_staff_post_number : topic.highest_post_number
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_read_post_number
|
|
|
|
topic_user&.last_read_post_number
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_user
|
|
|
|
@topic_user ||= topic.topic_users.find { |tu| tu.user_id == scope.user.id }
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def bumped_at
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.bumped_at
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def raw
|
2020-03-13 08:44:39 +08:00
|
|
|
post.raw
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def cooked
|
2021-09-22 10:47:36 +08:00
|
|
|
@cooked ||= \
|
|
|
|
if object.for_topic && last_read_post_number.present?
|
|
|
|
for_topic_cooked_post
|
|
|
|
else
|
|
|
|
post.cooked
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def for_topic_cooked_post
|
|
|
|
post_number = [last_read_post_number + 1, highest_post_number].min
|
|
|
|
posts = Post.where(topic: topic, post_type: Post.types[:regular]).order(:post_number)
|
|
|
|
first_unread_cooked = posts.where("post_number >= ?", post_number).pluck_first(:cooked)
|
|
|
|
|
|
|
|
# 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_cooked || posts.last.cooked
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def slug
|
2020-03-13 08:44:39 +08:00
|
|
|
topic.slug
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
|
2020-05-13 12:03:24 +08:00
|
|
|
def post_user
|
|
|
|
@post_user ||= post.user
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_user_username
|
|
|
|
post_user.username
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_user_avatar_template
|
|
|
|
post_user.avatar_template
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_user_name
|
|
|
|
post_user.name
|
2020-03-12 13:20:56 +08:00
|
|
|
end
|
|
|
|
end
|