Pleroma.Web.PleromaAPI.ChatController: fix dialyzer errors with replace_params: false

This commit is contained in:
Mark Felder 2024-01-30 19:03:11 -05:00
parent fdddba100e
commit 4d20fbc6d9
2 changed files with 52 additions and 17 deletions

View file

@ -53,10 +53,15 @@ defmodule Pleroma.Web.ControllerHelper do
end end
end end
# TODO: Only fetch the params from open_api_spex when everything is converted
@id_keys Pagination.page_keys() -- ["limit", "order"] @id_keys Pagination.page_keys() -- ["limit", "order"]
defp build_pagination_fields(conn, min_id, max_id, extra_params) do defp build_pagination_fields(conn, min_id, max_id, extra_params) do
params = params =
if Map.has_key?(conn.private, :open_api_spex) do
get_in(conn, [Access.key(:private), Access.key(:open_api_spex), Access.key(:params)])
else
conn.params conn.params
end
|> Map.drop(Map.keys(conn.path_params) |> Enum.map(&String.to_existing_atom/1)) |> Map.drop(Map.keys(conn.path_params) |> Enum.map(&String.to_existing_atom/1))
|> Map.merge(extra_params) |> Map.merge(extra_params)
|> Map.drop(@id_keys) |> Map.drop(@id_keys)

View file

@ -38,14 +38,24 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
%{scopes: ["read:chats"]} when action in [:messages, :index, :index2, :show] %{scopes: ["read:chats"]} when action in [:messages, :index, :index2, :show]
) )
plug(Pleroma.Web.ApiSpec.CastAndValidate) plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation
def delete_message(%{assigns: %{user: %{id: user_id} = user}} = conn, %{ def delete_message(
%{
assigns: %{user: %{id: user_id} = user},
private: %{
open_api_spex: %{
params: %{
message_id: message_id, message_id: message_id,
id: chat_id id: chat_id
}) do }
}
}
} = conn,
_
) do
with %MessageReference{} = cm_ref <- with %MessageReference{} = cm_ref <-
MessageReference.get_by_id(message_id), MessageReference.get_by_id(message_id),
^chat_id <- to_string(cm_ref.chat_id), ^chat_id <- to_string(cm_ref.chat_id),
@ -72,8 +82,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
defp remove_or_delete(cm_ref, _), do: MessageReference.delete(cm_ref) defp remove_or_delete(cm_ref, _), do: MessageReference.delete(cm_ref)
def post_chat_message( def post_chat_message(
%{body_params: params, assigns: %{user: user}} = conn, %{
%{id: id} private: %{open_api_spex: %{body_params: params, params: %{id: id}}},
assigns: %{user: user}
} = conn,
_
) do ) do
with {:ok, chat} <- Chat.get_by_user_and_id(user, id), with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
{_, %User{} = recipient} <- {:user, User.get_cached_by_ap_id(chat.recipient)}, {_, %User{} = recipient} <- {:user, User.get_cached_by_ap_id(chat.recipient)},
@ -106,8 +119,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
end end
def mark_message_as_read( def mark_message_as_read(
%{assigns: %{user: %{id: user_id}}} = conn, %{
%{id: chat_id, message_id: message_id} assigns: %{user: %{id: user_id}},
private: %{open_api_spex: %{params: %{id: chat_id, message_id: message_id}}}
} = conn,
_
) do ) do
with %MessageReference{} = cm_ref <- MessageReference.get_by_id(message_id), with %MessageReference{} = cm_ref <- MessageReference.get_by_id(message_id),
^chat_id <- to_string(cm_ref.chat_id), ^chat_id <- to_string(cm_ref.chat_id),
@ -120,8 +136,16 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
end end
def mark_as_read( def mark_as_read(
%{body_params: %{last_read_id: last_read_id}, assigns: %{user: user}} = conn, %{
%{id: id} assigns: %{user: user},
private: %{
open_api_spex: %{
body_params: %{last_read_id: last_read_id},
params: %{id: id}
}
}
} = conn,
_
) do ) do
with {:ok, chat} <- Chat.get_by_user_and_id(user, id), with {:ok, chat} <- Chat.get_by_user_and_id(user, id),
{_n, _} <- MessageReference.set_all_seen_for_chat(chat, last_read_id) do {_n, _} <- MessageReference.set_all_seen_for_chat(chat, last_read_id) do
@ -129,7 +153,13 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
end end
end end
def messages(%{assigns: %{user: user}} = conn, %{id: id} = params) do def messages(
%{
assigns: %{user: user},
private: %{open_api_spex: %{params: %{id: id} = params}}
} = conn,
_
) do
with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
chat_message_refs = chat_message_refs =
chat chat
@ -143,7 +173,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
end end
end end
def index(%{assigns: %{user: user}} = conn, params) do def index(%{assigns: %{user: user}, private: %{open_api_spex: %{params: params}}} = conn, _) do
chats = chats =
index_query(user, params) index_query(user, params)
|> Repo.all() |> Repo.all()
@ -151,7 +181,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
render(conn, "index.json", chats: chats) render(conn, "index.json", chats: chats)
end end
def index2(%{assigns: %{user: user}} = conn, params) do def index2(%{assigns: %{user: user}, private: %{open_api_spex: %{params: params}}} = conn, _) do
chats = chats =
index_query(user, params) index_query(user, params)
|> Pagination.fetch_paginated(params) |> Pagination.fetch_paginated(params)
@ -171,14 +201,14 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
|> where([c], c.recipient not in ^exclude_users) |> where([c], c.recipient not in ^exclude_users)
end end
def create(%{assigns: %{user: user}} = conn, %{id: id}) do def create(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
with %User{ap_id: recipient} <- User.get_cached_by_id(id), with %User{ap_id: recipient} <- User.get_cached_by_id(id),
{:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
render(conn, "show.json", chat: chat) render(conn, "show.json", chat: chat)
end end
end end
def show(%{assigns: %{user: user}} = conn, %{id: id}) do def show(%{assigns: %{user: user}, private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do with {:ok, chat} <- Chat.get_by_user_and_id(user, id) do
render(conn, "show.json", chat: chat) render(conn, "show.json", chat: chat)
end end