2020-01-22 22:40:40 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.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
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2022-04-06 14:42:01 +02:00
|
|
|
alias Mobilizon.Events.Categories
|
2019-05-22 14:12:11 +02:00
|
|
|
alias Mobilizon.Events.Event, as: EventModel
|
2021-08-10 11:03:03 +02:00
|
|
|
alias Mobilizon.Medias.Media
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Address, as: AddressConverter
|
2021-08-10 11:03:03 +02:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.EventMetadata, as: EventMetadataConverter
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Media, as: MediaConverter
|
2021-10-12 11:10:16 +02:00
|
|
|
alias Mobilizon.Service.TimezoneDetector
|
2021-07-26 17:19:51 +02:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2021-11-16 15:45:05 +01:00
|
|
|
import Mobilizon.Federation.ActivityPub.Utils, only: [get_url: 1]
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
import Mobilizon.Federation.ActivityStream.Converter.Utils,
|
|
|
|
only: [
|
|
|
|
fetch_tags: 1,
|
|
|
|
fetch_mentions: 1,
|
|
|
|
build_tags: 1,
|
2020-12-16 09:54:56 +01:00
|
|
|
maybe_fetch_actor_and_attributed_to_id: 1,
|
2021-11-06 10:09:07 +01:00
|
|
|
process_pictures: 2,
|
2021-11-16 15:45:05 +01:00
|
|
|
get_address: 1,
|
|
|
|
fetch_actor: 1
|
2020-07-09 17:24:28 +02:00
|
|
|
]
|
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
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Event, as: EventConverter
|
2019-09-22 18:29:13 +02:00
|
|
|
|
|
|
|
defdelegate model_to_as(event), to: EventConverter
|
|
|
|
end
|
|
|
|
|
2020-12-16 09:54:56 +01:00
|
|
|
@online_address_name "Website"
|
|
|
|
@banner_picture_name "Banner"
|
2021-07-29 17:50:23 +02:00
|
|
|
@ap_public "https://www.w3.org/ns/activitystreams#Public"
|
2020-12-16 09:54:56 +01:00
|
|
|
|
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
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec as_to_model_data(map) :: map() | {:error, atom()}
|
2019-05-22 14:12:11 +02:00
|
|
|
def as_to_model_data(object) do
|
2021-09-28 19:40:37 +02:00
|
|
|
case maybe_fetch_actor_and_attributed_to_id(object) do
|
|
|
|
{:ok, %Actor{id: actor_id}, attributed_to} ->
|
2021-10-12 11:10:16 +02:00
|
|
|
address = get_address(object["location"])
|
2021-09-28 19:40:37 +02:00
|
|
|
tags = fetch_tags(object["tag"])
|
|
|
|
mentions = fetch_mentions(object["tag"])
|
|
|
|
visibility = get_visibility(object)
|
2021-10-12 11:10:16 +02:00
|
|
|
options = get_options(object, address)
|
2021-09-28 19:40:37 +02:00
|
|
|
metadata = get_metdata(object)
|
2021-11-16 15:45:05 +01:00
|
|
|
contacts = get_contacts(object)
|
2021-09-28 19:40:37 +02: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 09:32:47 +02:00
|
|
|
category: Categories.get_category(object["category"]),
|
2021-09-28 19:40:37 +02:00
|
|
|
visibility: visibility,
|
|
|
|
join_options: Map.get(object, "joinMode", "free"),
|
2024-01-04 13:35:02 +01:00
|
|
|
local: local?(object["id"]),
|
2021-10-19 15:56:18 +02:00
|
|
|
external_participation_url: object["externalParticipationUrl"],
|
2021-09-28 19:40:37 +02:00
|
|
|
options: options,
|
|
|
|
metadata: metadata,
|
2023-12-04 13:48:04 +01:00
|
|
|
# Remove fallback in MBZ 5.x
|
|
|
|
status:
|
|
|
|
object
|
|
|
|
|> Map.get("status", Map.get(object, "ical:status", "CONFIRMED"))
|
|
|
|
|> String.downcase(),
|
2021-09-28 19:40:37 +02:00
|
|
|
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 11:10:16 +02:00
|
|
|
physical_address_id: if(address, do: address.id, else: nil),
|
2021-09-28 19:40:37 +02:00
|
|
|
updated_at: object["updated"],
|
|
|
|
publish_at: object["published"],
|
2021-11-16 15:45:05 +01:00
|
|
|
language: object["inLanguage"],
|
|
|
|
contacts: contacts
|
2021-09-28 19:40:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
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
|
2021-07-29 17:50:23 +02:00
|
|
|
{to, cc} =
|
2019-09-22 18:29:13 +02:00
|
|
|
if event.visibility == :public,
|
2021-11-19 17:56:48 +01:00
|
|
|
do: {[@ap_public], [event.organizer_actor.followers_url]},
|
2021-07-29 17:50:23 +02:00
|
|
|
else: {[attributed_to_or_default(event).followers_url], [@ap_public]}
|
2019-09-22 18:29:13 +02:00
|
|
|
|
2022-02-08 15:27:53 +01:00
|
|
|
participant_count = Mobilizon.Events.count_participant_participants(event.id)
|
|
|
|
|
2020-09-02 08:59:59 +02:00
|
|
|
%{
|
2019-09-22 18:29:13 +02:00
|
|
|
"type" => "Event",
|
|
|
|
"to" => to,
|
2021-07-29 17:50:23 +02:00
|
|
|
"cc" => cc,
|
2021-02-04 12:28:53 +01:00
|
|
|
"attributedTo" => attributed_to_or_default(event).url,
|
2019-09-22 18:29:13 +02:00
|
|
|
"name" => event.title,
|
2020-09-02 08:59:59 +02:00
|
|
|
"actor" =>
|
|
|
|
if(Ecto.assoc_loaded?(event.organizer_actor), do: event.organizer_actor.url, else: nil),
|
2019-09-22 18:29:13 +02:00
|
|
|
"uuid" => event.uuid,
|
|
|
|
"category" => event.category,
|
|
|
|
"content" => event.description,
|
2019-12-03 11:29:51 +01:00
|
|
|
"published" => (event.publish_at || event.inserted_at) |> date_to_string(),
|
|
|
|
"updated" => event.updated_at |> date_to_string(),
|
2019-09-22 18:29:13 +02:00
|
|
|
"mediaType" => "text/html",
|
2021-10-12 11:10:16 +02:00
|
|
|
"startTime" => event.begins_on |> shift_tz(event.options.timezone) |> date_to_string(),
|
2019-12-03 11:29:51 +01:00
|
|
|
"joinMode" => to_string(event.join_options),
|
2021-10-19 15:56:18 +02:00
|
|
|
"externalParticipationUrl" => event.external_participation_url,
|
2021-10-12 11:10:16 +02:00
|
|
|
"endTime" => event.ends_on |> shift_tz(event.options.timezone) |> date_to_string(),
|
2020-07-09 17:24:28 +02:00
|
|
|
"tag" => event.tags |> build_tags(),
|
2019-12-03 11:29:51 +01:00
|
|
|
"maximumAttendeeCapacity" => event.options.maximum_attendee_capacity,
|
2022-02-08 15:27:53 +01:00
|
|
|
"remainingAttendeeCapacity" =>
|
|
|
|
remaining_attendee_capacity(event.options, participant_count),
|
|
|
|
"participantCount" => participant_count,
|
2019-12-03 11:29:51 +01:00
|
|
|
"repliesModerationOption" => event.options.comment_moderation,
|
2019-12-16 11:46:19 +01:00
|
|
|
"commentsEnabled" => event.options.comment_moderation == :allow_all,
|
2019-12-20 13:04:34 +01:00
|
|
|
"anonymousParticipationEnabled" => event.options.anonymous_participation,
|
2021-08-10 11:03:03 +02:00
|
|
|
"attachment" => Enum.map(event.metadata, &EventMetadataConverter.metadata_to_as/1),
|
2021-03-08 15:58:25 +01:00
|
|
|
"draft" => event.draft,
|
2023-12-04 13:48:04 +01:00
|
|
|
# Remove me in MBZ 5.x
|
2019-12-03 11:29:51 +01:00
|
|
|
"ical:status" => event.status |> to_string |> String.upcase(),
|
2023-12-04 13:48:04 +01:00
|
|
|
"status" => event.status |> to_string |> String.upcase(),
|
2019-09-22 18:29:13 +02:00
|
|
|
"id" => event.url,
|
2021-08-19 20:43:35 +02:00
|
|
|
"url" => event.url,
|
2021-10-12 11:10:16 +02:00
|
|
|
"inLanguage" => event.language,
|
2021-11-16 15:45:05 +01:00
|
|
|
"timezone" => event.options.timezone,
|
2022-02-08 15:27:17 +01:00
|
|
|
"contacts" => Enum.map(event.contacts, & &1.url),
|
|
|
|
"isOnline" => event.options.is_online
|
2019-09-22 18:29:13 +02:00
|
|
|
}
|
2020-09-02 08:59:59 +02:00
|
|
|
|> maybe_add_physical_address(event)
|
|
|
|
|> maybe_add_event_picture(event)
|
|
|
|
|> maybe_add_online_address(event)
|
2020-12-16 09:54:56 +01:00
|
|
|
|> maybe_add_inline_media(event)
|
2019-09-22 18:29:13 +02:00
|
|
|
end
|
|
|
|
|
2021-08-10 11:03:03 +02:00
|
|
|
@spec attributed_to_or_default(EventModel.t()) :: Actor.t()
|
|
|
|
defp attributed_to_or_default(%EventModel{} = event) do
|
2021-02-04 12:28:53 +01: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 11:28:27 +02:00
|
|
|
# Get only elements that we have in EventOptions
|
2021-10-12 11:10:16 +02:00
|
|
|
@spec get_options(map, Address.t() | nil) :: map
|
|
|
|
defp get_options(object, address) do
|
2019-12-03 11:29:51 +01:00
|
|
|
%{
|
|
|
|
maximum_attendee_capacity: object["maximumAttendeeCapacity"],
|
2019-12-20 13:04:34 +01:00
|
|
|
anonymous_participation: object["anonymousParticipationEnabled"],
|
2019-12-16 11:46:19 +01:00
|
|
|
comment_moderation:
|
|
|
|
Map.get(
|
|
|
|
object,
|
|
|
|
"repliesModerationOption",
|
|
|
|
if(Map.get(object, "commentsEnabled", true), do: :allow_all, else: :closed)
|
2021-10-12 11:10:16 +02:00
|
|
|
),
|
2022-02-08 15:27:17 +01:00
|
|
|
timezone: calculate_timezone(object, address),
|
|
|
|
is_online: object["isOnline"] == true
|
2019-12-03 11:29:51 +01:00
|
|
|
}
|
2019-08-28 11:28:27 +02:00
|
|
|
end
|
|
|
|
|
2021-10-12 11:10:16 +02:00
|
|
|
defp calculate_timezone(%{"timezone" => timezone}, %Address{geom: geom}) do
|
|
|
|
TimezoneDetector.detect(
|
|
|
|
timezone,
|
|
|
|
geom,
|
|
|
|
"Etc/UTC"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp calculate_timezone(_object, nil), do: nil
|
|
|
|
|
2023-12-19 10:53:12 +01:00
|
|
|
defp calculate_timezone(_object, %Address{geom: nil}), do: nil
|
|
|
|
|
2021-10-12 11:10:16 +02:00
|
|
|
defp calculate_timezone(_object, %Address{geom: geom}) do
|
|
|
|
TimezoneDetector.detect(
|
|
|
|
nil,
|
|
|
|
geom,
|
|
|
|
"Etc/UTC"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-08-10 11:03:03 +02: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 11:29:51 +01:00
|
|
|
defp get_visibility(object), do: if(@ap_public in object["to"], do: :public, else: :unlisted)
|
2019-09-20 19:43:29 +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)
|
2020-02-07 18:41:36 +01:00
|
|
|
|
2021-10-12 11:10:16 +02: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 18:41:36 +01:00
|
|
|
defp get_online_address(attachments) do
|
2020-06-09 11:13:22 +02:00
|
|
|
Enum.find_value(attachments, fn attachment ->
|
2020-02-07 18:41:36 +01:00
|
|
|
case attachment do
|
|
|
|
%{
|
|
|
|
"type" => "Link",
|
|
|
|
"href" => url,
|
|
|
|
"mediaType" => "text/html",
|
2020-12-16 09:54:56 +01:00
|
|
|
"name" => @online_address_name
|
2020-02-07 18:41:36 +01:00
|
|
|
} ->
|
|
|
|
url
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2020-09-02 08:59:59 +02:00
|
|
|
|
2021-08-10 11:03:03 +02: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 08:59:59 +02:00
|
|
|
end
|
|
|
|
|
2021-08-10 11:03:03 +02: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 08:59:59 +02:00
|
|
|
end
|
|
|
|
|
2021-08-10 11:03:03 +02: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 08:59:59 +02:00
|
|
|
end
|
2020-12-16 09:54:56 +01:00
|
|
|
|
2021-08-10 11:03:03 +02: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 09:54:56 +01:00
|
|
|
|
|
|
|
Map.update(
|
|
|
|
res,
|
|
|
|
"attachment",
|
|
|
|
[],
|
|
|
|
&(&1 ++ medias)
|
|
|
|
)
|
|
|
|
end
|
2021-07-26 17:19:51 +02:00
|
|
|
|
2024-01-04 13:35:02 +01:00
|
|
|
@spec local?(String.t()) :: boolean()
|
|
|
|
defp local?(url) do
|
2021-07-26 17:19:51 +02:00
|
|
|
%URI{host: url_domain} = URI.parse(url)
|
|
|
|
%URI{host: local_domain} = URI.parse(Endpoint.url())
|
|
|
|
url_domain == local_domain
|
|
|
|
end
|
2021-11-16 15:45:05 +01: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 15:27:53 +01: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 14:12:11 +02:00
|
|
|
end
|