From aa97fe8f175c3618a04bb45e6aa7c37e26259577 Mon Sep 17 00:00:00 2001
From: lain <lain@soykaf.club>
Date: Wed, 4 Dec 2019 12:48:34 +0100
Subject: [PATCH 1/3] ChatChannel: Ignore messages that are too long.

---
 config/config.exs               |  1 +
 lib/pleroma/application.ex      |  2 --
 lib/pleroma/web/chat_channel.ex |  2 +-
 test/support/channel_case.ex    |  1 +
 test/web/chat_channel_test.exs  | 37 +++++++++++++++++++++++++++++++++
 5 files changed, 40 insertions(+), 3 deletions(-)
 create mode 100644 test/web/chat_channel_test.exs

diff --git a/config/config.exs b/config/config.exs
index b60ffef7d..4624bded2 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -225,6 +225,7 @@
   notify_email: "noreply@example.com",
   description: "A Pleroma instance, an alternative fediverse server",
   limit: 5_000,
+  chat_limit: 5_000,
   remote_limit: 100_000,
   upload_limit: 16_000_000,
   avatar_upload_limit: 2_000_000,
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex
index 9dbd1e26b..57462740c 100644
--- a/lib/pleroma/application.ex
+++ b/lib/pleroma/application.ex
@@ -147,8 +147,6 @@ defp oauth_cleanup_child(true),
 
   defp oauth_cleanup_child(_), do: []
 
-  defp chat_child(:test, _), do: []
-
   defp chat_child(_env, true) do
     [Pleroma.Web.ChatChannel.ChatChannelState]
   end
diff --git a/lib/pleroma/web/chat_channel.ex b/lib/pleroma/web/chat_channel.ex
index 08841a3e8..840414933 100644
--- a/lib/pleroma/web/chat_channel.ex
+++ b/lib/pleroma/web/chat_channel.ex
@@ -20,7 +20,7 @@ def handle_info(:after_join, socket) do
   def handle_in("new_msg", %{"text" => text}, %{assigns: %{user_name: user_name}} = socket) do
     text = String.trim(text)
 
-    if String.length(text) > 0 do
+    if String.length(text) in 1..Pleroma.Config.get([:instance, :chat_limit]) do
       author = User.get_cached_by_nickname(user_name)
       author = Pleroma.Web.MastodonAPI.AccountView.render("show.json", user: author)
       message = ChatChannelState.add_message(%{text: text, author: author})
diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex
index 466d8986f..4a4585844 100644
--- a/test/support/channel_case.ex
+++ b/test/support/channel_case.ex
@@ -23,6 +23,7 @@ defmodule Pleroma.Web.ChannelCase do
     quote do
       # Import conveniences for testing with channels
       use Phoenix.ChannelTest
+      use Pleroma.Tests.Helpers
 
       # The default endpoint for testing
       @endpoint Pleroma.Web.Endpoint
diff --git a/test/web/chat_channel_test.exs b/test/web/chat_channel_test.exs
new file mode 100644
index 000000000..68c24a9f9
--- /dev/null
+++ b/test/web/chat_channel_test.exs
@@ -0,0 +1,37 @@
+defmodule Pleroma.Web.ChatChannelTest do
+  use Pleroma.Web.ChannelCase
+  alias Pleroma.Web.ChatChannel
+  alias Pleroma.Web.UserSocket
+
+  import Pleroma.Factory
+
+  setup do
+    user = insert(:user)
+
+    {:ok, _, socket} =
+      socket(UserSocket, "", %{user_name: user.nickname})
+      |> subscribe_and_join(ChatChannel, "chat:public")
+
+    {:ok, socket: socket}
+  end
+
+  test "it broadcasts a message", %{socket: socket} do
+    push(socket, "new_msg", %{"text" => "why is tenshi eating a corndog so cute?"})
+    assert_broadcast("new_msg", %{text: "why is tenshi eating a corndog so cute?"})
+  end
+
+  describe "message lengths" do
+    clear_config([:instance, :chat_limit])
+
+    test "it ignores messages of length zero", %{socket: socket} do
+      push(socket, "new_msg", %{"text" => ""})
+      refute_broadcast("new_msg", %{text: ""})
+    end
+
+    test "it ignores messages above a certain length", %{socket: socket} do
+      Pleroma.Config.put([:instance, :chat_limit], 2)
+      push(socket, "new_msg", %{"text" => "123"})
+      refute_broadcast("new_msg", %{text: "123"})
+    end
+  end
+end

From 21353a8ee900fc5b48d7de5168f0b81e7b15dc97 Mon Sep 17 00:00:00 2001
From: lain <lain@soykaf.club>
Date: Wed, 4 Dec 2019 12:49:46 +0100
Subject: [PATCH 2/3] Changelog: Add information about chat limit.

---
 CHANGELOG.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index a06ea211e..b162026e0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -38,6 +38,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 </details>
 
 ### Added
+- `:chat_limit` option to limit chat characters.
 - Refreshing poll results for remote polls
 - Authentication: Added rate limit for password-authorized actions / login existence checks
 - Static Frontend: Add the ability to render user profiles and notices server-side without requiring JS app.

From 9487995e8b2172bd720d17d639d5d782d38a45a2 Mon Sep 17 00:00:00 2001
From: lain <lain@soykaf.club>
Date: Wed, 4 Dec 2019 12:51:06 +0100
Subject: [PATCH 3/3] Cheat Sheet: Addg chat_limit information.

---
 docs/configuration/cheatsheet.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md
index dc2f55229..ef2711e3c 100644
--- a/docs/configuration/cheatsheet.md
+++ b/docs/configuration/cheatsheet.md
@@ -12,6 +12,7 @@ You shouldn't edit the base config directly to avoid breakages and merge conflic
 * `notify_email`: Email used for notifications.
 * `description`: The instance’s description, can be seen in nodeinfo and ``/api/v1/instance``.
 * `limit`: Posts character limit (CW/Subject included in the counter).
+* `chat_limit`: Character limit of the instance chat messages.
 * `remote_limit`: Hard character limit beyond which remote posts will be dropped.
 * `upload_limit`: File size limit of uploads (except for avatar, background, banner).
 * `avatar_upload_limit`: File size limit of user’s profile avatars.