Merge branch 'develop' into issue/1934-welcome-email
authorMaksim Pechnikov <parallel588@gmail.com>
Thu, 23 Jul 2020 13:36:27 +0000 (16:36 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Thu, 23 Jul 2020 13:36:27 +0000 (16:36 +0300)
1  2 
CHANGELOG.md
config/config.exs
config/description.exs
docs/configuration/cheatsheet.md
lib/pleroma/application_requirements.ex
lib/pleroma/user.ex
test/application_requirements_test.exs
test/user_test.exs

diff --cc CHANGELOG.md
Simple merge
Simple merge
Simple merge
Simple merge
index b4d8ff23bd09e7359e0ee9125e2a9173df943574,ee88c3346e6398775c28a38412e93a43bd5ee78b..16f62b6f5d43e00caa18e612479f66e82bffcaae
@@@ -16,8 -16,8 +16,9 @@@ defmodule Pleroma.ApplicationRequiremen
    @spec verify!() :: :ok | VerifyError.t()
    def verify! do
      :ok
+     |> check_confirmation_accounts!
      |> check_migrations_applied!()
 +    |> check_welcome_message_config!()
      |> check_rum!()
      |> handle_result()
    end
    defp handle_result(:ok), do: :ok
    defp handle_result({:error, message}), do: raise(VerifyError, message: message)
  
 +  defp check_welcome_message_config!(:ok) do
 +    if Pleroma.Config.get([:welcome, :email, :enabled], false) and
 +         not Pleroma.Emails.Mailer.enabled?() do
 +      Logger.error("""
 +      To send welcome email do you need to enable mail.
 +      \nconfig :pleroma, Pleroma.Emails.Mailer, enabled: true
 +      """)
 +
 +      {:error, "The mail disabled."}
 +    else
 +      :ok
 +    end
 +  end
 +
 +  defp check_welcome_message_config!(result), do: result
 +
+   # Checks account confirmation email
+   #
+   def check_confirmation_accounts!(:ok) do
+     if Pleroma.Config.get([:instance, :account_activation_required]) &&
+          not Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
+       Logger.error(
+         "Account activation enabled, but no Mailer settings enabled.\nPlease set config :pleroma, :instance, account_activation_required: false\nOtherwise setup and enable Mailer."
+       )
+       {:error,
+        "Account activation enabled, but Mailer is disabled. Cannot send confirmation emails."}
+     else
+       :ok
+     end
+   end
+   def check_confirmation_accounts!(result), do: result
    # Checks for pending migrations.
    #
    def check_migrations_applied!(:ok) do
index 95047b592002d292c9796a9cd5d10b441131ecde,714ec9a4bac3dd74964f13a5212bfe246bcbe6e5..ed1db04c9af8540c5a0eb90f0fcb3354283b3a92
@@@ -720,33 -719,10 +720,30 @@@ defmodule Pleroma.User d
      end
    end
  
-   def try_send_confirmation_email(%User{} = user) do
-     if user.confirmation_pending &&
-          Config.get([:instance, :account_activation_required]) do
-       user
-       |> Pleroma.Emails.UserEmail.account_confirmation_email()
-       |> Pleroma.Emails.Mailer.deliver_async()
 +  def send_welcome_message(user) do
 +    if User.WelcomeMessage.enabled?() do
 +      User.WelcomeMessage.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)
 +      {:ok, :enqueued}
 +    else
 +      {:ok, :noop}
 +    end
 +  end
 +
 +  def send_welcome_email(_), do: {:ok, :noop}
 +
+   @spec try_send_confirmation_email(User.t()) :: {:ok, :enqueued | :noop}
+   def try_send_confirmation_email(%User{confirmation_pending: true} = user) do
+     if Config.get([:instance, :account_activation_required]) do
+       send_confirmation_email(user)
        {:ok, :enqueued}
      else
        {:ok, :noop}
index b59a9988e68facae024c666c6a751e4a12a9a0ac,fc609d174aadda9a19bdbbea67c73cfb0215c9aa..21d24ddd069514d2aa67f3855e9ecc21ddddd79a
@@@ -9,20 -9,42 +9,56 @@@ defmodule Pleroma.ApplicationRequiremen
  
    alias Pleroma.Repo
  
 +  describe "check_welcome_message_config!/1" do
 +    setup do: clear_config([:welcome])
 +    setup do: clear_config([Pleroma.Emails.Mailer])
 +
 +    test "raises if welcome email enabled but mail disabled" do
 +      Pleroma.Config.put([:welcome, :email, :enabled], true)
 +      Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
 +
 +      assert_raise Pleroma.ApplicationRequirements.VerifyError, "The mail disabled.", fn ->
 +        capture_log(&Pleroma.ApplicationRequirements.verify!/0)
 +      end
 +    end
 +  end
 +
+   describe "check_confirmation_accounts!" do
+     setup_with_mocks([
+       {Pleroma.ApplicationRequirements, [:passthrough],
+        [
+          check_migrations_applied!: fn _ -> :ok end
+        ]}
+     ]) do
+       :ok
+     end
+     setup do: clear_config([:instance, :account_activation_required])
+     test "raises if account confirmation is required but mailer isn't enable" do
+       Pleroma.Config.put([:instance, :account_activation_required], true)
+       Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
+       assert_raise Pleroma.ApplicationRequirements.VerifyError,
+                    "Account activation enabled, but Mailer is disabled. Cannot send confirmation emails.",
+                    fn ->
+                      capture_log(&Pleroma.ApplicationRequirements.verify!/0)
+                    end
+     end
+     test "doesn't do anything if account confirmation is disabled" do
+       Pleroma.Config.put([:instance, :account_activation_required], false)
+       Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], false)
+       assert Pleroma.ApplicationRequirements.verify!() == :ok
+     end
+     test "doesn't do anything if account confirmation is required and mailer is enabled" do
+       Pleroma.Config.put([:instance, :account_activation_required], true)
+       Pleroma.Config.put([Pleroma.Emails.Mailer, :enabled], true)
+       assert Pleroma.ApplicationRequirements.verify!() == :ok
+     end
+   end
    describe "check_rum!" do
      setup_with_mocks([
        {Pleroma.ApplicationRequirements, [:passthrough],
index 132697139040f6def071d956a270d2b2cffc59eb,21c03b470fab5ff4c1e098e10610ff5ecf1f54bc..d087e9101b1f745735f482e1082b825ffa1630e1
@@@ -386,9 -386,11 +386,10 @@@ defmodule Pleroma.UserTest d
        password_confirmation: "test",
        email: "email@example.com"
      }
-     setup do: clear_config([:instance, :autofollowed_nicknames])
  
 -    setup do: clear_config([:instance, :welcome_message])
 -    setup do: clear_config([:instance, :welcome_user_nickname])
+     setup do: clear_config([:instance, :autofollowed_nicknames])
 +    setup do: clear_config([:welcome])
+     setup do: clear_config([:instance, :account_activation_required])
  
      test "it autofollows accounts that are set for it" do
        user = insert(:user)
        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},
 +        subject: "Hello, welcome to cool site: #{instance_name}",
 +        html_body: "Welcome to #{instance_name}"
 +      )
      end
  
-     setup do: clear_config([:instance, :account_activation_required])
+     test "it sends a confirm email" do
+       Pleroma.Config.put([:instance, :account_activation_required], true)
+       cng = User.register_changeset(%User{}, @full_user_data)
+       {:ok, registered_user} = User.register(cng)
+       ObanHelpers.perform_all()
+       assert_email_sent(Pleroma.Emails.UserEmail.account_confirmation_email(registered_user))
+     end
  
      test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do
        Pleroma.Config.put([:instance, :account_activation_required], true)