2021-05-06 18:39:59 +02:00
|
|
|
defmodule Mobilizon.Service.Activity.Renderer do
|
|
|
|
@moduledoc """
|
|
|
|
Behavior for Activity renderers
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Activities.Activity
|
2021-06-03 15:00:49 +02:00
|
|
|
alias Mobilizon.Config
|
2021-06-01 18:08:03 +02:00
|
|
|
|
|
|
|
alias Mobilizon.Service.Activity.Renderer.{
|
|
|
|
Comment,
|
|
|
|
Discussion,
|
|
|
|
Event,
|
|
|
|
Group,
|
|
|
|
Member,
|
|
|
|
Post,
|
|
|
|
Resource
|
|
|
|
}
|
|
|
|
|
2021-05-06 18:39:59 +02:00
|
|
|
require Logger
|
|
|
|
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@type render :: %{
|
|
|
|
body: String.t(),
|
|
|
|
url: String.t(),
|
|
|
|
timestamp: String.t(),
|
|
|
|
locale: String.t(),
|
|
|
|
title: String.t()
|
|
|
|
}
|
2021-05-06 18:39:59 +02:00
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@type common_render :: %{body: String.t(), url: String.t()}
|
|
|
|
|
|
|
|
@callback render(entity :: Activity.t(), Keyword.t()) :: common_render()
|
2021-05-06 18:39:59 +02:00
|
|
|
|
|
|
|
@spec render(Activity.t()) :: render()
|
|
|
|
def render(%Activity{} = activity, options \\ []) do
|
|
|
|
locale = Keyword.get(options, :locale, "en")
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
res =
|
|
|
|
activity
|
|
|
|
|> do_render(options)
|
|
|
|
|> Map.put(:timestamp, DateTime.utc_now() |> DateTime.to_iso8601())
|
|
|
|
|> Map.put(:locale, Keyword.get(options, :locale, "en"))
|
|
|
|
|> Map.put(
|
|
|
|
:title,
|
|
|
|
dgettext("activity", "Activity on %{instance}", %{instance: Config.instance_name()})
|
|
|
|
)
|
|
|
|
|
|
|
|
Logger.debug("notification to be sent")
|
|
|
|
Logger.debug(inspect(res))
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec do_render(Activity.t(), Keyword.t()) :: common_render()
|
2021-05-06 18:39:59 +02:00
|
|
|
defp do_render(%Activity{type: type} = activity, options) do
|
|
|
|
case type do
|
|
|
|
:discussion -> Discussion.render(activity, options)
|
|
|
|
:event -> Event.render(activity, options)
|
|
|
|
:group -> Group.render(activity, options)
|
|
|
|
:member -> Member.render(activity, options)
|
|
|
|
:post -> Post.render(activity, options)
|
|
|
|
:resource -> Resource.render(activity, options)
|
2021-06-01 18:08:03 +02:00
|
|
|
:comment -> Comment.render(activity, options)
|
2021-05-06 18:39:59 +02:00
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|