Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/v1/comments_controller.rb
Expand Up @@ -104,7 +104,7 @@ def post_service
end

def comment_as_json(comment)
CommentPresenter.new(comment).as_api_response
CommentPresenter.new(comment, current_user).as_api_response
end

def find_post
Expand Down
3 changes: 2 additions & 1 deletion app/presenters/comment_presenter.rb
Expand Up @@ -18,7 +18,8 @@ def as_api_response
body: message.plain_text_for_json,
author: PersonPresenter.new(author).as_api_json,
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

Expand Down
6 changes: 4 additions & 2 deletions app/presenters/post_presenter.rb
Expand Up @@ -162,13 +162,15 @@ def build_own_interaction_state
{
liked: @post.likes.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
{
liked: false,
reshared: false,
subscribed: false
subscribed: false,
reported: false
}
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/schemas/api_v1.json
Expand Up @@ -100,9 +100,10 @@
"mentioned_people": {
"type": "array",
"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
}
},
Expand Down Expand Up @@ -282,9 +283,10 @@
"properties": {
"liked": { "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
},
"mentioned_people": {
Expand Down
14 changes: 13 additions & 1 deletion spec/integration/api/comments_controller_spec.rb
Expand Up @@ -139,7 +139,7 @@
before do
@comment_text1 = "This is a comment"
@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)
end

Expand All @@ -154,9 +154,21 @@
expect(comments.length).to eq(2)
confirm_comment_format(comments[0], auth.user, @comment_text1)
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")
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

context "wrong post id" do
Expand Down
3 changes: 3 additions & 0 deletions spec/integration/api/posts_controller_spec.rb
Expand Up @@ -109,6 +109,7 @@
it "gets post" do
auth.user.like!(@status)
auth.user.reshare!(@status)
auth.user.reports.create!(item: @status, text: "Meh!")
@status.reload

get(
Expand All @@ -123,6 +124,7 @@
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["own_interaction_state"]["reported"]).to be true

expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end
Expand Down Expand Up @@ -722,6 +724,7 @@ 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?)
expect(state["reported"]).to eq(reference_post.reports.where(user: auth.user).exists?)
end

def confirm_person_format(post_person, user)
Expand Down