AdminAPI: list chats for a user

This commit is contained in:
Alex Gleason 2020-08-31 20:23:33 -05:00
parent b40a627ab0
commit fb0de07343
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 48 additions and 0 deletions

View File

@ -5,6 +5,7 @@
defmodule Pleroma.Web.AdminAPI.AdminAPIController do defmodule Pleroma.Web.AdminAPI.AdminAPIController do
use Pleroma.Web, :controller use Pleroma.Web, :controller
import Ecto.Query
import Pleroma.Web.ControllerHelper, only: [json_response: 3] import Pleroma.Web.ControllerHelper, only: [json_response: 3]
alias Pleroma.Config alias Pleroma.Config
@ -21,6 +22,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
alias Pleroma.Web.AdminAPI.ModerationLogView alias Pleroma.Web.AdminAPI.ModerationLogView
alias Pleroma.Web.AdminAPI.Search alias Pleroma.Web.AdminAPI.Search
alias Pleroma.Web.Endpoint alias Pleroma.Web.Endpoint
alias Pleroma.Web.PleromaAPI
alias Pleroma.Web.Router alias Pleroma.Web.Router
require Logger require Logger
@ -70,6 +72,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
plug( plug(
OAuthScopesPlug, OAuthScopesPlug,
%{scopes: ["read:chats"], admin: true}
when action in [:list_user_chats]
)
plug(
OAuthScopesPlug,
%{scopes: ["read"], admin: true} %{scopes: ["read"], admin: true}
when action in [ when action in [
:list_log, :list_log,
@ -256,6 +264,25 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end end
end end
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.Repo.all()
conn
|> put_view(PleromaAPI.ChatView)
|> render("index.json", chats: chats)
else
_ -> {:error, :not_found}
end
end
def user_toggle_activation(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do def user_toggle_activation(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
user = User.get_cached_by_nickname(nickname) user = User.get_cached_by_nickname(nickname)

View File

@ -178,6 +178,7 @@ defmodule Pleroma.Web.Router do
get("/users", AdminAPIController, :list_users) get("/users", AdminAPIController, :list_users)
get("/users/:nickname", AdminAPIController, :user_show) get("/users/:nickname", AdminAPIController, :user_show)
get("/users/:nickname/statuses", AdminAPIController, :list_user_statuses) get("/users/:nickname/statuses", AdminAPIController, :list_user_statuses)
get("/users/:nickname/chats", AdminAPIController, :list_user_chats)
get("/instances/:instance/statuses", AdminAPIController, :list_instance_statuses) get("/instances/:instance/statuses", AdminAPIController, :list_instance_statuses)
@ -215,6 +216,8 @@ defmodule Pleroma.Web.Router do
post("/media_proxy_caches/delete", MediaProxyCacheController, :delete) post("/media_proxy_caches/delete", MediaProxyCacheController, :delete)
post("/media_proxy_caches/purge", MediaProxyCacheController, :purge) post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
# get("/chats/:id", ChatController, :show)
# get("/chats/:id/messages", ChatController, :messages)
delete("/chats/:id/messages/:message_id", ChatController, :delete_message) delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
end end

View File

@ -1510,6 +1510,24 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
end end
end end
describe "GET /api/pleroma/admin/users/:nickname/chats" do
setup do
user = insert(:user)
insert(:chat, user: user)
insert(:chat, user: user)
insert(:chat, user: user)
%{user: user}
end
test "renders user's statuses", %{conn: conn, user: user} do
conn = get(conn, "/api/pleroma/admin/users/#{user.nickname}/chats")
assert json_response(conn, 200) |> length() == 3
end
end
describe "GET /api/pleroma/admin/moderation_log" do describe "GET /api/pleroma/admin/moderation_log" do
setup do setup do
moderator = insert(:user, is_moderator: true) moderator = insert(:user, is_moderator: true)