Skip to content

Commit

Permalink
fix(thread-view): re-add original format_board_data_for_find
Browse files Browse the repository at this point in the history
change thread_json to use proxy_format_board_data_for_find

in the case when metadata is not available, we need to use the original
format_board_data_for_find (such as in tests)
  • Loading branch information
unenglishable committed Nov 11, 2024
1 parent ea39654 commit a0948d9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
60 changes: 59 additions & 1 deletion lib/epochtalk_server_web/json/board_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ defmodule EpochtalkServerWeb.Controllers.BoardJSON do
def movelist(%{movelist: movelist}), do: movelist

@doc """
Proxy version
Board view helper method for mapping childboards and other metadata to board using board mapping and user priority
"""
def format_board_data_for_find(
def proxy_format_board_data_for_find(
board_moderators,
board_mapping,
board_id,
Expand Down Expand Up @@ -192,6 +193,63 @@ defmodule EpochtalkServerWeb.Controllers.BoardJSON do
board
end

@doc """
Board view helper method for mapping childboards and other metadata to board using board mapping and user priority
"""
def format_board_data_for_find(board_moderators, board_mapping, board_id, user_priority) do
# filter out board by id
[board] = Enum.filter(board_mapping, fn bm -> bm.board_id == board_id end)

# append board moderators to board
moderators =
board_moderators
|> Enum.filter(fn mod -> Map.get(mod, :board_id) == Map.get(board, :board_id) end)
|> Enum.map(fn mod -> %{id: mod.user_id, username: mod.user.username} end)

board = Map.put(board, :moderators, moderators)

# flatten needed boards data
board =
board
|> Map.merge(remove_nil(board.board))
|> Map.merge(
remove_nil(board.stats)
|> Map.delete(:id)
)
|> Map.merge(board.thread)
|> Map.merge(board.board.meta || board.stats)
|> Map.delete(:meta)

# delete unneeded properties
board =
board
|> Map.delete(:board)
|> Map.delete(:stats)
|> Map.delete(:thread)
|> Map.delete(:parent)
|> Map.delete(:category)
|> Map.delete(:__meta__)
|> Map.delete(:__struct__)

# handle deleted last post data
if !!Map.get(board, :post_deleted) or !!Map.get(board, :user_deleted),
do: board |> Map.put(:last_post_username, "deleted"),
else: board

# iterate each child board, attempt to map nested children from board mapping
board =
process_children_from_board_mapping(
:parent_id,
board_mapping,
:children,
board,
user_priority
)

# return flattened board data with children, mod and last post data
board
end

defp process_children_from_board_mapping(
board_mapping_key,
board_mapping,
Expand Down
2 changes: 1 addition & 1 deletion lib/epochtalk_server_web/json/thread_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ defmodule EpochtalkServerWeb.Controllers.ThreadJSON do
}) do
# format board data
board =
BoardJSON.format_board_data_for_find(
BoardJSON.proxy_format_board_data_for_find(
board_moderators,
board_mapping,
board_id,
Expand Down

0 comments on commit a0948d9

Please sign in to comment.