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,
|
|
|
|
|
Config,
|
2020-07-09 17:24:28 +02:00
|
|
|
|
Discussions,
|
2020-02-18 08:57:00 +01:00
|
|
|
|
Events,
|
|
|
|
|
Resources,
|
|
|
|
|
Share,
|
|
|
|
|
Users
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.{Actor, Follower, Member}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
alias Mobilizon.Discussions.Comment
|
2020-02-18 08:57:00 +01:00
|
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
2019-11-15 18:36:47 +01:00
|
|
|
|
alias Mobilizon.Tombstone
|
2020-01-22 02:14:42 +01:00
|
|
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{
|
|
|
|
|
Activity,
|
|
|
|
|
Audience,
|
|
|
|
|
Federator,
|
2020-07-09 17:24:28 +02:00
|
|
|
|
Fetcher,
|
|
|
|
|
Preloader,
|
2020-01-22 02:14:42 +01:00
|
|
|
|
Relay,
|
|
|
|
|
Transmogrifier,
|
2020-07-09 17:24:28 +02:00
|
|
|
|
Types,
|
2020-01-22 22:40:40 +01:00
|
|
|
|
Visibility
|
2020-01-22 02:14:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Types.{Managable, Ownable}
|
|
|
|
|
|
2020-01-22 22:40:40 +01:00
|
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
2020-01-22 02:14:42 +01:00
|
|
|
|
alias Mobilizon.Federation.HTTPSignatures.Signature
|
2020-01-22 22:40:40 +01:00
|
|
|
|
alias Mobilizon.Federation.WebFinger
|
2020-01-22 02:14:42 +01:00
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
alias Mobilizon.Service.Notifications.Scheduler
|
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
|
2020-08-14 11:32:23 +02:00
|
|
|
|
alias Mobilizon.Web.Email.{Admin, Group, Mailer}
|
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
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Wraps an object into an activity
|
|
|
|
|
"""
|
2020-02-18 08:57:00 +01:00
|
|
|
|
@spec create_activity(map(), boolean()) :: {:ok, Activity.t()}
|
2019-09-03 08:38:04 +02:00
|
|
|
|
def create_activity(map, local \\ true) when is_map(map) do
|
|
|
|
|
with map <- lazy_put_activity_defaults(map) do
|
2019-09-04 18:24:31 +02:00
|
|
|
|
{:ok,
|
|
|
|
|
%Activity{
|
|
|
|
|
data: map,
|
|
|
|
|
local: local,
|
|
|
|
|
actor: map["actor"],
|
|
|
|
|
recipients: get_recipients(map)
|
|
|
|
|
}}
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
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()) ::
|
|
|
|
|
{:ok, struct()} | {:error, any()}
|
|
|
|
|
def fetch_object_from_url(url, options \\ []) do
|
2018-12-14 17:41:55 +01:00
|
|
|
|
Logger.info("Fetching object from url #{url}")
|
2020-07-09 17:24:28 +02:00
|
|
|
|
force_fetch = Keyword.get(options, :force, false)
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
|
with {:not_http, true} <- {:not_http, String.starts_with?(url, "http")},
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{: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, Actors.get_actor_by_url_2(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)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
else
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:existing, entity} ->
|
|
|
|
|
Logger.debug("Entity is already existing")
|
|
|
|
|
|
|
|
|
|
entity =
|
2020-07-30 18:16:32 +02:00
|
|
|
|
if force_fetch and not are_same_origin?(url, Endpoint.url()) do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
Logger.debug("Entity is external and we want a force fetch")
|
|
|
|
|
|
|
|
|
|
with {:ok, _activity, entity} <- Fetcher.fetch_and_update(url, options) do
|
|
|
|
|
entity
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
entity
|
|
|
|
|
end
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
Logger.debug("Going to preload an existing entity")
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
Preloader.maybe_preload(entity)
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
|
e ->
|
2020-02-18 08:57:00 +01:00
|
|
|
|
Logger.warn("Something failed while fetching url #{inspect(e)}")
|
2019-07-30 10:35:29 +02:00
|
|
|
|
{:error, e}
|
2018-08-24 11:34:00 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
|
@doc """
|
2020-02-14 17:56:36 +01:00
|
|
|
|
Getting an actor from url, eventually creating it if we don't have it locally or if it needs an update
|
2019-09-09 00:52:49 +02:00
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
|
@spec get_or_fetch_actor_by_url(String.t(), boolean) :: {:ok, Actor.t()} | {:error, String.t()}
|
2019-12-17 13:36:45 +01:00
|
|
|
|
def get_or_fetch_actor_by_url(url, preload \\ false)
|
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
def get_or_fetch_actor_by_url(nil, _preload), do: {:error, "Can't fetch a nil url"}
|
|
|
|
|
|
2019-12-17 13:36:45 +01:00
|
|
|
|
def get_or_fetch_actor_by_url("https://www.w3.org/ns/activitystreams#Public", _preload) do
|
|
|
|
|
with %Actor{url: url} <- Relay.get_actor() do
|
|
|
|
|
get_or_fetch_actor_by_url(url)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-06-15 11:13:20 +02:00
|
|
|
|
@spec get_or_fetch_actor_by_url(String.t(), boolean()) :: {:ok, Actor.t()} | {:error, any()}
|
2019-12-17 13:36:45 +01:00
|
|
|
|
def get_or_fetch_actor_by_url(url, preload) do
|
2020-02-14 17:56:36 +01:00
|
|
|
|
with {:ok, %Actor{} = cached_actor} <- Actors.get_actor_by_url(url, preload),
|
|
|
|
|
false <- Actors.needs_update?(cached_actor) do
|
|
|
|
|
{:ok, cached_actor}
|
|
|
|
|
else
|
2019-09-09 00:52:49 +02:00
|
|
|
|
_ ->
|
2020-02-14 17:56:36 +01:00
|
|
|
|
# For tests, see https://github.com/jjh42/mock#not-supported---mocking-internal-function-calls and Mobilizon.Federation.ActivityPubTest
|
|
|
|
|
case __MODULE__.make_actor_from_url(url, preload) do
|
2019-09-09 00:52:49 +02:00
|
|
|
|
{:ok, %Actor{} = actor} ->
|
|
|
|
|
{:ok, actor}
|
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
|
err ->
|
2019-09-09 00:52:49 +02:00
|
|
|
|
Logger.warn("Could not fetch by AP id")
|
2019-12-03 11:29:51 +01:00
|
|
|
|
Logger.debug(inspect(err))
|
2019-09-09 00:52:49 +02:00
|
|
|
|
{:error, "Could not fetch by AP id"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-02-22 16:11:57 +01:00
|
|
|
|
@doc """
|
2019-10-25 17:43:37 +02:00
|
|
|
|
Create an activity of type `Create`
|
|
|
|
|
|
2020-06-04 10:58:27 +02:00
|
|
|
|
* Creates the object, which returns AS data
|
|
|
|
|
* Wraps ActivityStreams data into a `Create` activity
|
|
|
|
|
* Creates an `Mobilizon.Federation.ActivityPub.Activity` from this
|
|
|
|
|
* Federates (asynchronously) the activity
|
|
|
|
|
* Returns the activity
|
2019-02-22 16:11:57 +01:00
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
|
@spec create(atom(), map(), boolean, map()) :: {:ok, Activity.t(), struct()} | any()
|
|
|
|
|
def create(type, args, local \\ false, additional \\ %{}) do
|
2018-11-12 09:05:31 +01:00
|
|
|
|
Logger.debug("creating an activity")
|
2019-10-25 17:43:37 +02:00
|
|
|
|
Logger.debug(inspect(args))
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
|
with {:tombstone, nil} <- {:tombstone, check_for_tombstones(args)},
|
|
|
|
|
{:ok, entity, create_data} <-
|
2019-11-18 18:40:03 +01:00
|
|
|
|
(case type do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
:event -> Types.Events.create(args, additional)
|
|
|
|
|
:comment -> Types.Comments.create(args, additional)
|
|
|
|
|
:discussion -> Types.Discussions.create(args, additional)
|
|
|
|
|
:actor -> Types.Actors.create(args, additional)
|
|
|
|
|
:todo_list -> Types.TodoLists.create(args, additional)
|
|
|
|
|
:todo -> Types.Todos.create(args, additional)
|
|
|
|
|
:resource -> Types.Resources.create(args, additional)
|
|
|
|
|
:post -> Types.Posts.create(args, additional)
|
2019-11-18 18:40:03 +01:00
|
|
|
|
end),
|
|
|
|
|
{:ok, activity} <- create_activity(create_data, local),
|
2020-07-09 17:24:28 +02:00
|
|
|
|
:ok <- maybe_federate(activity),
|
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity) do
|
2019-10-25 17:43:37 +02:00
|
|
|
|
{:ok, activity, entity}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
else
|
|
|
|
|
err ->
|
2019-07-30 16:40:59 +02:00
|
|
|
|
Logger.error("Something went wrong while creating an activity")
|
2018-12-14 17:41:55 +01:00
|
|
|
|
err
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
|
@doc """
|
|
|
|
|
Create an activity of type `Update`
|
2019-07-30 16:40:59 +02:00
|
|
|
|
|
2020-06-04 10:58:27 +02:00
|
|
|
|
* Updates the object, which returns AS data
|
|
|
|
|
* Wraps ActivityStreams data into a `Update` activity
|
|
|
|
|
* Creates an `Mobilizon.Federation.ActivityPub.Activity` from this
|
|
|
|
|
* Federates (asynchronously) the activity
|
|
|
|
|
* Returns the activity
|
2019-10-25 17:43:37 +02:00
|
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
|
@spec update(struct(), map(), boolean, map()) :: {:ok, Activity.t(), struct()} | any()
|
|
|
|
|
def update(old_entity, args, local \\ false, additional \\ %{}) do
|
2019-10-25 17:43:37 +02:00
|
|
|
|
Logger.debug("updating an activity")
|
|
|
|
|
Logger.debug(inspect(args))
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
with {:ok, entity, update_data} <- Managable.update(old_entity, args, additional),
|
2019-11-18 18:40:03 +01:00
|
|
|
|
{:ok, activity} <- create_activity(update_data, local),
|
2020-07-09 17:24:28 +02:00
|
|
|
|
:ok <- maybe_federate(activity),
|
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity) do
|
2019-10-25 17:43:37 +02:00
|
|
|
|
{:ok, activity, entity}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
2019-07-30 16:40:59 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
|
def accept(type, entity, local \\ true, additional \\ %{}) do
|
|
|
|
|
Logger.debug("We're accepting something")
|
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
|
{:ok, entity, update_data} =
|
|
|
|
|
case type do
|
2019-12-03 11:29:51 +01:00
|
|
|
|
:join -> accept_join(entity, additional)
|
|
|
|
|
:follow -> accept_follow(entity, additional)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
:invite -> accept_invite(entity, additional)
|
2019-10-25 17:43:37 +02:00
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
|
with {:ok, activity} <- create_activity(update_data, local),
|
2020-08-14 11:32:23 +02:00
|
|
|
|
:ok <- maybe_federate(activity),
|
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity) do
|
2019-10-25 17:43:37 +02:00
|
|
|
|
{:ok, activity, entity}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
|
def reject(type, entity, local \\ true, additional \\ %{}) do
|
|
|
|
|
{:ok, entity, update_data} =
|
|
|
|
|
case type do
|
|
|
|
|
:join -> reject_join(entity, additional)
|
|
|
|
|
:follow -> reject_follow(entity, additional)
|
2020-08-14 11:32:23 +02:00
|
|
|
|
:invite -> reject_invite(entity, additional)
|
2019-12-03 11:29:51 +01:00
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
|
with {:ok, activity} <- create_activity(update_data, local),
|
2020-08-14 11:32:23 +02:00
|
|
|
|
:ok <- maybe_federate(activity),
|
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity) do
|
2019-12-03 11:29:51 +01:00
|
|
|
|
{:ok, activity, entity}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
|
def announce(
|
|
|
|
|
%Actor{} = actor,
|
|
|
|
|
object,
|
|
|
|
|
activity_id \\ nil,
|
|
|
|
|
local \\ true,
|
|
|
|
|
public \\ true
|
|
|
|
|
) do
|
2020-06-15 11:13:20 +02:00
|
|
|
|
with {:ok, %Actor{id: object_owner_actor_id}} <- get_or_fetch_actor_by_url(object["actor"]),
|
2019-12-03 11:29:51 +01:00
|
|
|
|
{:ok, %Share{} = _share} <- Share.create(object["id"], actor.id, object_owner_actor_id),
|
2019-07-30 16:40:59 +02:00
|
|
|
|
announce_data <- make_announce_data(actor, object, activity_id, public),
|
2019-09-03 08:38:04 +02:00
|
|
|
|
{:ok, activity} <- create_activity(announce_data, local),
|
2019-07-30 16:40:59 +02:00
|
|
|
|
:ok <- maybe_federate(activity) do
|
|
|
|
|
{:ok, activity, object}
|
|
|
|
|
else
|
|
|
|
|
error ->
|
|
|
|
|
{:error, error}
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
|
def unannounce(
|
|
|
|
|
%Actor{} = actor,
|
|
|
|
|
object,
|
|
|
|
|
activity_id \\ nil,
|
|
|
|
|
cancelled_activity_id \\ nil,
|
|
|
|
|
local \\ true
|
|
|
|
|
) do
|
|
|
|
|
with announce_activity <- make_announce_data(actor, object, cancelled_activity_id),
|
|
|
|
|
unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
|
2019-09-03 08:38:04 +02:00
|
|
|
|
{:ok, unannounce_activity} <- create_activity(unannounce_data, local),
|
2019-07-30 16:40:59 +02:00
|
|
|
|
:ok <- maybe_federate(unannounce_activity) do
|
|
|
|
|
{:ok, unannounce_activity, object}
|
|
|
|
|
else
|
|
|
|
|
_e -> {:ok, object}
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
2019-02-22 16:11:57 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Make an actor follow another
|
|
|
|
|
"""
|
2018-12-14 17:41:55 +01:00
|
|
|
|
def follow(%Actor{} = follower, %Actor{} = followed, activity_id \\ nil, local \\ true) do
|
2019-10-25 17:43:37 +02:00
|
|
|
|
with {:ok, %Follower{} = follower} <-
|
2019-09-09 00:52:49 +02:00
|
|
|
|
Actors.follow(followed, follower, activity_id, false),
|
2019-10-25 17:43:37 +02:00
|
|
|
|
follower_as_data <- Convertible.model_to_as(follower),
|
|
|
|
|
{:ok, activity} <- create_activity(follower_as_data, local),
|
2018-05-17 11:32:23 +02:00
|
|
|
|
:ok <- maybe_federate(activity) do
|
2019-10-25 17:43:37 +02:00
|
|
|
|
{:ok, activity, follower}
|
2018-11-12 09:05:31 +01:00
|
|
|
|
else
|
2019-07-30 16:40:59 +02:00
|
|
|
|
{:error, err, msg} when err in [:already_following, :suspended] ->
|
|
|
|
|
{:error, msg}
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-02-22 16:11:57 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Make an actor unfollow another
|
|
|
|
|
"""
|
2018-12-14 17:41:55 +01:00
|
|
|
|
@spec unfollow(Actor.t(), Actor.t(), String.t(), boolean()) :: {:ok, map()} | any()
|
2019-07-30 16:40:59 +02:00
|
|
|
|
def unfollow(%Actor{} = follower, %Actor{} = followed, activity_id \\ nil, local \\ true) do
|
2019-10-25 17:43:37 +02:00
|
|
|
|
with {:ok, %Follower{id: follow_id} = follow} <- Actors.unfollow(followed, follower),
|
2018-12-14 17:41:55 +01:00
|
|
|
|
# We recreate the follow activity
|
2019-10-25 17:43:37 +02:00
|
|
|
|
follow_as_data <-
|
|
|
|
|
Convertible.model_to_as(%{follow | actor: follower, target_actor: followed}),
|
|
|
|
|
{:ok, follow_activity} <- create_activity(follow_as_data, local),
|
2019-07-30 16:40:59 +02:00
|
|
|
|
activity_unfollow_id <-
|
2020-01-28 19:18:33 +01:00
|
|
|
|
activity_id || "#{Endpoint.url()}/unfollow/#{follow_id}/activity",
|
2019-07-30 16:40:59 +02:00
|
|
|
|
unfollow_data <-
|
|
|
|
|
make_unfollow_data(follower, followed, follow_activity, activity_unfollow_id),
|
2019-09-03 08:38:04 +02:00
|
|
|
|
{:ok, activity} <- create_activity(unfollow_data, local),
|
2018-12-14 17:41:55 +01:00
|
|
|
|
:ok <- maybe_federate(activity) do
|
2019-12-03 11:29:51 +01:00
|
|
|
|
{:ok, activity, follow}
|
2018-12-14 17:41:55 +01:00
|
|
|
|
else
|
|
|
|
|
err ->
|
2019-07-30 16:40:59 +02:00
|
|
|
|
Logger.debug("Error while unfollowing an actor #{inspect(err)}")
|
2018-12-14 17:41:55 +01:00
|
|
|
|
err
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
def delete(object, actor, local \\ true) do
|
|
|
|
|
with {:ok, activity_data, actor, object} <-
|
|
|
|
|
Managable.delete(object, actor, local),
|
|
|
|
|
group <- Ownable.group_actor(object),
|
2020-02-14 17:56:36 +01:00
|
|
|
|
:ok <- check_for_actor_key_rotation(actor),
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:ok, activity} <- create_activity(activity_data, local),
|
|
|
|
|
:ok <- maybe_federate(activity),
|
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity, group) do
|
|
|
|
|
{:ok, activity, object}
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
def join(%Event{} = event, %Actor{} = actor, local \\ true, additional \\ %{}) do
|
|
|
|
|
with {:ok, activity_data, participant} <- Types.Events.join(event, actor, local, additional),
|
|
|
|
|
{:ok, activity} <- create_activity(activity_data, local),
|
2018-08-24 11:34:00 +02:00
|
|
|
|
:ok <- maybe_federate(activity) do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:ok, activity, participant}
|
|
|
|
|
else
|
|
|
|
|
{:maximum_attendee_capacity, err} ->
|
|
|
|
|
{:maximum_attendee_capacity, err}
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:accept, accept} ->
|
|
|
|
|
accept
|
2018-12-14 17:41:55 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
def join_group(
|
|
|
|
|
%{parent_id: parent_id, actor_id: actor_id, role: role},
|
|
|
|
|
local \\ true,
|
|
|
|
|
additional \\ %{}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
) do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
with {:ok, %Member{} = member} <-
|
|
|
|
|
Mobilizon.Actors.create_member(%{
|
|
|
|
|
parent_id: parent_id,
|
|
|
|
|
actor_id: actor_id,
|
|
|
|
|
role: role
|
2019-08-14 17:45:11 +02:00
|
|
|
|
}),
|
2020-07-09 17:24:28 +02:00
|
|
|
|
activity_data when is_map(activity_data) <-
|
|
|
|
|
Convertible.model_to_as(member),
|
|
|
|
|
{:ok, activity} <- create_activity(Map.merge(activity_data, additional), local),
|
2019-08-14 17:45:11 +02:00
|
|
|
|
:ok <- maybe_federate(activity) do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:ok, activity, member}
|
2019-12-03 11:29:51 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
|
def leave(object, actor, local \\ true, additional \\ %{})
|
2019-08-14 17:45:11 +02:00
|
|
|
|
|
2020-08-14 11:32:23 +02:00
|
|
|
|
@doc """
|
|
|
|
|
Leave an event
|
|
|
|
|
"""
|
2019-08-14 17:45:11 +02:00
|
|
|
|
def leave(
|
2019-12-03 11:29:51 +01:00
|
|
|
|
%Event{id: event_id, url: event_url} = _event,
|
2019-08-14 17:45:11 +02:00
|
|
|
|
%Actor{id: actor_id, url: actor_url} = _actor,
|
2019-12-20 13:04:34 +01:00
|
|
|
|
local,
|
|
|
|
|
additional
|
2019-08-14 17:45:11 +02:00
|
|
|
|
) do
|
|
|
|
|
with {:only_organizer, false} <-
|
2019-09-13 01:01:17 +02:00
|
|
|
|
{:only_organizer, Participant.is_not_only_organizer(event_id, actor_id)},
|
2019-08-14 17:45:11 +02:00
|
|
|
|
{:ok, %Participant{} = participant} <-
|
2019-12-20 13:04:34 +01:00
|
|
|
|
Mobilizon.Events.get_participant(
|
|
|
|
|
event_id,
|
|
|
|
|
actor_id,
|
|
|
|
|
Map.get(additional, :metadata, %{})
|
|
|
|
|
),
|
2019-09-30 13:48:47 +02:00
|
|
|
|
{:ok, %Participant{} = participant} <-
|
|
|
|
|
Events.delete_participant(participant),
|
2019-08-14 17:45:11 +02:00
|
|
|
|
leave_data <- %{
|
|
|
|
|
"type" => "Leave",
|
|
|
|
|
# If it's an exclusion it should be something else
|
|
|
|
|
"actor" => actor_url,
|
|
|
|
|
"object" => event_url,
|
2020-01-28 19:18:33 +01:00
|
|
|
|
"id" => "#{Endpoint.url()}/leave/event/#{participant.id}"
|
2019-08-14 17:45:11 +02:00
|
|
|
|
},
|
2019-12-03 11:29:51 +01:00
|
|
|
|
audience <-
|
|
|
|
|
Audience.calculate_to_and_cc_from_mentions(participant),
|
|
|
|
|
{:ok, activity} <- create_activity(Map.merge(leave_data, audience), local),
|
2019-08-14 17:45:11 +02:00
|
|
|
|
:ok <- maybe_federate(activity) do
|
|
|
|
|
{:ok, activity, participant}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-08-14 11:32:23 +02:00
|
|
|
|
@doc """
|
|
|
|
|
Leave a group
|
|
|
|
|
"""
|
|
|
|
|
def leave(
|
|
|
|
|
%Actor{type: :Group, id: group_id, url: group_url, members_url: group_members_url},
|
|
|
|
|
%Actor{id: actor_id, url: actor_url},
|
|
|
|
|
local,
|
|
|
|
|
_additional
|
|
|
|
|
) do
|
|
|
|
|
with {:member, {:ok, %Member{id: member_id} = member}} <-
|
|
|
|
|
{:member, Actors.get_member(actor_id, group_id)},
|
|
|
|
|
{:is_only_admin, false} <-
|
|
|
|
|
{:is_only_admin, Actors.is_only_administrator?(member_id, group_id)},
|
|
|
|
|
{:delete, {:ok, %Member{} = member}} <- {:delete, Actors.delete_member(member)},
|
|
|
|
|
leave_data <- %{
|
|
|
|
|
"to" => [group_members_url],
|
|
|
|
|
"cc" => [group_url],
|
|
|
|
|
"attributedTo" => group_url,
|
|
|
|
|
"type" => "Leave",
|
|
|
|
|
"actor" => actor_url,
|
|
|
|
|
"object" => group_url
|
|
|
|
|
},
|
|
|
|
|
{:ok, activity} <- create_activity(leave_data, local),
|
|
|
|
|
:ok <- maybe_federate(activity),
|
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity) do
|
|
|
|
|
{:ok, activity, member}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def remove(
|
|
|
|
|
%Member{} = member,
|
|
|
|
|
%Actor{type: :Group, url: group_url, members_url: group_members_url},
|
|
|
|
|
%Actor{url: moderator_url},
|
|
|
|
|
local,
|
|
|
|
|
_additional \\ %{}
|
|
|
|
|
) do
|
|
|
|
|
with {:ok, %Member{id: member_id}} <- Actors.update_member(member, %{role: :rejected}),
|
|
|
|
|
%Member{} = member <- Actors.get_member(member_id),
|
|
|
|
|
:ok <- Group.send_notification_to_removed_member(member),
|
|
|
|
|
remove_data <- %{
|
|
|
|
|
"to" => [group_members_url],
|
|
|
|
|
"type" => "Remove",
|
|
|
|
|
"actor" => moderator_url,
|
|
|
|
|
"object" => member.actor.url,
|
|
|
|
|
"origin" => group_url
|
|
|
|
|
},
|
|
|
|
|
{:ok, activity} <- create_activity(remove_data, local),
|
|
|
|
|
:ok <- maybe_federate(activity),
|
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity) do
|
|
|
|
|
{:ok, activity, member}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
@spec invite(Actor.t(), Actor.t(), Actor.t(), boolean, map()) ::
|
|
|
|
|
{:ok, map(), Member.t()} | {:error, :member_not_found}
|
|
|
|
|
def invite(
|
2020-08-14 11:32:23 +02:00
|
|
|
|
%Actor{url: group_url, id: group_id, members_url: members_url} = group,
|
2020-02-18 08:57:00 +01:00
|
|
|
|
%Actor{url: actor_url, id: actor_id} = actor,
|
|
|
|
|
%Actor{url: target_actor_url, id: target_actor_id} = _target_actor,
|
|
|
|
|
local \\ true,
|
|
|
|
|
additional \\ %{}
|
|
|
|
|
) do
|
|
|
|
|
Logger.debug("Handling #{actor_url} invite to #{group_url} sent to #{target_actor_url}")
|
|
|
|
|
|
|
|
|
|
with {:is_able_to_invite, true} <- {:is_able_to_invite, is_able_to_invite(actor, group)},
|
|
|
|
|
{:ok, %Member{url: member_url} = member} <-
|
|
|
|
|
Actors.create_member(%{
|
|
|
|
|
parent_id: group_id,
|
|
|
|
|
actor_id: target_actor_id,
|
|
|
|
|
role: :invited,
|
|
|
|
|
invited_by_id: actor_id,
|
|
|
|
|
url: Map.get(additional, :url)
|
|
|
|
|
}),
|
|
|
|
|
invite_data <- %{
|
|
|
|
|
"type" => "Invite",
|
2020-08-14 11:32:23 +02:00
|
|
|
|
"attributedTo" => group_url,
|
2020-02-18 08:57:00 +01:00
|
|
|
|
"actor" => actor_url,
|
|
|
|
|
"object" => group_url,
|
|
|
|
|
"target" => target_actor_url,
|
|
|
|
|
"id" => member_url
|
|
|
|
|
},
|
|
|
|
|
{:ok, activity} <-
|
|
|
|
|
create_activity(
|
|
|
|
|
invite_data
|
2020-08-14 11:32:23 +02:00
|
|
|
|
|> Map.merge(%{"to" => [target_actor_url, members_url], "cc" => [group_url]})
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|> Map.merge(additional),
|
|
|
|
|
local
|
|
|
|
|
),
|
2020-08-14 11:32:23 +02:00
|
|
|
|
:ok <- maybe_federate(activity),
|
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity) do
|
2020-02-18 08:57:00 +01:00
|
|
|
|
{:ok, activity, member}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp is_able_to_invite(%Actor{domain: actor_domain, id: actor_id}, %Actor{
|
|
|
|
|
domain: group_domain,
|
|
|
|
|
id: group_id
|
|
|
|
|
}) do
|
|
|
|
|
# If the actor comes from the same domain we trust it
|
|
|
|
|
if actor_domain == group_domain do
|
|
|
|
|
true
|
|
|
|
|
else
|
|
|
|
|
# If local group, we'll send the invite
|
|
|
|
|
with {:ok, %Member{} = admin_member} <- Actors.get_member(actor_id, group_id) do
|
|
|
|
|
Member.is_administrator(admin_member)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def move(type, old_entity, args, local \\ false, additional \\ %{}) do
|
|
|
|
|
Logger.debug("We're moving something")
|
|
|
|
|
Logger.debug(inspect(args))
|
|
|
|
|
|
|
|
|
|
with {:ok, entity, update_data} <-
|
|
|
|
|
(case type do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
:resource -> Types.Resources.move(old_entity, args, additional)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
end),
|
|
|
|
|
{:ok, activity} <- create_activity(update_data, local),
|
|
|
|
|
:ok <- maybe_federate(activity) do
|
|
|
|
|
{:ok, activity, entity}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating a Move activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
def flag(args, local \\ false, additional \\ %{}) do
|
|
|
|
|
with {report, report_as_data} <- Types.Reports.flag(args, local, additional),
|
|
|
|
|
{:ok, activity} <- create_activity(report_as_data, local),
|
|
|
|
|
:ok <- maybe_federate(activity) do
|
|
|
|
|
Enum.each(Users.list_moderators(), fn moderator ->
|
|
|
|
|
moderator
|
|
|
|
|
|> Admin.report(report)
|
|
|
|
|
|> Mailer.deliver_later()
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
{:ok, activity, report}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
|
@doc """
|
2019-10-15 21:18:03 +02:00
|
|
|
|
Create an actor locally by its URL (AP ID)
|
2018-11-12 18:17:53 +01:00
|
|
|
|
"""
|
|
|
|
|
@spec make_actor_from_url(String.t(), boolean()) :: {:ok, %Actor{}} | {:error, any()}
|
2018-11-12 09:05:31 +01:00
|
|
|
|
def make_actor_from_url(url, preload \\ false) do
|
2020-07-30 18:16:32 +02:00
|
|
|
|
if are_same_origin?(url, Endpoint.url()) do
|
|
|
|
|
{:error, "Can't make a local actor from URL"}
|
|
|
|
|
else
|
|
|
|
|
case fetch_and_prepare_actor_from_url(url) do
|
|
|
|
|
{:ok, data} ->
|
|
|
|
|
Actors.upsert_actor(data, preload)
|
|
|
|
|
|
|
|
|
|
# Request returned 410
|
|
|
|
|
{:error, :actor_deleted} ->
|
|
|
|
|
Logger.info("Actor was deleted")
|
|
|
|
|
{:error, :actor_deleted}
|
|
|
|
|
|
|
|
|
|
e ->
|
|
|
|
|
Logger.warn("Failed to make actor from url")
|
|
|
|
|
{:error, e}
|
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
|
@doc """
|
2019-02-22 16:11:57 +01:00
|
|
|
|
Find an actor in our local database or call WebFinger to find what's its AP ID is and then fetch it
|
2018-11-12 18:17:53 +01:00
|
|
|
|
"""
|
2018-11-23 15:03:53 +01:00
|
|
|
|
@spec find_or_make_actor_from_nickname(String.t(), atom() | nil) :: tuple()
|
|
|
|
|
def find_or_make_actor_from_nickname(nickname, type \\ nil) do
|
2019-07-23 18:06:22 +02:00
|
|
|
|
case Actors.get_actor_by_name(nickname, type) do
|
|
|
|
|
%Actor{} = actor ->
|
|
|
|
|
{:ok, actor}
|
|
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
|
make_actor_from_nickname(nickname)
|
2018-05-30 14:27:21 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
|
@spec find_or_make_person_from_nickname(String.t()) :: tuple()
|
|
|
|
|
def find_or_make_person_from_nickname(nick), do: find_or_make_actor_from_nickname(nick, :Person)
|
|
|
|
|
|
|
|
|
|
@spec find_or_make_group_from_nickname(String.t()) :: tuple()
|
|
|
|
|
def find_or_make_group_from_nickname(nick), do: find_or_make_actor_from_nickname(nick, :Group)
|
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
|
@doc """
|
2019-10-15 21:18:03 +02:00
|
|
|
|
Create an actor inside our database from username, using WebFinger to find out its AP ID and then fetch it
|
2018-11-12 18:17:53 +01:00
|
|
|
|
"""
|
|
|
|
|
@spec make_actor_from_nickname(String.t()) :: {:ok, %Actor{}} | {:error, any()}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
def make_actor_from_nickname(nickname) do
|
2019-07-23 18:06:22 +02:00
|
|
|
|
case WebFinger.finger(nickname) do
|
|
|
|
|
{:ok, %{"url" => url}} when not is_nil(url) ->
|
|
|
|
|
make_actor_from_url(url)
|
|
|
|
|
|
|
|
|
|
_e ->
|
|
|
|
|
{:error, "No ActivityPub URL found in WebFinger"}
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
|
@spec is_create_activity?(Activity.t()) :: boolean
|
|
|
|
|
defp is_create_activity?(%Activity{data: %{"type" => "Create"}}), do: true
|
|
|
|
|
defp is_create_activity?(_), do: false
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
@spec convert_members_in_recipients(list(String.t())) :: {list(String.t()), list(Actor.t())}
|
|
|
|
|
defp convert_members_in_recipients(recipients) do
|
|
|
|
|
Enum.reduce(recipients, {recipients, []}, fn recipient, {recipients, member_actors} = acc ->
|
|
|
|
|
case Actors.get_group_by_members_url(recipient) do
|
|
|
|
|
# If the group is local just add external members
|
|
|
|
|
%Actor{domain: domain} = group when is_nil(domain) ->
|
|
|
|
|
{Enum.filter(recipients, fn recipient -> recipient != group.members_url end),
|
|
|
|
|
member_actors ++ Actors.list_external_actors_members_for_group(group)}
|
|
|
|
|
|
|
|
|
|
# If it's remote add the remote group actor as well
|
|
|
|
|
%Actor{} = group ->
|
|
|
|
|
{Enum.filter(recipients, fn recipient -> recipient != group.members_url end),
|
|
|
|
|
member_actors ++ Actors.list_external_actors_members_for_group(group) ++ [group]}
|
|
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
|
acc
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# @spec is_announce_activity?(Activity.t()) :: boolean
|
|
|
|
|
# defp is_announce_activity?(%Activity{data: %{"type" => "Announce"}}), do: true
|
|
|
|
|
# defp is_announce_activity?(_), do: false
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
2019-02-22 16:11:57 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Publish an activity to all appropriated audiences inboxes
|
|
|
|
|
"""
|
2020-02-18 08:57:00 +01:00
|
|
|
|
# credo:disable-for-lines:47
|
2019-12-03 11:29:51 +01:00
|
|
|
|
@spec publish(Actor.t(), Activity.t()) :: :ok
|
2020-07-07 15:51:42 +02:00
|
|
|
|
def publish(actor, %Activity{recipients: recipients} = activity) do
|
2018-05-18 09:56:21 +02:00
|
|
|
|
Logger.debug("Publishing an activity")
|
2019-07-30 16:40:59 +02:00
|
|
|
|
Logger.debug(inspect(activity))
|
|
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
|
public = Visibility.is_public?(activity)
|
2019-12-03 11:29:51 +01:00
|
|
|
|
Logger.debug("is public ? #{public}")
|
2019-07-30 16:40:59 +02:00
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
|
if public && is_create_activity?(activity) && Config.get([:instance, :allow_relay]) do
|
2019-07-30 16:40:59 +02:00
|
|
|
|
Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
|
2019-09-22 18:29:13 +02:00
|
|
|
|
|
|
|
|
|
Relay.publish(activity)
|
2019-07-30 16:40:59 +02:00
|
|
|
|
end
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
2020-07-07 15:51:42 +02:00
|
|
|
|
{recipients, followers} =
|
2018-05-18 09:56:21 +02:00
|
|
|
|
if actor.followers_url in activity.recipients do
|
2020-07-07 15:51:42 +02:00
|
|
|
|
{Enum.filter(recipients, fn recipient -> recipient != actor.followers_url end),
|
|
|
|
|
Actors.list_external_followers_for_actor(actor)}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
else
|
2020-07-07 15:51:42 +02:00
|
|
|
|
{recipients, []}
|
2018-05-18 09:56:21 +02:00
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{recipients, members} = convert_members_in_recipients(recipients)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
|
remote_inboxes =
|
2020-07-07 15:51:42 +02:00
|
|
|
|
(remote_actors(recipients) ++ followers ++ members)
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|> Enum.map(fn actor -> actor.shared_inbox_url || actor.inbox_url end)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|> Enum.uniq()
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
|
|
{:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
|
|
|
|
|
json = Jason.encode!(data)
|
2019-01-03 14:59:59 +01:00
|
|
|
|
Logger.debug(fn -> "Remote inboxes are : #{inspect(remote_inboxes)}" end)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
|
|
Enum.each(remote_inboxes, fn inbox ->
|
|
|
|
|
Federator.enqueue(:publish_single_ap, %{
|
|
|
|
|
inbox: inbox,
|
|
|
|
|
json: json,
|
|
|
|
|
actor: actor,
|
|
|
|
|
id: activity.data["id"]
|
|
|
|
|
})
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
2019-02-22 16:11:57 +01:00
|
|
|
|
@doc """
|
|
|
|
|
Publish an activity to a specific inbox
|
|
|
|
|
"""
|
2018-05-17 11:32:23 +02:00
|
|
|
|
def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do
|
|
|
|
|
Logger.info("Federating #{id} to #{inbox}")
|
2019-12-03 11:29:51 +01:00
|
|
|
|
%URI{host: host, path: path} = URI.parse(inbox)
|
2018-12-07 10:47:31 +01:00
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
|
digest = Signature.build_digest(json)
|
|
|
|
|
date = Signature.generate_date_header()
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
|
# request_target = Signature.generate_request_target("POST", path)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
|
|
signature =
|
2019-07-30 16:40:59 +02:00
|
|
|
|
Signature.sign(actor, %{
|
2019-12-03 11:29:51 +01:00
|
|
|
|
"(request-target)": "post #{path}",
|
2018-10-11 17:47:02 +02:00
|
|
|
|
host: host,
|
2018-12-07 10:47:31 +01:00
|
|
|
|
"content-length": byte_size(json),
|
|
|
|
|
digest: digest,
|
|
|
|
|
date: date
|
2018-10-11 17:47:02 +02:00
|
|
|
|
})
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
Tesla.post(
|
2018-11-12 23:30:47 +01:00
|
|
|
|
inbox,
|
|
|
|
|
json,
|
2020-07-09 17:24:28 +02:00
|
|
|
|
headers: [
|
2018-12-07 10:47:31 +01:00
|
|
|
|
{"Content-Type", "application/activity+json"},
|
|
|
|
|
{"signature", signature},
|
|
|
|
|
{"digest", digest},
|
|
|
|
|
{"date", date}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
]
|
2018-11-12 23:30:47 +01:00
|
|
|
|
)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-10-15 21:18:03 +02:00
|
|
|
|
# Fetching a remote actor's information through its AP ID
|
2018-11-12 18:17:53 +01:00
|
|
|
|
@spec fetch_and_prepare_actor_from_url(String.t()) :: {:ok, struct()} | {:error, atom()} | any()
|
|
|
|
|
defp fetch_and_prepare_actor_from_url(url) do
|
2018-11-12 09:05:31 +01:00
|
|
|
|
Logger.debug("Fetching and preparing actor from url")
|
2019-07-30 16:40:59 +02:00
|
|
|
|
Logger.debug(inspect(url))
|
|
|
|
|
|
|
|
|
|
res =
|
2020-07-09 17:24:28 +02:00
|
|
|
|
with {:ok, %{status: 200, body: body}} <-
|
|
|
|
|
Tesla.get(url, headers: [{"Accept", "application/activity+json"}]),
|
2019-07-30 16:40:59 +02:00
|
|
|
|
:ok <- Logger.debug("response okay, now decoding json"),
|
|
|
|
|
{:ok, data} <- Jason.decode(body) do
|
|
|
|
|
Logger.debug("Got activity+json response at actor's endpoint, now converting data")
|
2020-02-18 08:57:00 +01:00
|
|
|
|
{:ok, Converter.Actor.as_to_model_data(data)}
|
2019-07-30 16:40:59 +02:00
|
|
|
|
else
|
|
|
|
|
# Actor is gone, probably deleted
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:ok, %{status: 410}} ->
|
2019-07-30 16:40:59 +02:00
|
|
|
|
Logger.info("Response HTTP 410")
|
|
|
|
|
{:error, :actor_deleted}
|
|
|
|
|
|
|
|
|
|
e ->
|
|
|
|
|
Logger.warn("Could not decode actor at fetch #{url}, #{inspect(e)}")
|
|
|
|
|
{:error, e}
|
|
|
|
|
end
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
|
res
|
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
|
|
|
|
|
"""
|
2019-04-25 19:05:05 +02:00
|
|
|
|
@spec fetch_public_activities_for_actor(Actor.t(), integer(), integer()) :: map()
|
|
|
|
|
def fetch_public_activities_for_actor(%Actor{} = actor, page \\ 1, limit \\ 10) do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
%Page{total: total_events, elements: events} =
|
|
|
|
|
Events.list_public_events_for_actor(actor, page, limit)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
%Page{total: total_comments, elements: comments} =
|
|
|
|
|
Discussions.list_public_comments_for_actor(actor, page, limit)
|
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
|
2018-11-12 18:17:53 +01:00
|
|
|
|
@spec event_to_activity(%Event{}, 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{
|
|
|
|
|
recipients: ["https://www.w3.org/ns/activitystreams#Public"],
|
2018-05-18 09:56:21 +02:00
|
|
|
|
actor: event.organizer_actor.url,
|
2019-09-22 18:29:13 +02:00
|
|
|
|
data: Converter.Event.model_to_as(event),
|
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
|
2018-11-12 18:17:53 +01:00
|
|
|
|
@spec comment_to_activity(%Comment{}, 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{
|
|
|
|
|
recipients: ["https://www.w3.org/ns/activitystreams#Public"],
|
2018-08-24 11:34:00 +02:00
|
|
|
|
actor: comment.actor.url,
|
2019-09-22 18:29:13 +02:00
|
|
|
|
data: Converter.Comment.model_to_as(comment),
|
2018-11-12 18:17:53 +01:00
|
|
|
|
local: local
|
2018-08-24 11:34:00 +02:00
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
|
# Get recipients for an activity or object
|
|
|
|
|
@spec get_recipients(map()) :: list()
|
|
|
|
|
defp get_recipients(data) do
|
2020-01-30 20:27:25 +01:00
|
|
|
|
Map.get(data, "to", []) ++ Map.get(data, "cc", [])
|
2019-10-25 17:43:37 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
|
@spec check_for_tombstones(map()) :: Tombstone.t() | nil
|
|
|
|
|
defp check_for_tombstones(%{url: url}), do: Tombstone.find_tombstone(url)
|
|
|
|
|
defp check_for_tombstones(_), do: nil
|
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
|
@spec accept_follow(Follower.t(), map) :: {:ok, Follower.t(), Activity.t()} | any
|
|
|
|
|
defp accept_follow(%Follower{} = follower, additional) do
|
2019-12-03 11:29:51 +01:00
|
|
|
|
with {:ok, %Follower{} = follower} <- Actors.update_follower(follower, %{approved: true}),
|
2019-10-25 17:43:37 +02:00
|
|
|
|
follower_as_data <- Convertible.model_to_as(follower),
|
|
|
|
|
update_data <-
|
2019-12-03 11:29:51 +01:00
|
|
|
|
make_accept_join_data(
|
2019-10-25 17:43:37 +02:00
|
|
|
|
follower_as_data,
|
2019-12-03 11:29:51 +01:00
|
|
|
|
Map.merge(additional, %{
|
2020-01-28 19:18:33 +01:00
|
|
|
|
"id" => "#{Endpoint.url()}/accept/follow/#{follower.id}",
|
2019-12-03 11:29:51 +01:00
|
|
|
|
"to" => [follower.actor.url],
|
|
|
|
|
"cc" => [],
|
|
|
|
|
"actor" => follower.target_actor.url
|
2019-10-25 17:43:37 +02:00
|
|
|
|
})
|
|
|
|
|
) do
|
|
|
|
|
{:ok, follower, update_data}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an update activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
|
@spec accept_join(Participant.t(), map) :: {:ok, Participant.t(), Activity.t()} | any
|
|
|
|
|
defp accept_join(%Participant{} = participant, additional) do
|
2019-12-03 11:29:51 +01:00
|
|
|
|
with {:ok, %Participant{} = participant} <-
|
|
|
|
|
Events.update_participant(participant, %{role: :participant}),
|
2020-01-28 19:18:33 +01:00
|
|
|
|
Absinthe.Subscription.publish(Endpoint, participant.actor,
|
2019-12-03 11:29:51 +01:00
|
|
|
|
event_person_participation_changed: participant.actor.id
|
|
|
|
|
),
|
2020-02-18 08:57:00 +01:00
|
|
|
|
{:ok, _} <-
|
|
|
|
|
Scheduler.before_event_notification(participant),
|
2019-10-25 17:43:37 +02:00
|
|
|
|
participant_as_data <- Convertible.model_to_as(participant),
|
|
|
|
|
audience <-
|
2019-12-03 11:29:51 +01:00
|
|
|
|
Audience.calculate_to_and_cc_from_mentions(participant),
|
2019-10-25 17:43:37 +02:00
|
|
|
|
update_data <-
|
|
|
|
|
make_accept_join_data(
|
|
|
|
|
participant_as_data,
|
|
|
|
|
Map.merge(Map.merge(audience, additional), %{
|
2020-01-28 19:18:33 +01:00
|
|
|
|
"id" => "#{Endpoint.url()}/accept/join/#{participant.id}"
|
2019-10-25 17:43:37 +02:00
|
|
|
|
})
|
|
|
|
|
) do
|
|
|
|
|
{:ok, participant, update_data}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an update activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
@spec accept_invite(Member.t(), map()) :: {:ok, Member.t(), Activity.t()} | any
|
|
|
|
|
defp accept_invite(
|
|
|
|
|
%Member{invited_by_id: invited_by_id, actor_id: actor_id} = member,
|
|
|
|
|
_additional
|
|
|
|
|
) do
|
|
|
|
|
with %Actor{} = inviter <- Actors.get_actor(invited_by_id),
|
|
|
|
|
%Actor{url: actor_url} <- Actors.get_actor(actor_id),
|
|
|
|
|
{:ok, %Member{url: member_url, id: member_id} = member} <-
|
|
|
|
|
Actors.update_member(member, %{role: :member}),
|
|
|
|
|
accept_data <- %{
|
|
|
|
|
"type" => "Accept",
|
2020-08-14 11:32:23 +02:00
|
|
|
|
"attributedTo" => member.parent.url,
|
|
|
|
|
"to" => [inviter.url, member.parent.members_url],
|
2020-02-18 08:57:00 +01:00
|
|
|
|
"cc" => [member.parent.url],
|
2020-08-14 11:32:23 +02:00
|
|
|
|
"actor" => actor_url,
|
2020-02-18 08:57:00 +01:00
|
|
|
|
"object" => member_url,
|
|
|
|
|
"id" => "#{Endpoint.url()}/accept/invite/member/#{member_id}"
|
|
|
|
|
} do
|
|
|
|
|
{:ok, member, accept_data}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
|
@spec reject_join(Participant.t(), map()) :: {:ok, Participant.t(), Activity.t()} | any()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
defp reject_join(%Participant{} = participant, additional) do
|
|
|
|
|
with {:ok, %Participant{} = participant} <-
|
2019-12-20 13:04:34 +01:00
|
|
|
|
Events.update_participant(participant, %{role: :rejected}),
|
2020-01-28 19:18:33 +01:00
|
|
|
|
Absinthe.Subscription.publish(Endpoint, participant.actor,
|
2019-12-03 11:29:51 +01:00
|
|
|
|
event_person_participation_changed: participant.actor.id
|
|
|
|
|
),
|
|
|
|
|
participant_as_data <- Convertible.model_to_as(participant),
|
|
|
|
|
audience <-
|
|
|
|
|
participant
|
|
|
|
|
|> Audience.calculate_to_and_cc_from_mentions()
|
|
|
|
|
|> Map.merge(additional),
|
|
|
|
|
reject_data <- %{
|
|
|
|
|
"type" => "Reject",
|
|
|
|
|
"object" => participant_as_data
|
|
|
|
|
},
|
|
|
|
|
update_data <-
|
|
|
|
|
reject_data
|
|
|
|
|
|> Map.merge(audience)
|
|
|
|
|
|> Map.merge(%{
|
2020-01-28 19:18:33 +01:00
|
|
|
|
"id" => "#{Endpoint.url()}/reject/join/#{participant.id}"
|
2019-12-03 11:29:51 +01:00
|
|
|
|
}) do
|
|
|
|
|
{:ok, participant, update_data}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an update activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
|
@spec reject_follow(Follower.t(), map()) :: {:ok, Follower.t(), Activity.t()} | any()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
defp reject_follow(%Follower{} = follower, additional) do
|
|
|
|
|
with {:ok, %Follower{} = follower} <- Actors.delete_follower(follower),
|
|
|
|
|
follower_as_data <- Convertible.model_to_as(follower),
|
|
|
|
|
audience <-
|
|
|
|
|
follower.actor |> Audience.calculate_to_and_cc_from_mentions() |> Map.merge(additional),
|
|
|
|
|
reject_data <- %{
|
2020-01-30 20:27:25 +01:00
|
|
|
|
"to" => [follower.actor.url],
|
2019-12-03 11:29:51 +01:00
|
|
|
|
"type" => "Reject",
|
2020-01-30 20:27:25 +01:00
|
|
|
|
"actor" => follower.target_actor.url,
|
2019-12-03 11:29:51 +01:00
|
|
|
|
"object" => follower_as_data
|
|
|
|
|
},
|
|
|
|
|
update_data <-
|
2020-01-30 20:27:25 +01:00
|
|
|
|
audience
|
|
|
|
|
|> Map.merge(reject_data)
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|> Map.merge(%{
|
2020-01-28 19:18:33 +01:00
|
|
|
|
"id" => "#{Endpoint.url()}/reject/follow/#{follower.id}"
|
2019-12-03 11:29:51 +01:00
|
|
|
|
}) do
|
|
|
|
|
{:ok, follower, update_data}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an update activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-08-14 11:32:23 +02:00
|
|
|
|
|
|
|
|
|
@spec reject_invite(Member.t(), map()) :: {:ok, Member.t(), Activity.t()} | any
|
|
|
|
|
defp reject_invite(
|
|
|
|
|
%Member{invited_by_id: invited_by_id, actor_id: actor_id} = member,
|
|
|
|
|
_additional
|
|
|
|
|
) do
|
|
|
|
|
with %Actor{} = inviter <- Actors.get_actor(invited_by_id),
|
|
|
|
|
%Actor{url: actor_url} <- Actors.get_actor(actor_id),
|
|
|
|
|
{:ok, %Member{url: member_url, id: member_id} = member} <-
|
|
|
|
|
Actors.delete_member(member),
|
|
|
|
|
accept_data <- %{
|
|
|
|
|
"type" => "Reject",
|
|
|
|
|
"actor" => actor_url,
|
|
|
|
|
"attributedTo" => member.parent.url,
|
|
|
|
|
"to" => [inviter.url, member.parent.members_url],
|
|
|
|
|
"cc" => [member.parent.url],
|
|
|
|
|
"object" => member_url,
|
|
|
|
|
"id" => "#{Endpoint.url()}/reject/invite/member/#{member_id}"
|
|
|
|
|
} do
|
|
|
|
|
{:ok, member, accept_data}
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|