Merge pull request 'remove unneeded function' (#143) from compile-fix into develop
authorfloatingghost <hannah@coffee-and-dreams.uk>
Wed, 3 Aug 2022 11:12:05 +0000 (11:12 +0000)
committerfloatingghost <hannah@coffee-and-dreams.uk>
Wed, 3 Aug 2022 11:12:05 +0000 (11:12 +0000)
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/143

CODE_OF_CONDUCT.md [new file with mode: 0644]
lib/pleroma/config/release_runtime_provider.ex
lib/pleroma/user/backup.ex
lib/pleroma/workers/backup_worker.ex
test/pleroma/user/backup_test.exs
test/pleroma/web/pleroma_api/controllers/backup_controller_test.exs

diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md
new file mode 100644 (file)
index 0000000..5996107
--- /dev/null
@@ -0,0 +1,24 @@
+# Akkoma Code of Conduct
+
+The Akkoma project aims to be **enjoyable** for anyone to participate in, regardless of their identity or level of expertise. To achieve this, the community must create an environment which is **safe** and **equitable**; the following guidelines have been created with these goals in mind.
+
+1. **Treat individuals with respect.** Differing experiences and viewpoints deserve to be respected, and bigotry and harassment are not tolerated under any circumstances.
+    - Individuals should at all times be treated as equals, regardless of their age, gender, sexuality, race, ethnicity, _or any other characteristic_, intrinsic or otherwise.
+    - Behaviour that is harmful in nature should be addressed and corrected *regardless of intent*.
+    - Respect personal boundaries and ask for clarification whenever they are unclear.
+    - (Obviously, hate does not count as merely a "differing viewpoint", because it is harmful in nature.)
+
+2. **Be understanding of differences in communication.** Not everyone is aware of unspoken social cues, and speech that is not intended to be offensive should not be treated as such simply due to an atypical manner of communication.
+    - Somebody who speaks bluntly is not necessarily rude, and somebody who swears a lot is not necessarily volatile.
+    - Try to confirm your interpretation of their intent rather than assuming bad faith.
+    - Someone may not communicate as, or come across as a picture of "professionalism", but this should not be seen as a reason to dismiss them. This is a **casual** space, and communication styles can reflect that.
+
+3. **"Uncomfortable" does not mean "unsafe".** In an ideal world, the community would be safe, equitable, enjoyable, *and* comfortable for all members at all times. Unfortunately, this is not always possible in reality.
+    - Safety and equity will be prioritized over comfort whenever it is necessary to do so.
+    - Weaponizing one's own discomfort to deflect accountability or censor an individual (e.g. "white fragility") is a form of discriminatory conduct.
+
+4. **Let people grow from their mistakes.** Nobody is perfect; even the most well-meaning individual can do something hurtful. Everyone should be given a fair opportunity to explain themselves and correct their behaviour. Portraying someone as inherently malicious prevents improvement and shifts focus away from the *action* that was problematic.
+    - Avoid bringing up past events that do not accurately reflect an individual's current actions or beliefs. (This is, of course, different from providing evidence of a recurring pattern of behaviour.)
+
+---
+This document was adapted from one created by ~keith as part of punks default repository template, and is licensed under CC-BY-SA 4.0. The original template is here: <https://bytes.keithhacks.cyou/keith/default-template>
index 1e4bddc59bdf1e4ac2581c58cf42859872419a5e..e5f2d6339676bb21cbbe6becc9f4322e66a0b087 100644 (file)
@@ -14,10 +14,10 @@ defmodule Pleroma.Config.ReleaseRuntimeProvider do
     config_path =
       cond do
         opts[:config_path] -> opts[:config_path]
-        System.get_env("PLEROMA_CONFIG_PATH") -> System.get_env("PLEROMA_CONFIG_PATH")
         System.get_env("AKKOMA_CONFIG_PATH") -> System.get_env("AKKOMA_CONFIG_PATH")
-        File.exists?("/etc/akkoma/config.exs") -> "/etc/akkoma/config.exs"
-        true -> "/etc/pleroma/config.exs"
+        System.get_env("PLEROMA_CONFIG_PATH") -> System.get_env("PLEROMA_CONFIG_PATH")
+        File.exists?("/etc/pleroma/config.exs") -> "/etc/pleroma/config.exs"
+        true -> "/etc/akkoma/config.exs"
       end
 
     with_runtime_config =
@@ -31,7 +31,7 @@ defmodule Pleroma.Config.ReleaseRuntimeProvider do
         warning = [
           IO.ANSI.red(),
           IO.ANSI.bright(),
-          "!!! Config path is not declared! Please ensure it exists and that PLEROMA_CONFIG_PATH is unset or points to an existing file",
+          "!!! Config path is not declared! Please ensure it exists and that AKKOMA_CONFIG_PATH and/or PLEROMA_CONFIG_PATH is unset or points to an existing file",
           IO.ANSI.reset()
         ]
 
index cba94248f969958949dba986aacf1f6726395f39..2c63782651fcd95eb2689492a07bd6ed88d5824f 100644 (file)
@@ -32,9 +32,7 @@ defmodule Pleroma.User.Backup do
   end
 
   def create(user, admin_id \\ nil) do
-    with :ok <- validate_email_enabled(),
-         :ok <- validate_user_email(user),
-         :ok <- validate_limit(user, admin_id),
+    with :ok <- validate_limit(user, admin_id),
          {:ok, backup} <- user |> new() |> Repo.insert() do
       BackupWorker.process(backup, admin_id)
     end
@@ -86,20 +84,6 @@ defmodule Pleroma.User.Backup do
     end
   end
 
-  defp validate_email_enabled do
-    if Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
-      :ok
-    else
-      {:error, dgettext("errors", "Backups require enabled email")}
-    end
-  end
-
-  defp validate_user_email(%User{email: nil}) do
-    {:error, dgettext("errors", "Email is required")}
-  end
-
-  defp validate_user_email(%User{email: email}) when is_binary(email), do: :ok
-
   def get_last(user_id) do
     __MODULE__
     |> where(user_id: ^user_id)
index 9b763b04b915b59a0f5afdc239ae7662020a7c52..66c5c35916e9b59da4fe69ca84f43dfa9833ccc3 100644 (file)
@@ -37,10 +37,7 @@ defmodule Pleroma.Workers.BackupWorker do
            backup_id |> Backup.get() |> Backup.process(),
          {:ok, _job} <- schedule_deletion(backup),
          :ok <- Backup.remove_outdated(backup),
-         {:ok, _} <-
-           backup
-           |> Pleroma.Emails.UserEmail.backup_is_ready_email(admin_user_id)
-           |> Pleroma.Emails.Mailer.deliver() do
+         :ok <- maybe_deliver_email(backup, admin_user_id) do
       {:ok, backup}
     end
   end
@@ -51,4 +48,23 @@ defmodule Pleroma.Workers.BackupWorker do
       nil -> :ok
     end
   end
+
+  defp has_email?(user) do
+    not is_nil(user.email) and user.email != ""
+  end
+
+  defp maybe_deliver_email(backup, admin_user_id) do
+    has_mailer = Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled])
+    backup = backup |> Pleroma.Repo.preload(:user)
+
+    if has_email?(backup.user) and has_mailer do
+      backup
+      |> Pleroma.Emails.UserEmail.backup_is_ready_email(admin_user_id)
+      |> Pleroma.Emails.Mailer.deliver()
+
+      :ok
+    else
+      :ok
+    end
+  end
 end
index b16152876aac735446589db3e7afcea8ff596285..029bbbf5644640dbb4bea8c68aca12a3f5ef8f7c 100644 (file)
@@ -22,15 +22,15 @@ defmodule Pleroma.User.BackupTest do
     clear_config([Pleroma.Emails.Mailer, :enabled], true)
   end
 
-  test "it requries enabled email" do
+  test "it does not requrie enabled email" do
     clear_config([Pleroma.Emails.Mailer, :enabled], false)
     user = insert(:user)
-    assert {:error, "Backups require enabled email"} == Backup.create(user)
+    assert {:ok, _} = Backup.create(user)
   end
 
-  test "it requries user's email" do
+  test "it does not require user's email" do
     user = insert(:user, %{email: nil})
-    assert {:error, "Email is required"} == Backup.create(user)
+    assert {:ok, _} = Backup.create(user)
   end
 
   test "it creates a backup record and an Oban job" do
@@ -75,6 +75,43 @@ defmodule Pleroma.User.BackupTest do
     )
   end
 
+  test "it does not send an email if the user does not have an email" do
+    clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
+    %{id: user_id} = user = insert(:user, %{email: nil})
+
+    assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
+    assert {:ok, backup} = perform_job(BackupWorker, args)
+    assert backup.file_size > 0
+    assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
+
+    assert_no_email_sent()
+  end
+
+  test "it does not send an email if mailer is not on" do
+    clear_config([Pleroma.Emails.Mailer, :enabled], false)
+    clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
+    %{id: user_id} = user = insert(:user)
+
+    assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
+    assert {:ok, backup} = perform_job(BackupWorker, args)
+    assert backup.file_size > 0
+    assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
+
+    assert_no_email_sent()
+  end
+
+  test "it does not send an email if the user has an empty email" do
+    clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
+    %{id: user_id} = user = insert(:user, %{email: ""})
+
+    assert {:ok, %Oban.Job{args: %{"backup_id" => backup_id} = args}} = Backup.create(user)
+    assert {:ok, backup} = perform_job(BackupWorker, args)
+    assert backup.file_size > 0
+    assert %Backup{id: ^backup_id, processed: true, user_id: ^user_id} = backup
+
+    assert_no_email_sent()
+  end
+
   test "it removes outdated backups after creating a fresh one" do
     clear_config([Backup, :limit_days], -1)
     clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
index 3ee660a0581711a510daf651cde686bca3c4fd21..ba17636daf0905477b82f1fd4233708764fa577a 100644 (file)
@@ -82,4 +82,24 @@ defmodule Pleroma.Web.PleromaAPI.BackupControllerTest do
              |> post("/api/v1/pleroma/backups")
              |> json_response_and_validate_schema(400)
   end
+
+  test "Backup without email address" do
+    user = Pleroma.Factory.insert(:user, email: nil)
+    %{conn: conn} = oauth_access(["read:accounts"], user: user)
+
+    assert is_nil(user.email)
+
+    assert [
+             %{
+               "content_type" => "application/zip",
+               "url" => _url,
+               "file_size" => 0,
+               "processed" => false,
+               "inserted_at" => _
+             }
+           ] =
+             conn
+             |> post("/api/v1/pleroma/backups")
+             |> json_response_and_validate_schema(:ok)
+  end
 end