mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 23:36:11 +08: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:
@ -149,6 +149,7 @@ end
|
||||
# custom_homepage :boolean
|
||||
# serialize_post_user_badges :string is an Array
|
||||
# theme_setting_modifiers :jsonb
|
||||
# serialize_topic_op_likes_data :boolean
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
@ -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
|
||||
|
@ -0,0 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
class AddSerializeTopicOpLikesDataThemeModifier < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
add_column :theme_modifier_sets, :serialize_topic_op_likes_data, :boolean, null: true
|
||||
end
|
||||
end
|
@ -96,4 +96,58 @@ RSpec.describe TopicListItemSerializer do
|
||||
expect(json[:posters].length).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "correctly serializes op_likes data" do
|
||||
let(:user) { Fabricate(:user) }
|
||||
let(:moderator) { Fabricate(:moderator) }
|
||||
let(:first_post) { Fabricate(:post, topic: topic, user: user) }
|
||||
|
||||
before { topic.update!(first_post: first_post) }
|
||||
|
||||
it "serializes op_can_like" do
|
||||
allow_any_instance_of(ThemeModifierHelper).to receive(
|
||||
:serialize_topic_op_likes_data,
|
||||
).and_return(true)
|
||||
json = TopicListItemSerializer.new(topic, scope: Guardian.new(moderator), root: false).as_json
|
||||
|
||||
expect(json[:op_can_like]).to eq(true)
|
||||
end
|
||||
|
||||
it "does not include op_can_like when theme modifier disallows" do
|
||||
allow_any_instance_of(ThemeModifierHelper).to receive(
|
||||
:serialize_topic_op_likes_data,
|
||||
).and_return(false)
|
||||
json = TopicListItemSerializer.new(topic, scope: Guardian.new(moderator), root: false).as_json
|
||||
|
||||
expect(json.key?(:op_can_like)).to eq(false)
|
||||
end
|
||||
|
||||
it "serializes op_liked" do
|
||||
allow_any_instance_of(ThemeModifierHelper).to receive(
|
||||
:serialize_topic_op_likes_data,
|
||||
).and_return(true)
|
||||
PostAction.create!(
|
||||
user: user,
|
||||
post: first_post,
|
||||
post_action_type_id: PostActionType.types[:like],
|
||||
)
|
||||
json = TopicListItemSerializer.new(topic, scope: Guardian.new(user), root: false).as_json
|
||||
|
||||
expect(json[:op_liked]).to eq(true)
|
||||
end
|
||||
|
||||
it "does not include op_liked when theme modifier disallows" do
|
||||
allow_any_instance_of(ThemeModifierHelper).to receive(
|
||||
:serialize_topic_op_likes_data,
|
||||
).and_return(false)
|
||||
PostAction.create!(
|
||||
user: user,
|
||||
post: first_post,
|
||||
post_action_type_id: PostActionType.types[:like],
|
||||
)
|
||||
json = TopicListItemSerializer.new(topic, scope: Guardian.new(user), root: false).as_json
|
||||
|
||||
expect(json.key?(:op_liked)).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user