AdminAPI: list messages in a chat

This commit is contained in:
Alex Gleason 2020-09-01 19:39:34 -05:00
parent c41430b23e
commit f13b52a703
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 107 additions and 2 deletions

View file

@ -6,14 +6,23 @@ defmodule Pleroma.Web.AdminAPI.ChatController do
use Pleroma.Web, :controller use Pleroma.Web, :controller
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.Chat.MessageReference
alias Pleroma.ModerationLog alias Pleroma.ModerationLog
alias Pleroma.Pagination
alias Pleroma.Plugs.OAuthScopesPlug alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
require Logger require Logger
plug(Pleroma.Web.ApiSpec.CastAndValidate) plug(Pleroma.Web.ApiSpec.CastAndValidate)
plug(
OAuthScopesPlug,
%{scopes: ["read:chats"], admin: true} when action in [:messages]
)
plug( plug(
OAuthScopesPlug, OAuthScopesPlug,
%{scopes: ["write:chats"], admin: true} when action in [:delete_message] %{scopes: ["write:chats"], admin: true} when action in [:delete_message]
@ -34,4 +43,22 @@ def delete_message(%{assigns: %{user: user}} = conn, %{message_id: id}) do
json(conn, %{}) json(conn, %{})
end end
end end
def messages(conn, %{id: id} = params) do
with %Chat{} = chat <- Chat.get_by_id(id) do
cm_refs =
chat
|> MessageReference.for_chat_query()
|> Pagination.fetch_paginated(params)
conn
|> put_view(MessageReferenceView)
|> render("index.json", chat_message_references: cm_refs)
else
_ ->
conn
|> put_status(:not_found)
|> json(%{error: "not found"})
end
end
end end

View file

@ -16,7 +16,7 @@ def open_api_operation(action) do
def delete_message_operation do def delete_message_operation do
%Operation{ %Operation{
tags: ["Admin", "Chats"], tags: ["admin", "chat"],
summary: "Delete an individual chat message", summary: "Delete an individual chat message",
operationId: "AdminAPI.ChatController.delete", operationId: "AdminAPI.ChatController.delete",
parameters: [id_param(), message_id_param()] ++ admin_api_params(), parameters: [id_param(), message_id_param()] ++ admin_api_params(),
@ -28,6 +28,30 @@ def delete_message_operation do
} }
end end
def messages_operation do
%Operation{
tags: ["admin", "chat"],
summary: "Get the most recent messages of the chat",
operationId: "AdminAPI.ChatController.messages",
parameters:
[Operation.parameter(:id, :path, :string, "The ID of the Chat")] ++
pagination_params(),
responses: %{
200 =>
Operation.response(
"The messages in the chat",
"application/json",
Pleroma.Web.ApiSpec.ChatOperation.chat_messages_response()
)
},
security: [
%{
"oAuth" => ["read:chats"]
}
]
}
end
def id_param do def id_param do
Operation.parameter(:id, :path, FlakeID, "Chat ID", Operation.parameter(:id, :path, FlakeID, "Chat ID",
example: "9umDrYheeY451cQnEe", example: "9umDrYheeY451cQnEe",

View file

@ -217,7 +217,7 @@ defmodule Pleroma.Web.Router do
post("/media_proxy_caches/purge", MediaProxyCacheController, :purge) post("/media_proxy_caches/purge", MediaProxyCacheController, :purge)
# get("/chats/:id", ChatController, :show) # get("/chats/:id", ChatController, :show)
# get("/chats/:id/messages", ChatController, :messages) 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

@ -8,9 +8,11 @@ defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.Config alias Pleroma.Config
alias Pleroma.ModerationLog alias Pleroma.ModerationLog
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Web.CommonAPI
setup do setup do
admin = insert(:user, is_admin: true) admin = insert(:user, is_admin: true)
@ -50,4 +52,56 @@ test "returns 404 when the chat message does not exist", %{conn: conn} do
assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"} assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
end end
end end
describe "GET /api/pleroma/admin/chats/:id/messages" do
test "it paginates", %{conn: conn} do
user = insert(:user)
recipient = insert(:user)
Enum.each(1..30, fn _ ->
{:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
end)
chat = Chat.get(user.id, recipient.ap_id)
result =
conn
|> get("/api/pleroma/admin/chats/#{chat.id}/messages")
|> json_response_and_validate_schema(200)
assert length(result) == 20
result =
conn
|> get("/api/pleroma/admin/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
|> json_response_and_validate_schema(200)
assert length(result) == 10
end
test "it returns the messages for a given chat", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)
third_user = insert(:user)
{:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
{:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
{:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
{:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
chat = Chat.get(user.id, other_user.ap_id)
result =
conn
|> get("/api/pleroma/admin/chats/#{chat.id}/messages")
|> json_response_and_validate_schema(200)
result
|> Enum.each(fn message ->
assert message["chat_id"] == chat.id |> to_string()
end)
assert length(result) == 3
end
end
end end