8000 Parameterize notification time formatting by mwoods79 · Pull Request #143 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# General application configuration
import Config

config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase

config :ash_graphql, authorize_update_destroy_with_error?: true

config :mime,
Expand Down
39 changes: 39 additions & 0 deletions lib/huddlz/notifications/date_time_formatter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Huddlz.Notifications.DateTimeFormatter do
@moduledoc """
Shared date/time formatting for notification copy.
"""

@default_time_zone "Etc/UTC"
@starts_at_format "%a %b %-d, %Y at %-I:%M %p %Z"

def time_zone_from_payload(%{"time_zone" => time_zone}), do: time_zone
def time_zone_from_payload(%{"timezone" => time_zone}), do: time_zone
def time_zone_from_payload(_), do: nil

def format_starts_at(%DateTime{} = datetime, time_zone \\ @default_time_zone) do
datetime
|> shift_or_default(time_zone)
|> Calendar.strftime(@starts_at_format)
end

def format_starts_at_iso(iso, time_zone \\ @default_time_zone, fallback \\ "the scheduled time")

def format_starts_at_iso(iso, time_zone, fallback) when is_binary(iso) do
case DateTime.from_iso8601(iso) do
{:ok, datetime, _offset} -> format_starts_at(datetime, time_zone)
_ -> fallback
end
end

def format_starts_at_iso(_, _, fallback), do: fallback

defp shift_or_default(datetime, time_zone) do
case DateTime.shift_zone(datetime, normalize_time_zone(time_zone)) do
{:ok, shifted} -> shifted
_ -> DateTime.shift_zone!(datetime, @default_time_zone)
end
end

defp normalize_time_zone(time_zone) when is_binary(time_zone) and time_zone != "", do: time_zone
defp normalize_time_zone(_), do: @default_time_zone
end
19 changes: 9 additions & 10 deletions lib/huddlz/notifications/senders/huddl_cancelled.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Huddlz.Notifications.Senders.HuddlCancelled do
import Swoo 8000 sh.Email

alias Huddlz.Mailer
alias Huddlz.Notifications.DateTimeFormatter
alias Huddlz.Notifications.Senders.HeaderSafe
alias Huddlz.Notifications.Senders.HtmlEscape

Expand All @@ -29,7 +30,14 @@ defmodule Huddlz.Notifications.Senders.HuddlCancelled do
safe_name = HtmlEscape.escape(user.display_name)
safe_title = HtmlEscape.escape(huddl_title(payload))
safe_group = HtmlEscape.escape(group_name(payload))
when_text = format_starts_at(payload)

when_text =
DateTimeFormatter.format_starts_at_iso(
payload["starts_at_iso"],
DateTimeFormatter.time_zone_from_payload(payload),
payload["starts_at_iso"] || "the scheduled time"
)

safe_when = HtmlEscape.escape(when_text)
group_url = group_url(payload)

Expand Down Expand Up @@ -70,13 +78,4 @@ defmodule Huddlz.Notifications.Senders.HuddlCancelled do

defp group_url(%{"group_slug" => slug}) when is_binary(slug), do: url(~p"/groups/#{slug}")
defp group_url(_), do: url(~p"/groups")

defp format_starts_at(%{"starts_at_iso" => iso}) when is_binary(iso) do
case DateTime.from_iso8601(iso) do
{:ok, dt, _offset} -> Calendar.strftime(dt, "%a %b %-d, %Y at %-I:%M %p UTC")
_ -> iso
end
end

defp format_starts_at(_), do: "the scheduled time"
end
19 changes: 9 additions & 10 deletions lib/huddlz/notifications/senders/huddl_new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule Huddlz.Notifications.Senders.HuddlNew do
import Swoosh.Email

alias Huddlz.Mailer
alias Huddlz.Notifications.DateTimeFormatter
alias Huddlz.Notifications.Footer
alias Huddlz.Notifications.Senders.HeaderSafe
alias Huddlz.Notifications.Senders.HtmlEscape
Expand All @@ -31,7 +32,14 @@ defmodule Huddlz.Notifications.Senders.HuddlNew do
safe_name = HtmlEscape.escape(user.display_name)
safe_title = HtmlEscape.escape(huddl_title(payload))
safe_group = HtmlEscape.escape(group_name(payload))
when_text = format_starts_at(payload)

when_text =
DateTimeFormatter.format_starts_at_iso(
payload["starts_at_iso"],
DateTimeFormatter.time_zone_from_payload(payload),
payload["starts_at_iso"] || "the scheduled time"
)

safe_when = HtmlEscape.escape(when_text)
huddl_url = huddl_url(payload)

Expand Down Expand Up @@ -74,13 +82,4 @@ defmodule Huddlz.Notifications.Senders.HuddlNew do

defp huddl_url(%{"group_slug" => slug}) when is_binary(slug), do: url(~p"/groups/#{slug}")
defp huddl_url(_), do: url(~p"/")

defp format_starts_at(%{"starts_at_iso" => iso}) when is_binary(iso) do
case DateTime.from_iso8601(iso) do
{:ok, dt, _offset} -> Calendar.strftime(dt, "%a %b %-d, %Y at %-I:%M %p UTC")
_ -> iso
end
end

defp format_starts_at(_), do: "the scheduled time"
end
13 changes: 8 additions & 5 deletions lib/huddlz/notifications/senders/huddl_reminder_24h.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Huddlz.Notifications.Senders.HuddlReminder24h do

alias Huddlz.Communities.Huddl
alias Huddlz.Mailer
alias Huddlz.Notifications.DateTimeFormatter
alias Huddlz.Notifications.Footer
alias Huddlz.Notifications.ICS
alias Huddlz.Notifications.Senders.HeaderSafe
Expand All @@ -35,7 +36,13 @@ defmodule Huddlz.Notifications.Senders.HuddlReminder24h do
safe_name = HtmlEscape.escape(user.display_name)
safe_title = HtmlEscape.escape(huddl.title)
safe_group = HtmlEscape.escape(huddl.group.name)
when_text = format_starts_at(huddl.starts_at)

when_text =
DateTimeFormatter.format_starts_at(
huddl.starts_at,
DateTimeFormatter.time_zone_from_payload(payload)
)

safe_when = HtmlEscape.escape(when_text)
huddl_url = url(~p"/groups/#{huddl.group.slug}/huddlz/#{huddl.id}")

Expand Down Expand Up @@ -77,8 +84,4 @@ defmodule Huddlz.Notifications.Senders.HuddlReminder24h do
defp fetch_huddl!(%{"huddl_id" => id}) when is_binary(id) do
Ash.get!(Huddl, id, authorize?: false, load: [:group])
end

defp format_starts_at(%DateTime{} = dt) do
Calendar.strftime(dt, "%a %b %-d, %Y at %-I:%M %p UTC")
end
end
19 changes: 9 additions & 10 deletions lib/huddlz/notifications/senders/huddl_series_updated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule Huddlz.Notifications.Senders.HuddlSeriesUpdated do
import Swoosh.Email

alias Huddlz.Mailer
alias Huddlz.Notifications.DateTimeFormatter
alias Huddlz.Notifications.Footer
alias Huddlz.Notifications.Senders.HeaderSafe
alias Huddlz.Notifications.Senders.HtmlEscape
Expand All @@ -29,7 +30,14 @@ defmodule Huddlz.Notifications.Senders.HuddlSeriesUpdated do
safe_name = HtmlEscape.escape(user.display_name)
safe_title = HtmlEscape.escape(huddl_title(payload))
safe_group = HtmlEscape.escape(group_name(payload))
when_text = format_starts_at(payload)

when_text =
DateTimeFormatter.format_starts_at_iso(
payload["starts_at_iso"],
DateTimeFormatter.time_zone_from_payload(payload),
payload["starts_at_iso"] || "the scheduled time"
)

safe_when = HtmlEscape.escape(when_text)
safe_changed = HtmlEscape.escape(changed_summary(payload))
huddl_url = huddl_url(payload)
Expand Down Expand Up @@ -86,15 +94,6 @@ defmodule Huddlz.Notifications.Senders.HuddlSeriesUpdated do
defp huddl_url(%{"group_slug" => slug}) when is_binary(slug), do: url(~p"/groups/#{slug}")
defp huddl_url(_), do: url(~p"/")

defp format_starts_at(%{"starts_at_iso" => iso}) when is_binary(iso) do
case DateTime.from_iso8601(iso) do
{:ok, dt, _offset} -> Calendar.strftime(dt, "%a %b %-d, %Y at %-I:%M %p UTC")
_ -> iso
end
end

defp format_starts_at(_), do: "the scheduled time"

defp changed_summary(%{"changed_fields" => fields}) when is_list(fields) and fields != [] do
Enum.map_join(fields, ", ", &humanize_field/1)
end
Expand Down
19 changes: 9 additions & 10 deletions lib/huddlz/notifications/senders/huddl_updated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule Huddlz.Notifications.Senders.HuddlUpdated do
import Swoosh.Email

alias Huddlz.Mailer
alias Huddlz.Notifications.DateTimeFormatter
alias Huddlz.Notifications.Footer
alias Huddlz.Notifications.Senders.HeaderSafe
alias Huddlz.Notifications.Senders.HtmlEscape
Expand All @@ -29,7 +30,14 @@ defmodule Huddlz.Notifications.Senders.HuddlUpdated do
safe_name = HtmlEscape.escape(user.display_name)
safe_title = HtmlEscape.escape(huddl_title(payload))
safe_group = HtmlEscape.escape(group_name(payload))
when_text = format_starts_at(payload)

when_text =
DateTimeFormatter.format_starts_at_iso(
payload["starts_at_iso"],
DateTimeFormatter.time_zone_from_payload(payload),
payload["starts_at_iso"] || "the scheduled time"
)

safe_when = HtmlEscape.escape(when_text)
safe_changed = HtmlEscape.escape(changed_summary(payload))
huddl_url = huddl_url(payload)
Expand Down Expand Up @@ -79,15 +87,6 @@ defmodule Huddlz.Notifications.Senders.HuddlUpdated do
defp huddl_url(%{"group_slug" => slug}) when is_binary(slug), do: url(~p"/groups/#{slug}")
defp huddl_url(_), do: url(~p"/")

defp format_starts_at(%{"starts_at_iso" => iso}) when is_binary(iso) do
case DateTime.from_iso8601(iso) do
{:ok, dt, _offset} -> Calendar.strftime(dt, "%a %b %-d, %Y at %-I:%M %p UTC")
_ -> iso
end
end

defp format_starts_at(_), do: "the scheduled time"

defp changed_summary(%{"changed_fields" => fields}) when is_list(fields) and fields != [] do
Enum.map_join(fields, ", ", &humanize_field/1)
end
Expand Down
13 changes: 8 additions & 5 deletions lib/huddlz/notifications/senders/rsvp_confirmation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Huddlz.Notifications.Senders.RsvpConfirmation do

alias Huddlz.Communities.Huddl
alias Huddlz.Mailer
alias Huddlz.Notifications.DateTimeFormatter
alias Huddlz.Notifications.Footer
alias Huddlz.Notifications.ICS
alias Huddlz.Notifications.Senders.HeaderSafe
Expand All @@ -33,7 +34,13 @@ defmodule Huddlz.Notifications.Senders.RsvpConfirmation do
safe_name = HtmlEscape.escape(user.display_name)
safe_title = HtmlEscape.escape(huddl.title)
safe_group = HtmlEscape.escape(huddl.group.name)
when_text = format_starts_at(huddl.starts_at)

when_text =
DateTimeFormatter.format_starts_at(
huddl.starts_at,
DateTimeFormatter.time_zone_from_payload(payload)
)

safe_when = HtmlEscape.escape(when_text)
huddl_url = url(~p"/groups/#{huddl.group.slug}/huddlz/#{huddl.id}")

Expand Down Expand Up @@ -77,8 +84,4 @@ defmodule Huddlz.Notifications.Senders.RsvpConfirmation do
defp fetch_huddl!(_) do
raise ArgumentError, "RsvpConfirmation requires payload key \"huddl_id\" with a binary UUID"
end

defp format_starts_at(%DateTime{} = dt) do
Calendar.strftime(dt, "%a %b %-d, %Y at %-I:%M %p UTC")
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ defmodule Huddlz.MixProject do
{:telemetry_metrics, "~> 1.0"},
{:telemetry_poller, "~> 1.0"},
{:gettext, "~> 1.0.2"},
{:tzdata, "~> 1.1"},
{:jason, "~> 1.2"},
{:dns_cluster, "~> 0.2.0"},
{:bandit, "~> 1.5"},
Expand Down
Loading
Loading
0