Fix MRF reject for ChatMessage
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Mon, 14 Sep 2020 12:07:22 +0000 (14:07 +0200)
committerrinpatch <rinpatch@sdf.org>
Thu, 17 Sep 2020 19:07:54 +0000 (22:07 +0300)
lib/pleroma/web/api_spec/operations/chat_operation.ex
lib/pleroma/web/api_spec/operations/status_operation.ex
lib/pleroma/web/common_api/common_api.ex
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
test/web/common_api/common_api_test.exs
test/web/pleroma_api/controllers/chat_controller_test.exs

index b1a0d26ab6e51f3315db9e7508c19717c6b91eef..56554d5b4a3d0913733ad8391390ef4f94b56689 100644 (file)
@@ -184,7 +184,8 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
             "application/json",
             ChatMessage
           ),
-        400 => Operation.response("Bad Request", "application/json", ApiError)
+        400 => Operation.response("Bad Request", "application/json", ApiError),
+        422 => Operation.response("MRF Rejection", "application/json", ApiError)
       },
       security: [
         %{
index 5bd4619d519dba85b043fcc95b3ac1d7091be548..d7ebde6f6f5c0050e73f8f4c5c3e5433c3e1b345 100644 (file)
@@ -55,7 +55,7 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
             "application/json",
             %Schema{oneOf: [Status, ScheduledStatus]}
           ),
-        422 => Operation.response("Bad Request", "application/json", ApiError)
+        422 => Operation.response("Bad Request / MRF Rejection", "application/json", ApiError)
       }
     }
   end
index a8c83bc8febdd054331b061da253f6f53ab38d34..60a50b027f5c5981adc3406c0b086bdcff49e039 100644 (file)
@@ -48,6 +48,9 @@ defmodule Pleroma.Web.CommonAPI do
               local: true
             )} do
       {:ok, activity}
+    else
+      {:common_pipeline, {:reject, _} = e} -> e
+      e -> e
     end
   end
 
index 27c9a2e0f77a0022019e21caa4b224ca9e81b6ae..867cff8294af5b2835d1f687bf10e5de5c17cb9e 100644 (file)
@@ -90,6 +90,16 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
       conn
       |> put_view(MessageReferenceView)
       |> render("show.json", chat_message_reference: cm_ref)
+    else
+      {:reject, message} ->
+        conn
+        |> put_status(:unprocessable_entity)
+        |> json(%{error: message})
+
+      {:error, message} ->
+        conn
+        |> put_status(:bad_request)
+        |> json(%{error: message})
     end
   end
 
index f5559f932b0f1462771e56565d689a760077b577..2eab64e8b96dbdb4373094887e0784e32534a7e3 100644 (file)
@@ -217,6 +217,17 @@ defmodule Pleroma.Web.CommonAPITest do
 
       assert message == :content_too_long
     end
+
+    test "it reject messages via MRF" do
+      clear_config([:mrf_keyword, :reject], ["GNO"])
+      clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
+
+      author = insert(:user)
+      recipient = insert(:user)
+
+      assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} ==
+               CommonAPI.post_chat_message(author, recipient, "GNO/Linux")
+    end
   end
 
   describe "unblocking" do
index 32c23e9d75a680fddab1cbf7691d37d7808087e2..44a78a738f35527b3153db96dcce6c05a6fe9be5 100644 (file)
@@ -100,7 +100,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
         |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
         |> json_response_and_validate_schema(400)
 
-      assert result
+      assert %{"error" => "no_content"} == result
     end
 
     test "it works with an attachment", %{conn: conn, user: user} do
@@ -139,9 +139,9 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
         conn
         |> put_req_header("content-type", "application/json")
         |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "GNO/Linux"})
-        |> json_response_and_validate_schema(200)
+        |> json_response_and_validate_schema(422)
 
-      assert result == %{}
+      assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} == result
     end
   end