Skip to content

Commit

Permalink
refactor(json/post): wip implement function that cleans posts
Browse files Browse the repository at this point in the history
  • Loading branch information
akinsey committed Jul 26, 2023
1 parent 4032a0f commit 7c79281
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/epochtalk_server_web/controllers/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule EpochtalkServerWeb.Controllers.Post do
:ok <- ACL.allow!(conn, "posts.byThread"),
{:can_read, {:ok, true}} <-
{:can_read, Board.get_read_access_by_thread_id(thread_id, user_priority)},
_view_deleted_posts <- can_authed_user_view_deleted_posts(user, thread_id),
view_deleted_posts <- can_authed_user_view_deleted_posts(user, thread_id),
{:ok, write_access} <- Board.get_write_access_by_thread_id(thread_id, user_priority),
{:ok, board_banned} <- BoardBan.is_banned_from_board(user, thread_id: thread_id),
board_mapping <- BoardMapping.all(),
Expand Down Expand Up @@ -94,7 +94,8 @@ defmodule EpochtalkServerWeb.Controllers.Post do
desc: desc,
metric_rank_maps: metric_rank_maps,
ranks: ranks,
watched: watching_thread
watched: watching_thread,
view_deleted_posts: view_deleted_posts
})
else
{:error, :board_does_not_exist} ->
Expand Down
24 changes: 22 additions & 2 deletions lib/epochtalk_server_web/json/post_json.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule EpochtalkServerWeb.Controllers.PostJSON do
alias EpochtalkServerWeb.Controllers.BoardJSON
alias EpochtalkServerWeb.Controllers.ThreadJSON
alias EpochtalkServerWeb.Helpers.ACL

@moduledoc """
Renders and formats `Post` data, in JSON format for frontend
Expand All @@ -27,7 +28,8 @@ defmodule EpochtalkServerWeb.Controllers.PostJSON do
desc: desc,
metric_rank_maps: metric_rank_maps,
ranks: ranks,
watched: watched
watched: watched,
view_deleted_posts: view_deleted_posts
}) do
formatted_board =
BoardJSON.format_board_data_for_find(
Expand All @@ -45,7 +47,9 @@ defmodule EpochtalkServerWeb.Controllers.PostJSON do
|> Map.put(:poll, formatted_poll)
|> Map.put(:watched, watched)

formatted_posts = posts |> Enum.map(&format_post_data_for_by_thread(&1))
formatted_posts = posts
|> Enum.map(&format_post_data_for_by_thread(&1))
|> clean_posts(formatted_thread, user, user_priority, view_deleted_posts)

%{
board: formatted_board,
Expand All @@ -66,6 +70,22 @@ defmodule EpochtalkServerWeb.Controllers.PostJSON do

## === Private Helper Functions ===

defp clean_posts(posts, thread, user, user_priority, view_deleted_posts, override_allow_view \\ false) do
authed_user_id = if user, do: user.id, else: nil
has_self_mod_bypass = ACL.has_permission(user, "posts.byThread.bypass.viewDeletedPosts.selfMod")

Check warning on line 75 in lib/epochtalk_server_web/json/post_json.ex

View workflow job for this annotation

GitHub Actions / Run tests (1.14.4, 25.3.1)

variable "has_self_mod_bypass" is unused (if the variable is not meant to be used, prefix it with an underscore)
has_priority_bypass = ACL.has_permission(user, "posts.byThread.bypass.viewDeletedPosts.priority")

Check warning on line 76 in lib/epochtalk_server_web/json/post_json.ex

View workflow job for this annotation

GitHub Actions / Run tests (1.14.4, 25.3.1)

variable "has_priority_bypass" is unused (if the variable is not meant to be used, prefix it with an underscore)
is_self_mod = if thread, do: thread.user.id == authed_user_id and thread.moderated, else: false

view_deleted_posts_is_board_list = is_list(view_deleted_posts)

Enum.map(posts, fn post ->

Check warning on line 81 in lib/epochtalk_server_web/json/post_json.ex

View workflow job for this annotation

GitHub Actions / Run tests (1.14.4, 25.3.1)

variable "post" is unused (if the variable is not meant to be used, prefix it with an underscore)

end)

# return posts for now
posts
end

defp format_poll_data_for_by_thread(nil, _), do: nil

defp format_poll_data_for_by_thread(poll, user) do
Expand Down

0 comments on commit 7c79281

Please sign in to comment.