@@ -10,6 +10,8 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events.Event, as: EventModel
|
||||
alias Mobilizon.Service.ActivityPub.Converter
|
||||
alias Mobilizon.Service.ActivityPub.Converters.Address, as: AddressConverter
|
||||
alias Mobilizon.Service.ActivityPub.Utils
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.Tag
|
||||
alias Mobilizon.Addresses
|
||||
@@ -26,6 +28,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
@spec as_to_model_data(map()) :: map()
|
||||
def as_to_model_data(object) do
|
||||
Logger.debug("event as_to_model_data")
|
||||
Logger.debug(inspect(object))
|
||||
|
||||
with {:actor, {:ok, %Actor{id: actor_id}}} <-
|
||||
{:actor, Actors.get_actor_by_url(object["actor"])},
|
||||
@@ -99,6 +102,8 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
end
|
||||
|
||||
defp fetch_tags(tags) do
|
||||
Logger.debug("fetching tags")
|
||||
|
||||
Enum.reduce(tags, [], fn tag, acc ->
|
||||
with true <- tag["type"] == "Hashtag",
|
||||
{:ok, %Tag{} = tag} <- Events.get_or_create_tag(tag) do
|
||||
@@ -110,23 +115,62 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
end)
|
||||
end
|
||||
|
||||
defp build_tags(tags) do
|
||||
Enum.map(tags, fn %Tag{} = tag ->
|
||||
%{
|
||||
"href" => MobilizonWeb.Endpoint.url() <> "/tags/#{tag.slug}",
|
||||
"name" => "##{tag.title}",
|
||||
"type" => "Hashtag"
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convert an event struct to an ActivityStream representation
|
||||
"""
|
||||
@impl Converter
|
||||
@spec model_to_as(EventModel.t()) :: map()
|
||||
def model_to_as(%EventModel{} = event) do
|
||||
%{
|
||||
to =
|
||||
if event.visibility == :public,
|
||||
do: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
else: [event.organizer_actor.followers_url]
|
||||
|
||||
res = %{
|
||||
"type" => "Event",
|
||||
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
"title" => event.title,
|
||||
"to" => to,
|
||||
"cc" => [],
|
||||
"attributedTo" => event.organizer_actor.url,
|
||||
"name" => event.title,
|
||||
"actor" => event.organizer_actor.url,
|
||||
"uuid" => event.uuid,
|
||||
"category" => event.category,
|
||||
"summary" => event.description,
|
||||
"publish_at" => (event.publish_at || event.inserted_at) |> DateTime.to_iso8601(),
|
||||
"updated_at" => event.updated_at |> DateTime.to_iso8601(),
|
||||
"content" => event.description,
|
||||
"publish_at" => (event.publish_at || event.inserted_at) |> date_to_string(),
|
||||
"updated_at" => event.updated_at |> date_to_string(),
|
||||
"mediaType" => "text/html",
|
||||
"startTime" => event.begins_on |> date_to_string(),
|
||||
"endTime" => event.ends_on |> date_to_string(),
|
||||
"tag" => event.tags |> build_tags(),
|
||||
"id" => event.url
|
||||
}
|
||||
|
||||
res =
|
||||
if is_nil(event.physical_address),
|
||||
do: res,
|
||||
else: Map.put(res, "location", AddressConverter.model_to_as(event.physical_address))
|
||||
|
||||
if is_nil(event.picture),
|
||||
do: res,
|
||||
else: Map.put(res, "attachment", [Utils.make_picture_data(event.picture)])
|
||||
end
|
||||
|
||||
defp date_to_string(nil), do: nil
|
||||
defp date_to_string(date), do: DateTime.to_iso8601(date)
|
||||
end
|
||||
|
||||
defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Events.Event do
|
||||
alias Mobilizon.Service.ActivityPub.Converters.Event, as: EventConverter
|
||||
|
||||
defdelegate model_to_as(event), to: EventConverter
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user