mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: add first post likes data serializer (#31216)
This PR adds two attributes (`op_can_like` & `op_liked`) to `TopicListItemSerializer`. We've also added `serialize_topic_op_likes_data` theme modifier so that these two attributes are only added when a theme or component needs this data.
This commit is contained in:
@@ -10,6 +10,8 @@ class TopicListItemSerializer < ListableTopicSerializer
|
||||
:last_poster_username,
|
||||
:category_id,
|
||||
:op_like_count,
|
||||
:op_can_like,
|
||||
:op_liked,
|
||||
:pinned_globally,
|
||||
:liked_post_numbers,
|
||||
:featured_link,
|
||||
@@ -32,6 +34,42 @@ class TopicListItemSerializer < ListableTopicSerializer
|
||||
object.first_post && object.first_post.like_count
|
||||
end
|
||||
|
||||
def include_op_can_like?
|
||||
theme_modifier_helper.serialize_topic_op_likes_data
|
||||
end
|
||||
|
||||
def op_can_like
|
||||
return false if !scope.user || !object.first_post
|
||||
|
||||
first_post = object.first_post
|
||||
return false if first_post.user_id == scope.user.id
|
||||
return false unless scope.post_can_act?(first_post, :like)
|
||||
|
||||
first_post_liked =
|
||||
PostAction.where(
|
||||
user_id: scope.user.id,
|
||||
post_id: first_post.id,
|
||||
post_action_type_id: PostActionType.types[:like],
|
||||
).first
|
||||
return scope.can_delete?(first_post_liked) if first_post_liked
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
def include_op_liked?
|
||||
theme_modifier_helper.serialize_topic_op_likes_data
|
||||
end
|
||||
|
||||
def op_liked
|
||||
return false if !scope.user || !object.first_post
|
||||
|
||||
PostAction.where(
|
||||
user_id: scope.user.id,
|
||||
post_id: object.first_post.id,
|
||||
post_action_type_id: PostActionType.types[:like],
|
||||
).exists?
|
||||
end
|
||||
|
||||
def last_poster_username
|
||||
posters.find { |poster| poster.user.id == object.last_post_user_id }.try(:user).try(:username)
|
||||
end
|
||||
@@ -93,4 +131,10 @@ class TopicListItemSerializer < ListableTopicSerializer
|
||||
def include_allowed_user_count?
|
||||
object.private_message?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def theme_modifier_helper
|
||||
@theme_modifier_helper ||= ThemeModifierHelper.new(request: scope.request)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user