From 6e7b220549c7c950ecbd27bbe7ad62537c6acf52 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 14 Jun 2021 20:51:16 -0500 Subject: [PATCH] EmailList: add unsubscribers csv, fix query --- lib/pleroma/user/email_list.ex | 21 ++++++---- lib/pleroma/user/query.ex | 4 ++ .../admin_api/controllers/email_list_controller.ex | 16 +++++--- lib/pleroma/web/router.ex | 1 + test/pleroma/user/email_list_test.exs | 26 ++++++++++--- .../controllers/email_list_controller_test.exs | 45 ++++++++++++++++++++++ 6 files changed, 96 insertions(+), 17 deletions(-) diff --git a/lib/pleroma/user/email_list.ex b/lib/pleroma/user/email_list.ex index 70850941a..d2a24f6ab 100644 --- a/lib/pleroma/user/email_list.ex +++ b/lib/pleroma/user/email_list.ex @@ -13,23 +13,30 @@ defmodule Pleroma.User.EmailList do @header_row ["Email Address"] - defp subscribers_query do + defp query(:subscribers) do User.Query.build(%{ local: true, - is_active: true, - is_approved: true, - is_confirmed: true, + active: true, accepts_email_list: true }) |> where([u], not is_nil(u.email)) end - def generate_csv do - subscribers_query() + defp query(:unsubscribers) do + User.Query.build(%{ + local: true, + accepts_email_list: false + }) + |> where([u], not is_nil(u.email)) + end + + def generate_csv(audience) when is_atom(audience) do + audience + |> query() |> generate_csv() end - def generate_csv(query) do + def generate_csv(%Ecto.Query{} = query) do query |> Repo.all() |> Enum.map(&build_row/1) diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index fa46545da..fbe8a36d2 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -124,6 +124,10 @@ defmodule Pleroma.User.Query do where(query, [u], u.is_moderator == ^bool) end + defp compose_query({:accepts_email_list, bool}, query) do + where(query, [u], u.accepts_email_list == ^bool) + end + defp compose_query({:super_users, _}, query) do where( 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 d1fb9a306..2b07ff417 100644 --- a/lib/pleroma/web/admin_api/controllers/email_list_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/email_list_controller.ex @@ -10,13 +10,19 @@ defmodule Pleroma.Web.AdminAPI.EmailListController do require Logger - plug( - OAuthScopesPlug, - %{scopes: ["admin:read:accounts"]} when action in [:subscribers] - ) + plug(OAuthScopesPlug, %{scopes: ["admin:read:accounts"]}) def subscribers(conn, _params) do - csv = EmailList.generate_csv() + csv = EmailList.generate_csv(:subscribers) + + conn + |> put_resp_content_type("text/csv") + |> resp(200, csv) + |> send_resp() + end + + def unsubscribers(conn, _params) do + csv = EmailList.generate_csv(:unsubscribers) conn |> put_resp_content_type("text/csv") diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index f825fadcd..885f7c317 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -259,6 +259,7 @@ defmodule Pleroma.Web.Router do post("/backups", AdminAPIController, :create_backup) get("/email_list/subscribers.csv", EmailListController, :subscribers) + get("/email_list/unsubscribers.csv", EmailListController, :unsubscribers) 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 fb6f447d2..3522bc4fb 100644 --- a/test/pleroma/user/email_list_test.exs +++ b/test/pleroma/user/email_list_test.exs @@ -9,10 +9,11 @@ defmodule Pleroma.User.EmailListTest do import Pleroma.Factory - test "generate_csv/0" do - user1 = insert(:user) - user2 = insert(:user) - user3 = insert(:user) + test "generate_csv/1 with :subscribers" do + user1 = insert(:user, accepts_email_list: true) + user2 = insert(:user, accepts_email_list: true) + user3 = insert(:user, accepts_email_list: true) + insert(:user, accepts_email_list: false) expected = """ Email Address\r @@ -21,6 +22,21 @@ defmodule Pleroma.User.EmailListTest do #{user3.email}\r """ - assert EmailList.generate_csv() == expected + assert EmailList.generate_csv(:subscribers) == expected + end + + test "generate_csv/1 with :unsubscribers" do + user1 = insert(:user, accepts_email_list: false) + user2 = insert(:user, accepts_email_list: false) + insert(:user, accepts_email_list: true) + insert(:user, accepts_email_list: true) + + expected = """ + Email Address\r + #{user1.email}\r + #{user2.email}\r + """ + + assert EmailList.generate_csv(:unsubscribers) == 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 9fcd63a07..4a9f52812 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 @@ -19,6 +19,18 @@ defmodule Pleroma.Web.AdminAPI.EmailListControllerTest do {:ok, %{admin: admin, token: token, conn: conn}} end + defp user_setup do + user = insert(:user) + token = insert(:oauth_token, user: user) + + conn = + build_conn() + |> assign(:user, user) + |> assign(:token, token) + + {:ok, %{user: user, token: token, conn: conn}} + end + describe "GET /api/v1/pleroma/admin/email_list/subscribers.csv" do setup do: admin_setup() @@ -31,4 +43,37 @@ defmodule Pleroma.Web.AdminAPI.EmailListControllerTest do assert result end end + + describe "GET /api/v1/pleroma/admin/email_list/subscribers.csv unauthorized" do + setup do: user_setup() + + test "returns 403", %{conn: conn} do + conn + |> get("/api/v1/pleroma/admin/email_list/subscribers.csv") + |> response(403) + end + end + + describe "GET /api/v1/pleroma/admin/email_list/unsubscribers.csv" do + setup do: admin_setup() + + test "returns a CSV", %{conn: conn} do + result = + conn + |> get("/api/v1/pleroma/admin/email_list/unsubscribers.csv") + |> response(200) + + assert result + end + end + + describe "GET /api/v1/pleroma/admin/email_list/unsubscribers.csv unauthorized" do + setup do: user_setup() + + test "returns 403", %{conn: conn} do + conn + |> get("/api/v1/pleroma/admin/email_list/unsubscribers.csv") + |> response(403) + end + end end