who_need_help/lib/who_need_help/notifications/text.ex

152 lines
4.5 KiB
Elixir

defmodule WhoNeedHelp.Notifications.Text do
@moduledoc """
Renders privacy-safe notification copy in the recipient's locale.
Notification rows retain their original title and body as a compatibility
fallback. Known product events are rendered from `kind` and structured
`data`, so the inbox and remote delivery can follow a user's current locale
without storing private conversation text or coordinates.
"""
use Gettext, backend: WhoNeedHelpWeb.Gettext
alias WhoNeedHelp.Locales
alias WhoNeedHelp.Notifications.Notification
def render(%Notification{} = notification, locale) do
with_locale(locale, fn ->
notification
|> content()
|> Map.put(:locale, normalized_locale(locale))
end)
end
def localize(%Notification{} = notification, locale) do
%{title: title, body: body} = render(notification, locale)
%{notification | title: title, body: body}
end
defp content(%Notification{kind: :nearby_request}) do
%{
title: gettext("New help request nearby"),
body: gettext("A request matching one of your nearby-help alerts is available.")
}
end
defp content(%Notification{kind: :request_accepted}) do
%{
title: gettext("A helper responded"),
body: gettext("Open Who Need Help to see the request update.")
}
end
defp content(%Notification{kind: :request_reopened}) do
%{
title: gettext("Your request needs a new helper"),
body: gettext("The previous helper withdrew, so the request is open again.")
}
end
defp content(%Notification{
kind: :request_cancelled,
data: %{"event_variant" => "requester_cancelled"}
}) do
%{
title: gettext("Request cancelled"),
body: gettext("The requester cancelled this request. Open Who Need Help for details.")
}
end
defp content(%Notification{
kind: :request_cancelled,
data: %{"event_variant" => "helper_withdrew_after_expiry"}
}) do
%{
title: gettext("Request closed after the helper withdrew"),
body: gettext("The helper withdrew after the request expired, so it was not reopened.")
}
end
defp content(%Notification{kind: :request_cancelled}) do
%{
title: gettext("Request updated"),
body: gettext("Open Who Need Help to review the latest request status.")
}
end
defp content(%Notification{kind: :assignment_started}) do
%{
title: gettext("Your helper started"),
body: gettext("Open Who Need Help to follow the request status.")
}
end
defp content(%Notification{kind: :helper_arrived}) do
%{
title: gettext("Your helper arrived"),
body: gettext("Open Who Need Help to coordinate the handover safely.")
}
end
defp content(%Notification{kind: :handover_verified}) do
%{
title: gettext("Handover code verified"),
body: gettext("The helper verified the one-time handover code.")
}
end
defp content(%Notification{kind: :request_completed}) do
%{
title: gettext("Help completed"),
body: gettext("Both participants confirmed completion and the handover was verified.")
}
end
defp content(%Notification{kind: :message_created}) do
%{
title: gettext("New message"),
body: gettext("Open Who Need Help to read the conversation.")
}
end
defp content(%Notification{kind: :review_revealed}) do
%{
title: gettext("Your review is now visible"),
body: gettext("Both participants submitted a review. Open Who Need Help to see it.")
}
end
defp content(%Notification{
kind: :support_update,
data: %{"kind" => "report_updated", "status" => status}
}) do
%{
title: gettext("Your report was updated"),
body: gettext("The latest moderation status is %{status}.", status: status_label(status))
}
end
defp content(%Notification{kind: :support_update}) do
%{
title: gettext("Support request updated"),
body: gettext("Open Who Need Help to review the latest update.")
}
end
defp content(%Notification{} = notification) do
%{title: notification.title, body: notification.body}
end
defp status_label("open"), do: gettext("Open")
defp status_label("reviewing"), do: gettext("Reviewing")
defp status_label("resolved"), do: gettext("Resolved")
defp status_label("dismissed"), do: gettext("Dismissed")
defp status_label(status), do: status
defp with_locale(locale, fun) do
Gettext.with_locale(WhoNeedHelpWeb.Gettext, normalized_locale(locale), fun)
end
defp normalized_locale(locale), do: Locales.normalize(locale)
end