2018-12-24 13:34:45 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2018-12-27 11:24:04 +01:00
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/activity_pub/activity_pub.ex
|
2018-12-24 13:34:45 +01:00
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub do
|
2018-06-14 18:15:27 +02:00
|
|
|
@moduledoc """
|
2020-01-22 02:14:42 +01:00
|
|
|
The ActivityPub context.
|
2018-06-14 18:15:27 +02:00
|
|
|
"""
|
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
import Mobilizon.Federation.ActivityPub.Utils
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.{
|
|
|
|
Actors,
|
2020-07-09 17:24:28 +02:00
|
|
|
Discussions,
|
2020-02-18 08:57:00 +01:00
|
|
|
Events,
|
2020-10-19 09:32:37 +02:00
|
|
|
Posts,
|
2021-09-28 19:40:37 +02:00
|
|
|
Resources
|
2020-02-18 08:57:00 +01:00
|
|
|
}
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions.Comment
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2019-11-15 18:36:47 +01:00
|
|
|
alias Mobilizon.Tombstone
|
2020-01-22 02:14:42 +01:00
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{
|
|
|
|
Activity,
|
2020-07-09 17:24:28 +02:00
|
|
|
Fetcher,
|
|
|
|
Preloader,
|
2021-09-28 19:40:37 +02:00
|
|
|
Relay
|
2020-01-22 02:14:42 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 12:17:56 +02:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Convertible
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Storage.Page
|
2020-01-26 21:11:16 +01:00
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
require Logger
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2020-09-02 08:59:59 +02:00
|
|
|
@public_ap_adress "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
@doc """
|
|
|
|
Fetch an object from an URL, from our local database of events and comments, then eventually remote
|
|
|
|
"""
|
2019-02-21 18:11:49 +01:00
|
|
|
# TODO: Make database calls parallel
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec fetch_object_from_url(String.t(), Keyword.t()) ::
|
2021-09-24 16:46:42 +02:00
|
|
|
{:ok, struct()} | {:ok, atom(), struct()} | {:error, any()}
|
2020-07-09 17:24:28 +02:00
|
|
|
def fetch_object_from_url(url, options \\ []) do
|
2018-12-14 17:41:55 +01:00
|
|
|
Logger.info("Fetching object from url #{url}")
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
if String.starts_with?(url, "http") do
|
|
|
|
with {:existing, nil} <-
|
|
|
|
{:existing, Tombstone.find_tombstone(url)},
|
|
|
|
{:existing, nil} <- {:existing, Events.get_event_by_url(url)},
|
|
|
|
{:existing, nil} <-
|
|
|
|
{:existing, Discussions.get_discussion_by_url(url)},
|
|
|
|
{:existing, nil} <- {:existing, Discussions.get_comment_from_url(url)},
|
|
|
|
{:existing, nil} <- {:existing, Resources.get_resource_by_url(url)},
|
|
|
|
{:existing, nil} <- {:existing, Posts.get_post_by_url(url)},
|
|
|
|
{:existing, nil} <-
|
|
|
|
{:existing, Actors.get_actor_by_url_2(url)},
|
|
|
|
{:existing, nil} <- {:existing, Actors.get_member_by_url(url)},
|
|
|
|
:ok <- Logger.info("Data for URL not found anywhere, going to fetch it"),
|
|
|
|
{:ok, _activity, entity} <- Fetcher.fetch_and_create(url, options) do
|
|
|
|
Logger.debug("Going to preload the new entity")
|
|
|
|
Preloader.maybe_preload(entity)
|
|
|
|
else
|
|
|
|
{:existing, entity} ->
|
|
|
|
handle_existing_entity(url, entity, options)
|
2021-04-28 18:06:17 +02:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
{:error, e} ->
|
2023-08-02 09:59:09 +02:00
|
|
|
Logger.warning("Something failed while fetching url #{url} #{inspect(e)}")
|
2021-09-28 19:40:37 +02:00
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, :url_not_http}
|
2021-04-21 18:57:23 +02:00
|
|
|
end
|
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
@spec handle_existing_entity(String.t(), struct(), Keyword.t()) ::
|
|
|
|
{:ok, struct()}
|
2021-09-24 16:46:42 +02:00
|
|
|
| {:ok, atom(), struct()}
|
2021-04-21 18:57:23 +02:00
|
|
|
| {:error, String.t(), struct()}
|
|
|
|
| {:error, String.t()}
|
|
|
|
defp handle_existing_entity(url, entity, options) do
|
|
|
|
Logger.debug("Entity is already existing")
|
|
|
|
Logger.debug("Going to preload an existing entity")
|
2020-08-27 11:53:24 +02:00
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
case refresh_entity(url, entity, options) do
|
|
|
|
{:ok, entity} ->
|
|
|
|
Preloader.maybe_preload(entity)
|
|
|
|
|
|
|
|
{:error, status, entity} ->
|
|
|
|
{:ok, entity} = Preloader.maybe_preload(entity)
|
|
|
|
{:error, status, entity}
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2021-04-21 18:57:23 +02:00
|
|
|
end
|
|
|
|
end
|
2020-10-19 09:32:37 +02:00
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
@spec refresh_entity(String.t(), struct(), Keyword.t()) ::
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, struct()} | {:error, atom(), struct()} | {:error, atom()}
|
2021-04-21 18:57:23 +02:00
|
|
|
defp refresh_entity(url, entity, options) do
|
|
|
|
force_fetch = Keyword.get(options, :force, false)
|
2019-07-30 10:35:29 +02:00
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
if force_fetch and not are_same_origin?(url, Endpoint.url()) do
|
|
|
|
Logger.debug("Entity is external and we want a force fetch")
|
2020-09-02 08:59:59 +02:00
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
case Fetcher.fetch_and_update(url, options) do
|
|
|
|
{:ok, _activity, entity} ->
|
|
|
|
{:ok, entity}
|
2020-09-02 08:59:59 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
{:error, :http_gone} ->
|
|
|
|
{:error, :http_gone, entity}
|
2019-07-30 10:35:29 +02:00
|
|
|
|
2021-09-10 11:36:05 +02:00
|
|
|
{:error, :http_not_found} ->
|
|
|
|
{:error, :http_not_found, entity}
|
2019-12-03 11:29:51 +01:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
2018-11-12 09:05:31 +01:00
|
|
|
else
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, entity}
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
@doc """
|
|
|
|
Return all public activities (events & comments) for an actor
|
|
|
|
"""
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec fetch_public_activities_for_actor(Actor.t(), pos_integer(), pos_integer()) :: map()
|
2020-08-31 17:26:08 +02:00
|
|
|
def fetch_public_activities_for_actor(%Actor{id: actor_id} = actor, page \\ 1, limit \\ 10) do
|
|
|
|
%Actor{id: relay_actor_id} = Relay.get_actor()
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
%Page{total: total_events, elements: events} =
|
2020-08-31 17:26:08 +02:00
|
|
|
if actor_id == relay_actor_id do
|
|
|
|
Events.list_public_local_events(page, limit)
|
|
|
|
else
|
|
|
|
Events.list_public_events_for_actor(actor, page, limit)
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
%Page{total: total_comments, elements: comments} =
|
2020-08-31 17:26:08 +02:00
|
|
|
if actor_id == relay_actor_id do
|
|
|
|
Discussions.list_local_comments(page, limit)
|
|
|
|
else
|
|
|
|
Discussions.list_public_comments_for_actor(actor, page, limit)
|
|
|
|
end
|
2018-12-14 11:23:36 +01:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
event_activities = Enum.map(events, &event_to_activity/1)
|
|
|
|
comment_activities = Enum.map(comments, &comment_to_activity/1)
|
|
|
|
activities = event_activities ++ comment_activities
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
%{elements: activities, total: total_events + total_comments}
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
# Create an activity from an event
|
2021-11-26 14:30:46 +01:00
|
|
|
@spec event_to_activity(Event.t(), boolean()) :: Activity.t()
|
2018-05-30 14:27:21 +02:00
|
|
|
defp event_to_activity(%Event{} = event, local \\ true) do
|
2018-11-12 18:17:53 +01:00
|
|
|
%Activity{
|
2020-09-02 08:59:59 +02:00
|
|
|
recipients: [@public_ap_adress],
|
2018-05-18 09:56:21 +02:00
|
|
|
actor: event.organizer_actor.url,
|
2020-09-02 08:59:59 +02:00
|
|
|
data: event |> Convertible.model_to_as() |> make_create_data(%{"to" => @public_ap_adress}),
|
2018-11-12 18:17:53 +01:00
|
|
|
local: local
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
end
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
# Create an activity from a comment
|
2021-11-26 14:30:46 +01:00
|
|
|
@spec comment_to_activity(Comment.t(), boolean()) :: Activity.t()
|
2019-02-22 16:11:57 +01:00
|
|
|
defp comment_to_activity(%Comment{} = comment, local \\ true) do
|
2018-11-12 18:17:53 +01:00
|
|
|
%Activity{
|
2020-09-02 08:59:59 +02:00
|
|
|
recipients: [@public_ap_adress],
|
2018-08-24 11:34:00 +02:00
|
|
|
actor: comment.actor.url,
|
2020-09-02 08:59:59 +02:00
|
|
|
data:
|
|
|
|
comment |> Convertible.model_to_as() |> make_create_data(%{"to" => @public_ap_adress}),
|
2018-11-12 18:17:53 +01:00
|
|
|
local: local
|
2018-08-24 11:34:00 +02:00
|
|
|
}
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|