2019-09-22 18:29:13 +02:00
|
|
|
defmodule Mobilizon.Service.ActivityPub.Converter.Event do
|
2019-05-22 14:12:11 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Event converter.
|
2019-05-22 14:12:11 +02:00
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
This module allows to convert events from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
2019-05-22 14:12:11 +02:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
|
|
alias Mobilizon.{Actors, Addresses, Events, Media}
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Events.Event, as: EventModel
|
2019-09-22 18:29:13 +02:00
|
|
|
alias Mobilizon.Events.{EventOptions, Tag}
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Media.Picture
|
2019-09-22 18:29:13 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub.{Converter, Convertible, Utils}
|
|
|
|
alias Mobilizon.Service.ActivityPub.Converter.Address, as: AddressConverter
|
|
|
|
alias Mobilizon.Service.ActivityPub.Converter.Picture, as: PictureConverter
|
2019-05-22 14:12:11 +02:00
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
require Logger
|
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
@behaviour Converter
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
defimpl Convertible, for: EventModel do
|
|
|
|
alias Mobilizon.Service.ActivityPub.Converter.Event, as: EventConverter
|
|
|
|
|
|
|
|
defdelegate model_to_as(event), to: EventConverter
|
|
|
|
end
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
@doc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Converts an AP object data to our internal data structure.
|
2019-05-22 14:12:11 +02:00
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec as_to_model_data(map) :: map
|
2019-05-22 14:12:11 +02:00
|
|
|
def as_to_model_data(object) do
|
2019-07-30 10:35:29 +02:00
|
|
|
Logger.debug("event as_to_model_data")
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug(inspect(object))
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
|
|
with {:actor, {:ok, %Actor{id: actor_id}}} <-
|
|
|
|
{:actor, Actors.get_actor_by_url(object["actor"])},
|
|
|
|
{:address, address_id} <-
|
|
|
|
{:address, get_address(object["location"])},
|
2019-08-28 11:28:27 +02:00
|
|
|
{:tags, tags} <- {:tags, fetch_tags(object["tag"])},
|
2019-09-20 19:43:29 +02:00
|
|
|
{:visibility, visibility} <- {:visibility, get_visibility(object)},
|
2019-08-28 11:28:27 +02:00
|
|
|
{:options, options} <- {:options, get_options(object)} do
|
2019-05-22 14:12:11 +02:00
|
|
|
picture_id =
|
2019-07-30 10:35:29 +02:00
|
|
|
with true <- Map.has_key?(object, "attachment") && length(object["attachment"]) > 0,
|
2019-05-22 14:12:11 +02:00
|
|
|
%Picture{id: picture_id} <-
|
|
|
|
Media.get_picture_by_url(
|
|
|
|
object["attachment"]
|
|
|
|
|> hd
|
|
|
|
|> Map.get("url")
|
|
|
|
|> hd
|
|
|
|
|> Map.get("href")
|
|
|
|
) do
|
|
|
|
picture_id
|
|
|
|
else
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
|
2019-08-28 11:28:27 +02:00
|
|
|
entity = %{
|
|
|
|
"title" => object["name"],
|
|
|
|
"description" => object["content"],
|
|
|
|
"organizer_actor_id" => actor_id,
|
|
|
|
"picture_id" => picture_id,
|
|
|
|
"begins_on" => object["startTime"],
|
2019-09-04 18:24:31 +02:00
|
|
|
"ends_on" => object["endTime"],
|
2019-08-28 11:28:27 +02:00
|
|
|
"category" => object["category"],
|
2019-09-20 19:43:29 +02:00
|
|
|
"visibility" => visibility,
|
2019-09-20 18:22:03 +02:00
|
|
|
"join_options" => object["joinOptions"],
|
2019-09-23 10:26:23 +02:00
|
|
|
"status" => object["status"],
|
|
|
|
"online_address" => object["onlineAddress"],
|
|
|
|
"phone_address" => object["phoneAddress"],
|
2019-08-28 11:28:27 +02:00
|
|
|
"url" => object["id"],
|
|
|
|
"uuid" => object["uuid"],
|
|
|
|
"tags" => tags,
|
|
|
|
"physical_address_id" => address_id
|
|
|
|
}
|
|
|
|
|
|
|
|
{:ok, Map.put(entity, "options", options)}
|
2019-07-30 10:35:29 +02:00
|
|
|
else
|
2019-09-22 18:29:13 +02:00
|
|
|
error ->
|
|
|
|
{:error, error}
|
2019-07-30 10:35:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
@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" => to,
|
|
|
|
"cc" => [],
|
|
|
|
"attributedTo" => event.organizer_actor.url,
|
|
|
|
"name" => event.title,
|
|
|
|
"actor" => event.organizer_actor.url,
|
|
|
|
"uuid" => event.uuid,
|
|
|
|
"category" => event.category,
|
|
|
|
"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,
|
|
|
|
"url" => 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", [PictureConverter.model_to_as(event.picture)])
|
|
|
|
end
|
|
|
|
|
2019-08-28 11:28:27 +02:00
|
|
|
# Get only elements that we have in EventOptions
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec get_options(map) :: map
|
2019-08-28 11:28:27 +02:00
|
|
|
defp get_options(object) do
|
|
|
|
keys =
|
2019-09-22 18:29:13 +02:00
|
|
|
EventOptions
|
2019-08-28 11:28:27 +02:00
|
|
|
|> struct
|
|
|
|
|> Map.keys()
|
|
|
|
|> List.delete(:__struct__)
|
|
|
|
|> Enum.map(&Utils.camelize/1)
|
|
|
|
|
|
|
|
Enum.reduce(object, %{}, fn {key, value}, acc ->
|
|
|
|
(value && key in keys && Map.put(acc, Utils.underscore(key), value)) ||
|
|
|
|
acc
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec get_address(map | binary | nil) :: integer | nil
|
2019-08-22 15:57:44 +02:00
|
|
|
defp get_address(address_url) when is_bitstring(address_url) do
|
|
|
|
get_address(%{"id" => address_url})
|
|
|
|
end
|
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
defp get_address(%{"id" => url} = map) when is_map(map) and is_binary(url) do
|
|
|
|
Logger.debug("Address with an URL, let's check against our own database")
|
|
|
|
|
|
|
|
case Addresses.get_address_by_url(url) do
|
|
|
|
%Address{id: address_id} ->
|
|
|
|
address_id
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Logger.debug("not in our database, let's try to create it")
|
|
|
|
map = Map.put(map, "url", map["id"])
|
|
|
|
do_get_address(map)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_address(map) when is_map(map) do
|
|
|
|
do_get_address(map)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_address(nil), do: nil
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec do_get_address(map) :: integer | nil
|
2019-07-30 10:35:29 +02:00
|
|
|
defp do_get_address(map) do
|
2019-09-22 18:29:13 +02:00
|
|
|
map = Mobilizon.Service.ActivityPub.Converter.Address.as_to_model_data(map)
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
|
|
case Addresses.create_address(map) do
|
|
|
|
{:ok, %Address{id: address_id}} ->
|
|
|
|
address_id
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec fetch_tags([String.t()]) :: [String.t()]
|
2019-07-26 11:30:28 +02:00
|
|
|
defp fetch_tags(tags) do
|
2019-07-30 16:40:59 +02:00
|
|
|
Logger.debug("fetching tags")
|
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
Enum.reduce(tags, [], fn tag, acc ->
|
2019-07-30 10:35:29 +02:00
|
|
|
with true <- tag["type"] == "Hashtag",
|
|
|
|
{:ok, %Tag{} = tag} <- Events.get_or_create_tag(tag) do
|
|
|
|
acc ++ [tag]
|
|
|
|
else
|
|
|
|
_err ->
|
2019-07-26 11:30:28 +02:00
|
|
|
acc
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec build_tags([String.t()]) :: String.t()
|
2019-07-30 16:40:59 +02:00
|
|
|
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
|
|
|
|
|
2019-09-20 19:43:29 +02:00
|
|
|
@ap_public "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
|
|
|
|
defp get_visibility(object) do
|
|
|
|
cond do
|
|
|
|
@ap_public in object["to"] -> :public
|
|
|
|
@ap_public in object["cc"] -> :unlisted
|
|
|
|
true -> :private
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
@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
|
2019-07-30 16:40:59 +02:00
|
|
|
to =
|
|
|
|
if event.visibility == :public,
|
|
|
|
do: ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
else: [event.organizer_actor.followers_url]
|
|
|
|
|
|
|
|
res = %{
|
2019-05-22 14:12:11 +02:00
|
|
|
"type" => "Event",
|
2019-07-30 16:40:59 +02:00
|
|
|
"to" => to,
|
|
|
|
"cc" => [],
|
|
|
|
"attributedTo" => event.organizer_actor.url,
|
|
|
|
"name" => event.title,
|
2019-05-22 14:12:11 +02:00
|
|
|
"actor" => event.organizer_actor.url,
|
|
|
|
"uuid" => event.uuid,
|
|
|
|
"category" => event.category,
|
2019-07-30 16:40:59 +02:00
|
|
|
"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(),
|
2019-09-22 11:22:16 +02:00
|
|
|
"joinOptions" => to_string(event.join_options),
|
2019-07-30 16:40:59 +02:00
|
|
|
"tag" => event.tags |> build_tags(),
|
2019-09-04 18:24:31 +02:00
|
|
|
"id" => event.url,
|
|
|
|
"url" => event.url
|
2019-05-22 14:12:11 +02:00
|
|
|
}
|
2019-07-30 16:40:59 +02:00
|
|
|
|
|
|
|
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)])
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
2019-07-30 16:40:59 +02:00
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec date_to_string(DateTime.t() | nil) :: String.t()
|
2019-07-30 16:40:59 +02:00
|
|
|
defp date_to_string(nil), do: nil
|
2019-09-22 18:29:13 +02:00
|
|
|
defp date_to_string(%DateTime{} = date), do: DateTime.to_iso8601(date)
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|