From 18c8b7bc14baa286df36abd6962484358c477684 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 7 Feb 2020 18:41:36 +0100 Subject: [PATCH] Fix event online URL AP attachment Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/activity_pub.ex | 1 - .../activity_stream/converter/event.ex | 60 +++++++++++++++++-- lib/graphql/resolvers/event.ex | 3 - .../activity_pub/transmogrifier_test.exs | 2 + test/fixtures/mobilizon-post-activity.json | 15 ++++- 5 files changed, 70 insertions(+), 11 deletions(-) diff --git a/lib/federation/activity_pub/activity_pub.ex b/lib/federation/activity_pub/activity_pub.ex index a3f07d1e5..ba693fe4d 100644 --- a/lib/federation/activity_pub/activity_pub.ex +++ b/lib/federation/activity_pub/activity_pub.ex @@ -881,7 +881,6 @@ defmodule Mobilizon.Federation.ActivityPub do |> Map.merge(%{ "id" => "#{Endpoint.url()}/reject/follow/#{follower.id}" }) do - Logger.error(inspect(update_data)) {:ok, follower, update_data} else err -> diff --git a/lib/federation/activity_stream/converter/event.ex b/lib/federation/activity_stream/converter/event.ex index 489db42a6..6bf200a8f 100644 --- a/lib/federation/activity_stream/converter/event.ex +++ b/lib/federation/activity_stream/converter/event.ex @@ -46,11 +46,16 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do {:mentions, mentions} <- {:mentions, ConverterUtils.fetch_mentions(object["tag"])}, {:visibility, visibility} <- {:visibility, get_visibility(object)}, {:options, options} <- {:options, get_options(object)} do + attachments = + object + |> Map.get("attachment", []) + |> Enum.filter(fn attachment -> Map.get(attachment, "type", "Document") == "Document" end) + picture_id = - with true <- Map.has_key?(object, "attachment") && length(object["attachment"]) > 0, + with true <- length(attachments) > 0, {:ok, %Picture{id: picture_id}} <- - object["attachment"] - |> hd + attachments + |> hd() |> PictureConverter.find_or_create_picture(actor_id) do picture_id else @@ -71,7 +76,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do local: is_nil(actor_domain), options: options, status: object |> Map.get("ical:status", "CONFIRMED") |> String.downcase(), - online_address: object["onlineAddress"], + online_address: object |> Map.get("attachment", []) |> get_online_address(), phone_address: object["phoneAddress"], draft: false, url: object["id"], @@ -122,6 +127,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do "repliesModerationOption" => event.options.comment_moderation, "commentsEnabled" => event.options.comment_moderation == :allow_all, "anonymousParticipationEnabled" => event.options.anonymous_participation, + "attachment" => [], # "draft" => event.draft, "ical:status" => event.status |> to_string |> String.upcase(), "id" => event.url, @@ -133,9 +139,34 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do do: res, else: Map.put(res, "location", AddressConverter.model_to_as(event.physical_address)) - if is_nil(event.picture), + res = + if is_nil(event.picture), + do: res, + else: + Map.update( + res, + "attachment", + [], + &(&1 ++ [PictureConverter.model_to_as(event.picture)]) + ) + + if is_nil(event.online_address), do: res, - else: Map.put(res, "attachment", [PictureConverter.model_to_as(event.picture)]) + else: + Map.update( + res, + "attachment", + [], + &(&1 ++ + [ + %{ + "type" => "Link", + "href" => event.online_address, + "mediaType" => "text/html", + "name" => "Website" + } + ]) + ) end # Get only elements that we have in EventOptions @@ -198,4 +229,21 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do @spec date_to_string(DateTime.t() | nil) :: String.t() defp date_to_string(nil), do: nil defp date_to_string(%DateTime{} = date), do: DateTime.to_iso8601(date) + + defp get_online_address(attachments) do + Enum.find_value(attachments, [], fn attachment -> + case attachment do + %{ + "type" => "Link", + "href" => url, + "mediaType" => "text/html", + "name" => "Website" + } -> + url + + _ -> + nil + end + end) + end end diff --git a/lib/graphql/resolvers/event.ex b/lib/graphql/resolvers/event.ex index addfc1ffa..ee519a51f 100644 --- a/lib/graphql/resolvers/event.ex +++ b/lib/graphql/resolvers/event.ex @@ -46,9 +46,6 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do end def find_event(parent, %{uuid: uuid} = args, %{context: context} = resolution) do - require Logger - Logger.error(inspect(context)) - with {:has_event, %Event{} = event} <- {:has_event, Events.get_public_event_by_uuid_with_preload(uuid)}, {:access_valid, true} <- diff --git a/test/federation/activity_pub/transmogrifier_test.exs b/test/federation/activity_pub/transmogrifier_test.exs index d4961ab70..08c1fae40 100644 --- a/test/federation/activity_pub/transmogrifier_test.exs +++ b/test/federation/activity_pub/transmogrifier_test.exs @@ -70,6 +70,8 @@ defmodule Mobilizon.Federation.ActivityPub.TransmogrifierTest do assert event.physical_address.url == "https://event1.tcit.fr/address/eeecc11d-0030-43e8-a897-6422876372jd" + assert event.online_address == "https://google.com" + {:ok, %Actor{}} = Actors.get_actor_by_url(object["actor"]) end end diff --git a/test/fixtures/mobilizon-post-activity.json b/test/fixtures/mobilizon-post-activity.json index 1cba4dd04..839ecef4b 100644 --- a/test/fixtures/mobilizon-post-activity.json +++ b/test/fixtures/mobilizon-post-activity.json @@ -35,7 +35,20 @@ ], "id": "https://test.mobilizon.org/events/39026210-0c69-4238-b3cc-986f33f98ed0/activity", "object": { - "attachment": [], + "attachment": [ + { + "href": "https://something.org", + "mediaType": "text/html", + "name": "Another link", + "type": "Link" + }, + { + "href": "https://google.com", + "mediaType": "text/html", + "name": "Website", + "type": "Link" + } + ], "attributedTo": "https://test.mobilizon.org/@Alicia", "startTime": "2018-02-12T14:08:20Z", "cc": [