This commit is contained in:
Thomas Citharel
2018-11-12 09:05:31 +01:00
parent 6e691640de
commit 5721c5fe05
16 changed files with 289 additions and 228 deletions

View File

@@ -16,7 +16,7 @@ defmodule Mobilizon.Service.ActivityPub do
alias Mobilizon.Service.Federator
import Logger
require Logger
import Mobilizon.Service.ActivityPub.Utils
def get_recipients(data) do
@@ -24,9 +24,21 @@ defmodule Mobilizon.Service.ActivityPub do
end
def insert(map, local \\ true) when is_map(map) do
Logger.debug("preparing an activity")
Logger.debug(inspect(map))
with map <- lazy_put_activity_defaults(map),
:ok <- insert_full_object(map) do
map = Map.put(map, "id", Ecto.UUID.generate())
:ok <- insert_full_object(map, local) do
object_id =
cond do
is_map(map["object"]) ->
map["object"]["id"]
is_binary(map["object"]) ->
map["id"]
end
map = Map.put(map, "id", "#{object_id}/activity")
activity = %Activity{
data: map,
@@ -106,7 +118,8 @@ defmodule Mobilizon.Service.ActivityPub do
end
end
def create(%{to: to, actor: actor, context: context, object: object} = params) do
def create(%{to: to, actor: actor, object: object} = params) do
Logger.debug("creating an activity")
additional = params[:additional] || %{}
# only accept false as false value
local = !(params[:local] == false)
@@ -114,7 +127,7 @@ defmodule Mobilizon.Service.ActivityPub do
with create_data <-
make_create_data(
%{to: to, actor: actor, published: published, context: context, object: object},
%{to: to, actor: actor, published: published, object: object},
additional
),
{:ok, activity} <- insert(create_data, local),
@@ -157,10 +170,14 @@ defmodule Mobilizon.Service.ActivityPub do
end
def follow(%Actor{} = follower, %Actor{} = followed, activity_id \\ nil, local \\ true) do
with data <- make_follow_data(follower, followed, activity_id),
with {:ok, follow} <- Actor.follow(follower, followed, true),
data <- make_follow_data(follower, followed, follow.id),
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
else
{err, _} when err in [:already_following, :suspended] ->
{:error, err}
end
end
@@ -199,9 +216,9 @@ defmodule Mobilizon.Service.ActivityPub do
def create_public_activities(%Actor{} = actor) do
end
def make_actor_from_url(url) do
with {:ok, data} <- fetch_and_prepare_user_from_url(url) do
Actors.insert_or_update_actor(data)
def make_actor_from_url(url, preload \\ false) do
with {:ok, data} <- fetch_and_prepare_actor_from_url(url) do
Actors.insert_or_update_actor(data, preload)
else
# Request returned 410
{:error, :actor_deleted} ->
@@ -243,12 +260,13 @@ defmodule Mobilizon.Service.ActivityPub do
end
remote_inboxes =
followers
(remote_actors(activity) ++ followers)
|> Enum.map(fn follower -> follower.shared_inbox_url end)
|> Enum.uniq()
{:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
json = Jason.encode!(data)
Logger.debug("Remote inboxes are : #{inspect(remote_inboxes)}")
Enum.each(remote_inboxes, fn inbox ->
Federator.enqueue(:publish_single_ap, %{
@@ -273,6 +291,9 @@ defmodule Mobilizon.Service.ActivityPub do
Logger.debug("signature")
Logger.debug(inspect(signature))
Logger.debug("body json")
Logger.debug(inspect(json))
{:ok, response} =
HTTPoison.post(
inbox,
@@ -284,8 +305,8 @@ defmodule Mobilizon.Service.ActivityPub do
Logger.debug(inspect(response))
end
def fetch_and_prepare_user_from_url(url) do
Logger.debug("Fetching and preparing user from url")
def fetch_and_prepare_actor_from_url(url) do
Logger.debug("Fetching and preparing actor from url")
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
HTTPoison.get(url, [Accept: "application/activity+json"], follow_redirect: true),
@@ -297,7 +318,7 @@ defmodule Mobilizon.Service.ActivityPub do
{:error, :actor_deleted}
e ->
Logger.error("Could not decode user at fetch #{url}, #{inspect(e)}")
Logger.error("Could not decode actor at fetch #{url}, #{inspect(e)}")
e
end
end

View File

@@ -18,7 +18,6 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
object
|> Map.put("actor", object["attributedTo"])
|> fix_attachments
|> fix_context
# |> fix_in_reply_to
|> fix_tag
end
@@ -31,10 +30,6 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
#
# object
# |> Map.put("inReplyTo", replied_object.data["id"])
# |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
# |> Map.put("inReplyToStatusId", activity.id)
# |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
# |> Map.put("context", replied_object.data["context"] || object["conversation"])
#
# e ->
# Logger.error("Couldn't fetch #{object["inReplyTo"]} #{inspect(e)}")
@@ -44,11 +39,6 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
def fix_in_reply_to(object), do: object
def fix_context(object) do
object
|> Map.put("context", object["conversation"])
end
def fix_attachments(object) do
attachments =
(object["attachment"] || [])
@@ -87,7 +77,6 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
to: data["to"],
object: object,
actor: actor,
context: object["conversation"],
local: false,
published: data["published"],
additional:
@@ -104,12 +93,11 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
def handle_incoming(
%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data
) do
with {:ok, %Actor{} = followed} <- Actors.get_or_fetch_by_url(followed),
with {:ok, %Actor{} = followed} <- Actors.get_or_fetch_by_url(followed, true),
{:ok, %Actor{} = follower} <- Actors.get_or_fetch_by_url(follower),
{:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
ActivityPub.accept(%{to: [follower.url], actor: followed.url, object: data, local: true})
# Actors.follow(follower, followed)
{:ok, activity}
else
e ->
@@ -225,11 +213,10 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
object
# |> set_sensitive
# |> add_hashtags
# |> add_mention_tags
|> add_mention_tags
# |> add_emoji_tags
|> add_attributed_to
# |> prepare_attachments
|> set_conversation
|> set_reply_to_uri
end
@@ -239,6 +226,8 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
"""
def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
Logger.debug("Prepare outgoing for a note creation")
object =
object
|> prepare_object
@@ -248,6 +237,8 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
|> Map.put("object", object)
|> Map.put("@context", "https://www.w3.org/ns/activitystreams")
Logger.debug("Finished prepare outgoing for a note creation")
{:ok, data}
end
@@ -304,22 +295,23 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
# end
# end
#
# def add_mention_tags(object) do
# recipients = object["to"] ++ (object["cc"] || [])
#
# mentions =
# recipients
# |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
# |> Enum.filter(& &1)
# |> Enum.map(fn user ->
# %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
# end)
#
# tags = object["tag"] || []
#
# object
# |> Map.put("tag", tags ++ mentions)
# end
def add_mention_tags(object) do
recipients = object["to"] ++ (object["cc"] || [])
mentions =
recipients
|> Enum.map(fn url -> Actors.get_actor_by_url!(url) end)
|> Enum.filter(& &1)
|> Enum.map(fn actor ->
%{"type" => "Mention", "href" => actor.url, "name" => "@#{actor.preferred_username}"}
end)
tags = object["tag"] || []
object
|> Map.put("tag", tags ++ mentions)
end
#
# # TODO: we should probably send mtime instead of unix epoch time for updated
# def add_emoji_tags(object) do
@@ -342,9 +334,6 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
# |> Map.put("tag", tags ++ out)
# end
#
def set_conversation(object) do
Map.put(object, "conversation", object["context"])
end
#
# def set_sensitive(object) do
@@ -370,84 +359,4 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
# object
# |> Map.put("attachment", attachments)
# end
#
# defp user_upgrade_task(user) do
# old_follower_address = User.ap_followers(user)
#
# q =
# from(
# u in User,
# where: ^old_follower_address in u.following,
# update: [
# set: [
# following:
# fragment(
# "array_replace(?,?,?)",
# u.following,
# ^old_follower_address,
# ^user.follower_address
# )
# ]
# ]
# )
#
# Repo.update_all(q, [])
#
# maybe_retire_websub(user.ap_id)
#
# # Only do this for recent activties, don't go through the whole db.
# # Only look at the last 1000 activities.
# since = (Repo.aggregate(Activity, :max, :id) || 0) - 1_000
#
# q =
# from(
# a in Activity,
# where: ^old_follower_address in a.recipients,
# where: a.id > ^since,
# update: [
# set: [
# recipients:
# fragment(
# "array_replace(?,?,?)",
# a.recipients,
# ^old_follower_address,
# ^user.follower_address
# )
# ]
# ]
# )
#
# Repo.update_all(q, [])
# end
#
# def upgrade_user_from_ap_id(ap_id, async \\ true) do
# with %User{local: false} = user <- User.get_by_ap_id(ap_id),
# {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
# data =
# data
# |> Map.put(:info, Map.merge(user.info, data[:info]))
#
# already_ap = User.ap_enabled?(user)
#
# {:ok, user} =
# User.upgrade_changeset(user, data)
# |> Repo.update()
#
# if !already_ap do
# # This could potentially take a long time, do it in the background
# if async do
# Task.start(fn ->
# user_upgrade_task(user)
# end)
# else
# user_upgrade_task(user)
# end
# end
#
# {:ok, user}
# else
# e -> e
# end
# end
#
end

View File

@@ -20,16 +20,19 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
import Ecto.Query
require Logger
def make_context(%Activity{data: %{"context" => context}}), do: context
def make_context(_), do: generate_context_id()
def make_json_ld_header do
%{
"@context" => [
"https://www.w3.org/ns/activitystreams",
"https://litepub.github.io/litepub/context.jsonld",
%{
"manuallyApprovesFollowers" => "as:manuallyApprovesFollowers",
"sensitive" => "as:sensitive",
"sc" => "http://schema.org#",
"Hashtag" => "as:Hashtag",
"toot" => "http://joinmastodon.org/ns#",
"Emoji" => "toot:Emoji"
"category" => "sc:category",
"uuid" => "sc:identifier"
}
]
}
@@ -74,6 +77,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
Enqueues an activity for federation if it's local
"""
def maybe_federate(%Activity{local: true} = activity) do
Logger.debug("Maybe federate an activity")
priority =
case activity.data["type"] do
"Delete" -> 10
@@ -87,6 +92,14 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
def maybe_federate(_), do: :ok
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
@doc """
Adds an id and a published data if they aren't there,
also adds it to an included object
@@ -123,7 +136,12 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
@doc """
Inserts a full object if it is contained in an activity.
"""
def insert_full_object(%{"object" => %{"type" => type} = object_data})
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)
when is_map(object_data) and type == "Event" do
with {:ok, _} <- Events.create_event(object_data) do
:ok
@@ -133,7 +151,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
@doc """
Inserts a full object if it is contained in an activity.
"""
def insert_full_object(%{"object" => %{"type" => type} = object_data})
def insert_full_object(%{"object" => %{"type" => type} = object_data}, local)
when is_map(object_data) and type == "Note" do
with {:ok, %Actor{id: actor_id}} <- Actors.get_or_fetch_by_url(object_data["actor"]) do
data = %{
@@ -142,8 +160,9 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
"actor_id" => actor_id,
"in_reply_to_comment_id" => nil,
"event_id" => nil,
"uuid" => object_data["uuid"],
# probably
"local" => false
"local" => local
}
# We fetch the parent object
@@ -193,7 +212,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
end
end
def insert_full_object(_), do: :ok
def insert_full_object(_, _), do: :ok
# def update_object_in_activities(%{data: %{"id" => id}} = object) do
# # TODO
@@ -236,30 +255,31 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
def make_comment_data(
actor,
to,
context,
content_html,
attachments,
inReplyTo,
tags,
# attachments,
inReplyTo \\ nil,
# tags,
cw \\ nil,
cc \\ []
) do
Logger.debug("Making comment data")
uuid = Ecto.UUID.generate()
object = %{
"type" => "Note",
"to" => to,
"cc" => cc,
# "cc" => cc,
"content" => content_html,
"summary" => cw,
"context" => context,
"attachment" => attachments,
# "summary" => cw,
# "attachment" => attachments,
"actor" => actor,
"tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
"id" => "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}"
# "tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
}
if inReplyTo do
object
|> Map.put("inReplyTo", inReplyTo.data["object"]["id"])
|> Map.put("inReplyToStatusId", inReplyTo.id)
|> Map.put("inReplyTo", inReplyTo)
else
object
end
@@ -311,6 +331,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
Makes a follow activity data for the given follower and followed
"""
def make_follow_data(%Actor{url: follower_id}, %Actor{url: followed_id}, activity_id) do
Logger.debug("Make follow data")
data = %{
"type" => "Follow",
"actor" => follower_id,
@@ -319,7 +341,11 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
"object" => followed_id
}
if activity_id, do: Map.put(data, "id", activity_id), else: data
Logger.debug(inspect(data))
if activity_id,
do: Map.put(data, "id", "#{MobilizonWeb.Endpoint.url()}/follow/#{activity_id}/activity"),
else: data
end
# def fetch_latest_follow(%Actor{url: follower_id}, %Actor{url: followed_id}) do
@@ -388,8 +414,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
"to" => params.to |> Enum.uniq(),
"actor" => params.actor.url,
"object" => params.object,
"published" => published,
"context" => params.context
"published" => published
}
|> Map.merge(additional)
end

View File

@@ -36,7 +36,7 @@ defmodule Mobilizon.Service.Federator do
Logger.debug(inspect(activity))
Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
with actor when not is_nil(actor) <- Actors.get_actor_by_url(activity.data["actor"]) do
with actor when not is_nil(actor) <- Actors.get_actor_by_url!(activity.data["actor"]) do
Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
ActivityPub.publish(actor, activity)
end

View File

@@ -90,17 +90,21 @@ defmodule Mobilizon.Service.HTTPSignatures do
end
def sign(%Actor{} = actor, headers) do
sigstring = build_signing_string(headers, Map.keys(headers))
signature = sigstring |> :public_key.sign(:sha256, actor.keys) |> Base.encode64()
[
keyId: actor.url <> "#main-key",
algorithm: "rsa-sha256",
headers: headers |> Map.keys() |> Enum.join(" "),
signature: signature
]
|> Enum.map(fn {k, v} -> "#{k}=\"#{v}\"" end)
|> Enum.join(",")
with sigstring <- build_signing_string(headers, Map.keys(headers)),
{:ok, key} <- actor.keys |> prepare_public_key(),
signature <- sigstring |> :public_key.sign(:sha256, key) |> Base.encode64() do
[
keyId: actor.url <> "#main-key",
algorithm: "rsa-sha256",
headers: headers |> Map.keys() |> Enum.join(" "),
signature: signature
]
|> Enum.map(fn {k, v} -> "#{k}=\"#{v}\"" end)
|> Enum.join(",")
else
err ->
Logger.error("Unable to sign headers")
Logger.error(inspect(err))
end
end
end

View File

@@ -38,7 +38,7 @@ defmodule Mobilizon.Service.WebFinger do
{:ok, represent_user(user, "JSON")}
else
_e ->
with user when not is_nil(user) <- Actors.get_actor_by_url(resource) do
with user when not is_nil(user) <- Actors.get_actor_by_url!(resource) do
{:ok, represent_user(user, "JSON")}
else
_e ->