API: return whether post or item was already reported or not

This commit is contained in:
Jonne Haß 2020-02-07 23:40:00 +01:00
parent 8068d8747b
commit 6dbef95951
6 changed files with 29 additions and 9 deletions

View file

@ -104,7 +104,7 @@ module Api
end end
def comment_as_json(comment) def comment_as_json(comment)
CommentPresenter.new(comment).as_api_response CommentPresenter.new(comment, current_user).as_api_response
end end
def find_post def find_post

View file

@ -18,7 +18,8 @@ class CommentPresenter < BasePresenter
body: message.plain_text_for_json, body: message.plain_text_for_json,
author: PersonPresenter.new(author).as_api_json, author: PersonPresenter.new(author).as_api_json,
created_at: created_at, created_at: created_at,
mentioned_people: build_mentioned_people_json mentioned_people: build_mentioned_people_json,
reported: current_user.present? && reports.where(user: current_user).exists?
} }
end end

View file

@ -162,13 +162,15 @@ class PostPresenter < BasePresenter
{ {
liked: @post.likes.where(author: current_user.person).exists?, liked: @post.likes.where(author: current_user.person).exists?,
reshared: @post.reshares.where(author: current_user.person).exists?, reshared: @post.reshares.where(author: current_user.person).exists?,
subscribed: participates? subscribed: participates?,
reported: @post.reports.where(user: current_user).exists?
} }
else else
{ {
liked: false, liked: false,
reshared: false, reshared: false,
subscribed: false subscribed: false,
reported: false
} }
end end
end end

View file

@ -100,9 +100,10 @@
"mentioned_people": { "mentioned_people": {
"type": "array", "type": "array",
"items": { "$ref": "https://diaspora.software/api/v1/schema.json#/definitions/short_profile" } "items": { "$ref": "https://diaspora.software/api/v1/schema.json#/definitions/short_profile" }
} },
"reported": { "type": "boolean" }
}, },
"required": ["guid", "created_at", "author", "body"], "required": ["guid", "created_at", "author", "body", "reported"],
"additionalProperties": false "additionalProperties": false
} }
}, },
@ -282,9 +283,10 @@
"properties": { "properties": {
"liked": { "type": "boolean" }, "liked": { "type": "boolean" },
"reshared": { "type": "boolean" }, "reshared": { "type": "boolean" },
"subscribed": { "type": "boolean" } "subscribed": { "type": "boolean" },
"reported": { "type": "boolean" }
}, },
"required": ["liked", "reshared", "subscribed"], "required": ["liked", "reshared", "subscribed", "reported"],
"additionalProperties": false "additionalProperties": false
}, },
"mentioned_people": { "mentioned_people": {

View file

@ -139,7 +139,7 @@ describe Api::V1::CommentsController do
before do before do
@comment_text1 = "This is a comment" @comment_text1 = "This is a comment"
@comment_text2 = "This is a comment 2" @comment_text2 = "This is a comment 2"
comment_service.create(@status.guid, @comment_text1) @comment1 = comment_service.create(@status.guid, @comment_text1)
comment_service.create(@status.guid, @comment_text2) comment_service.create(@status.guid, @comment_text2)
end end
@ -154,9 +154,21 @@ describe Api::V1::CommentsController do
expect(comments.length).to eq(2) expect(comments.length).to eq(2)
confirm_comment_format(comments[0], auth.user, @comment_text1) confirm_comment_format(comments[0], auth.user, @comment_text1)
confirm_comment_format(comments[1], auth.user, @comment_text2) confirm_comment_format(comments[1], auth.user, @comment_text2)
expect(comments).to all(include("reported" => false))
expect(comments.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/comments") expect(comments.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/comments")
end end
it "returns reported status of a comment" do
auth_minimum_scopes.user.reports.create!(item: @comment1, text: "Meh!")
get(
api_v1_post_comments_path(post_id: @status.guid),
params: {access_token: access_token_minimum_scopes}
)
comments = response_body(response)
expect(comments[0]["reported"]).to eq(true)
expect(comments[1]["reported"]).to eq(false)
end
end end
context "wrong post id" do context "wrong post id" do

View file

@ -109,6 +109,7 @@ describe Api::V1::PostsController do
it "gets post" do it "gets post" do
auth.user.like!(@status) auth.user.like!(@status)
auth.user.reshare!(@status) auth.user.reshare!(@status)
auth.user.reports.create!(item: @status, text: "Meh!")
@status.reload @status.reload
get( get(
@ -123,6 +124,7 @@ describe Api::V1::PostsController do
expect(post["own_interaction_state"]["liked"]).to be true expect(post["own_interaction_state"]["liked"]).to be true
expect(post["own_interaction_state"]["reshared"]).to be true expect(post["own_interaction_state"]["reshared"]).to be true
expect(post["own_interaction_state"]["subscribed"]).to be true expect(post["own_interaction_state"]["subscribed"]).to be true
expect(post["own_interaction_state"]["reported"]).to be true
expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post") expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end end
@ -722,6 +724,7 @@ describe Api::V1::PostsController do
expect(state["liked"]).to eq(reference_post.likes.where(author: auth.user.person).exists?) 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["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?) expect(state["subscribed"]).to eq(reference_post.participations.where(author: auth.user.person).exists?)
expect(state["reported"]).to eq(reference_post.reports.where(user: auth.user).exists?)
end end
def confirm_person_format(post_person, user) def confirm_person_format(post_person, user)