2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Service.ActivityPub.Utils do
|
2018-06-14 18:15:27 +02:00
|
|
|
@moduledoc """
|
|
|
|
# Utils
|
|
|
|
|
|
|
|
Various utils
|
|
|
|
"""
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Repo
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events.Event
|
2018-11-08 16:13:47 +01:00
|
|
|
alias Mobilizon.Events.Comment
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Activity
|
|
|
|
alias MobilizonWeb
|
|
|
|
alias MobilizonWeb.Router.Helpers
|
|
|
|
alias MobilizonWeb.Endpoint
|
2018-11-08 15:57:46 +01:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-05-17 11:32:23 +02:00
|
|
|
alias Ecto.{Changeset, UUID}
|
|
|
|
import Ecto.Query
|
2018-11-08 17:41:47 +01:00
|
|
|
require Logger
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
def make_context(%Activity{data: %{"context" => context}}), do: context
|
|
|
|
def make_context(_), do: generate_context_id()
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
def make_json_ld_header do
|
|
|
|
%{
|
|
|
|
"@context" => [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
2018-11-12 09:05:31 +01:00
|
|
|
"https://litepub.github.io/litepub/context.jsonld",
|
2018-05-17 11:32:23 +02:00
|
|
|
%{
|
2018-11-12 09:05:31 +01:00
|
|
|
"sc" => "http://schema.org#",
|
2018-05-17 11:32:23 +02:00
|
|
|
"Hashtag" => "as:Hashtag",
|
2018-11-12 09:05:31 +01:00
|
|
|
"category" => "sc:category",
|
|
|
|
"uuid" => "sc:identifier"
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_date do
|
|
|
|
DateTime.utc_now() |> DateTime.to_iso8601()
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_activity_id do
|
|
|
|
generate_id("activities")
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_context_id do
|
|
|
|
generate_id("contexts")
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_id(type) do
|
2018-10-11 17:37:39 +02:00
|
|
|
"#{MobilizonWeb.Endpoint.url()}/#{type}/#{UUID.generate()}"
|
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")
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
priority =
|
|
|
|
case activity.data["type"] do
|
|
|
|
"Delete" -> 10
|
|
|
|
"Create" -> 1
|
|
|
|
_ -> 5
|
|
|
|
end
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
Mobilizon.Service.Federator.enqueue(:publish, activity, priority)
|
2018-05-17 11:32:23 +02:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def maybe_federate(_), do: :ok
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
def remote_actors(%{data: %{"to" => to} = data}) do
|
|
|
|
to = to ++ (data["cc"] || [])
|
|
|
|
|
|
|
|
to
|
|
|
|
|> Enum.map(fn url -> Actors.get_actor_by_url!(url) end)
|
|
|
|
|> 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
|
|
|
|
"""
|
|
|
|
def lazy_put_activity_defaults(map) do
|
|
|
|
if is_map(map["object"]) do
|
|
|
|
object = lazy_put_object_defaults(map["object"], map)
|
|
|
|
%{map | "object" => object}
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Adds an id and published date if they aren't there.
|
|
|
|
"""
|
|
|
|
def lazy_put_object_defaults(map, activity \\ %{}) do
|
|
|
|
map
|
|
|
|
|> Map.put_new_lazy("published", &make_date/0)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
2018-11-12 09:05:31 +01:00
|
|
|
def insert_full_object(object_data, local \\ false)
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
|
|
|
def insert_full_object(%{"object" => %{"type" => type} = object_data}, _local)
|
2018-05-17 11:32:23 +02:00
|
|
|
when is_map(object_data) and type == "Event" do
|
|
|
|
with {:ok, _} <- Events.create_event(object_data) do
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
2018-11-12 09:05:31 +01:00
|
|
|
def insert_full_object(%{"object" => %{"type" => type} = object_data}, local)
|
2018-05-17 11:32:23 +02:00
|
|
|
when is_map(object_data) and type == "Note" do
|
2018-08-24 11:34:00 +02:00
|
|
|
with {:ok, %Actor{id: actor_id}} <- Actors.get_or_fetch_by_url(object_data["actor"]) do
|
|
|
|
data = %{
|
|
|
|
"text" => object_data["content"],
|
|
|
|
"url" => object_data["id"],
|
|
|
|
"actor_id" => actor_id,
|
2018-11-08 15:57:46 +01:00
|
|
|
"in_reply_to_comment_id" => nil,
|
|
|
|
"event_id" => nil,
|
2018-11-12 09:05:31 +01:00
|
|
|
"uuid" => object_data["uuid"],
|
|
|
|
"local" => local
|
2018-08-24 11:34:00 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 15:57:46 +01:00
|
|
|
# We fetch the parent object
|
2018-11-08 17:40:26 +01:00
|
|
|
data =
|
|
|
|
if Map.has_key?(object_data, "inReplyTo") && object_data["inReplyTo"] != nil &&
|
|
|
|
object_data["inReplyTo"] != "" do
|
|
|
|
Logger.debug("Object has inReplyTo #{object_data["inReplyTo"]}")
|
|
|
|
|
2018-11-08 17:17:35 +01:00
|
|
|
case ActivityPub.fetch_object_from_url(object_data["inReplyTo"]) do
|
|
|
|
# Reply to an event (Comment)
|
|
|
|
{:ok, %Event{id: id}} ->
|
2018-11-08 17:40:26 +01:00
|
|
|
Logger.debug("Parent object is an event")
|
2018-11-08 17:17:35 +01:00
|
|
|
data |> Map.put("event_id", id)
|
|
|
|
|
|
|
|
# Reply to a comment (Comment)
|
|
|
|
{:ok, %Comment{id: id} = comment} ->
|
2018-11-08 17:40:26 +01:00
|
|
|
Logger.debug("Parent object is another comment")
|
|
|
|
|
2018-11-08 17:17:35 +01:00
|
|
|
data
|
|
|
|
|> Map.put("in_reply_to_comment_id", id)
|
|
|
|
|> Map.put("origin_comment_id", comment |> Comment.get_thread_id())
|
|
|
|
|
|
|
|
# Anthing else is kind of a MP
|
2018-11-08 17:48:05 +01:00
|
|
|
object ->
|
2018-11-08 17:40:26 +01:00
|
|
|
Logger.debug("Parent object is something we don't handle")
|
2018-11-08 17:48:05 +01:00
|
|
|
Logger.debug(inspect(object))
|
2018-11-08 17:17:35 +01:00
|
|
|
data
|
|
|
|
end
|
2018-11-08 17:40:26 +01:00
|
|
|
else
|
|
|
|
Logger.debug("No parent object for this comment")
|
|
|
|
data
|
|
|
|
end
|
2018-11-08 15:57:46 +01:00
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
Logger.info("comment data ready to be inserted")
|
|
|
|
Logger.info(inspect(data))
|
|
|
|
|
|
|
|
with {:ok, comm} <- Events.create_comment(data) do
|
|
|
|
Logger.info("comment inserted")
|
|
|
|
Logger.info(inspect(comm))
|
|
|
|
:ok
|
2018-11-07 19:14:31 +01:00
|
|
|
else
|
|
|
|
err ->
|
|
|
|
Logger.error("Error while inserting a remote comment inside database")
|
2018-11-07 19:15:11 +01:00
|
|
|
Logger.error(inspect(err))
|
2018-11-07 19:14:31 +01:00
|
|
|
{:error, err}
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
def insert_full_object(_, _), do: :ok
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
#### Like-related helpers
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# @doc """
|
|
|
|
# Returns an existing like if a user already liked an object
|
|
|
|
# """
|
|
|
|
# def get_existing_like(actor, %{data: %{"id" => id}}) do
|
|
|
|
# query =
|
|
|
|
# from(
|
|
|
|
# activity in Activity,
|
|
|
|
# where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
|
|
|
|
# # this is to use the index
|
|
|
|
# where:
|
|
|
|
# fragment(
|
|
|
|
# "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
|
|
|
|
# activity.data,
|
|
|
|
# activity.data,
|
|
|
|
# ^id
|
|
|
|
# ),
|
|
|
|
# where: fragment("(?)->>'type' = 'Like'", activity.data)
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# Repo.one(query)
|
|
|
|
# end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-11-07 19:14:59 +01:00
|
|
|
def make_comment_data(
|
|
|
|
actor,
|
|
|
|
to,
|
|
|
|
content_html,
|
2018-11-12 09:05:31 +01:00
|
|
|
# attachments,
|
|
|
|
inReplyTo \\ nil,
|
|
|
|
# tags,
|
2018-11-07 19:14:59 +01:00
|
|
|
cw \\ nil,
|
|
|
|
cc \\ []
|
|
|
|
) do
|
2018-11-12 09:05:31 +01:00
|
|
|
Logger.debug("Making comment data")
|
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
2018-11-07 19:14:59 +01:00
|
|
|
object = %{
|
|
|
|
"type" => "Note",
|
|
|
|
"to" => to,
|
2018-11-12 09:05:31 +01:00
|
|
|
# "cc" => cc,
|
2018-11-07 19:14:59 +01:00
|
|
|
"content" => content_html,
|
2018-11-12 09:05:31 +01:00
|
|
|
# "summary" => cw,
|
|
|
|
# "attachment" => attachments,
|
2018-11-07 19:14:59 +01:00
|
|
|
"actor" => actor,
|
2018-11-12 09:06:44 +01:00
|
|
|
"id" => "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}",
|
|
|
|
"uuid" => uuid
|
2018-11-12 09:05:31 +01:00
|
|
|
# "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
|
2018-11-07 19:14:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if inReplyTo do
|
|
|
|
object
|
2018-11-12 09:05:31 +01:00
|
|
|
|> Map.put("inReplyTo", inReplyTo)
|
2018-11-07 19:14:59 +01:00
|
|
|
else
|
|
|
|
object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def make_like_data(%Actor{url: url} = actor, %{data: %{"id" => id}} = object, activity_id) do
|
2018-05-17 11:32:23 +02:00
|
|
|
data = %{
|
|
|
|
"type" => "Like",
|
|
|
|
"actor" => url,
|
|
|
|
"object" => id,
|
|
|
|
"to" => [actor.follower_address, object.data["actor"]],
|
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"context" => object.data["context"]
|
|
|
|
}
|
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_element_in_object(property, element, object) do
|
|
|
|
with new_data <-
|
|
|
|
object.data
|
|
|
|
|> Map.put("#{property}_count", length(element))
|
|
|
|
|> Map.put("#{property}s", element),
|
|
|
|
changeset <- Changeset.change(object, data: new_data),
|
|
|
|
{:ok, object} <- Repo.update(changeset) do
|
|
|
|
{:ok, object}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
# def update_likes_in_object(likes, object) do
|
|
|
|
# update_element_in_object("like", likes, object)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
|
|
|
|
# with likes <- [actor | object.data["likes"] || []] |> Enum.uniq() do
|
|
|
|
# update_likes_in_object(likes, object)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
|
|
|
|
# with likes <- (object.data["likes"] || []) |> List.delete(actor) do
|
|
|
|
# update_likes_in_object(likes, object)
|
|
|
|
# end
|
|
|
|
# end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
#### Follow-related helpers
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Makes a follow activity data for the given follower and followed
|
|
|
|
"""
|
2018-05-18 09:56:21 +02:00
|
|
|
def make_follow_data(%Actor{url: follower_id}, %Actor{url: followed_id}, activity_id) do
|
2018-11-12 09:05:31 +01:00
|
|
|
Logger.debug("Make follow data")
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
data = %{
|
|
|
|
"type" => "Follow",
|
|
|
|
"actor" => follower_id,
|
|
|
|
"to" => [followed_id],
|
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"object" => followed_id
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:05:31 +01:00
|
|
|
Logger.debug(inspect(data))
|
|
|
|
|
|
|
|
if activity_id,
|
|
|
|
do: Map.put(data, "id", "#{MobilizonWeb.Endpoint.url()}/follow/#{activity_id}/activity"),
|
|
|
|
else: data
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
#### Announce-related helpers
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make announce activity data for the given actor and object
|
|
|
|
"""
|
|
|
|
def make_announce_data(
|
2018-05-18 09:56:21 +02:00
|
|
|
%Actor{url: url} = user,
|
2018-05-17 11:32:23 +02:00
|
|
|
%Event{id: id} = object,
|
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
|
|
|
"type" => "Announce",
|
|
|
|
"actor" => url,
|
|
|
|
"object" => id,
|
|
|
|
"to" => [user.follower_address, object.data["actor"]],
|
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"context" => object.data["context"]
|
|
|
|
}
|
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_announce_to_object(%Activity{data: %{"actor" => actor}}, object) do
|
|
|
|
with announcements <- [actor | object.data["announcements"] || []] |> Enum.uniq() do
|
|
|
|
update_element_in_object("announcement", announcements, object)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#### Unfollow-related helpers
|
|
|
|
|
|
|
|
def make_unfollow_data(follower, followed, follow_activity) do
|
|
|
|
%{
|
|
|
|
"type" => "Undo",
|
|
|
|
"actor" => follower.url,
|
|
|
|
"to" => [followed.url],
|
|
|
|
"object" => follow_activity.data["id"]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#### Create-related helpers
|
|
|
|
|
|
|
|
def make_create_data(params, additional) do
|
|
|
|
published = params.published || make_date()
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Create",
|
|
|
|
"to" => params.to |> Enum.uniq(),
|
|
|
|
"actor" => params.actor.url,
|
|
|
|
"object" => params.object,
|
2018-11-12 09:05:31 +01:00
|
|
|
"published" => published
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
|> 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
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts PEM encoded keys to a private key representation
|
|
|
|
"""
|
|
|
|
def pem_to_private_key(pem) do
|
|
|
|
[private_key_code] = :public_key.pem_decode(pem)
|
|
|
|
:public_key.pem_entry_decode(private_key_code)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts PEM encoded keys to a PEM public key representation
|
|
|
|
"""
|
|
|
|
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
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|