Explorar el Código

ChatController: Support deletion of chat messages.

1570-levenshtein-distance-user-search
lain hace 4 años
padre
commit
b5aa204eb8
Se han modificado 5 ficheros con 75 adiciones y 2 borrados
  1. +2
    -1
      lib/pleroma/web/activity_pub/object_validators/delete_validator.ex
  2. +25
    -0
      lib/pleroma/web/api_spec/operations/chat_operation.ex
  3. +23
    -1
      lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
  4. +1
    -0
      lib/pleroma/web/router.ex
  5. +24
    -0
      test/web/pleroma_api/controllers/chat_controller_test.exs

+ 2
- 1
lib/pleroma/web/activity_pub/object_validators/delete_validator.ex Ver fichero

@@ -46,12 +46,13 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator do
Answer
Article
Audio
ChatMessage
Event
Note
Page
Question
Video
Tombstone
Video
}
def validate_data(cng) do
cng


+ 25
- 0
lib/pleroma/web/api_spec/operations/chat_operation.ex Ver fichero

@@ -166,6 +166,31 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
}
end

def delete_message_operation do
%Operation{
tags: ["chat"],
summary: "delete_message",
operationId: "ChatController.delete_message",
parameters: [
Operation.parameter(:id, :path, :string, "The ID of the Chat"),
Operation.parameter(:message_id, :path, :string, "The ID of the message")
],
responses: %{
200 =>
Operation.response(
"The deleted ChatMessage",
"application/json",
ChatMessage
)
},
security: [
%{
"oAuth" => ["write"]
}
]
}
end

def chats_response do
%Schema{
title: "ChatsResponse",


+ 23
- 1
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex Ver fichero

@@ -4,6 +4,7 @@
defmodule Pleroma.Web.PleromaAPI.ChatController do
use Pleroma.Web, :controller

alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.Object
alias Pleroma.Pagination
@@ -22,7 +23,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do

plug(
OAuthScopesPlug,
%{scopes: ["write:statuses"]} when action in [:post_chat_message, :create, :mark_as_read]
%{scopes: ["write:statuses"]}
when action in [:post_chat_message, :create, :mark_as_read, :delete_message]
)

plug(
@@ -34,6 +36,26 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do

defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.ChatOperation

def delete_message(%{assigns: %{user: %{ap_id: actor} = user}} = conn, %{
message_id: id
}) do
with %Object{
data: %{
"actor" => ^actor,
"id" => object,
"to" => [recipient],
"type" => "ChatMessage"
}
} = message <- Object.get_by_id(id),
%Chat{} = chat <- Chat.get(user.id, recipient),
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object),
{:ok, _delete} <- CommonAPI.delete(activity.id, user) do
conn
|> put_view(ChatMessageView)
|> render("show.json", for: user, object: message, chat: chat)
end
end

def post_chat_message(
%{body_params: %{content: content} = params, assigns: %{user: %{id: user_id} = user}} =
conn,


+ 1
- 0
lib/pleroma/web/router.ex Ver fichero

@@ -310,6 +310,7 @@ defmodule Pleroma.Web.Router do
get("/chats/:id", ChatController, :show)
get("/chats/:id/messages", ChatController, :messages)
post("/chats/:id/messages", ChatController, :post_chat_message)
delete("/chats/:id/messages/:message_id", ChatController, :delete_message)
post("/chats/:id/read", ChatController, :mark_as_read)
end



+ 24
- 0
test/web/pleroma_api/controllers/chat_controller_test.exs Ver fichero

@@ -4,6 +4,7 @@
defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
use Pleroma.Web.ConnCase, async: true

alias Pleroma.Object
alias Pleroma.Chat
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.CommonAPI
@@ -78,6 +79,29 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
end
end

describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
setup do: oauth_access(["write:statuses"])

test "it deletes a message for the author of the message", %{conn: conn, user: user} do
recipient = insert(:user)

{:ok, message} =
CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")

object = Object.normalize(message, false)

chat = Chat.get(user.id, recipient.ap_id)

result =
conn
|> put_req_header("content-type", "application/json")
|> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
|> json_response_and_validate_schema(200)

assert result["id"] == to_string(object.id)
end
end

describe "GET /api/v1/pleroma/chats/:id/messages" do
setup do: oauth_access(["read:statuses"])



Cargando…
Cancelar
Guardar