use Ecto.Schema
import Ecto.Changeset
+ import Ecto.Query
+ alias Pleroma.Chat
alias Pleroma.Repo
alias Pleroma.User
conflict_target: [:user_id, :recipient]
)
end
+
+ @spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t()
+ def for_user_query(user_id) do
+ from(c in Chat,
+ where: c.user_id == ^user_id,
+ order_by: [desc: c.updated_at],
+ inner_join: u in User,
+ on: u.ap_id == c.recipient
+ )
+ end
end
defmodule Pleroma.Web.AdminAPI.AdminAPIController do
use Pleroma.Web, :controller
- import Ecto.Query
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
alias Pleroma.Config
def list_user_chats(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname} = _params) do
with %User{id: user_id} <- User.get_cached_by_nickname_or_id(nickname, for: admin) do
chats =
- from(c in Pleroma.Chat,
- where: c.user_id == ^user_id,
- order_by: [desc: c.updated_at],
- inner_join: u in User,
- on: u.ap_id == c.recipient
- )
+ Pleroma.Chat.for_user_query(user_id)
|> Pleroma.Repo.all()
conn
blocked_ap_ids = User.blocked_users_ap_ids(user)
chats =
- from(c in Chat,
- where: c.user_id == ^user_id,
- where: c.recipient not in ^blocked_ap_ids,
- order_by: [desc: c.updated_at],
- inner_join: u in User,
- on: u.ap_id == c.recipient
- )
+ Chat.for_user_query(user_id)
+ |> where([c], c.recipient not in ^blocked_ap_ids)
|> Repo.all()
conn