logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git
commit: ff00b354fa5067c898e860e275748dd757cb04cd
parent ee52fc840d4d6eb1caab811a9b00ab1b95662d77
Author: Mark Felder <feld@FreeBSD.org>
Date:   Mon,  3 Aug 2020 17:08:35 -0500

Rename the non-federating Chat feature to Shout

Diffstat:

MCHANGELOG.md1+
Mconfig/config.exs4++--
Mconfig/description.exs8++++----
Mlib/pleroma/config/transfer_task.ex2+-
Mlib/pleroma/web/channels/user_socket.ex4++--
Dlib/pleroma/web/chat_channel.ex59-----------------------------------------------------------
Mlib/pleroma/web/mastodon_api/views/instance_view.ex6+++---
Alib/pleroma/web/shout_channel.ex59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest/pleroma/config/transfer_task_test.exs8++++----
Mtest/pleroma/web/admin_api/controllers/config_controller_test.exs12++++++------
Dtest/pleroma/web/chat_channel_test.exs41-----------------------------------------
Mtest/pleroma/web/mastodon_api/controllers/instance_controller_test.exs2+-
Atest/pleroma/web/shout_channel_test.ex41+++++++++++++++++++++++++++++++++++++++++
13 files changed, 124 insertions(+), 123 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed +- **Breaking:** Configuration: `:chat, enabled` moved to `:shout, enabled` and `:instance, chat_limit` moved to `:instance, shout_limit` - The `application` metadata returned with statuses is no longer hardcoded. Apps that want to display these details will now have valid data for new posts after this change. - HTTPSecurityPlug now sends a response header to opt out of Google's FLoC (Federated Learning of Cohorts) targeted advertising. - Email address is now returned if requesting user is the owner of the user account so it can be exposed in client and FE user settings UIs. diff --git a/config/config.exs b/config/config.exs @@ -190,7 +190,7 @@ config :pleroma, :instance, instance_thumbnail: "/instance/thumbnail.jpeg", limit: 5_000, description_limit: 5_000, - chat_limit: 5_000, + shout_limit: 5_000, remote_limit: 100_000, upload_limit: 16_000_000, avatar_upload_limit: 2_000_000, @@ -457,7 +457,7 @@ config :pleroma, :media_preview_proxy, image_quality: 85, min_content_length: 100 * 1024 -config :pleroma, :chat, enabled: true +config :pleroma, :shoutbox, enabled: true config :phoenix, :format_encoders, json: Jason diff --git a/config/description.exs b/config/description.exs @@ -545,9 +545,9 @@ config :pleroma, :config_description, [ ] }, %{ - key: :chat_limit, + key: :shout_limit, type: :integer, - description: "Character limit of the instance chat messages", + description: "Character limit of the instance shout messages", suggestions: [ 5_000 ] @@ -2652,9 +2652,9 @@ config :pleroma, :config_description, [ }, %{ group: :pleroma, - key: :chat, + key: :shout, type: :group, - description: "Pleroma chat settings", + description: "Pleroma shout settings", children: [ %{ key: :enabled, diff --git a/lib/pleroma/config/transfer_task.ex b/lib/pleroma/config/transfer_task.ex @@ -16,7 +16,7 @@ defmodule Pleroma.Config.TransferTask do defp reboot_time_keys, do: [ {:pleroma, :hackney_pools}, - {:pleroma, :chat}, + {:pleroma, :shout}, {:pleroma, Oban}, {:pleroma, :rate_limit}, {:pleroma, :markup}, diff --git a/lib/pleroma/web/channels/user_socket.ex b/lib/pleroma/web/channels/user_socket.ex @@ -8,7 +8,7 @@ defmodule Pleroma.Web.UserSocket do ## Channels # channel "room:*", Pleroma.Web.RoomChannel - channel("chat:*", Pleroma.Web.ChatChannel) + channel("shout:*", Pleroma.Web.ShoutChannel) # Socket params are passed from the client and can # be used to verify and authenticate a user. After @@ -22,7 +22,7 @@ defmodule Pleroma.Web.UserSocket do # See `Phoenix.Token` documentation for examples in # performing token verification on connect. def connect(%{"token" => token}, socket) do - with true <- Pleroma.Config.get([:chat, :enabled]), + with true <- Pleroma.Config.get([:shout, :enabled]), {:ok, user_id} <- Phoenix.Token.verify(socket, "user socket", token, max_age: 84_600), %User{} = user <- Pleroma.User.get_cached_by_id(user_id) do {:ok, assign(socket, :user_name, user.nickname)} diff --git a/lib/pleroma/web/chat_channel.ex b/lib/pleroma/web/chat_channel.ex @@ -1,59 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.ChatChannel do - use Phoenix.Channel - - alias Pleroma.User - alias Pleroma.Web.ChatChannel.ChatChannelState - alias Pleroma.Web.MastodonAPI.AccountView - - def join("chat:public", _message, socket) do - send(self(), :after_join) - {:ok, socket} - end - - def handle_info(:after_join, socket) do - push(socket, "messages", %{messages: ChatChannelState.messages()}) - {:noreply, socket} - end - - def handle_in("new_msg", %{"text" => text}, %{assigns: %{user_name: user_name}} = socket) do - text = String.trim(text) - - if String.length(text) in 1..Pleroma.Config.get([:instance, :chat_limit]) do - author = User.get_cached_by_nickname(user_name) - author_json = AccountView.render("show.json", user: author, skip_visibility_check: true) - - message = ChatChannelState.add_message(%{text: text, author: author_json}) - - broadcast!(socket, "new_msg", message) - end - - {:noreply, socket} - end -end - -defmodule Pleroma.Web.ChatChannel.ChatChannelState do - use Agent - - @max_messages 20 - - def start_link(_) do - Agent.start_link(fn -> %{max_id: 1, messages: []} end, name: __MODULE__) - end - - def add_message(message) do - Agent.get_and_update(__MODULE__, fn state -> - id = state[:max_id] + 1 - message = Map.put(message, "id", id) - messages = [message | state[:messages]] |> Enum.take(@max_messages) - {message, %{max_id: id, messages: messages}} - end) - end - - def messages do - Agent.get(__MODULE__, fn state -> state[:messages] |> Enum.reverse() end) - end -end diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -37,7 +37,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do background_upload_limit: Keyword.get(instance, :background_upload_limit), banner_upload_limit: Keyword.get(instance, :banner_upload_limit), background_image: Pleroma.Web.Endpoint.url() <> Keyword.get(instance, :background_image), - chat_limit: Keyword.get(instance, :chat_limit), + shout_limit: Keyword.get(instance, :shout_limit), description_limit: Keyword.get(instance, :description_limit), pleroma: %{ metadata: %{ @@ -69,8 +69,8 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do if Config.get([:gopher, :enabled]) do "gopher" end, - if Config.get([:chat, :enabled]) do - "chat" + if Config.get([:shout, :enabled]) do + "shout" end, if Config.get([:instance, :allow_relay]) do "relay" diff --git a/lib/pleroma/web/shout_channel.ex b/lib/pleroma/web/shout_channel.ex @@ -0,0 +1,59 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ShoutChannel do + use Phoenix.Channel + + alias Pleroma.User + alias Pleroma.Web.MastodonAPI.AccountView + alias Pleroma.Web.ShoutChannel.ShoutChannelState + + def join("shout:public", _message, socket) do + send(self(), :after_join) + {:ok, socket} + end + + def handle_info(:after_join, socket) do + push(socket, "messages", %{messages: ShoutChannelState.messages()}) + {:noreply, socket} + end + + def handle_in("new_msg", %{"text" => text}, %{assigns: %{user_name: user_name}} = socket) do + text = String.trim(text) + + if String.length(text) in 1..Pleroma.Config.get([:instance, :shout_limit]) do + author = User.get_cached_by_nickname(user_name) + author_json = AccountView.render("show.json", user: author, skip_visibility_check: true) + + message = ShoutChannelState.add_message(%{text: text, author: author_json}) + + broadcast!(socket, "new_msg", message) + end + + {:noreply, socket} + end +end + +defmodule Pleroma.Web.ShoutChannel.ShoutChannelState do + use Agent + + @max_messages 20 + + def start_link(_) do + Agent.start_link(fn -> %{max_id: 1, messages: []} end, name: __MODULE__) + end + + def add_message(message) do + Agent.get_and_update(__MODULE__, fn state -> + id = state[:max_id] + 1 + message = Map.put(message, "id", id) + messages = [message | state[:messages]] |> Enum.take(@max_messages) + {message, %{max_id: id, messages: messages}} + end) + end + + def messages do + Agent.get(__MODULE__, fn state -> state[:messages] |> Enum.reverse() end) + end +end diff --git a/test/pleroma/config/transfer_task_test.exs b/test/pleroma/config/transfer_task_test.exs @@ -93,8 +93,8 @@ defmodule Pleroma.Config.TransferTaskTest do end test "on reboot time key" do - clear_config(:chat) - insert(:config, key: :chat, value: [enabled: false]) + clear_config(:shout) + insert(:config, key: :shout, value: [enabled: false]) assert capture_log(fn -> TransferTask.start_link([]) end) =~ "pleroma restarted" end @@ -105,10 +105,10 @@ defmodule Pleroma.Config.TransferTaskTest do end test "don't restart pleroma on reboot time key and subkey if there is false flag" do - clear_config(:chat) + clear_config(:shout) clear_config(Pleroma.Captcha) - insert(:config, key: :chat, value: [enabled: false]) + insert(:config, key: :shout, value: [enabled: false]) insert(:config, key: Pleroma.Captcha, value: [seconds_valid: 60]) refute String.contains?( diff --git a/test/pleroma/web/admin_api/controllers/config_controller_test.exs b/test/pleroma/web/admin_api/controllers/config_controller_test.exs @@ -409,7 +409,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do end test "saving config which need pleroma reboot", %{conn: conn} do - clear_config([:chat, :enabled], true) + clear_config([:shout, :enabled], true) assert conn |> put_req_header("content-type", "application/json") @@ -417,7 +417,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "/api/pleroma/admin/config", %{ configs: [ - %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]} + %{group: ":pleroma", key: ":shout", value: [%{"tuple" => [":enabled", true]}]} ] } ) @@ -426,7 +426,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do %{ "db" => [":enabled"], "group" => ":pleroma", - "key" => ":chat", + "key" => ":shout", "value" => [%{"tuple" => [":enabled", true]}] } ], @@ -454,7 +454,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do end test "update setting which need reboot, don't change reboot flag until reboot", %{conn: conn} do - clear_config([:chat, :enabled], true) + clear_config([:shout, :enabled], true) assert conn |> put_req_header("content-type", "application/json") @@ -462,7 +462,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do "/api/pleroma/admin/config", %{ configs: [ - %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]} + %{group: ":pleroma", key: ":shout", value: [%{"tuple" => [":enabled", true]}]} ] } ) @@ -471,7 +471,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do %{ "db" => [":enabled"], "group" => ":pleroma", - "key" => ":chat", + "key" => ":shout", "value" => [%{"tuple" => [":enabled", true]}] } ], diff --git a/test/pleroma/web/chat_channel_test.exs b/test/pleroma/web/chat_channel_test.exs @@ -1,41 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -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 - setup 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 - clear_config([:instance, :chat_limit], 2) - push(socket, "new_msg", %{"text" => "123"}) - refute_broadcast("new_msg", %{text: "123"}) - end - end -end diff --git a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs @@ -38,7 +38,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do "background_upload_limit" => _, "banner_upload_limit" => _, "background_image" => from_config_background, - "chat_limit" => _, + "shout_limit" => _, "description_limit" => _ } = result diff --git a/test/pleroma/web/shout_channel_test.ex b/test/pleroma/web/shout_channel_test.ex @@ -0,0 +1,41 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ShoutChannelTest do + use Pleroma.Web.ChannelCase + alias Pleroma.Web.ShoutChannel + alias Pleroma.Web.UserSocket + + import Pleroma.Factory + + setup do + user = insert(:user) + + {:ok, _, socket} = + socket(UserSocket, "", %{user_name: user.nickname}) + |> subscribe_and_join(ShoutChannel, "shout: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 + setup do: clear_config([:instance, :shout_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, :shout_limit], 2) + push(socket, "new_msg", %{"text" => "123"}) + refute_broadcast("new_msg", %{text: "123"}) + end + end +end