diff options
author | Alex Gleason <alex@alexgleason.me> | 2020-09-01 19:39:34 -0500 |
---|---|---|
committer | Alex Gleason <alex@alexgleason.me> | 2020-09-11 14:10:19 -0500 |
commit | f13b52a703d5c60cf12b2fff69f458e5c467c783 (patch) | |
tree | 68d2e1aaa4fb30a24553f27cbc340729d4d8e07f /lib | |
parent | c41430b23eaf3fd15b227e66215aa2a4ff31dfdb (diff) | |
download | pleroma-f13b52a703d5c60cf12b2fff69f458e5c467c783.tar.gz |
AdminAPI: list messages in a chat
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/web/admin_api/controllers/chat_controller.ex | 27 | ||||
-rw-r--r-- | lib/pleroma/web/api_spec/operations/admin/chat_operation.ex | 26 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 2 |
3 files changed, 53 insertions, 2 deletions
diff --git a/lib/pleroma/web/admin_api/controllers/chat_controller.ex b/lib/pleroma/web/admin_api/controllers/chat_controller.ex index bcce824d2..b423188d7 100644 --- a/lib/pleroma/web/admin_api/controllers/chat_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/chat_controller.ex @@ -6,9 +6,13 @@ defmodule Pleroma.Web.AdminAPI.ChatController do use Pleroma.Web, :controller alias Pleroma.Activity + alias Pleroma.Chat + alias Pleroma.Chat.MessageReference alias Pleroma.ModerationLog + alias Pleroma.Pagination alias Pleroma.Plugs.OAuthScopesPlug alias Pleroma.Web.CommonAPI + alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView require Logger @@ -16,6 +20,11 @@ defmodule Pleroma.Web.AdminAPI.ChatController do plug( OAuthScopesPlug, + %{scopes: ["read:chats"], admin: true} when action in [:messages] + ) + + plug( + OAuthScopesPlug, %{scopes: ["write:chats"], admin: true} when action in [:delete_message] ) @@ -34,4 +43,22 @@ defmodule Pleroma.Web.AdminAPI.ChatController do json(conn, %{}) 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 diff --git a/lib/pleroma/web/api_spec/operations/admin/chat_operation.ex b/lib/pleroma/web/api_spec/operations/admin/chat_operation.ex index 7045fd7ce..a382bd35a 100644 --- a/lib/pleroma/web/api_spec/operations/admin/chat_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/chat_operation.ex @@ -16,7 +16,7 @@ defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do def delete_message_operation do %Operation{ - tags: ["Admin", "Chats"], + tags: ["admin", "chat"], summary: "Delete an individual chat message", operationId: "AdminAPI.ChatController.delete", parameters: [id_param(), message_id_param()] ++ admin_api_params(), @@ -28,6 +28,30 @@ defmodule Pleroma.Web.ApiSpec.Admin.ChatOperation do } 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 Operation.parameter(:id, :path, FlakeID, "Chat ID", example: "9umDrYheeY451cQnEe", diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index ad3282df4..02836114a 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -217,7 +217,7 @@ defmodule Pleroma.Web.Router do post("/media_proxy_caches/purge", MediaProxyCacheController, :purge) # 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) end |