EmailList: add unsubscribers csv, fix query

This commit is contained in:
Alex Gleason 2021-06-14 20:51:16 -05:00
parent 1f3dd2115c
commit 6e7b220549
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
6 changed files with 96 additions and 17 deletions

View File

@ -13,23 +13,30 @@ defmodule Pleroma.User.EmailList do
@header_row ["Email Address"] @header_row ["Email Address"]
defp subscribers_query do defp query(:subscribers) do
User.Query.build(%{ User.Query.build(%{
local: true, local: true,
is_active: true, active: true,
is_approved: true,
is_confirmed: true,
accepts_email_list: true accepts_email_list: true
}) })
|> where([u], not is_nil(u.email)) |> where([u], not is_nil(u.email))
end end
def generate_csv do defp query(:unsubscribers) do
subscribers_query() 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() |> generate_csv()
end end
def generate_csv(query) do def generate_csv(%Ecto.Query{} = query) do
query query
|> Repo.all() |> Repo.all()
|> Enum.map(&build_row/1) |> Enum.map(&build_row/1)

View File

@ -124,6 +124,10 @@ defmodule Pleroma.User.Query do
where(query, [u], u.is_moderator == ^bool) where(query, [u], u.is_moderator == ^bool)
end 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 defp compose_query({:super_users, _}, query) do
where( where(
query, query,

View File

@ -10,13 +10,19 @@ defmodule Pleroma.Web.AdminAPI.EmailListController do
require Logger require Logger
plug( plug(OAuthScopesPlug, %{scopes: ["admin:read:accounts"]})
OAuthScopesPlug,
%{scopes: ["admin:read:accounts"]} when action in [:subscribers]
)
def subscribers(conn, _params) do 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 conn
|> put_resp_content_type("text/csv") |> put_resp_content_type("text/csv")

View File

@ -259,6 +259,7 @@ defmodule Pleroma.Web.Router do
post("/backups", AdminAPIController, :create_backup) post("/backups", AdminAPIController, :create_backup)
get("/email_list/subscribers.csv", EmailListController, :subscribers) get("/email_list/subscribers.csv", EmailListController, :subscribers)
get("/email_list/unsubscribers.csv", EmailListController, :unsubscribers)
end end
scope "/api/v1/pleroma/emoji", Pleroma.Web.PleromaAPI do scope "/api/v1/pleroma/emoji", Pleroma.Web.PleromaAPI do

View File

@ -9,10 +9,11 @@ defmodule Pleroma.User.EmailListTest do
import Pleroma.Factory import Pleroma.Factory
test "generate_csv/0" do test "generate_csv/1 with :subscribers" do
user1 = insert(:user) user1 = insert(:user, accepts_email_list: true)
user2 = insert(:user) user2 = insert(:user, accepts_email_list: true)
user3 = insert(:user) user3 = insert(:user, accepts_email_list: true)
insert(:user, accepts_email_list: false)
expected = """ expected = """
Email Address\r Email Address\r
@ -21,6 +22,21 @@ defmodule Pleroma.User.EmailListTest do
#{user3.email}\r #{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
end end

View File

@ -19,6 +19,18 @@ defmodule Pleroma.Web.AdminAPI.EmailListControllerTest do
{:ok, %{admin: admin, token: token, conn: conn}} {:ok, %{admin: admin, token: token, conn: conn}}
end 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 describe "GET /api/v1/pleroma/admin/email_list/subscribers.csv" do
setup do: admin_setup() setup do: admin_setup()
@ -31,4 +43,37 @@ defmodule Pleroma.Web.AdminAPI.EmailListControllerTest do
assert result assert result
end end
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 end