Chat Controller: Add basic error handling.
authorlain <lain@soykaf.club>
Tue, 12 May 2020 11:23:09 +0000 (13:23 +0200)
committerlain <lain@soykaf.club>
Tue, 12 May 2020 11:23:09 +0000 (13:23 +0200)
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
test/web/pleroma_api/controllers/chat_controller_test.exs

index 8eed8875285cf2eb6ff7d16063ae60ab74bde250..4ce3e7419cd44bb07e24af4c120c1d15ea8e7c72 100644 (file)
@@ -18,8 +18,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
   import Ecto.Query
   import Pleroma.Web.ActivityPub.ObjectValidator, only: [stringify_keys: 1]
 
-  # TODO
-  # - Error handling
+  action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
 
   plug(
     OAuthScopesPlug,
@@ -53,6 +52,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
       conn
       |> put_view(ChatMessageView)
       |> render("show.json", for: user, object: message, chat: chat)
+    else
+      _e -> {:error, :could_not_delete}
     end
   end
 
index 86ccbb117f332598623bdcfced4ba16cf1a8d9c1..75d4903ed74f7361d913632fa2ba308e39bfa8cf 100644 (file)
@@ -88,6 +88,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
       {:ok, message} =
         CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
 
+      {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
+
       object = Object.normalize(message, false)
 
       chat = Chat.get(user.id, recipient.ap_id)
@@ -99,6 +101,16 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
         |> json_response_and_validate_schema(200)
 
       assert result["id"] == to_string(object.id)
+
+      object = Object.normalize(other_message, false)
+
+      result =
+        conn
+        |> put_req_header("content-type", "application/json")
+        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
+        |> json_response(400)
+
+      assert result == %{"error" => "could_not_delete"}
     end
   end