feat(export): add date of participant creation in participant exports

Closes #1343

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2023-08-23 16:10:32 +02:00
parent 8617382af2
commit fef60ed0f9
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
3 changed files with 40 additions and 16 deletions

View File

@ -3,6 +3,8 @@ defmodule Mobilizon.Service.DateTime do
Module to represent a datetime in a given locale
"""
alias Cldr.DateTime.Relative
alias Mobilizon.Cldr, as: MobilizonCldr
import Mobilizon.Cldr, only: [locale_or_default: 1]
@typep to_string_format :: :short | :medium | :long | :full
@ -10,25 +12,25 @@ defmodule Mobilizon.Service.DateTime do
@spec datetime_to_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
def datetime_to_string(%DateTime{} = datetime, locale \\ "en", format \\ :medium) do
Mobilizon.Cldr.DateTime.to_string!(datetime,
MobilizonCldr.DateTime.to_string!(datetime,
format: format,
locale: Mobilizon.Cldr.locale_or_default(locale)
locale: locale_or_default(locale)
)
end
@spec datetime_to_time_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
def datetime_to_time_string(%DateTime{} = datetime, locale \\ "en", format \\ :short) do
Mobilizon.Cldr.Time.to_string!(datetime,
MobilizonCldr.Time.to_string!(datetime,
format: format,
locale: Mobilizon.Cldr.locale_or_default(locale)
locale: locale_or_default(locale)
)
end
@spec datetime_to_date_string(DateTime.t(), String.t(), to_string_format()) :: String.t()
def datetime_to_date_string(%DateTime{} = datetime, locale \\ "en", format \\ :short) do
Mobilizon.Cldr.Date.to_string!(datetime,
MobilizonCldr.Date.to_string!(datetime,
format: format,
locale: Mobilizon.Cldr.locale_or_default(locale)
locale: locale_or_default(locale)
)
end
@ -47,9 +49,9 @@ defmodule Mobilizon.Service.DateTime do
@spec datetime_relative(DateTime.t(), String.t()) :: String.t()
def datetime_relative(%DateTime{} = datetime, locale \\ "en") do
Relative.to_string!(datetime, Mobilizon.Cldr,
Relative.to_string!(datetime, MobilizonCldr,
relative_to: DateTime.utc_now(),
locale: Mobilizon.Cldr.locale_or_default(locale)
locale: locale_or_default(locale)
)
end

View File

@ -9,6 +9,7 @@ defmodule Mobilizon.Service.Export.Participants.Common do
alias Mobilizon.Events.Participant.Metadata
alias Mobilizon.Storage.Repo
import Mobilizon.Web.Gettext, only: [gettext: 1]
import Mobilizon.Service.DateTime, only: [datetime_to_string: 2]
@spec save_upload(String.t(), String.t(), String.t(), String.t(), String.t()) ::
{:ok, Export.t()} | {:error, atom() | Ecto.Changeset.t()}
@ -58,7 +59,12 @@ defmodule Mobilizon.Service.Export.Participants.Common do
@spec columns :: list(String.t())
def columns do
[gettext("Participant name"), gettext("Participant status"), gettext("Participant message")]
[
gettext("Participant name"),
gettext("Participant status"),
gettext("Participant registration date"),
gettext("Participant message")
]
end
# One hour
@ -82,14 +88,26 @@ defmodule Mobilizon.Service.Export.Participants.Common do
@spec to_list({Participant.t(), Actor.t()}) :: list(String.t())
def to_list(
{%Participant{role: role, metadata: metadata},
{%Participant{role: role, metadata: metadata, inserted_at: inserted_at},
%Actor{domain: nil, preferred_username: "anonymous"}}
) do
[gettext("Anonymous participant"), translate_role(role), convert_metadata(metadata)]
[
gettext("Anonymous participant"),
translate_role(role),
datetime_to_string(inserted_at, Gettext.get_locale()),
convert_metadata(metadata)
]
end
def to_list({%Participant{role: role, metadata: metadata}, %Actor{} = actor}) do
[Actor.display_name_and_username(actor), translate_role(role), convert_metadata(metadata)]
def to_list(
{%Participant{role: role, metadata: metadata, inserted_at: inserted_at}, %Actor{} = actor}
) do
[
Actor.display_name_and_username(actor),
translate_role(role),
datetime_to_string(inserted_at, Gettext.get_locale()),
convert_metadata(metadata)
]
end
@spec convert_metadata(Metadata.t() | nil) :: String.t()

View File

@ -5,24 +5,28 @@ defmodule Mobilizon.Service.Export.Participants.CommonTest do
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Export.Participants.Common
import Mobilizon.Service.DateTime, only: [datetime_to_string: 1]
test "convert participants to list items" do
participant = insert(:participant)
actor = insert(:actor)
name = Actor.display_name_and_username(actor)
assert [^name, _, ""] = Common.to_list({participant, actor})
date = datetime_to_string(participant.inserted_at)
assert [^name, _, ^date, ""] = Common.to_list({participant, actor})
end
test "convert participants with metadata to list items" do
participant = insert(:participant, metadata: %{message: "a message"})
actor = insert(:actor)
name = Actor.display_name_and_username(actor)
assert [^name, _, "a message"] = Common.to_list({participant, actor})
date = datetime_to_string(participant.inserted_at)
assert [^name, _, ^date, "a message"] = Common.to_list({participant, actor})
end
test "convert anonymous participants to list items" do
participant = insert(:participant)
actor = insert(:actor, domain: nil, preferred_username: "anonymous")
assert ["Anonymous participant", _, ""] = Common.to_list({participant, actor})
date = datetime_to_string(participant.inserted_at)
assert ["Anonymous participant", _, ^date, ""] = Common.to_list({participant, actor})
end
end