2020-01-22 21:40:40 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
|
2019-05-22 12:12:11 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Event converter.
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
This module allows to convert events from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
2019-05-22 12:12:11 +00:00
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-22 14:26:23 +00:00
|
|
|
alias Mobilizon.Addresses.Address
|
2022-04-06 12:42:01 +00:00
|
|
|
alias Mobilizon.Events.Categories
|
2019-05-22 12:12:11 +00:00
|
|
|
alias Mobilizon.Events.Event, as: EventModel
|
2021-08-10 09:03:03 +00:00
|
|
|
alias Mobilizon.Medias.Media
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Address, as: AddressConverter
|
2021-08-10 09:03:03 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.EventMetadata, as: EventMetadataConverter
|
2020-11-26 10:41:13 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Media, as: MediaConverter
|
2021-10-12 09:10:16 +00:00
|
|
|
alias Mobilizon.Service.TimezoneDetector
|
2021-07-26 15:19:51 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2020-07-09 15:24:28 +00:00
|
|
|
|
2021-11-16 14:45:05 +00:00
|
|
|
import Mobilizon.Federation.ActivityPub.Utils, only: [get_url: 1]
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
import Mobilizon.Federation.ActivityStream.Converter.Utils,
|
|
|
|
only: [
|
|
|
|
fetch_tags: 1,
|
|
|
|
fetch_mentions: 1,
|
|
|
|
build_tags: 1,
|
2020-12-16 08:54:56 +00:00
|
|
|
maybe_fetch_actor_and_attributed_to_id: 1,
|
2021-11-06 09:09:07 +00:00
|
|
|
process_pictures: 2,
|
2021-11-16 14:45:05 +00:00
|
|
|
get_address: 1,
|
|
|
|
fetch_actor: 1
|
2020-07-09 15:24:28 +00:00
|
|
|
]
|
2019-05-22 12:12:11 +00:00
|
|
|
|
2019-07-30 08:35:29 +00:00
|
|
|
require Logger
|
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
@behaviour Converter
|
|
|
|
|
2019-09-22 16:29:13 +00:00
|
|
|
defimpl Convertible, for: EventModel do
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Event, as: EventConverter
|
2019-09-22 16:29:13 +00:00
|
|
|
|
|
|
|
defdelegate model_to_as(event), to: EventConverter
|
|
|
|
end
|
|
|
|
|
2020-12-16 08:54:56 +00:00
|
|
|
@online_address_name "Website"
|
|
|
|
@banner_picture_name "Banner"
|
2021-07-29 15:50:23 +00:00
|
|
|
@ap_public "https://www.w3.org/ns/activitystreams#Public"
|
2020-12-16 08:54:56 +00:00
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
@doc """
|
2019-09-22 16:29:13 +00:00
|
|
|
Converts an AP object data to our internal data structure.
|
2019-05-22 12:12:11 +00:00
|
|
|
"""
|
|
|
|
@impl Converter
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec as_to_model_data(map) :: map() | {:error, atom()}
|
2019-05-22 12:12:11 +00:00
|
|
|
def as_to_model_data(object) do
|
2021-09-28 17:40:37 +00:00
|
|
|
case maybe_fetch_actor_and_attributed_to_id(object) do
|
|
|
|
{:ok, %Actor{id: actor_id}, attributed_to} ->
|
2021-10-12 09:10:16 +00:00
|
|
|
address = get_address(object["location"])
|
2021-09-28 17:40:37 +00:00
|
|
|
tags = fetch_tags(object["tag"])
|
|
|
|
mentions = fetch_mentions(object["tag"])
|
|
|
|
visibility = get_visibility(object)
|
2021-10-12 09:10:16 +00:00
|
|
|
options = get_options(object, address)
|
2021-09-28 17:40:37 +00:00
|
|
|
metadata = get_metdata(object)
|
2021-11-16 14:45:05 +00:00
|
|
|
contacts = get_contacts(object)
|
2021-09-28 17:40:37 +00:00
|
|
|
|
|
|
|
[description: description, picture_id: picture_id, medias: medias] =
|
|
|
|
process_pictures(object, actor_id)
|
|
|
|
|
|
|
|
%{
|
|
|
|
title: object["name"],
|
|
|
|
description: description,
|
|
|
|
organizer_actor_id: actor_id,
|
|
|
|
attributed_to_id: if(is_nil(attributed_to), do: nil, else: attributed_to.id),
|
|
|
|
picture_id: picture_id,
|
|
|
|
medias: medias,
|
|
|
|
begins_on: object["startTime"],
|
|
|
|
ends_on: object["endTime"],
|
2022-10-06 07:32:47 +00:00
|
|
|
category: Categories.get_category(object["category"]),
|
2021-09-28 17:40:37 +00:00
|
|
|
visibility: visibility,
|
|
|
|
join_options: Map.get(object, "joinMode", "free"),
|
2021-11-16 14:48:34 +00:00
|
|
|
local: is_local?(object["id"]),
|
2021-09-28 17:40:37 +00:00
|
|
|
options: options,
|
|
|
|
metadata: metadata,
|
|
|
|
status: object |> Map.get("ical:status", "CONFIRMED") |> String.downcase(),
|
|
|
|
online_address: object |> Map.get("attachment", []) |> get_online_address(),
|
|
|
|
phone_address: object["phoneAddress"],
|
|
|
|
draft: object["draft"] == true,
|
|
|
|
url: object["id"],
|
|
|
|
uuid: object["uuid"],
|
|
|
|
tags: tags,
|
|
|
|
mentions: mentions,
|
2021-10-12 09:10:16 +00:00
|
|
|
physical_address_id: if(address, do: address.id, else: nil),
|
2021-09-28 17:40:37 +00:00
|
|
|
updated_at: object["updated"],
|
|
|
|
publish_at: object["published"],
|
2021-11-16 14:45:05 +00:00
|
|
|
language: object["inLanguage"],
|
|
|
|
contacts: contacts
|
2021-09-28 17:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2019-07-30 08:35:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-22 16:29:13 +00: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
|
2021-07-29 15:50:23 +00:00
|
|
|
{to, cc} =
|
2019-09-22 16:29:13 +00:00
|
|
|
if event.visibility == :public,
|
2021-11-19 16:56:48 +00:00
|
|
|
do: {[@ap_public], [event.organizer_actor.followers_url]},
|
2021-07-29 15:50:23 +00:00
|
|
|
else: {[attributed_to_or_default(event).followers_url], [@ap_public]}
|
2019-09-22 16:29:13 +00:00
|
|
|
|
2022-02-08 14:27:53 +00:00
|
|
|
participant_count = Mobilizon.Events.count_participant_participants(event.id)
|
|
|
|
|
2020-09-02 06:59:59 +00:00
|
|
|
%{
|
2019-09-22 16:29:13 +00:00
|
|
|
"type" => "Event",
|
|
|
|
"to" => to,
|
2021-07-29 15:50:23 +00:00
|
|
|
"cc" => cc,
|
2021-02-04 11:28:53 +00:00
|
|
|
"attributedTo" => attributed_to_or_default(event).url,
|
2019-09-22 16:29:13 +00:00
|
|
|
"name" => event.title,
|
2020-09-02 06:59:59 +00:00
|
|
|
"actor" =>
|
|
|
|
if(Ecto.assoc_loaded?(event.organizer_actor), do: event.organizer_actor.url, else: nil),
|
2019-09-22 16:29:13 +00:00
|
|
|
"uuid" => event.uuid,
|
|
|
|
"category" => event.category,
|
|
|
|
"content" => event.description,
|
2019-12-03 10:29:51 +00:00
|
|
|
"published" => (event.publish_at || event.inserted_at) |> date_to_string(),
|
|
|
|
"updated" => event.updated_at |> date_to_string(),
|
2019-09-22 16:29:13 +00:00
|
|
|
"mediaType" => "text/html",
|
2021-10-12 09:10:16 +00:00
|
|
|
"startTime" => event.begins_on |> shift_tz(event.options.timezone) |> date_to_string(),
|
2019-12-03 10:29:51 +00:00
|
|
|
"joinMode" => to_string(event.join_options),
|
2021-10-12 09:10:16 +00:00
|
|
|
"endTime" => event.ends_on |> shift_tz(event.options.timezone) |> date_to_string(),
|
2020-07-09 15:24:28 +00:00
|
|
|
"tag" => event.tags |> build_tags(),
|
2019-12-03 10:29:51 +00:00
|
|
|
"maximumAttendeeCapacity" => event.options.maximum_attendee_capacity,
|
2022-02-08 14:27:53 +00:00
|
|
|
"remainingAttendeeCapacity" =>
|
|
|
|
remaining_attendee_capacity(event.options, participant_count),
|
|
|
|
"participantCount" => participant_count,
|
2019-12-03 10:29:51 +00:00
|
|
|
"repliesModerationOption" => event.options.comment_moderation,
|
2019-12-16 10:46:19 +00:00
|
|
|
"commentsEnabled" => event.options.comment_moderation == :allow_all,
|
2019-12-20 12:04:34 +00:00
|
|
|
"anonymousParticipationEnabled" => event.options.anonymous_participation,
|
2021-08-10 09:03:03 +00:00
|
|
|
"attachment" => Enum.map(event.metadata, &EventMetadataConverter.metadata_to_as/1),
|
2021-03-08 14:58:25 +00:00
|
|
|
"draft" => event.draft,
|
2019-12-03 10:29:51 +00:00
|
|
|
"ical:status" => event.status |> to_string |> String.upcase(),
|
2019-09-22 16:29:13 +00:00
|
|
|
"id" => event.url,
|
2021-08-19 18:43:35 +00:00
|
|
|
"url" => event.url,
|
2021-10-12 09:10:16 +00:00
|
|
|
"inLanguage" => event.language,
|
2021-11-16 14:45:05 +00:00
|
|
|
"timezone" => event.options.timezone,
|
2022-02-08 14:27:17 +00:00
|
|
|
"contacts" => Enum.map(event.contacts, & &1.url),
|
|
|
|
"isOnline" => event.options.is_online
|
2019-09-22 16:29:13 +00:00
|
|
|
}
|
2020-09-02 06:59:59 +00:00
|
|
|
|> maybe_add_physical_address(event)
|
|
|
|
|> maybe_add_event_picture(event)
|
|
|
|
|> maybe_add_online_address(event)
|
2020-12-16 08:54:56 +00:00
|
|
|
|> maybe_add_inline_media(event)
|
2019-09-22 16:29:13 +00:00
|
|
|
end
|
|
|
|
|
2021-08-10 09:03:03 +00:00
|
|
|
@spec attributed_to_or_default(EventModel.t()) :: Actor.t()
|
|
|
|
defp attributed_to_or_default(%EventModel{} = event) do
|
2021-02-04 11:28:53 +00:00
|
|
|
if(is_nil(event.attributed_to) or not Ecto.assoc_loaded?(event.attributed_to),
|
|
|
|
do: nil,
|
|
|
|
else: event.attributed_to
|
|
|
|
) ||
|
|
|
|
event.organizer_actor
|
|
|
|
end
|
|
|
|
|
2019-08-28 09:28:27 +00:00
|
|
|
# Get only elements that we have in EventOptions
|
2021-10-12 09:10:16 +00:00
|
|
|
@spec get_options(map, Address.t() | nil) :: map
|
|
|
|
defp get_options(object, address) do
|
2019-12-03 10:29:51 +00:00
|
|
|
%{
|
|
|
|
maximum_attendee_capacity: object["maximumAttendeeCapacity"],
|
2019-12-20 12:04:34 +00:00
|
|
|
anonymous_participation: object["anonymousParticipationEnabled"],
|
2019-12-16 10:46:19 +00:00
|
|
|
comment_moderation:
|
|
|
|
Map.get(
|
|
|
|
object,
|
|
|
|
"repliesModerationOption",
|
|
|
|
if(Map.get(object, "commentsEnabled", true), do: :allow_all, else: :closed)
|
2021-10-12 09:10:16 +00:00
|
|
|
),
|
2022-02-08 14:27:17 +00:00
|
|
|
timezone: calculate_timezone(object, address),
|
|
|
|
is_online: object["isOnline"] == true
|
2019-12-03 10:29:51 +00:00
|
|
|
}
|
2019-08-28 09:28:27 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 09:10:16 +00:00
|
|
|
defp calculate_timezone(%{"timezone" => timezone}, %Address{geom: geom}) do
|
|
|
|
TimezoneDetector.detect(
|
|
|
|
timezone,
|
|
|
|
geom,
|
|
|
|
"Etc/UTC"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp calculate_timezone(_object, nil), do: nil
|
|
|
|
|
|
|
|
defp calculate_timezone(_object, %Address{geom: geom}) do
|
|
|
|
TimezoneDetector.detect(
|
|
|
|
nil,
|
|
|
|
geom,
|
|
|
|
"Etc/UTC"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-08-10 09:03:03 +00:00
|
|
|
defp get_metdata(%{"attachment" => attachments}) do
|
|
|
|
attachments
|
|
|
|
|> Enum.filter(&(&1["type"] == "PropertyValue"))
|
|
|
|
|> Enum.map(&EventMetadataConverter.as_to_metadata/1)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_metdata(_), do: []
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
defp get_visibility(object), do: if(@ap_public in object["to"], do: :public, else: :unlisted)
|
2019-09-20 17:43:29 +00:00
|
|
|
|
2019-09-22 16:29:13 +00:00
|
|
|
@spec date_to_string(DateTime.t() | nil) :: String.t()
|
2019-07-30 14:40:59 +00:00
|
|
|
defp date_to_string(nil), do: nil
|
2019-09-22 16:29:13 +00:00
|
|
|
defp date_to_string(%DateTime{} = date), do: DateTime.to_iso8601(date)
|
2020-02-07 17:41:36 +00:00
|
|
|
|
2021-10-12 09:10:16 +00:00
|
|
|
@spec shift_tz(DateTime.t(), String.t() | nil) :: DateTime.t()
|
|
|
|
defp shift_tz(%DateTime{} = date, timezone) when is_binary(timezone) do
|
|
|
|
DateTime.shift_zone!(date, timezone)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp shift_tz(datetime, _tz), do: datetime
|
|
|
|
|
2020-02-07 17:41:36 +00:00
|
|
|
defp get_online_address(attachments) do
|
2020-06-09 09:13:22 +00:00
|
|
|
Enum.find_value(attachments, fn attachment ->
|
2020-02-07 17:41:36 +00:00
|
|
|
case attachment do
|
|
|
|
%{
|
|
|
|
"type" => "Link",
|
|
|
|
"href" => url,
|
|
|
|
"mediaType" => "text/html",
|
2020-12-16 08:54:56 +00:00
|
|
|
"name" => @online_address_name
|
2020-02-07 17:41:36 +00:00
|
|
|
} ->
|
|
|
|
url
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2020-09-02 06:59:59 +00:00
|
|
|
|
2021-08-10 09:03:03 +00:00
|
|
|
@spec maybe_add_physical_address(map(), EventModel.t()) :: map()
|
|
|
|
defp maybe_add_physical_address(res, %EventModel{
|
|
|
|
physical_address: %Address{} = physical_address
|
|
|
|
}) do
|
|
|
|
Map.put(res, "location", AddressConverter.model_to_as(physical_address))
|
2020-09-02 06:59:59 +00:00
|
|
|
end
|
|
|
|
|
2021-08-10 09:03:03 +00:00
|
|
|
defp maybe_add_physical_address(res, %EventModel{physical_address: _}), do: res
|
|
|
|
|
|
|
|
@spec maybe_add_event_picture(map(), EventModel.t()) :: map()
|
|
|
|
defp maybe_add_event_picture(res, %EventModel{picture: %Media{} = picture}) do
|
|
|
|
Map.update(
|
|
|
|
res,
|
|
|
|
"attachment",
|
|
|
|
[],
|
|
|
|
&(&1 ++
|
|
|
|
[
|
|
|
|
picture
|
|
|
|
|> MediaConverter.model_to_as()
|
|
|
|
|> Map.put("name", @banner_picture_name)
|
|
|
|
])
|
|
|
|
)
|
2020-09-02 06:59:59 +00:00
|
|
|
end
|
|
|
|
|
2021-08-10 09:03:03 +00:00
|
|
|
defp maybe_add_event_picture(res, %EventModel{picture: _}), do: res
|
|
|
|
|
|
|
|
@spec maybe_add_online_address(map(), EventModel.t()) :: map()
|
|
|
|
defp maybe_add_online_address(res, %EventModel{online_address: online_address})
|
|
|
|
when is_binary(online_address) do
|
|
|
|
Map.update(
|
|
|
|
res,
|
|
|
|
"attachment",
|
|
|
|
[],
|
|
|
|
&(&1 ++
|
|
|
|
[
|
|
|
|
%{
|
|
|
|
"type" => "Link",
|
|
|
|
"href" => online_address,
|
|
|
|
"mediaType" => "text/html",
|
|
|
|
"name" => @online_address_name
|
|
|
|
}
|
|
|
|
])
|
|
|
|
)
|
2020-09-02 06:59:59 +00:00
|
|
|
end
|
2020-12-16 08:54:56 +00:00
|
|
|
|
2021-08-10 09:03:03 +00:00
|
|
|
defp maybe_add_online_address(res, %EventModel{online_address: _}), do: res
|
|
|
|
|
|
|
|
@spec maybe_add_inline_media(map(), EventModel.t()) :: map()
|
|
|
|
defp maybe_add_inline_media(res, %EventModel{media: media}) do
|
|
|
|
medias = Enum.map(media, &MediaConverter.model_to_as/1)
|
2020-12-16 08:54:56 +00:00
|
|
|
|
|
|
|
Map.update(
|
|
|
|
res,
|
|
|
|
"attachment",
|
|
|
|
[],
|
|
|
|
&(&1 ++ medias)
|
|
|
|
)
|
|
|
|
end
|
2021-07-26 15:19:51 +00:00
|
|
|
|
2021-11-16 14:48:34 +00:00
|
|
|
@spec is_local?(String.t()) :: boolean()
|
|
|
|
defp is_local?(url) do
|
2021-07-26 15:19:51 +00:00
|
|
|
%URI{host: url_domain} = URI.parse(url)
|
|
|
|
%URI{host: local_domain} = URI.parse(Endpoint.url())
|
|
|
|
url_domain == local_domain
|
|
|
|
end
|
2021-11-16 14:45:05 +00:00
|
|
|
|
|
|
|
@spec get_contacts(map()) :: list(Actor.t())
|
|
|
|
defp get_contacts(object) do
|
|
|
|
object
|
|
|
|
|> Map.get("contacts", [])
|
|
|
|
|> Enum.map(&get_contact/1)
|
|
|
|
|> Enum.filter(&match?({:ok, _}, &1))
|
|
|
|
|> Enum.map(fn {:ok, contact} -> contact end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_contact(contact) do
|
|
|
|
contact
|
|
|
|
|> get_url()
|
|
|
|
|> fetch_actor()
|
|
|
|
end
|
2022-02-08 14:27:53 +00:00
|
|
|
|
|
|
|
@spec remaining_attendee_capacity(map(), integer()) :: integer() | nil
|
|
|
|
defp remaining_attendee_capacity(
|
|
|
|
%{maximum_attendee_capacity: maximum_attendee_capacity},
|
|
|
|
participant_count
|
|
|
|
)
|
|
|
|
when is_integer(maximum_attendee_capacity) and maximum_attendee_capacity > 0 do
|
|
|
|
maximum_attendee_capacity - participant_count
|
|
|
|
end
|
|
|
|
|
|
|
|
defp remaining_attendee_capacity(
|
|
|
|
%{maximum_attendee_capacity: _},
|
|
|
|
_participant_count
|
|
|
|
),
|
|
|
|
do: nil
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|