EmailList: add combined.csv view to display all candidates with subscription status

This commit is contained in:
Alex Gleason 2021-06-15 12:27:34 -05:00
parent 73ed23e2fd
commit 8c62cc95e6
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 58 additions and 7 deletions

View File

@ -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()

View File

@ -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")

View File

@ -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

View File

@ -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

View File

@ -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