EmailList: add unsubscribers csv, fix query
This commit is contained in:
parent
1f3dd2115c
commit
6e7b220549
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user