diff --git a/lib/pleroma/user/email_list.ex b/lib/pleroma/user/email_list.ex index b466fd92a..d9e4e8658 100644 --- a/lib/pleroma/user/email_list.ex +++ b/lib/pleroma/user/email_list.ex @@ -30,6 +30,13 @@ defmodule Pleroma.User.EmailList do |> where([u], not is_nil(u.email)) end + defp query(:combined) do + User.Query.build(%{ + local: true + }) + |> where([u], not is_nil(u.email)) + end + def generate_csv(audience) when is_atom(audience) do audience |> query() diff --git a/lib/pleroma/web/admin_api/controllers/email_list_controller.ex b/lib/pleroma/web/admin_api/controllers/email_list_controller.ex index 2b07ff417..861ad7a33 100644 --- a/lib/pleroma/web/admin_api/controllers/email_list_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/email_list_controller.ex @@ -13,16 +13,19 @@ defmodule Pleroma.Web.AdminAPI.EmailListController do plug(OAuthScopesPlug, %{scopes: ["admin:read:accounts"]}) def subscribers(conn, _params) do - csv = EmailList.generate_csv(:subscribers) - - conn - |> put_resp_content_type("text/csv") - |> resp(200, csv) - |> send_resp() + render_csv(conn, :subscribers) end def unsubscribers(conn, _params) do - csv = EmailList.generate_csv(:unsubscribers) + render_csv(conn, :unsubscribers) + end + + def combined(conn, _params) do + render_csv(conn, :combined) + end + + defp render_csv(conn, audience) when is_atom(audience) do + csv = EmailList.generate_csv(audience) conn |> put_resp_content_type("text/csv") diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 885f7c317..7af9b2bd2 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -260,6 +260,7 @@ defmodule Pleroma.Web.Router do get("/email_list/subscribers.csv", EmailListController, :subscribers) get("/email_list/unsubscribers.csv", EmailListController, :unsubscribers) + get("/email_list/combined.csv", EmailListController, :combined) end scope "/api/v1/pleroma/emoji", Pleroma.Web.PleromaAPI do diff --git a/test/pleroma/user/email_list_test.exs b/test/pleroma/user/email_list_test.exs index 8dbe877c5..162e21ff8 100644 --- a/test/pleroma/user/email_list_test.exs +++ b/test/pleroma/user/email_list_test.exs @@ -39,4 +39,21 @@ defmodule Pleroma.User.EmailListTest do assert EmailList.generate_csv(:unsubscribers) == expected end + + test "generate_csv/1 with :combined" do + user1 = insert(:user, accepts_email_list: true) + user2 = insert(:user, accepts_email_list: false) + user3 = insert(:user, accepts_email_list: false) + user4 = insert(:user, accepts_email_list: true) + + expected = """ + Email Address,Nickname,Subscribe?\r + #{user1.email},#{user1.nickname},true\r + #{user2.email},#{user2.nickname},false\r + #{user3.email},#{user3.nickname},false\r + #{user4.email},#{user4.nickname},true\r + """ + + assert EmailList.generate_csv(:combined) == expected + end end diff --git a/test/pleroma/web/admin_api/controllers/email_list_controller_test.exs b/test/pleroma/web/admin_api/controllers/email_list_controller_test.exs index 4a9f52812..ed2592afd 100644 --- a/test/pleroma/web/admin_api/controllers/email_list_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/email_list_controller_test.exs @@ -76,4 +76,27 @@ defmodule Pleroma.Web.AdminAPI.EmailListControllerTest do |> response(403) end end + + describe "GET /api/v1/pleroma/admin/email_list/combined.csv" do + setup do: admin_setup() + + test "returns a CSV", %{conn: conn} do + result = + conn + |> get("/api/v1/pleroma/admin/email_list/combined.csv") + |> response(200) + + assert result + end + end + + describe "GET /api/v1/pleroma/admin/email_list/combined.csv unauthorized" do + setup do: user_setup() + + test "returns 403", %{conn: conn} do + conn + |> get("/api/v1/pleroma/admin/email_list/combined.csv") + |> response(403) + end + end end