API: return current users like, reshare and subcription status in post infos
This commit is contained in:
parent
6bbcb7415b
commit
fbd0a51829
4 changed files with 62 additions and 5 deletions
|
|
@ -108,7 +108,7 @@ module Api
|
|||
end
|
||||
|
||||
def post_as_json(post)
|
||||
PostPresenter.new(post).as_api_response
|
||||
PostPresenter.new(post, current_user).as_api_response
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@ class PostPresenter < BasePresenter
|
|||
poll: PollPresenter.new(@post.poll, current_user).as_api_json,
|
||||
mentioned_people: build_mentioned_people_json,
|
||||
photos: build_photos_json,
|
||||
root: root_api_response
|
||||
root: root_api_response,
|
||||
own_interaction_state: build_own_interaction_state
|
||||
}.compact
|
||||
end
|
||||
|
||||
|
|
@ -90,7 +91,7 @@ class PostPresenter < BasePresenter
|
|||
location: @post.post_location,
|
||||
poll: @post.poll,
|
||||
poll_participation_answer_id: poll_participation_answer_id,
|
||||
participation: participate?,
|
||||
participation: participates?,
|
||||
interactions: build_interactions_json
|
||||
}
|
||||
end
|
||||
|
|
@ -140,6 +141,22 @@ class PostPresenter < BasePresenter
|
|||
}
|
||||
end
|
||||
|
||||
def build_own_interaction_state
|
||||
if current_user
|
||||
{
|
||||
liked: @post.likes.where(author: current_user.person).exists?,
|
||||
reshared: @post.reshares.where(author: current_user.person).exists?,
|
||||
subscribed: participates?
|
||||
}
|
||||
else
|
||||
{
|
||||
liked: false,
|
||||
reshared: false,
|
||||
subscribed: false
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def user_like
|
||||
@post.like_for(current_user).try(:as_api_response, :backbone)
|
||||
end
|
||||
|
|
@ -152,7 +169,7 @@ class PostPresenter < BasePresenter
|
|||
@post.poll&.participation_answer(current_user)&.poll_answer_id if user_signed_in?
|
||||
end
|
||||
|
||||
def participate?
|
||||
def participates?
|
||||
user_signed_in? && current_user.participations.where(target_id: @post).exists?
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -256,6 +256,16 @@
|
|||
"required": ["comments", "likes", "reshares"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"own_interaction_state": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"liked": { "type": "boolean" },
|
||||
"reshared": { "type": "boolean" },
|
||||
"subscribed": { "type": "boolean" }
|
||||
},
|
||||
"required": ["liked", "reshared", "subscribed"],
|
||||
"additionalProperties": false
|
||||
},
|
||||
"mentioned_people": {
|
||||
"type": "array",
|
||||
"items": { "$ref": "https://diaspora.software/api/v1/schema.json#/definitions/short_profile" }
|
||||
|
|
@ -306,7 +316,7 @@
|
|||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"required": ["guid", "created_at", "title", "body", "public", "nsfw", "author", "interaction_counters", "mentioned_people", "photos"]
|
||||
"required": ["guid", "created_at", "title", "body", "public", "nsfw", "author", "interaction_counters", "own_interaction_state", "mentioned_people", "photos"]
|
||||
},
|
||||
|
||||
"post": {
|
||||
|
|
|
|||
|
|
@ -102,6 +102,29 @@ describe Api::V1::PostsController do
|
|||
end
|
||||
end
|
||||
|
||||
context "access interacted with post by ID" do
|
||||
it "gets post" do
|
||||
auth.user.like!(@status)
|
||||
auth.user.reshare!(@status)
|
||||
@status.reload
|
||||
|
||||
get(
|
||||
api_v1_post_path(@status.guid),
|
||||
params: {
|
||||
access_token: access_token
|
||||
}
|
||||
)
|
||||
expect(response.status).to eq(200)
|
||||
post = response_body(response)
|
||||
confirm_post_format(post, alice, @status, [bob, eve])
|
||||
expect(post["own_interaction_state"]["liked"]).to be true
|
||||
expect(post["own_interaction_state"]["reshared"]).to be true
|
||||
expect(post["own_interaction_state"]["subscribed"]).to be true
|
||||
|
||||
expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
|
||||
end
|
||||
end
|
||||
|
||||
context "access reshare style post by post ID" do
|
||||
it "gets post" do
|
||||
reshare_post = FactoryGirl.create(:reshare, root: @status, author: bob.person)
|
||||
|
|
@ -659,6 +682,7 @@ describe Api::V1::PostsController do
|
|||
confirm_post_top_level(post, reference_post)
|
||||
confirm_person_format(post["author"], user)
|
||||
confirm_interactions(post["interaction_counters"], reference_post)
|
||||
confirm_own_interaction_state(post["own_interaction_state"], reference_post)
|
||||
|
||||
mentions.each do |mention|
|
||||
post_mentions = post["mentioned_people"]
|
||||
|
|
@ -689,6 +713,12 @@ describe Api::V1::PostsController do
|
|||
expect(interactions["reshares"]).to eq(reference_post.reshares_count)
|
||||
end
|
||||
|
||||
def confirm_own_interaction_state(state, reference_post)
|
||||
expect(state["liked"]).to eq(reference_post.likes.where(author: auth.user.person).exists?)
|
||||
expect(state["reshared"]).to eq(reference_post.reshares.where(author: auth.user.person).exists?)
|
||||
expect(state["subscribed"]).to eq(reference_post.participations.where(author: auth.user.person).exists?)
|
||||
end
|
||||
|
||||
def confirm_person_format(post_person, user)
|
||||
expect(post_person["guid"]).to eq(user.guid)
|
||||
expect(post_person["diaspora_id"]).to eq(user.diaspora_handle)
|
||||
|
|
|
|||
Loading…
Reference in a new issue