Build CSV from subscriber list
This commit is contained in:
parent
6109532083
commit
784b8b5f83
43
lib/pleroma/user/mailing_list.ex
Normal file
43
lib/pleroma/user/mailing_list.ex
Normal 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
|
26
test/pleroma/user/mailing_list_test.exs
Normal file
26
test/pleroma/user/mailing_list_test.exs
Normal 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
|
Loading…
Reference in New Issue
Block a user