philomena/lib/philomena_web/comment_json.ex
SomewhatDamaged 9b22d2719a Adding /api/v1/json/comment/:id endpoint (#13)
* Add `/api/v1/json/comment/:id` endpoint

* Add controller for `/api/v1/json/comment/:id`

Add controller for `/api/v1/json/comment/:id` endpoint

* Fixes permissions

Now will show `403 forbidden` if a comment on a deleted image is requested.

* Fixed endpoint schema

Now conforms to endpoint schema

* Permission adjustment

Will prevent `user_id` and `author` from leaking in the event the image is `hidden_from_users`

* Permission lockout

Lockout of all data if destroyed.

* Lockout on destroyed_content

Return 404 when `comment.destroyed_content`

* Refactored for neatness
2019-12-31 18:25:11 -05:00

37 lines
1,009 B
Elixir

defmodule PhilomenaWeb.CommentJson do
alias PhilomenaWeb.UserAttributionView
def as_json(%{destroyed_content: true}) do
nil
end
def as_json(%{image: %{hidden_from_users: true}} = comment) do
%{
id: comment.id,
image_id: comment.image_id,
user_id: nil,
author: nil,
body: nil
}
end
def as_json(%{hidden_from_users: true} = comment) do
%{
id: comment.id,
image_id: comment.image_id,
user_id: if(not comment.anonymous, do: comment.user_id),
author: if(comment.anonymous or is_nil(comment.user), do: UserAttributionView.anonymous_name(comment), else: comment.user.name),
body: nil
}
end
def as_json(comment) do
%{
id: comment.id,
image_id: comment.image_id,
user_id: if(not comment.anonymous, do: comment.user_id),
author: if(comment.anonymous or is_nil(comment.user), do: UserAttributionView.anonymous_name(comment), else: comment.user.name),
body: comment.body
}
end
end