From fef60ed0f92fc4e09ee261ff03f1139aff2449c3 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 23 Aug 2023 16:10:32 +0200 Subject: [PATCH] feat(export): add date of participant creation in participant exports Closes #1343 Signed-off-by: Thomas Citharel --- lib/service/date_time/date_time.ex | 18 ++++++------ lib/service/export/participants/common.ex | 28 +++++++++++++++---- .../export/participants/common_test.exs | 10 +++++-- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/service/date_time/date_time.ex b/lib/service/date_time/date_time.ex index bccf9e7d0..dbceb7cc4 100644 --- a/lib/service/date_time/date_time.ex +++ b/lib/service/date_time/date_time.ex @@ -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 diff --git a/lib/service/export/participants/common.ex b/lib/service/export/participants/common.ex index afc0b9d88..84c848e1e 100644 --- a/lib/service/export/participants/common.ex +++ b/lib/service/export/participants/common.ex @@ -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() diff --git a/test/service/export/participants/common_test.exs b/test/service/export/participants/common_test.exs index 830772866..df0bb0581 100644 --- a/test/service/export/participants/common_test.exs +++ b/test/service/export/participants/common_test.exs @@ -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