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
|
2020-11-26 11:41:13 +01:00
|
|
|
|
alias Mobilizon.Medias.Media
|
2020-01-22 02:14:42 +01:00
|
|
|
|
|
2021-10-05 15:29:06 +02:00
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Activity, Federator}
|
2021-04-22 12:17:56 +02:00
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
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"]
|
2021-11-24 16:14:09 +01:00
|
|
|
|
@all_actor_types @actor_types ++ ["Organization", "Service"]
|
2019-09-04 18:24:31 +02:00
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
|
# Wraps an object into an activity
|
|
|
|
|
@spec create_activity(map(), boolean()) :: {:ok, Activity.t()}
|
|
|
|
|
def create_activity(map, local) when is_map(map) do
|
|
|
|
|
with map <- lazy_put_activity_defaults(map) do
|
|
|
|
|
{:ok,
|
|
|
|
|
%Activity{
|
|
|
|
|
data: map,
|
|
|
|
|
local: local,
|
|
|
|
|
actor: map["actor"],
|
|
|
|
|
recipients: get_recipients(map)
|
|
|
|
|
}}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Get recipients for an activity or object
|
|
|
|
|
@spec get_recipients(map()) :: list()
|
|
|
|
|
defp get_recipients(data) do
|
|
|
|
|
Map.get(data, "to", []) ++ Map.get(data, "cc", [])
|
|
|
|
|
end
|
|
|
|
|
|
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.
|
2021-09-27 09:41:36 +02:00
|
|
|
|
@spec get_url(map() | String.t() | list(String.t()) | any()) :: String.t() | nil
|
2019-07-30 16:40:59 +02:00
|
|
|
|
def get_url(%{"id" => id}), do: id
|
2021-04-08 16:41:49 +02:00
|
|
|
|
def get_url(id) when is_binary(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
|
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
|
@spec make_json_ld_header :: map()
|
2018-05-17 11:32:23 +02:00
|
|
|
|
def make_json_ld_header do
|
|
|
|
|
%{
|
|
|
|
|
"@context" => [
|
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
2021-11-25 17:42:51 +01:00
|
|
|
|
"https://w3id.org/security/v1",
|
2018-05-17 11:32:23 +02:00
|
|
|
|
%{
|
2021-08-19 20:43:35 +02:00
|
|
|
|
"@language" => "und",
|
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"
|
2021-08-10 11:03:03 +02:00
|
|
|
|
},
|
|
|
|
|
"PropertyValue" => "sc:PropertyValue",
|
|
|
|
|
"value" => "sc:value",
|
2021-08-19 20:43:35 +02:00
|
|
|
|
"propertyID" => "sc:propertyID",
|
2021-10-12 11:10:16 +02:00
|
|
|
|
"inLanguage" => "sc:inLanguage",
|
|
|
|
|
"timezone" => %{
|
|
|
|
|
"@id" => "mz:timezone",
|
|
|
|
|
"@type" => "sc:Text"
|
2021-11-12 16:29:22 +01:00
|
|
|
|
},
|
|
|
|
|
"discussions" => %{
|
|
|
|
|
"@id" => "mz:discussions",
|
|
|
|
|
"@type" => "@id"
|
|
|
|
|
},
|
|
|
|
|
"events" => %{
|
|
|
|
|
"@id" => "mz:events",
|
|
|
|
|
"@type" => "@id"
|
|
|
|
|
},
|
|
|
|
|
"members" => %{
|
|
|
|
|
"@id" => "mz:members",
|
|
|
|
|
"@type" => "@id"
|
|
|
|
|
},
|
|
|
|
|
"openness" => %{
|
|
|
|
|
"@id" => "mz:openness",
|
|
|
|
|
"@type" => "@id"
|
|
|
|
|
},
|
|
|
|
|
"posts" => %{
|
|
|
|
|
"@id" => "mz:posts",
|
|
|
|
|
"@type" => "@id"
|
|
|
|
|
},
|
|
|
|
|
"resources" => %{
|
|
|
|
|
"@id" => "mz:resources",
|
|
|
|
|
"@type" => "@id"
|
|
|
|
|
},
|
|
|
|
|
"todos" => %{
|
|
|
|
|
"@id" => "mz:todos",
|
|
|
|
|
"@type" => "@id"
|
2021-10-12 11:10:16 +02:00
|
|
|
|
}
|
2018-05-17 11:32:23 +02:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
|
@spec make_date :: String.t()
|
2018-05-17 11:32:23 +02:00
|
|
|
|
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
|
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
|
@spec maybe_federate(activity :: Activity.t()) :: :ok
|
2018-05-17 11:32:23 +02:00
|
|
|
|
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.
|
|
|
|
|
"""
|
2021-09-27 09:41:36 +02:00
|
|
|
|
@spec maybe_relay_if_group_activity(Activity.t(), Actor.t() | nil | list(Actor.t())) :: :ok
|
2020-07-09 17:24:28 +02:00
|
|
|
|
def maybe_relay_if_group_activity(activity, attributed_to \\ nil)
|
|
|
|
|
|
|
|
|
|
def maybe_relay_if_group_activity(
|
2021-02-04 12:28:53 +01:00
|
|
|
|
%Activity{data: %{"object" => object}},
|
2020-07-09 17:24:28 +02:00
|
|
|
|
_attributed_to
|
|
|
|
|
)
|
|
|
|
|
when is_map(object) do
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug("Maybe relay if group activity (object is map)")
|
|
|
|
|
Logger.debug(inspect(object))
|
2020-07-09 17:24:28 +02:00
|
|
|
|
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(
|
2021-02-04 12:28:53 +01:00
|
|
|
|
%Activity{data: %{"object" => object}},
|
2020-07-09 17:24:28 +02:00
|
|
|
|
%Actor{url: attributed_to_url}
|
|
|
|
|
)
|
2021-09-28 19:40:37 +02:00
|
|
|
|
when is_binary(object) and is_binary(attributed_to_url) do
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug("Maybe relay if group activity (object is binary)")
|
2020-07-09 17:24:28 +02:00
|
|
|
|
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
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug("Will not replay : not a group activity")
|
2020-08-27 11:53:24 +02:00
|
|
|
|
:ok
|
|
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
|
# TODO: Is this a map or a String?
|
|
|
|
|
@spec do_maybe_relay_if_group_activity(map() | String.t(), list(String.t()) | String.t()) :: :ok
|
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
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug("Let's try to relay group activity")
|
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 ->
|
2021-09-28 19:40:37 +02:00
|
|
|
|
case Actions.Announce.announce(group, object, id, true, false) do
|
2020-07-09 17:24:28 +02:00
|
|
|
|
{:ok, _activity, _object} ->
|
|
|
|
|
Logger.info("Forwarded activity to external members of the group")
|
|
|
|
|
:ok
|
|
|
|
|
|
2021-11-19 17:40:42 +01:00
|
|
|
|
{:error, err} ->
|
2020-07-09 17:24:28 +02:00
|
|
|
|
Logger.info("Failed to forward activity to external members of the group")
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug(inspect(err))
|
2020-07-09 17:24:28 +02:00
|
|
|
|
:error
|
|
|
|
|
end
|
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
|
nil ->
|
2020-07-09 17:24:28 +02:00
|
|
|
|
:ok
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-11-19 17:40:42 +01:00
|
|
|
|
defp do_maybe_relay_if_group_activity(_, attributed_to) do
|
|
|
|
|
Logger.debug("Will not relay group activity, attributed to is : #{inspect(attributed_to)}")
|
|
|
|
|
end
|
2020-07-09 17:24:28 +02:00
|
|
|
|
|
2020-07-07 15:51:42 +02:00
|
|
|
|
@spec remote_actors(list(String.t())) :: list(Actor.t())
|
|
|
|
|
def remote_actors(recipients) do
|
|
|
|
|
recipients
|
2021-04-22 12:17:56 +02:00
|
|
|
|
|> Enum.map(fn url -> ActivityPubActor.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
|
|
|
|
|
"""
|
2021-09-27 09:41:36 +02:00
|
|
|
|
@spec lazy_put_activity_defaults(map()) :: map()
|
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
|
|
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
|
@spec get_actor(map()) :: String.t() | nil
|
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
|
|
|
|
|
2021-11-24 16:14:09 +01:00
|
|
|
|
def get_actor(%{"actor" => [actor | tail] = actor_list} = object)
|
|
|
|
|
when is_list(actor_list) and length(actor_list) > 0 do
|
|
|
|
|
res =
|
|
|
|
|
try do
|
|
|
|
|
object
|
|
|
|
|
|> Map.put("actor", actor)
|
|
|
|
|
|> get_actor()
|
|
|
|
|
rescue
|
|
|
|
|
ArgumentError -> nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
case res do
|
|
|
|
|
id when is_binary(id) ->
|
|
|
|
|
id
|
|
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
|
object
|
|
|
|
|
|> Map.put("actor", tail)
|
|
|
|
|
|> get_actor()
|
2018-12-14 17:41:55 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-11-24 16:14:09 +01:00
|
|
|
|
def get_actor(%{"actor" => %{"id" => id, "type" => type}})
|
|
|
|
|
when is_binary(id) and type in @all_actor_types do
|
2019-12-03 11:29:51 +01:00
|
|
|
|
id
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-11-24 16:14:09 +01:00
|
|
|
|
def get_actor(%{"actor" => _, "attributedTo" => actor}) when not is_nil(actor) do
|
2019-12-03 11:29:51 +01:00
|
|
|
|
get_actor(%{"actor" => actor})
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-11-24 16:14:09 +01:00
|
|
|
|
def get_actor(%{"actor" => %{"id" => id, "type" => type}})
|
|
|
|
|
when is_binary(id) do
|
|
|
|
|
raise ArgumentError,
|
|
|
|
|
message: "Object contains an actor object with invalid type: #{inspect(type)}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_actor(%{"actor" => nil, "attributedTo" => nil}) do
|
|
|
|
|
raise ArgumentError, message: "Object contains both actor and attributedTo fields being null"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_actor(%{"actor" => _}) do
|
|
|
|
|
raise ArgumentError, message: "Object contains not actor information"
|
|
|
|
|
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
|
|
|
|
"""
|
2021-09-27 09:41:36 +02:00
|
|
|
|
@spec origin_check?(String.t(), map()) :: boolean()
|
2021-08-10 20:32:50 +02:00
|
|
|
|
def origin_check?(id, %{"type" => "Tombstone", "id" => tombstone_id}), do: id == tombstone_id
|
|
|
|
|
|
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
|
|
|
|
|
|
2021-04-22 12:17:56 +02:00
|
|
|
|
def origin_check?(id, %{"type" => type, "id" => actor_id} = _params)
|
2021-10-25 18:32:46 +02:00
|
|
|
|
when type in ["Actor", "Person", "Group", "Application"],
|
2021-04-22 12:17:56 +02:00
|
|
|
|
do: id == actor_id
|
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
|
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
|
@spec compare_uris?(URI.t(), URI.t()) :: boolean()
|
2020-10-25 18:22:40 +01:00
|
|
|
|
defp compare_uris?(%URI{} = id_uri, %URI{} = other_uri),
|
|
|
|
|
do: id_uri.host == other_uri.host && id_uri.port == other_uri.port
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
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
|
|
|
|
|
2019-05-31 17:58:03 +02:00
|
|
|
|
@doc """
|
2020-11-06 15:43:38 +01:00
|
|
|
|
Return AS Link data from
|
|
|
|
|
|
|
|
|
|
* a `Plug.Upload` struct, stored an returned
|
2020-11-26 11:41:13 +01:00
|
|
|
|
* a `Media`, directly returned
|
|
|
|
|
* a map containing media information, stored, saved and returned
|
2020-11-06 15:43:38 +01:00
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
|
Save media data from %Plug.Upload{} and return AS Link data.
|
2019-05-31 17:58:03 +02:00
|
|
|
|
"""
|
2020-11-26 11:41:13 +01:00
|
|
|
|
def make_media_data(%Plug.Upload{} = media, opts) do
|
|
|
|
|
case Mobilizon.Web.Upload.store(media, opts) do
|
|
|
|
|
{:ok, media} ->
|
|
|
|
|
media
|
2019-07-23 18:06:22 +02:00
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
|
{:error, _err} ->
|
2019-07-23 18:06:22 +02:00
|
|
|
|
nil
|
2019-05-22 14:12:11 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
|
def make_media_data(media) when is_map(media) do
|
2021-06-10 09:36:25 +02:00
|
|
|
|
with {:ok, %{url: url} = uploaded} <-
|
2020-11-26 11:41:13 +01:00
|
|
|
|
Mobilizon.Web.Upload.store(media.file),
|
|
|
|
|
{:media_exists, nil} <- {:media_exists, Mobilizon.Medias.get_media_by_url(url)},
|
|
|
|
|
{:ok, %Media{file: _file} = media} <-
|
|
|
|
|
Mobilizon.Medias.create_media(%{
|
2021-06-10 09:36:25 +02:00
|
|
|
|
file: Map.take(uploaded, [:url, :name, :content_type, :size]),
|
|
|
|
|
metadata: Map.take(uploaded, [:width, :height, :blurhash]),
|
|
|
|
|
actor_id: media.actor_id
|
2019-05-22 14:12:11 +02:00
|
|
|
|
}) do
|
2020-11-26 11:41:13 +01:00
|
|
|
|
Converter.Media.model_to_as(media)
|
2019-10-14 11:41:57 +02:00
|
|
|
|
else
|
2020-11-26 11:41:13 +01:00
|
|
|
|
{:media_exists, %Media{file: _file} = media} ->
|
|
|
|
|
Converter.Media.model_to_as(media)
|
2019-10-14 11:41:57 +02:00
|
|
|
|
|
|
|
|
|
err ->
|
|
|
|
|
err
|
2019-05-22 14:12:11 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
|
def make_media_data(nil), do: nil
|
2019-05-22 14:12:11 +02:00
|
|
|
|
|
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
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug("Making announce data for a public object")
|
|
|
|
|
|
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
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug("Making announce data for a private object")
|
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
|
if actor_type == :Group do
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug("Making announce data for a group private object")
|
|
|
|
|
|
2021-03-08 11:43:07 +01:00
|
|
|
|
to =
|
2021-11-19 17:56:48 +01:00
|
|
|
|
Map.get(object, "to", []) ++
|
|
|
|
|
Map.get(object, "cc", []) ++ [actor.followers_url, actor.members_url]
|
2021-03-08 11:43:07 +01:00
|
|
|
|
|
|
|
|
|
{to, []}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
else
|
2021-11-19 17:40:42 +01:00
|
|
|
|
Logger.debug("Making announce data for a private object")
|
2020-02-18 08:57:00 +01:00
|
|
|
|
{[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
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 17:56:48 +01:00
|
|
|
|
data =
|
|
|
|
|
if object["attributedTo"],
|
|
|
|
|
do: Map.put(data, "attributedTo", object["attributedTo"]),
|
|
|
|
|
else: data
|
|
|
|
|
|
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"],
|
2021-07-29 17:50:47 +02:00
|
|
|
|
"attributedTo" => object["attributedTo"] || object["actor"],
|
2019-10-25 17:43:37 +02:00
|
|
|
|
"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
|
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
|
@spec make_add_data(map(), map(), map()) :: map()
|
2020-02-18 08:57:00 +01:00
|
|
|
|
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
|
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
|
@spec make_move_data(map(), map(), map(), map()) :: map()
|
2020-02-18 08:57:00 +01:00
|
|
|
|
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
|
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
|
@spec pem_to_public_key(String.t()) :: {:RSAPublicKey, any(), any()}
|
2018-06-14 17:25:55 +02:00
|
|
|
|
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
|
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
|
@spec pem_to_public_key_pem(String.t()) :: String.t()
|
2018-06-14 17:25:55 +02:00
|
|
|
|
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 an actor.
|
|
|
|
|
"""
|
2021-11-16 15:46:23 +01:00
|
|
|
|
@spec sign_fetch(Enum.t(), Actor.t(), String.t(), String.t(), Keyword.t()) :: Enum.t()
|
|
|
|
|
def sign_fetch(headers, actor, id, date, options \\ []) do
|
|
|
|
|
if Mobilizon.Config.get([:activitypub, :sign_object_fetches]) and
|
|
|
|
|
Keyword.get(options, :ignore_sign_object_fetches, false) == false 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
|
|
|
|
|
2021-04-21 18:57:23 +02:00
|
|
|
|
@spec label_in_collection?(any(), any()) :: boolean()
|
|
|
|
|
defp label_in_collection?(url, coll) when is_binary(coll), do: url == coll
|
|
|
|
|
defp label_in_collection?(url, coll) when is_list(coll), do: url in coll
|
|
|
|
|
defp label_in_collection?(_, _), do: false
|
|
|
|
|
|
|
|
|
|
@spec label_in_message?(String.t(), map()) :: boolean()
|
|
|
|
|
def label_in_message?(label, params),
|
|
|
|
|
do:
|
|
|
|
|
[params["to"], params["cc"], params["bto"], params["bcc"]]
|
|
|
|
|
|> Enum.any?(&label_in_collection?(label, &1))
|
|
|
|
|
|
|
|
|
|
@spec unaddressed_message?(map()) :: boolean()
|
|
|
|
|
def unaddressed_message?(params),
|
|
|
|
|
do:
|
|
|
|
|
[params["to"], params["cc"], params["bto"], params["bcc"]]
|
|
|
|
|
|> Enum.all?(&is_nil(&1))
|
|
|
|
|
|
|
|
|
|
@spec recipient_in_message(Actor.t(), Actor.t(), map()) :: boolean()
|
|
|
|
|
def recipient_in_message(%Actor{url: url} = _recipient, %Actor{} = _actor, params),
|
|
|
|
|
do: label_in_message?(url, params) || unaddressed_message?(params)
|
|
|
|
|
|
|
|
|
|
defp extract_list(target) when is_binary(target), do: [target]
|
|
|
|
|
defp extract_list(lst) when is_list(lst), do: lst
|
|
|
|
|
defp extract_list(_), do: []
|
|
|
|
|
|
|
|
|
|
def maybe_splice_recipient(url, params) do
|
|
|
|
|
need_splice? =
|
|
|
|
|
!label_in_collection?(url, params["to"]) &&
|
|
|
|
|
!label_in_collection?(url, params["cc"])
|
|
|
|
|
|
|
|
|
|
if need_splice? do
|
|
|
|
|
cc_list = extract_list(params["cc"])
|
|
|
|
|
Map.put(params, "cc", [url | cc_list])
|
|
|
|
|
else
|
|
|
|
|
params
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
end
|