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/utils.ex
|
2018-12-24 13:34:45 +01:00
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityPub.Utils do
|
2018-06-14 18:15:27 +02:00
|
|
|
@moduledoc """
|
2020-01-22 02:14:42 +01:00
|
|
|
Various ActivityPub related utils.
|
2018-06-14 18:15:27 +02:00
|
|
|
"""
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Actors
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-13 01:35:03 +02:00
|
|
|
alias Mobilizon.Media.Picture
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2020-06-15 11:13:20 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Activity, Federator, Relay}
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Types.Ownable
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.HTTPSignatures
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2019-09-13 01:35:03 +02:00
|
|
|
require Logger
|
|
|
|
|
2019-09-04 18:24:31 +02:00
|
|
|
@actor_types ["Group", "Person", "Application"]
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
# Some implementations send the actor URI as the actor field, others send the entire actor object,
|
|
|
|
# so figure out what the actor's URI is based on what we have.
|
2019-07-30 16:40:59 +02:00
|
|
|
def get_url(%{"id" => id}), do: id
|
|
|
|
def get_url(id) when is_bitstring(id), do: id
|
2020-02-18 08:57:00 +01:00
|
|
|
def get_url(ids) when is_list(ids), do: get_url(hd(ids))
|
2019-07-30 16:40:59 +02:00
|
|
|
def get_url(_), do: nil
|
2018-11-12 09:05:31 +01:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
def make_json_ld_header do
|
|
|
|
%{
|
|
|
|
"@context" => [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
2019-12-03 11:29:51 +01:00
|
|
|
"https://litepub.social/litepub/context.jsonld",
|
2018-05-17 11:32:23 +02:00
|
|
|
%{
|
2018-11-12 09:05:31 +01:00
|
|
|
"sc" => "http://schema.org#",
|
2019-12-03 11:29:51 +01:00
|
|
|
"ical" => "http://www.w3.org/2002/12/cal/ical#",
|
2019-12-16 11:46:19 +01:00
|
|
|
"pt" => "https://joinpeertube.org/ns#",
|
2020-08-11 18:00:35 +02:00
|
|
|
"toot" => "http://joinmastodon.org/ns#",
|
|
|
|
"discoverable" => "toot:discoverable",
|
|
|
|
"manuallyApprovesFollowers" => "as:manuallyApprovesFollowers",
|
2018-05-17 11:32:23 +02:00
|
|
|
"Hashtag" => "as:Hashtag",
|
2018-11-12 09:05:31 +01:00
|
|
|
"category" => "sc:category",
|
2019-12-03 11:29:51 +01:00
|
|
|
"uuid" => "sc:identifier",
|
|
|
|
"maximumAttendeeCapacity" => "sc:maximumAttendeeCapacity",
|
2019-12-16 11:47:57 +01:00
|
|
|
"location" => %{
|
|
|
|
"@id" => "sc:location",
|
|
|
|
"@type" => "sc:Place"
|
|
|
|
},
|
|
|
|
"PostalAddress" => "sc:PostalAddress",
|
|
|
|
"address" => %{
|
|
|
|
"@id" => "sc:address",
|
|
|
|
"@type" => "sc:PostalAddress"
|
|
|
|
},
|
|
|
|
"addressCountry" => "sc:addressCountry",
|
|
|
|
"addressRegion" => "sc:addressRegion",
|
|
|
|
"postalCode" => "sc:postalCode",
|
|
|
|
"addressLocality" => "sc:addressLocality",
|
|
|
|
"streetAddress" => "sc:streetAddress",
|
2019-12-03 11:29:51 +01:00
|
|
|
"mz" => "https://joinmobilizon.org/ns#",
|
|
|
|
"repliesModerationOptionType" => %{
|
|
|
|
"@id" => "mz:repliesModerationOptionType",
|
|
|
|
"@type" => "rdfs:Class"
|
|
|
|
},
|
|
|
|
"repliesModerationOption" => %{
|
|
|
|
"@id" => "mz:repliesModerationOption",
|
|
|
|
"@type" => "mz:repliesModerationOptionType"
|
|
|
|
},
|
2019-12-16 11:46:19 +01:00
|
|
|
"commentsEnabled" => %{
|
|
|
|
"@type" => "sc:Boolean",
|
|
|
|
"@id" => "pt:commentsEnabled"
|
|
|
|
},
|
2019-12-03 11:29:51 +01:00
|
|
|
"joinModeType" => %{
|
|
|
|
"@id" => "mz:joinModeType",
|
|
|
|
"@type" => "rdfs:Class"
|
|
|
|
},
|
|
|
|
"joinMode" => %{
|
|
|
|
"@id" => "mz:joinMode",
|
|
|
|
"@type" => "mz:joinModeType"
|
2019-12-20 13:04:34 +01:00
|
|
|
},
|
|
|
|
"anonymousParticipationEnabled" => %{
|
|
|
|
"@id" => "mz:anonymousParticipationEnabled",
|
|
|
|
"@type" => "sc:Boolean"
|
2020-03-05 19:32:34 +01:00
|
|
|
},
|
|
|
|
"participationMessage" => %{
|
|
|
|
"@id" => "mz:participationMessage",
|
|
|
|
"@type" => "sc:Text"
|
2019-12-03 11:29:51 +01:00
|
|
|
}
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_date do
|
2019-02-14 14:19:55 +01:00
|
|
|
DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Enqueues an activity for federation if it's local
|
|
|
|
"""
|
|
|
|
def maybe_federate(%Activity{local: true} = activity) do
|
2018-11-12 09:05:31 +01:00
|
|
|
Logger.debug("Maybe federate an activity")
|
|
|
|
|
2019-09-27 16:18:29 +02:00
|
|
|
if Mobilizon.Config.get!([:instance, :federating]) do
|
|
|
|
priority =
|
|
|
|
case activity.data["type"] do
|
|
|
|
"Delete" -> 10
|
|
|
|
"Create" -> 1
|
|
|
|
_ -> 5
|
|
|
|
end
|
|
|
|
|
|
|
|
Federator.enqueue(:publish, activity, priority)
|
|
|
|
end
|
2019-09-22 18:29:13 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def maybe_federate(_), do: :ok
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@doc """
|
|
|
|
Applies to activities sent by group members from outside this instance to a group of this instance,
|
|
|
|
we then need to relay (`Announce`) the object to other members on other instances.
|
|
|
|
"""
|
|
|
|
def maybe_relay_if_group_activity(activity, attributed_to \\ nil)
|
|
|
|
|
|
|
|
def maybe_relay_if_group_activity(
|
|
|
|
%Activity{local: false, data: %{"object" => object}},
|
|
|
|
_attributed_to
|
|
|
|
)
|
|
|
|
when is_map(object) do
|
|
|
|
do_maybe_relay_if_group_activity(object, object["attributedTo"])
|
|
|
|
end
|
|
|
|
|
|
|
|
# When doing a delete the object is just an AP ID string, so we pass the attributed_to actor as well
|
|
|
|
def maybe_relay_if_group_activity(
|
|
|
|
%Activity{local: false, data: %{"object" => object}},
|
|
|
|
%Actor{url: attributed_to_url}
|
|
|
|
)
|
|
|
|
when is_binary(object) do
|
|
|
|
do_maybe_relay_if_group_activity(object, attributed_to_url)
|
|
|
|
end
|
|
|
|
|
2020-08-27 11:53:24 +02:00
|
|
|
def maybe_relay_if_group_activity(_activity, _attributedTo) do
|
|
|
|
:ok
|
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2020-08-14 11:32:23 +02:00
|
|
|
defp do_maybe_relay_if_group_activity(object, attributed_to) when is_list(attributed_to),
|
|
|
|
do: do_maybe_relay_if_group_activity(object, hd(attributed_to))
|
|
|
|
|
|
|
|
defp do_maybe_relay_if_group_activity(object, attributed_to) when is_binary(attributed_to) do
|
2020-07-09 17:24:28 +02:00
|
|
|
id = "#{Endpoint.url()}/announces/#{Ecto.UUID.generate()}"
|
|
|
|
|
|
|
|
case Actors.get_local_group_by_url(attributed_to) do
|
|
|
|
%Actor{} = group ->
|
|
|
|
case ActivityPub.announce(group, object, id, true, false) do
|
|
|
|
{:ok, _activity, _object} ->
|
|
|
|
Logger.info("Forwarded activity to external members of the group")
|
|
|
|
:ok
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Logger.info("Failed to forward activity to external members of the group")
|
|
|
|
:error
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_maybe_relay_if_group_activity(_, _), do: :ok
|
|
|
|
|
2020-07-07 15:51:42 +02:00
|
|
|
@spec remote_actors(list(String.t())) :: list(Actor.t())
|
|
|
|
def remote_actors(recipients) do
|
|
|
|
recipients
|
2020-06-15 11:13:20 +02:00
|
|
|
|> Enum.map(fn url -> ActivityPub.get_or_fetch_actor_by_url(url) end)
|
2018-11-12 18:17:53 +01:00
|
|
|
|> Enum.map(fn {status, actor} ->
|
|
|
|
case status do
|
|
|
|
:ok ->
|
|
|
|
actor
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|> Enum.map(& &1)
|
2018-11-12 09:05:31 +01:00
|
|
|
|> Enum.filter(fn actor -> actor && !is_nil(actor.domain) end)
|
|
|
|
end
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
@doc """
|
|
|
|
Adds an id and a published data if they aren't there,
|
|
|
|
also adds it to an included object
|
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
def lazy_put_activity_defaults(%{"object" => _object} = map) do
|
2018-05-17 11:32:23 +02:00
|
|
|
if is_map(map["object"]) do
|
2018-11-12 23:30:47 +01:00
|
|
|
object = lazy_put_object_defaults(map["object"])
|
2018-05-17 11:32:23 +02:00
|
|
|
%{map | "object" => object}
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Adds an id and published date if they aren't there.
|
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
def lazy_put_object_defaults(map) when is_map(map) do
|
2018-11-12 23:30:47 +01:00
|
|
|
Map.put_new_lazy(map, "published", &make_date/0)
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
def get_actor(%{"actor" => actor}) when is_binary(actor) do
|
|
|
|
actor
|
|
|
|
end
|
2018-11-12 09:05:31 +01:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
def get_actor(%{"actor" => actor}) when is_list(actor) do
|
|
|
|
if is_binary(Enum.at(actor, 0)) do
|
|
|
|
Enum.at(actor, 0)
|
|
|
|
else
|
|
|
|
actor
|
|
|
|
|> Enum.find(fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
|
|
|
|
|> Map.get("id")
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
def get_actor(%{"actor" => %{"id" => id}}) when is_bitstring(id) do
|
|
|
|
id
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
|
|
|
|
get_actor(%{"actor" => actor})
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
@doc """
|
2019-12-03 11:29:51 +01:00
|
|
|
Checks that an incoming AP object's actor matches the domain it came from.
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
Takes the actor or attributedTo attributes (considers only the first elem if they're an array)
|
2019-07-23 13:49:22 +02:00
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
def origin_check?(id, %{"actor" => actor, "attributedTo" => _attributed_to} = params)
|
|
|
|
when not is_nil(actor) and actor != "" do
|
|
|
|
params = Map.delete(params, "attributedTo")
|
|
|
|
origin_check?(id, params)
|
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
def origin_check?(id, %{"attributedTo" => actor} = params) do
|
|
|
|
params = params |> Map.put("actor", actor) |> Map.delete("attributedTo")
|
|
|
|
origin_check?(id, params)
|
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def origin_check?(id, %{"actor" => actor} = params)
|
|
|
|
when not is_nil(actor) and is_list(actor) and length(actor) > 0 do
|
|
|
|
origin_check?(id, Map.put(params, "actor", hd(actor)))
|
|
|
|
end
|
2019-12-03 11:29:51 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def origin_check?(id, %{"actor" => actor} = params)
|
|
|
|
when not is_nil(actor) do
|
|
|
|
actor = get_actor(params)
|
|
|
|
Logger.debug("Performing origin check on #{id} and #{actor} URIs")
|
2020-07-30 18:16:32 +02:00
|
|
|
are_same_origin?(id, actor)
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def origin_check?(_id, %{"type" => type} = _params) when type in ["Actor", "Group"], do: true
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def origin_check?(_id, %{"actor" => nil} = _args), do: false
|
|
|
|
|
|
|
|
def origin_check?(_id, _args), do: false
|
|
|
|
|
2020-07-30 18:16:32 +02:00
|
|
|
@spec are_same_origin?(String.t(), String.t()) :: boolean()
|
|
|
|
def are_same_origin?(url_1, url_2) when is_binary(url_1) and is_binary(url_2) do
|
2020-07-09 17:24:28 +02:00
|
|
|
uri_1 = URI.parse(url_1)
|
|
|
|
uri_2 = URI.parse(url_2)
|
|
|
|
|
|
|
|
compare_uris?(uri_1, uri_2)
|
|
|
|
end
|
2019-09-04 18:24:31 +02:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
defp compare_uris?(%URI{} = id_uri, %URI{} = other_uri), do: id_uri.host == other_uri.host
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec origin_check_from_id?(String.t(), String.t()) :: boolean()
|
2019-12-03 11:29:51 +01:00
|
|
|
def origin_check_from_id?(id, other_id) when is_binary(other_id) do
|
|
|
|
id_uri = URI.parse(id)
|
|
|
|
other_uri = URI.parse(other_id)
|
|
|
|
|
|
|
|
compare_uris?(id_uri, other_uri)
|
2019-09-04 18:24:31 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec origin_check_from_id?(String.t(), map()) :: boolean()
|
2019-12-03 11:29:51 +01:00
|
|
|
def origin_check_from_id?(id, %{"id" => other_id} = _params) when is_binary(other_id),
|
|
|
|
do: origin_check_from_id?(id, other_id)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2020-10-19 19:21:39 +02:00
|
|
|
def activity_actor_is_group_member?(
|
|
|
|
%Actor{id: actor_id, url: actor_url},
|
|
|
|
object,
|
|
|
|
role \\ :member
|
|
|
|
) do
|
2020-07-09 17:24:28 +02:00
|
|
|
case Ownable.group_actor(object) do
|
2020-10-19 19:21:39 +02:00
|
|
|
%Actor{type: :Group, id: group_id, url: group_url} ->
|
|
|
|
Logger.debug("Group object url is #{group_url}")
|
|
|
|
|
|
|
|
case role do
|
|
|
|
:moderator ->
|
|
|
|
Logger.debug(
|
|
|
|
"Checking if activity actor #{actor_url} is a moderator from group from #{
|
|
|
|
object.url
|
|
|
|
}"
|
|
|
|
)
|
|
|
|
|
|
|
|
Actors.is_moderator?(actor_id, group_id)
|
|
|
|
|
|
|
|
:administrator ->
|
|
|
|
Logger.debug(
|
|
|
|
"Checking if activity actor #{actor_url} is an administrator from group from #{
|
|
|
|
object.url
|
|
|
|
}"
|
|
|
|
)
|
|
|
|
|
|
|
|
Actors.is_administrator?(actor_id, group_id)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Logger.debug(
|
|
|
|
"Checking if activity actor #{actor_url} is a member from group from #{object.url}"
|
|
|
|
)
|
|
|
|
|
|
|
|
Actors.is_member?(actor_id, group_id)
|
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-31 17:58:03 +02:00
|
|
|
@doc """
|
|
|
|
Save picture data from %Plug.Upload{} and return AS Link data.
|
|
|
|
"""
|
2019-10-25 17:43:37 +02:00
|
|
|
def make_picture_data(%Plug.Upload{} = picture, opts) do
|
2020-01-26 21:36:50 +01:00
|
|
|
case Mobilizon.Web.Upload.store(picture, opts) do
|
2019-07-23 18:06:22 +02:00
|
|
|
{:ok, picture} ->
|
|
|
|
picture
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-31 17:58:03 +02:00
|
|
|
@doc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Convert a picture model into an AS Link representation.
|
2019-05-31 17:58:03 +02:00
|
|
|
"""
|
2019-09-22 18:29:13 +02:00
|
|
|
def make_picture_data(%Picture{} = picture) do
|
|
|
|
Converter.Picture.model_to_as(picture)
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
|
2019-05-31 17:58:03 +02:00
|
|
|
@doc """
|
|
|
|
Save picture data from raw data and return AS Link data.
|
|
|
|
"""
|
2019-07-04 17:41:06 +02:00
|
|
|
def make_picture_data(picture) when is_map(picture) do
|
2019-06-03 17:13:47 +02:00
|
|
|
with {:ok, %{"url" => [%{"href" => url, "mediaType" => content_type}], "size" => size}} <-
|
2020-01-26 21:36:50 +01:00
|
|
|
Mobilizon.Web.Upload.store(picture.file),
|
2019-10-14 11:41:57 +02:00
|
|
|
{:picture_exists, nil} <- {:picture_exists, Mobilizon.Media.get_picture_by_url(url)},
|
2019-09-22 18:29:13 +02:00
|
|
|
{:ok, %Picture{file: _file} = picture} <-
|
2019-05-22 14:12:11 +02:00
|
|
|
Mobilizon.Media.create_picture(%{
|
|
|
|
"file" => %{
|
|
|
|
"url" => url,
|
2019-06-03 17:13:47 +02:00
|
|
|
"name" => picture.name,
|
|
|
|
"content_type" => content_type,
|
|
|
|
"size" => size
|
2019-05-31 17:58:03 +02:00
|
|
|
},
|
|
|
|
"actor_id" => picture.actor_id
|
2019-05-22 14:12:11 +02:00
|
|
|
}) do
|
2019-09-22 18:29:13 +02:00
|
|
|
Converter.Picture.model_to_as(picture)
|
2019-10-14 11:41:57 +02:00
|
|
|
else
|
|
|
|
{:picture_exists, %Picture{file: _file} = picture} ->
|
|
|
|
Converter.Picture.model_to_as(picture)
|
|
|
|
|
|
|
|
err ->
|
|
|
|
err
|
2019-05-22 14:12:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_picture_data(nil), do: nil
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
@doc """
|
|
|
|
Make announce activity data for the given actor and object
|
|
|
|
"""
|
2019-07-30 16:40:59 +02:00
|
|
|
def make_announce_data(actor, object, activity_id, public \\ true)
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
def make_announce_data(
|
2020-02-18 08:57:00 +01:00
|
|
|
%Actor{} = actor,
|
2019-07-30 16:40:59 +02:00
|
|
|
%{"id" => url, "type" => type} = _object,
|
|
|
|
activity_id,
|
|
|
|
public
|
|
|
|
)
|
2019-09-04 18:24:31 +02:00
|
|
|
when type in @actor_types do
|
2020-02-18 08:57:00 +01:00
|
|
|
do_make_announce_data(actor, url, url, activity_id, public)
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_announce_data(
|
2020-02-18 08:57:00 +01:00
|
|
|
%Actor{} = actor,
|
2020-08-14 11:32:23 +02:00
|
|
|
%{"actor" => object_actor_url} = object,
|
2019-07-30 16:40:59 +02:00
|
|
|
activity_id,
|
|
|
|
public
|
2020-08-14 11:32:23 +02:00
|
|
|
) do
|
2019-07-30 16:40:59 +02:00
|
|
|
do_make_announce_data(
|
2020-02-18 08:57:00 +01:00
|
|
|
actor,
|
2019-07-30 16:40:59 +02:00
|
|
|
object_actor_url,
|
2020-08-14 11:32:23 +02:00
|
|
|
object,
|
2019-07-30 16:40:59 +02:00
|
|
|
activity_id,
|
|
|
|
public
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_make_announce_data(
|
2020-02-18 08:57:00 +01:00
|
|
|
%Actor{type: actor_type} = actor,
|
2019-07-30 16:40:59 +02:00
|
|
|
object_actor_url,
|
2020-08-14 11:32:23 +02:00
|
|
|
object,
|
2019-07-30 16:40:59 +02:00
|
|
|
activity_id,
|
2019-08-14 17:45:11 +02:00
|
|
|
public
|
2019-07-30 16:40:59 +02:00
|
|
|
) do
|
|
|
|
{to, cc} =
|
|
|
|
if public do
|
2020-02-18 08:57:00 +01:00
|
|
|
{[actor.followers_url, object_actor_url],
|
2019-07-30 16:40:59 +02:00
|
|
|
["https://www.w3.org/ns/activitystreams#Public"]}
|
|
|
|
else
|
2020-02-18 08:57:00 +01:00
|
|
|
if actor_type == :Group do
|
|
|
|
{[actor.members_url], []}
|
|
|
|
else
|
|
|
|
{[actor.followers_url], []}
|
|
|
|
end
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
data = %{
|
|
|
|
"type" => "Announce",
|
2020-02-18 08:57:00 +01:00
|
|
|
"actor" => actor.url,
|
2020-08-14 11:32:23 +02:00
|
|
|
"object" => object,
|
2019-07-30 16:40:59 +02:00
|
|
|
"to" => to,
|
|
|
|
"cc" => cc
|
2018-12-14 17:41:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-07-30 16:40:59 +02:00
|
|
|
Make unannounce activity data for the given actor and object
|
2018-12-14 17:41:55 +01:00
|
|
|
"""
|
2019-07-30 16:40:59 +02:00
|
|
|
def make_unannounce_data(
|
|
|
|
%Actor{url: url} = actor,
|
|
|
|
activity,
|
2018-12-14 17:41:55 +01:00
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
2019-07-30 16:40:59 +02:00
|
|
|
"type" => "Undo",
|
|
|
|
"actor" => url,
|
|
|
|
"object" => activity,
|
|
|
|
"to" => [actor.followers_url, actor.url],
|
2018-12-14 17:41:55 +01:00
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"]
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
end
|
|
|
|
|
|
|
|
#### Unfollow-related helpers
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@spec make_unfollow_data(Actor.t(), Actor.t(), map(), String.t()) :: map()
|
|
|
|
def make_unfollow_data(
|
|
|
|
%Actor{url: follower_url},
|
|
|
|
%Actor{url: followed_url},
|
|
|
|
follow_activity,
|
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
2018-05-17 11:32:23 +02:00
|
|
|
"type" => "Undo",
|
2018-12-14 17:41:55 +01:00
|
|
|
"actor" => follower_url,
|
|
|
|
"to" => [followed_url],
|
|
|
|
"object" => follow_activity.data
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
#### Create-related helpers
|
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
@doc """
|
|
|
|
Make create activity data
|
|
|
|
"""
|
|
|
|
@spec make_create_data(map(), map()) :: map()
|
2019-10-25 17:43:37 +02:00
|
|
|
def make_create_data(object, additional \\ %{}) do
|
2018-12-14 17:41:55 +01:00
|
|
|
Logger.debug("Making create data")
|
2019-10-25 17:43:37 +02:00
|
|
|
Logger.debug(inspect(object))
|
|
|
|
Logger.debug(inspect(additional))
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Create",
|
2019-10-25 17:43:37 +02:00
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
2020-07-09 17:24:28 +02:00
|
|
|
"attributedTo" => object["attributedTo"] || object["actor"],
|
2019-10-25 17:43:37 +02:00
|
|
|
"actor" => object["actor"],
|
|
|
|
"object" => object,
|
|
|
|
"published" => make_date(),
|
|
|
|
"id" => object["id"] <> "/activity"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make update activity data
|
|
|
|
"""
|
|
|
|
@spec make_update_data(map(), map()) :: map()
|
|
|
|
def make_update_data(object, additional \\ %{}) do
|
|
|
|
Logger.debug("Making update data")
|
|
|
|
Logger.debug(inspect(object))
|
|
|
|
Logger.debug(inspect(additional))
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Update",
|
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"actor" => object["actor"],
|
|
|
|
"object" => object,
|
|
|
|
"id" => object["id"] <> "/activity"
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
2018-06-14 17:25:55 +02:00
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
@doc """
|
|
|
|
Make accept join activity data
|
|
|
|
"""
|
|
|
|
@spec make_accept_join_data(map(), map()) :: map()
|
|
|
|
def make_accept_join_data(object, additional \\ %{}) do
|
|
|
|
%{
|
|
|
|
"type" => "Accept",
|
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"object" => object,
|
|
|
|
"id" => object["id"] <> "/activity"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@doc """
|
|
|
|
Make add activity data
|
|
|
|
"""
|
|
|
|
@spec make_add_data(map(), map()) :: map()
|
|
|
|
def make_add_data(object, target, additional \\ %{}) do
|
|
|
|
Logger.debug("Making add data")
|
|
|
|
Logger.debug(inspect(object))
|
|
|
|
Logger.debug(inspect(additional))
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Add",
|
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"actor" => object["actor"],
|
|
|
|
"object" => object,
|
|
|
|
"target" => Map.get(target, :url, target),
|
|
|
|
"id" => object["id"] <> "/add"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make move activity data
|
|
|
|
"""
|
|
|
|
@spec make_add_data(map(), map()) :: map()
|
|
|
|
def make_move_data(object, origin, target, additional \\ %{}) do
|
|
|
|
Logger.debug("Making move data")
|
|
|
|
Logger.debug(inspect(object))
|
|
|
|
Logger.debug(inspect(origin))
|
|
|
|
Logger.debug(inspect(target))
|
|
|
|
Logger.debug(inspect(additional))
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Move",
|
|
|
|
"to" => object["to"],
|
|
|
|
"cc" => object["cc"],
|
|
|
|
"actor" => object["actor"],
|
|
|
|
"object" => object,
|
|
|
|
"origin" => if(is_nil(origin), do: origin, else: Map.get(origin, :url, origin)),
|
|
|
|
"target" => if(is_nil(target), do: target, else: Map.get(target, :url, target)),
|
|
|
|
"id" => object["id"] <> "/move"
|
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
|
|
|
|
2018-06-14 17:25:55 +02:00
|
|
|
@doc """
|
|
|
|
Converts PEM encoded keys to a public key representation
|
|
|
|
"""
|
|
|
|
def pem_to_public_key(pem) do
|
2018-08-03 10:16:22 +02:00
|
|
|
[key_code] = :public_key.pem_decode(pem)
|
|
|
|
key = :public_key.pem_entry_decode(key_code)
|
2018-08-03 10:19:28 +02:00
|
|
|
|
2018-08-03 10:16:22 +02:00
|
|
|
case key do
|
|
|
|
{:RSAPrivateKey, _, modulus, exponent, _, _, _, _, _, _, _} ->
|
|
|
|
{:RSAPublicKey, modulus, exponent}
|
2018-08-03 10:19:28 +02:00
|
|
|
|
|
|
|
{:RSAPublicKey, modulus, exponent} ->
|
|
|
|
{:RSAPublicKey, modulus, exponent}
|
2018-08-03 10:16:22 +02:00
|
|
|
end
|
2018-06-14 17:25:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def pem_to_public_key_pem(pem) do
|
|
|
|
public_key = pem_to_public_key(pem)
|
|
|
|
public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key)
|
|
|
|
:public_key.pem_encode([public_key])
|
|
|
|
end
|
2019-08-28 11:28:27 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
def make_signature(actor, id, date) do
|
2019-12-03 11:29:51 +01:00
|
|
|
uri = URI.parse(id)
|
|
|
|
|
|
|
|
signature =
|
2020-02-18 08:57:00 +01:00
|
|
|
actor
|
2020-01-22 02:14:42 +01:00
|
|
|
|> HTTPSignatures.Signature.sign(%{
|
2019-12-03 11:29:51 +01:00
|
|
|
"(request-target)": "get #{uri.path}",
|
|
|
|
host: uri.host,
|
|
|
|
date: date
|
|
|
|
})
|
2019-08-28 11:28:27 +02:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
[{:Signature, signature}]
|
2019-08-28 11:28:27 +02:00
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@doc """
|
|
|
|
Sign a request with the instance Relay actor.
|
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec sign_fetch_relay(Enum.t(), String.t(), String.t()) :: Enum.t()
|
2020-02-18 08:57:00 +01:00
|
|
|
def sign_fetch_relay(headers, id, date) do
|
|
|
|
with %Actor{} = actor <- Relay.get_actor() do
|
|
|
|
sign_fetch(headers, actor, id, date)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Sign a request with an actor.
|
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec sign_fetch(Enum.t(), Actor.t(), String.t(), String.t()) :: Enum.t()
|
2020-02-18 08:57:00 +01:00
|
|
|
def sign_fetch(headers, actor, id, date) do
|
2019-12-03 11:29:51 +01:00
|
|
|
if Mobilizon.Config.get([:activitypub, :sign_object_fetches]) do
|
2020-02-18 08:57:00 +01:00
|
|
|
headers ++ make_signature(actor, id, date)
|
2019-12-03 11:29:51 +01:00
|
|
|
else
|
|
|
|
headers
|
|
|
|
end
|
2019-08-28 11:28:27 +02:00
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@doc """
|
|
|
|
Add the Date header to the request if we sign object fetches
|
|
|
|
"""
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec maybe_date_fetch(Enum.t(), String.t()) :: Enum.t()
|
2019-12-03 11:29:51 +01:00
|
|
|
def maybe_date_fetch(headers, date) do
|
|
|
|
if Mobilizon.Config.get([:activitypub, :sign_object_fetches]) do
|
|
|
|
headers ++ [{:Date, date}]
|
|
|
|
else
|
|
|
|
headers
|
|
|
|
end
|
2019-08-28 11:28:27 +02:00
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
|
|
def check_for_actor_key_rotation(%Actor{} = actor) do
|
|
|
|
if Actors.should_rotate_actor_key(actor) do
|
|
|
|
Actors.schedule_key_rotation(
|
|
|
|
actor,
|
|
|
|
Application.get_env(:mobilizon, :activitypub)[:actor_key_rotation_delay]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2020-10-19 19:21:39 +02:00
|
|
|
|
|
|
|
def can_update_group_object?(%Actor{} = actor, object) do
|
|
|
|
can_manage_group_object?(:role_needed_to_update, actor, object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_group_object?(%Actor{} = actor, object) do
|
|
|
|
can_manage_group_object?(:role_needed_to_delete, actor, object)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec can_manage_group_object?(
|
|
|
|
:role_needed_to_update | :role_needed_to_delete,
|
|
|
|
Actor.t(),
|
|
|
|
any()
|
|
|
|
) :: boolean()
|
|
|
|
defp can_manage_group_object?(action_function, %Actor{url: actor_url} = actor, object) do
|
|
|
|
if Ownable.group_actor(object) != nil do
|
|
|
|
case apply(Ownable, action_function, [object]) do
|
|
|
|
role when role in [:member, :moderator, :administrator] ->
|
|
|
|
activity_actor_is_group_member?(actor, object, role)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
case action_function do
|
|
|
|
:role_needed_to_update ->
|
|
|
|
Logger.warn("Actor #{actor_url} can't update #{object.url}")
|
|
|
|
|
|
|
|
:role_needed_to_delete ->
|
|
|
|
Logger.warn("Actor #{actor_url} can't delete #{object.url}")
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|