Skip to content

Commit

Permalink
Merge pull request #30 from slickage/thread-view-board-meta
Browse files Browse the repository at this point in the history
Thread view board meta
  • Loading branch information
akinsey authored Nov 12, 2024
2 parents 3dd381d + d9213e6 commit 72e7857
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/epochtalk_server_web/controllers/thread.ex
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,8 @@ defmodule EpochtalkServerWeb.Controllers.Thread do
:ok <- ACL.allow!(conn, "threads.byBoard"),
board_mapping <- BoardMapping.all(),
board_moderators <- BoardModerator.all(),
{:ok, board_counts} <- ProxyConversion.build_model("boards.counts"),
{:ok, board_last_post_info} <- ProxyConversion.build_model("boards.last_post_info"),
{:ok, threads, data} <-
ProxyConversion.build_model("threads.by_board", board_id, page, limit) do
render(conn, :by_board_proxy, %{
Expand All @@ -826,7 +828,9 @@ defmodule EpochtalkServerWeb.Controllers.Thread do
board_moderators: board_moderators,
page: page,
limit: limit,
pagination_data: data
pagination_data: data,
board_counts: board_counts,
board_last_post_info: board_last_post_info
})
else
_ ->
Expand Down
70 changes: 70 additions & 0 deletions lib/epochtalk_server_web/json/board_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,76 @@ 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 proxy_format_board_data_for_find(
board_moderators,
board_mapping,
board_id,
user_priority,
board_counts \\ nil,
board_last_post_info \\ nil
) do
board_counts = map_to_id(board_counts)
board_last_post_info = map_to_id(board_last_post_info)
# 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,
# board counts and last post info for proxy version
board_counts,
board_last_post_info
)

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

@doc """
Board view helper method for mapping childboards and other metadata to board using board mapping and user priority
"""
Expand Down
10 changes: 7 additions & 3 deletions lib/epochtalk_server_web/json/thread_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,19 @@ defmodule EpochtalkServerWeb.Controllers.ThreadJSON do
board_mapping: board_mapping,
board_moderators: board_moderators,
page: page,
limit: limit
limit: limit,
board_counts: board_counts,
board_last_post_info: board_last_post_info
}) do
# format board data
board =
BoardJSON.format_board_data_for_find(
BoardJSON.proxy_format_board_data_for_find(
board_moderators,
board_mapping,
board_id,
user_priority
user_priority,
board_counts,
board_last_post_info
)

# format thread data
Expand Down

0 comments on commit 72e7857

Please sign in to comment.