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,
|
2020-10-19 09:32:37 +02:00
|
|
|
|
Posts,
|
2020-02-18 08:57:00 +01:00
|
|
|
|
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-11-06 11:34:32 +01:00
|
|
|
|
Refresher,
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 12:17:56 +02:00
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Types.{Managable, Ownable}
|
|
|
|
|
|
2021-04-22 12:17:56 +02:00
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Convertible
|
2020-01-22 02:14:42 +01:00
|
|
|
|
alias Mobilizon.Federation.HTTPSignatures.Signature
|
|
|
|
|
|
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
|
|
|
|
|
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 """
|
|
|
|
|
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}")
|
|
|
|
|
|
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)},
|
2020-10-19 09:32:37 +02:00
|
|
|
|
{:existing, nil} <- {:existing, Posts.get_post_by_url(url)},
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:existing, nil} <-
|
|
|
|
|
{:existing, Actors.get_actor_by_url_2(url)},
|
2020-08-20 10:54:58 +02:00
|
|
|
|
{:existing, nil} <- {:existing, Actors.get_member_by_url(url)},
|
2020-07-09 17:24:28 +02:00
|
|
|
|
: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} ->
|
2021-04-21 18:57:23 +02:00
|
|
|
|
handle_existing_entity(url, entity, options)
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
2021-04-28 18:06:17 +02:00
|
|
|
|
{:error, e} ->
|
2021-04-29 08:54:43 +02:00
|
|
|
|
Logger.warn("Something failed while fetching url #{url} #{inspect(e)}")
|
2021-04-28 18:06:17 +02:00
|
|
|
|
{:error, e}
|
|
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
|
e ->
|
2021-04-29 08:54:43 +02:00
|
|
|
|
Logger.warn("Something failed while fetching url #{url} #{inspect(e)}")
|
2021-04-21 18:57:23 +02:00
|
|
|
|
{:error, e}
|
|
|
|
|
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()}
|
|
|
|
|
| {:ok, struct()}
|
|
|
|
|
| {: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}
|
|
|
|
|
|
|
|
|
|
err ->
|
|
|
|
|
err
|
|
|
|
|
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()) ::
|
|
|
|
|
{:ok, struct()} | {:error, String.t(), struct()} | {:error, String.t()}
|
|
|
|
|
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-04-21 18:57:23 +02:00
|
|
|
|
{:error, "Gone"} ->
|
|
|
|
|
{:error, "Gone", entity}
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
|
{:error, "Not found"} ->
|
|
|
|
|
{:error, "Not found", entity}
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
|
{:error, "Object origin check failed"} ->
|
|
|
|
|
{:error, "Object origin check failed"}
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
{:ok, entity}
|
2018-08-24 11:34:00 +02:00
|
|
|
|
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")
|
2020-09-12 00:42:35 +02:00
|
|
|
|
Logger.debug(inspect(err))
|
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
|
2021-04-22 12:17:56 +02:00
|
|
|
|
with {:ok, %Actor{id: object_owner_actor_id}} <-
|
|
|
|
|
ActivityPubActor.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
|
|
|
|
|
"""
|
2021-01-20 18:16:44 +01:00
|
|
|
|
def follow(
|
|
|
|
|
%Actor{} = follower,
|
|
|
|
|
%Actor{} = followed,
|
|
|
|
|
activity_id \\ nil,
|
|
|
|
|
local \\ true,
|
|
|
|
|
additional \\ %{}
|
|
|
|
|
) do
|
2020-09-02 08:59:59 +02:00
|
|
|
|
with {:different_actors, true} <- {:different_actors, followed.id != follower.id},
|
2021-01-20 18:16:44 +01:00
|
|
|
|
{:ok, activity_data, %Follower{} = follower} <-
|
|
|
|
|
Types.Actors.follow(
|
|
|
|
|
follower,
|
|
|
|
|
followed,
|
|
|
|
|
local,
|
|
|
|
|
Map.merge(additional, %{"activity_id" => activity_id})
|
|
|
|
|
),
|
|
|
|
|
{:ok, activity} <- create_activity(activity_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
|
2021-01-22 19:44:37 +01:00
|
|
|
|
{:error, err, msg} when err in [:already_following, :suspended, :no_person] ->
|
2019-07-30 16:40:59 +02:00
|
|
|
|
{:error, msg}
|
2020-09-02 08:59:59 +02:00
|
|
|
|
|
|
|
|
|
{:different_actors, _} ->
|
|
|
|
|
{:error, "Can't follow yourself"}
|
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-08-27 11:53:24 +02:00
|
|
|
|
def delete(object, actor, local \\ true, additional \\ %{}) do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
with {:ok, activity_data, actor, object} <-
|
2020-08-27 11:53:24 +02:00
|
|
|
|
Managable.delete(object, actor, local, additional),
|
2020-07-09 17:24:28 +02:00
|
|
|
|
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-11-06 11:34:32 +01:00
|
|
|
|
def join(entity_to_join, actor_joining, local \\ true, additional \\ %{})
|
|
|
|
|
|
|
|
|
|
def join(%Event{} = event, %Actor{} = actor, local, additional) do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
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-11-06 11:34:32 +01:00
|
|
|
|
def join(%Actor{type: :Group} = group, %Actor{} = actor, local, additional) do
|
|
|
|
|
with {:ok, activity_data, %Member{} = member} <-
|
|
|
|
|
Types.Actors.join(group, actor, local, additional),
|
|
|
|
|
{:ok, activity} <- create_activity(activity_data, 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}
|
2020-11-06 11:34:32 +01:00
|
|
|
|
else
|
|
|
|
|
{:accept, accept} ->
|
|
|
|
|
accept
|
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 """
|
2020-11-06 15:43:38 +01:00
|
|
|
|
Leave an event or a group
|
2020-08-14 11:32:23 +02:00
|
|
|
|
"""
|
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 <-
|
2021-07-29 17:48:28 +02:00
|
|
|
|
Audience.get_audience(participant),
|
2019-12-03 11:29:51 +01:00
|
|
|
|
{: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
|
|
|
|
def leave(
|
|
|
|
|
%Actor{type: :Group, id: group_id, url: group_url, members_url: group_members_url},
|
|
|
|
|
%Actor{id: actor_id, url: actor_url},
|
|
|
|
|
local,
|
2020-08-27 11:53:24 +02:00
|
|
|
|
additional
|
2020-08-14 11:32:23 +02:00
|
|
|
|
) do
|
|
|
|
|
with {:member, {:ok, %Member{id: member_id} = member}} <-
|
|
|
|
|
{:member, Actors.get_member(actor_id, group_id)},
|
2020-08-27 11:53:24 +02:00
|
|
|
|
{:is_not_only_admin, true} <-
|
|
|
|
|
{:is_not_only_admin,
|
|
|
|
|
Map.get(additional, :force_member_removal, false) ||
|
|
|
|
|
!Actors.is_only_administrator?(member_id, group_id)},
|
2020-08-14 11:32:23 +02:00
|
|
|
|
{:delete, {:ok, %Member{} = member}} <- {:delete, Actors.delete_member(member)},
|
2021-02-24 19:06:48 +01:00
|
|
|
|
Mobilizon.Service.Activity.Member.insert_activity(member, subject: "member_quit"),
|
2020-08-14 11:32:23 +02:00
|
|
|
|
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},
|
2021-02-24 19:06:48 +01:00
|
|
|
|
%Actor{url: moderator_url} = moderator,
|
2020-08-14 11:32:23 +02:00
|
|
|
|
local,
|
|
|
|
|
_additional \\ %{}
|
|
|
|
|
) do
|
|
|
|
|
with {:ok, %Member{id: member_id}} <- Actors.update_member(member, %{role: :rejected}),
|
|
|
|
|
%Member{} = member <- Actors.get_member(member_id),
|
2021-02-24 19:06:48 +01:00
|
|
|
|
{:ok, _} <-
|
|
|
|
|
Mobilizon.Service.Activity.Member.insert_activity(member,
|
|
|
|
|
moderator: moderator,
|
|
|
|
|
subject: "member_removed"
|
|
|
|
|
),
|
2020-08-14 11:32:23 +02:00
|
|
|
|
:ok <- Group.send_notification_to_removed_member(member),
|
|
|
|
|
remove_data <- %{
|
|
|
|
|
"to" => [group_members_url],
|
|
|
|
|
"type" => "Remove",
|
|
|
|
|
"actor" => moderator_url,
|
2021-01-18 16:19:07 +01:00
|
|
|
|
"object" => member.url,
|
2020-08-14 11:32:23 +02:00
|
|
|
|
"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)
|
|
|
|
|
}),
|
2021-02-24 19:06:48 +01:00
|
|
|
|
{:ok, _} <-
|
|
|
|
|
Mobilizon.Service.Activity.Member.insert_activity(member,
|
|
|
|
|
moderator: actor,
|
|
|
|
|
subject: "member_invited"
|
|
|
|
|
),
|
2020-02-18 08:57:00 +01:00
|
|
|
|
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),
|
2020-12-01 10:21:07 +01:00
|
|
|
|
:ok <- maybe_relay_if_group_activity(activity),
|
|
|
|
|
:ok <- Group.send_invite_to_user(member) 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)
|
2021-04-20 15:02:24 +02:00
|
|
|
|
|> Mailer.send_email_later()
|
2020-07-09 17:24:28 +02:00
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
{:ok, activity, report}
|
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
|
|
|
|
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
|
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
|
defp convert_followers_in_recipients(recipients) do
|
|
|
|
|
Enum.reduce(recipients, {recipients, []}, fn recipient, {recipients, follower_actors} = acc ->
|
2020-09-02 08:59:59 +02:00
|
|
|
|
case Actors.get_actor_by_followers_url(recipient) do
|
2020-08-27 11:53:24 +02:00
|
|
|
|
%Actor{} = group ->
|
|
|
|
|
{Enum.filter(recipients, fn recipient -> recipient != group.followers_url end),
|
|
|
|
|
follower_actors ++ Actors.list_external_followers_for_actor(group)}
|
|
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
|
acc
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
|
# @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")
|
2021-03-08 11:43:07 +01:00
|
|
|
|
Logger.debug(inspect(activity, pretty: true))
|
2019-07-30 16:40:59 +02:00
|
|
|
|
|
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
|
|
|
|
|
2021-02-04 12:28:53 +01:00
|
|
|
|
recipients = Enum.uniq(recipients)
|
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
|
{recipients, followers} = convert_followers_in_recipients(recipients)
|
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
|
|
|
|
|
|
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()
|
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
|
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{
|
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
|
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{
|
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
|
|
|
|
|
|
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, _} <-
|
2020-09-30 10:45:01 +02:00
|
|
|
|
Scheduler.trigger_notifications_for_participant(participant),
|
2019-10-25 17:43:37 +02:00
|
|
|
|
participant_as_data <- Convertible.model_to_as(participant),
|
|
|
|
|
audience <-
|
2021-07-29 17:48:28 +02:00
|
|
|
|
Audience.get_audience(participant),
|
2021-07-30 17:24:37 +02:00
|
|
|
|
accept_join_data <-
|
2019-10-25 17:43:37 +02:00
|
|
|
|
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
|
2021-07-30 17:24:37 +02:00
|
|
|
|
{:ok, participant, accept_join_data}
|
2019-10-25 17:43:37 +02:00
|
|
|
|
else
|
|
|
|
|
err ->
|
|
|
|
|
Logger.error("Something went wrong while creating an update activity")
|
|
|
|
|
Logger.debug(inspect(err))
|
|
|
|
|
err
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-06 11:34:32 +01:00
|
|
|
|
@spec accept_join(Member.t(), map) :: {:ok, Member.t(), Activity.t()} | any
|
|
|
|
|
defp accept_join(%Member{} = member, additional) do
|
|
|
|
|
with {:ok, %Member{} = member} <-
|
|
|
|
|
Actors.update_member(member, %{role: :member}),
|
2021-02-24 19:06:48 +01:00
|
|
|
|
{:ok, _} <-
|
|
|
|
|
Mobilizon.Service.Activity.Member.insert_activity(member,
|
|
|
|
|
subject: "member_approved"
|
|
|
|
|
),
|
2021-04-26 10:13:29 +02:00
|
|
|
|
_ <- maybe_refresh_group(member),
|
2020-11-06 11:34:32 +01:00
|
|
|
|
Absinthe.Subscription.publish(Endpoint, member.actor,
|
2021-03-05 11:23:17 +01:00
|
|
|
|
group_membership_changed: [
|
|
|
|
|
Actor.preferred_username_and_domain(member.parent),
|
|
|
|
|
member.actor.id
|
|
|
|
|
]
|
2020-11-06 11:34:32 +01:00
|
|
|
|
),
|
|
|
|
|
member_as_data <- Convertible.model_to_as(member),
|
|
|
|
|
audience <-
|
2021-07-29 17:48:28 +02:00
|
|
|
|
Audience.get_audience(member),
|
2021-07-30 17:24:37 +02:00
|
|
|
|
accept_join_data <-
|
2020-11-06 11:34:32 +01:00
|
|
|
|
make_accept_join_data(
|
|
|
|
|
member_as_data,
|
|
|
|
|
Map.merge(Map.merge(audience, additional), %{
|
|
|
|
|
"id" => "#{Endpoint.url()}/accept/join/#{member.id}"
|
|
|
|
|
})
|
|
|
|
|
) do
|
2021-07-30 17:24:37 +02:00
|
|
|
|
{:ok, member, accept_join_data}
|
2020-11-06 11:34:32 +01:00
|
|
|
|
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),
|
2020-08-27 11:53:24 +02:00
|
|
|
|
{:ok, %Member{id: member_id} = member} <-
|
2020-02-18 08:57:00 +01:00
|
|
|
|
Actors.update_member(member, %{role: :member}),
|
2021-02-24 19:06:48 +01:00
|
|
|
|
{:ok, _} <-
|
|
|
|
|
Mobilizon.Service.Activity.Member.insert_activity(member,
|
|
|
|
|
subject: "member_accepted_invitation"
|
|
|
|
|
),
|
2021-04-26 10:13:29 +02:00
|
|
|
|
_ <- maybe_refresh_group(member),
|
2020-02-18 08:57:00 +01:00
|
|
|
|
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-08-27 11:53:24 +02:00
|
|
|
|
"object" => Convertible.model_to_as(member),
|
2020-02-18 08:57:00 +01:00
|
|
|
|
"id" => "#{Endpoint.url()}/accept/invite/member/#{member_id}"
|
|
|
|
|
} do
|
|
|
|
|
{:ok, member, accept_data}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-26 10:13:29 +02:00
|
|
|
|
defp maybe_refresh_group(%Member{
|
|
|
|
|
parent: %Actor{domain: parent_domain, url: parent_url},
|
|
|
|
|
actor: %Actor{} = actor
|
|
|
|
|
}) do
|
|
|
|
|
unless is_nil(parent_domain),
|
|
|
|
|
do: Refresher.fetch_group(parent_url, actor)
|
|
|
|
|
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
|
2021-07-29 17:48:28 +02:00
|
|
|
|
|> Audience.get_audience()
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|> 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 <-
|
2021-07-29 17:48:28 +02:00
|
|
|
|
follower.actor |> Audience.get_audience() |> Map.merge(additional),
|
2019-12-03 11:29:51 +01:00
|
|
|
|
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),
|
2021-02-24 19:06:48 +01:00
|
|
|
|
Mobilizon.Service.Activity.Member.insert_activity(member,
|
|
|
|
|
subject: "member_rejected_invitation"
|
|
|
|
|
),
|
2020-08-14 11:32:23 +02:00
|
|
|
|
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
|