Build CSV from subscriber list

This commit is contained in:
Alex Gleason 2021-06-12 17:58:44 -05:00
parent 6109532083
commit 784b8b5f83
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,43 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.User.MailingList do
@moduledoc """
Functions for generating email lists from local users.
"""
import Ecto.Query
alias Pleroma.Repo
alias Pleroma.User
defp subscribers_query do
User.Query.build(%{
local: true,
is_active: true,
is_approved: true,
is_confirmed: true,
accepts_newsletter: true
})
|> where([u], not is_nil(u.email))
end
def generate_csv do
subscribers_query()
|> generate_csv()
end
def generate_csv(query) do
query
|> Repo.all()
|> Enum.map(&build_row/1)
|> build_csv()
end
defp build_row(%User{email: email}), do: email
defp build_csv(lines) do
["Email Address" | lines]
|> Enum.join("\n")
end
end

View File

@ -0,0 +1,26 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.User.MailingListTest do
alias Pleroma.User.MailingList
use Pleroma.DataCase
import Pleroma.Factory
test "generate_csv/0" do
user1 = insert(:user)
user2 = insert(:user)
user3 = insert(:user)
expected = """
Email Address
#{user1.email}
#{user2.email}
#{user3.email}\
"""
assert MailingList.generate_csv() == expected
end
end