From fbd0a518291b72e5d7864d8c4c667de5333189ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Sun, 26 Jan 2020 20:17:27 +0100 Subject: [PATCH] API: return current users like, reshare and subcription status in post infos --- app/controllers/api/v1/posts_controller.rb | 2 +- app/presenters/post_presenter.rb | 23 ++++++++++++-- lib/schemas/api_v1.json | 12 +++++++- spec/integration/api/posts_controller_spec.rb | 30 +++++++++++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb index ae0bd663f..527bc181a 100644 --- a/app/controllers/api/v1/posts_controller.rb +++ b/app/controllers/api/v1/posts_controller.rb @@ -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 diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index f1c8c3979..ad39aa0e8 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -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 diff --git a/lib/schemas/api_v1.json b/lib/schemas/api_v1.json index 8271e879e..0f9b02ca3 100644 --- a/lib/schemas/api_v1.json +++ b/lib/schemas/api_v1.json @@ -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": { diff --git a/spec/integration/api/posts_controller_spec.rb b/spec/integration/api/posts_controller_spec.rb index 7a5b10516..dd03e1ac0 100644 --- a/spec/integration/api/posts_controller_spec.rb +++ b/spec/integration/api/posts_controller_spec.rb @@ -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)