CommonAPI: Obey local limit for chat messages.
authorlain <lain@soykaf.club>
Mon, 20 Apr 2020 10:08:47 +0000 (12:08 +0200)
committerlain <lain@soykaf.club>
Mon, 20 Apr 2020 10:08:47 +0000 (12:08 +0200)
lib/pleroma/web/common_api/common_api.ex
test/web/common_api/common_api_test.exs
test/web/pleroma_api/views/chat_message_view_test.exs
test/web/pleroma_api/views/chat_view_test.exs

index 2b8add2fa7b61022e1e8876f0dfcbfe6c7971508..fcb0af4e86d2633cfcff9a2c47c9d732ba70ef11 100644 (file)
@@ -28,7 +28,10 @@ defmodule Pleroma.Web.CommonAPI do
   def post_chat_message(%User{} = user, %User{} = recipient, content) do
     transaction =
       Repo.transaction(fn ->
-        with {_, {:ok, chat_message_data, _meta}} <-
+        with {_, true} <-
+               {:content_length,
+                String.length(content) <= Pleroma.Config.get([:instance, :chat_limit])},
+             {_, {:ok, chat_message_data, _meta}} <-
                {:build_object,
                 Builder.chat_message(
                   user,
@@ -43,6 +46,9 @@ defmodule Pleroma.Web.CommonAPI do
              {_, {:ok, %Activity{} = activity, _meta}} <-
                {:common_pipeline, Pipeline.common_pipeline(create_activity_data, local: true)} do
           {:ok, activity}
+        else
+          {:content_length, false} -> {:error, :content_too_long}
+          e -> e
         end
       end)
 
index 1984aac8d1bd06acb436a575a5d7a21107311524..c17e30210ccb128844694c9cc70a556a9bc18a4c 100644 (file)
@@ -23,6 +23,8 @@ defmodule Pleroma.Web.CommonAPITest do
   setup do: clear_config([:instance, :max_pinned_statuses])
 
   describe "posting chat messages" do
+    setup do: clear_config([:instance, :chat_limit])
+
     test "it posts a chat message" do
       author = insert(:user)
       recipient = insert(:user)
@@ -47,6 +49,22 @@ defmodule Pleroma.Web.CommonAPITest do
       assert Chat.get(author.id, recipient.ap_id)
       assert Chat.get(recipient.id, author.ap_id)
     end
+
+    test "it reject messages over the local limit" do
+      Pleroma.Config.put([:instance, :chat_limit], 2)
+
+      author = insert(:user)
+      recipient = insert(:user)
+
+      {:error, message} =
+        CommonAPI.post_chat_message(
+          author,
+          recipient,
+          "123"
+        )
+
+      assert message == :content_too_long
+    end
   end
 
   test "when replying to a conversation / participation, it will set the correct context id even if no explicit reply_to is given" do
index e690da022786cb831834b02b7d82722cd21d17b8..ad8febee6478cba5e1006cba0c8e8dd2bd76caf4 100644 (file)
@@ -23,7 +23,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageViewTest do
 
     chat_message = ChatMessageView.render("show.json", object: object, for: user, chat: chat)
 
-    assert chat_message[:id] == object.id
+    assert chat_message[:id] == object.id |> to_string()
     assert chat_message[:content] == "kippis"
     assert chat_message[:actor] == user.ap_id
     assert chat_message[:chat_id]
@@ -34,7 +34,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageViewTest do
 
     chat_message_two = ChatMessageView.render("show.json", object: object, for: user, chat: chat)
 
-    assert chat_message_two[:id] == object.id
+    assert chat_message_two[:id] == object.id |> to_string()
     assert chat_message_two[:content] == "gkgkgk"
     assert chat_message_two[:actor] == recipient.ap_id
     assert chat_message_two[:chat_id] == chat_message[:chat_id]
index 1eb0c6241deebdfb8082a0f26222a45ad210c8b3..3dca555e8600df27f7026811a5cf1c0f038065b1 100644 (file)
@@ -2,7 +2,7 @@
 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
-defmodule Pleroma.Web.PleromaAPI.ChatMessageViewTest do
+defmodule Pleroma.Web.PleromaAPI.ChatViewTest do
   use Pleroma.DataCase
 
   alias Pleroma.Chat