Add welcome chatmessages
authorIlja <domainepublic@spectraltheorem.be>
Sun, 2 Aug 2020 13:54:59 +0000 (15:54 +0200)
committerIlja <domainepublic@spectraltheorem.be>
Sun, 2 Aug 2020 13:54:59 +0000 (15:54 +0200)
* I added the option in config/config.exs
* created a new module lib/pleroma/user/welcome_chat_message.ex
* Added it to the registration flow
* added to the cheatsheet
* added to the config/description.ex
* added to the Changelog.md

CHANGELOG.md
config/config.exs
config/description.exs
docs/configuration/cheatsheet.md
lib/pleroma/user.ex
lib/pleroma/user/welcome_chat_message.ex [new file with mode: 0644]
test/user/welcome_chat_massage_test.exs [new file with mode: 0644]
test/user_test.exs

index 129c269aa03cfabca1536d17c6b0d479bf2c7c29..4b682d70b620b11ce3bcccdf26b6335917deee3f 100644 (file)
@@ -69,7 +69,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Support for viewing instances favicons next to posts and accounts
 - Added Pleroma.Upload.Filter.Exiftool as an alternate EXIF stripping mechanism targeting GPS/location metadata.
 - "By approval" registrations mode.
-- Configuration: Added `:welcome` settings for the welcome message to newly registered users.
+- Configuration: Added `:welcome` settings for the welcome message to newly registered users. You can send a welcome message as a direct message, chat or email.
 - Ability to hide favourites and emoji reactions in the API with `[:instance, :show_reactions]` config.
 
 <details>
index d31208c255f10f8fbd41c10ce1f50d0f727303aa..c0213612bbf5ad49112ec0fc0678125423015159 100644 (file)
@@ -261,6 +261,11 @@ config :pleroma, :welcome,
     sender_nickname: nil,
     message: nil
   ],
+  chat_message: [
+    enabled: false,
+    sender_nickname: nil,
+    message: nil
+  ],
   email: [
     enabled: false,
     sender: nil,
index 11fbe0d788b4181957fc38332b6827f0ab6427c5..9c8cbacb505315b1edf860c0dcc5f147775cb635 100644 (file)
@@ -997,6 +997,35 @@ config :pleroma, :config_description, [
           }
         ]
       },
+      %{
+        group: :chat_message,
+        type: :group,
+        descpiption: "Chat message settings",
+        children: [
+          %{
+            key: :enabled,
+            type: :boolean,
+            description: "Enables sends chat message for new user after registration"
+          },
+          %{
+            key: :message,
+            type: :string,
+            description:
+              "A message that will be sent to a newly registered users as a chat message",
+            suggestions: [
+              "Hi, @username! Welcome on board!"
+            ]
+          },
+          %{
+            key: :sender_nickname,
+            type: :string,
+            description: "The nickname of the local user that sends the welcome message",
+            suggestions: [
+              "lain"
+            ]
+          }
+        ]
+      },
       %{
         group: :email,
         type: :group,
index 9c768abef356cfbe2ed633a4b5254c14090d00a6..59c3fb06dd8b3300d76b69b5330cc10e600e5318 100644 (file)
@@ -69,6 +69,10 @@ To add configuration to your config file, you can copy it from the base config.
   * `enabled`: Enables the send a direct message to a newly registered user. Defaults to `false`.
   * `sender_nickname`: The nickname of the local user that sends the welcome message.
   * `message`: A message that will be send to a newly registered users as a direct message.
+* `chat_message`: - welcome message sent as a chat message.
+  * `enabled`: Enables the send a chat message to a newly registered user. Defaults to `false`.
+  * `sender_nickname`: The nickname of the local user that sends the welcome message.
+  * `message`: A message that will be send to a newly registered users as a chat message.
 * `email`: - welcome message sent as a email.
   * `enabled`: Enables the send a welcome email to a newly registered user. Defaults to `false`.
   * `sender`: The email address or tuple with `{nickname, email}` that will use as sender to the welcome email.
index dcf6ebee2e4a46d8c6f4150e03a8fd4c7d561fac..0c1fab2230744415bb994c397db8c7596c01c399 100644 (file)
@@ -734,6 +734,7 @@ defmodule Pleroma.User do
          {:ok, user} <- set_cache(user),
          {:ok, _} <- send_welcome_email(user),
          {:ok, _} <- send_welcome_message(user),
+         {:ok, _} <- send_welcome_chat_message(user),
          {:ok, _} <- try_send_confirmation_email(user) do
       {:ok, user}
     end
@@ -748,6 +749,15 @@ defmodule Pleroma.User do
     end
   end
 
+  def send_welcome_chat_message(user) do
+    if User.WelcomeChatMessage.enabled?() do
+      User.WelcomeChatMessage.post_message(user)
+      {:ok, :enqueued}
+    else
+      {:ok, :noop}
+    end
+  end
+
   def send_welcome_email(%User{email: email} = user) when is_binary(email) do
     if User.WelcomeEmail.enabled?() do
       User.WelcomeEmail.send_email(user)
diff --git a/lib/pleroma/user/welcome_chat_message.ex b/lib/pleroma/user/welcome_chat_message.ex
new file mode 100644 (file)
index 0000000..3e7d1f4
--- /dev/null
@@ -0,0 +1,45 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.User.WelcomeChatMessage do
+  alias Pleroma.Config
+  alias Pleroma.User
+  alias Pleroma.Web.CommonAPI
+
+  @spec enabled?() :: boolean()
+  def enabled?, do: Config.get([:welcome, :chat_message, :enabled], false)
+
+  @spec post_message(User.t()) :: {:ok, Pleroma.Activity.t() | nil}
+  def post_message(user) do
+    [:welcome, :chat_message, :sender_nickname]
+    |> Config.get(nil)
+    |> fetch_sender()
+    |> do_post(user, welcome_message())
+  end
+
+  defp do_post(%User{} = sender, recipient, message)
+       when is_binary(message) do
+    CommonAPI.post_chat_message(
+      sender,
+      recipient,
+      message
+    )
+  end
+
+  defp do_post(_sender, _recipient, _message), do: {:ok, nil}
+
+  defp fetch_sender(nickname) when is_binary(nickname) do
+    with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
+      user
+    else
+      _ -> nil
+    end
+  end
+
+  defp fetch_sender(_), do: nil
+
+  defp welcome_message do
+    Config.get([:welcome, :chat_message, :message], nil)
+  end
+end
diff --git a/test/user/welcome_chat_massage_test.exs b/test/user/welcome_chat_massage_test.exs
new file mode 100644 (file)
index 0000000..3fef6fa
--- /dev/null
@@ -0,0 +1,35 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.User.WelcomeChatMessageTest do
+  use Pleroma.DataCase
+
+  alias Pleroma.Config
+  alias Pleroma.User.WelcomeChatMessage
+
+  import Pleroma.Factory
+
+  setup do: clear_config([:welcome])
+
+  describe "post_message/1" do
+    test "send a chat welcome message" do
+      welcome_user = insert(:user)
+      user = insert(:user, name: "mewmew")
+
+      Config.put([:welcome, :chat_message, :enabled], true)
+      Config.put([:welcome, :chat_message, :sender_nickname], welcome_user.nickname)
+
+      Config.put(
+        [:welcome, :chat_message, :message],
+        "Hello. Welcome to blob.cat"
+      )
+
+      {:ok, %Pleroma.Activity{} = activity} = WelcomeChatMessage.post_message(user)
+
+      assert user.ap_id in activity.recipients
+      assert Pleroma.Object.normalize(activity).data["type"] == "ChatMessage"
+      assert Pleroma.Object.normalize(activity).data["content"] =~ "Hello. Welcome to "
+    end
+  end
+end
index 904cea5368a94bcf4957de64045e523cd329fda0..2c1f2b7c5d7e58edb21a453aae556487bb0921bb 100644 (file)
@@ -412,8 +412,36 @@ defmodule Pleroma.UserTest do
       welcome_user = insert(:user)
       Pleroma.Config.put([:welcome, :direct_message, :enabled], true)
       Pleroma.Config.put([:welcome, :direct_message, :sender_nickname], welcome_user.nickname)
-      Pleroma.Config.put([:welcome, :direct_message, :message], "Hello, this is a cool site")
+      Pleroma.Config.put([:welcome, :direct_message, :message], "Hello, this is a direct message")
 
+      cng = User.register_changeset(%User{}, @full_user_data)
+      {:ok, registered_user} = User.register(cng)
+      ObanHelpers.perform_all()
+
+      activity = Repo.one(Pleroma.Activity)
+      assert registered_user.ap_id in activity.recipients
+      assert Object.normalize(activity).data["content"] =~ "direct message"
+      assert activity.actor == welcome_user.ap_id
+    end
+
+    test "it sends a welcome chat message if it is set" do
+      welcome_user = insert(:user)
+      Pleroma.Config.put([:welcome, :chat_message, :enabled], true)
+      Pleroma.Config.put([:welcome, :chat_message, :sender_nickname], welcome_user.nickname)
+      Pleroma.Config.put([:welcome, :chat_message, :message], "Hello, this is a chat message")
+
+      cng = User.register_changeset(%User{}, @full_user_data)
+      {:ok, registered_user} = User.register(cng)
+      ObanHelpers.perform_all()
+
+      activity = Repo.one(Pleroma.Activity)
+      assert registered_user.ap_id in activity.recipients
+      assert Object.normalize(activity).data["content"] =~ "chat message"
+      assert activity.actor == welcome_user.ap_id
+    end
+
+    test "it sends a welcome email message if it is set" do
+      welcome_user = insert(:user)
       Pleroma.Config.put([:welcome, :email, :enabled], true)
       Pleroma.Config.put([:welcome, :email, :sender], welcome_user.email)
 
@@ -428,11 +456,6 @@ defmodule Pleroma.UserTest do
       {:ok, registered_user} = User.register(cng)
       ObanHelpers.perform_all()
 
-      activity = Repo.one(Pleroma.Activity)
-      assert registered_user.ap_id in activity.recipients
-      assert Object.normalize(activity).data["content"] =~ "cool site"
-      assert activity.actor == welcome_user.ap_id
-
       assert_email_sent(
         from: {instance_name, welcome_user.email},
         to: {registered_user.name, registered_user.email},