2018-05-17 11:32:23 +02:00
|
|
|
defmodule Eventos.Service.ActivityPub do
|
2018-06-14 18:15:27 +02:00
|
|
|
@moduledoc """
|
|
|
|
# ActivityPub
|
|
|
|
|
|
|
|
Every ActivityPub method
|
|
|
|
"""
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
alias Eventos.Events
|
2018-05-30 14:27:21 +02:00
|
|
|
alias Eventos.Events.{Event, Category}
|
2018-05-17 11:32:23 +02:00
|
|
|
alias Eventos.Service.ActivityPub.Transmogrifier
|
|
|
|
alias Eventos.Service.WebFinger
|
|
|
|
alias Eventos.Activity
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
alias Eventos.Actors
|
|
|
|
alias Eventos.Actors.Actor
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
alias Eventos.Service.Federator
|
|
|
|
|
|
|
|
import Logger
|
|
|
|
import Eventos.Service.ActivityPub.Utils
|
|
|
|
|
|
|
|
def get_recipients(data) do
|
|
|
|
(data["to"] || []) ++ (data["cc"] || [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert(map, local \\ true) when is_map(map) do
|
|
|
|
with map <- lazy_put_activity_defaults(map),
|
|
|
|
:ok <- insert_full_object(map) do
|
|
|
|
map = Map.put(map, "id", Ecto.UUID.generate())
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
activity = %Activity{
|
2018-07-27 10:45:35 +02:00
|
|
|
data: map,
|
|
|
|
local: local,
|
|
|
|
actor: map["actor"],
|
|
|
|
recipients: get_recipients(map)
|
|
|
|
}
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
# Notification.create_notifications(activity)
|
2018-05-19 20:29:11 +02:00
|
|
|
# stream_out(activity)
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, activity}
|
|
|
|
else
|
|
|
|
%Activity{} = activity -> {:ok, activity}
|
|
|
|
error -> {:error, error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_event_from_url(url) do
|
|
|
|
if object = Events.get_event_by_url!(url) do
|
|
|
|
{:ok, object}
|
|
|
|
else
|
|
|
|
Logger.info("Fetching #{url} via AP")
|
|
|
|
|
|
|
|
with true <- String.starts_with?(url, "http"),
|
|
|
|
{:ok, %{body: body, status_code: code}} when code in 200..299 <-
|
|
|
|
HTTPoison.get(
|
|
|
|
url,
|
|
|
|
[Accept: "application/activity+json"],
|
|
|
|
follow_redirect: true,
|
2018-06-14 18:15:27 +02:00
|
|
|
timeout: 10_000,
|
|
|
|
recv_timeout: 20_000
|
2018-05-17 11:32:23 +02:00
|
|
|
),
|
|
|
|
{:ok, data} <- Jason.decode(body),
|
|
|
|
nil <- Events.get_event_by_url!(data["id"]),
|
|
|
|
params <- %{
|
|
|
|
"type" => "Create",
|
|
|
|
"to" => data["to"],
|
|
|
|
"cc" => data["cc"],
|
|
|
|
"actor" => data["attributedTo"],
|
|
|
|
"object" => data
|
|
|
|
},
|
|
|
|
{:ok, activity} <- Transmogrifier.handle_incoming(params) do
|
|
|
|
{:ok, Events.get_event_by_url!(activity.data["object"]["id"])}
|
|
|
|
else
|
|
|
|
object = %Event{} -> {:ok, object}
|
2018-07-27 10:45:35 +02:00
|
|
|
e -> e
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(%{to: to, actor: actor, context: context, object: object} = params) do
|
|
|
|
additional = params[:additional] || %{}
|
|
|
|
# only accept false as false value
|
|
|
|
local = !(params[:local] == false)
|
|
|
|
published = params[:published]
|
|
|
|
|
|
|
|
with create_data <-
|
|
|
|
make_create_data(
|
|
|
|
%{to: to, actor: actor, published: published, context: context, object: object},
|
|
|
|
additional
|
|
|
|
),
|
|
|
|
{:ok, activity} <- insert(create_data, local),
|
|
|
|
:ok <- maybe_federate(activity) do
|
2018-07-27 10:45:35 +02:00
|
|
|
# {:ok, actor} <- Actors.increase_event_count(actor) do
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, activity}
|
2018-05-18 09:56:21 +02:00
|
|
|
else
|
|
|
|
err ->
|
|
|
|
Logger.debug("Something went wrong")
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(inspect(err))
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def accept(%{to: to, actor: actor, object: object} = params) do
|
|
|
|
# only accept false as false value
|
|
|
|
local = !(params[:local] == false)
|
|
|
|
|
|
|
|
with data <- %{"to" => to, "type" => "Accept", "actor" => actor, "object" => object},
|
|
|
|
{:ok, activity} <- insert(data, local),
|
|
|
|
:ok <- maybe_federate(activity) do
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
|
|
|
|
# only accept false as false value
|
|
|
|
local = !(params[:local] == false)
|
|
|
|
|
|
|
|
with data <- %{
|
2018-07-27 10:45:35 +02:00
|
|
|
"to" => to,
|
|
|
|
"cc" => cc,
|
|
|
|
"type" => "Update",
|
|
|
|
"actor" => actor,
|
|
|
|
"object" => object
|
|
|
|
},
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, activity} <- insert(data, local),
|
|
|
|
:ok <- maybe_federate(activity) do
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def follow(follower, followed, activity_id \\ nil, local \\ true) do
|
|
|
|
with data <- make_follow_data(follower, followed, activity_id),
|
|
|
|
{:ok, activity} <- insert(data, local),
|
|
|
|
:ok <- maybe_federate(activity) do
|
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def delete(%Event{url: url, organizer_actor: actor} = event, local \\ true) do
|
2018-05-17 11:32:23 +02:00
|
|
|
data = %{
|
|
|
|
"type" => "Delete",
|
2018-05-18 09:56:21 +02:00
|
|
|
"actor" => actor.url,
|
2018-05-17 11:32:23 +02:00
|
|
|
"object" => url,
|
2018-05-18 09:56:21 +02:00
|
|
|
"to" => [actor.url <> "/followers", "https://www.w3.org/ns/activitystreams#Public"]
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
with Events.delete_event(event),
|
|
|
|
{:ok, activity} <- insert(data, local),
|
2018-07-27 10:45:35 +02:00
|
|
|
:ok <- maybe_federate(activity) do
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, activity}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def create_public_activities(%Actor{} = actor) do
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def make_actor_from_url(url) do
|
2018-05-17 11:32:23 +02:00
|
|
|
with {:ok, data} <- fetch_and_prepare_user_from_url(url) do
|
2018-05-18 09:56:21 +02:00
|
|
|
Actors.insert_or_update_actor(data)
|
2018-05-17 11:32:23 +02:00
|
|
|
else
|
|
|
|
e ->
|
2018-05-18 09:56:21 +02:00
|
|
|
Logger.error("Failed to make actor from url")
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.error(inspect(e))
|
2018-05-17 11:32:23 +02:00
|
|
|
{:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
@spec find_or_make_actor_from_nickname(String.t()) :: tuple()
|
2018-05-30 14:27:21 +02:00
|
|
|
def find_or_make_actor_from_nickname(nickname) do
|
|
|
|
with %Actor{} = actor <- Actors.get_actor_by_name(nickname) do
|
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
nil -> make_actor_from_nickname(nickname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def make_actor_from_nickname(nickname) do
|
2018-05-17 11:32:23 +02:00
|
|
|
with {:ok, %{"url" => url}} when not is_nil(url) <- WebFinger.finger(nickname) do
|
2018-05-18 09:56:21 +02:00
|
|
|
make_actor_from_url(url)
|
2018-05-17 11:32:23 +02:00
|
|
|
else
|
|
|
|
_e -> {:error, "No ActivityPub URL found in WebFinger"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish(actor, activity) do
|
2018-05-18 09:56:21 +02:00
|
|
|
Logger.debug("Publishing an activity")
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
followers =
|
|
|
|
if actor.followers_url in activity.recipients do
|
|
|
|
{:ok, followers} = Actor.get_followers(actor)
|
|
|
|
followers |> Enum.filter(fn follower -> is_nil(follower.domain) end)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
remote_inboxes =
|
|
|
|
followers
|
|
|
|
|> Enum.map(fn follower -> follower.shared_inbox_url end)
|
|
|
|
|> Enum.uniq()
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
{:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
|
|
|
|
json = Jason.encode!(data)
|
|
|
|
|
|
|
|
Enum.each(remote_inboxes, fn inbox ->
|
|
|
|
Federator.enqueue(:publish_single_ap, %{
|
|
|
|
inbox: inbox,
|
|
|
|
json: json,
|
|
|
|
actor: actor,
|
|
|
|
id: activity.data["id"]
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do
|
|
|
|
Logger.info("Federating #{id} to #{inbox}")
|
|
|
|
host = URI.parse(inbox).host
|
|
|
|
|
|
|
|
signature =
|
|
|
|
Eventos.Service.HTTPSignatures.sign(actor, %{host: host, "content-length": byte_size(json)})
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
Logger.debug("signature")
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(inspect(signature))
|
|
|
|
|
|
|
|
{:ok, response} =
|
|
|
|
HTTPoison.post(
|
|
|
|
inbox,
|
|
|
|
json,
|
|
|
|
[{"Content-Type", "application/activity+json"}, {"signature", signature}],
|
|
|
|
hackney: [pool: :default]
|
|
|
|
)
|
|
|
|
|
|
|
|
Logger.debug(inspect(response))
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_and_prepare_user_from_url(url) do
|
|
|
|
Logger.debug("Fetching and preparing user from url")
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
with {:ok, %{status_code: 200, body: body}} <-
|
2018-07-27 10:45:35 +02:00
|
|
|
HTTPoison.get(url, [Accept: "application/activity+json"], follow_redirect: true),
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, data} <- Jason.decode(body) do
|
|
|
|
user_data_from_user_object(data)
|
|
|
|
else
|
|
|
|
e -> Logger.error("Could not decode user at fetch #{url}, #{inspect(e)}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_data_from_user_object(data) do
|
2018-07-27 10:45:35 +02:00
|
|
|
name =
|
|
|
|
if String.trim(data["name"]) === "" do
|
|
|
|
data["preferredUsername"]
|
|
|
|
else
|
|
|
|
data["name"]
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
user_data = %{
|
|
|
|
url: data["id"],
|
|
|
|
info: %{
|
|
|
|
"ap_enabled" => true,
|
2018-07-27 10:45:35 +02:00
|
|
|
"source_data" => data
|
2018-05-17 11:32:23 +02:00
|
|
|
},
|
2018-05-30 18:59:13 +02:00
|
|
|
avatar_url: data["icon"]["url"],
|
|
|
|
banner_url: data["image"]["url"],
|
2018-05-19 10:19:21 +02:00
|
|
|
name: name,
|
2018-05-18 09:56:21 +02:00
|
|
|
preferred_username: data["preferredUsername"],
|
2018-05-17 11:32:23 +02:00
|
|
|
follower_address: data["followers"],
|
2018-05-18 09:56:21 +02:00
|
|
|
summary: data["summary"],
|
2018-06-14 17:25:55 +02:00
|
|
|
keys: data["publicKey"]["publicKeyPem"],
|
2018-05-18 09:56:21 +02:00
|
|
|
inbox_url: data["inbox"],
|
|
|
|
outbox_url: data["outbox"],
|
|
|
|
following_url: data["following"],
|
|
|
|
followers_url: data["followers"],
|
2018-05-30 18:59:13 +02:00
|
|
|
shared_inbox_url: data["endpoints"]["sharedInbox"],
|
2018-05-18 09:56:21 +02:00
|
|
|
domain: URI.parse(data["id"]).host,
|
|
|
|
manually_approves_followers: data["manuallyApprovesFollowers"],
|
2018-07-27 10:45:35 +02:00
|
|
|
type: data["type"]
|
2018-05-17 11:32:23 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 18:59:13 +02:00
|
|
|
Logger.debug("user_data_from_user_object")
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(inspect(user_data))
|
2018-05-30 18:59:13 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, user_data}
|
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
@spec fetch_public_activities_for_actor(Actor.t(), integer(), integer()) :: list()
|
2018-05-30 14:27:21 +02:00
|
|
|
def fetch_public_activities_for_actor(%Actor{} = actor, page \\ 1, limit \\ 10) do
|
|
|
|
case actor.type do
|
|
|
|
:Person ->
|
|
|
|
{:ok, events, total} = Events.get_events_for_actor(actor, page, limit)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
|
|
activities =
|
|
|
|
Enum.map(events, fn event ->
|
|
|
|
{:ok, activity} = event_to_activity(event)
|
|
|
|
activity
|
|
|
|
end)
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
{activities, total}
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
:Service ->
|
|
|
|
bot = Actors.get_bot_by_actor(actor)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
case bot.type do
|
|
|
|
"ics" ->
|
|
|
|
{:ok, %HTTPoison.Response{body: body} = _resp} = HTTPoison.get(bot.source)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
|
|
|
ical_events =
|
|
|
|
body
|
|
|
|
|> ExIcal.parse()
|
|
|
|
|> ExIcal.by_range(DateTime.utc_now(), DateTime.utc_now() |> Timex.shift(years: 1))
|
|
|
|
|
|
|
|
activities =
|
|
|
|
ical_events
|
|
|
|
|> Enum.chunk_every(limit)
|
|
|
|
|> Enum.at(page - 1)
|
|
|
|
|> Enum.map(fn event ->
|
|
|
|
{:ok, activity} = ical_event_to_activity(event, actor, bot.source)
|
|
|
|
activity
|
|
|
|
end)
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
{activities, length(ical_events)}
|
|
|
|
end
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
defp event_to_activity(%Event{} = event, local \\ true) do
|
2018-05-17 11:32:23 +02:00
|
|
|
activity = %Activity{
|
|
|
|
data: event,
|
2018-05-30 14:27:21 +02:00
|
|
|
local: local,
|
2018-05-18 09:56:21 +02:00
|
|
|
actor: event.organizer_actor.url,
|
2018-05-17 11:32:23 +02:00
|
|
|
recipients: ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Notification.create_notifications(activity)
|
2018-07-27 10:45:35 +02:00
|
|
|
# stream_out(activity)
|
2018-05-17 11:32:23 +02:00
|
|
|
{:ok, activity}
|
|
|
|
end
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
defp ical_event_to_activity(%ExIcal.Event{} = ical_event, %Actor{} = actor, source) do
|
|
|
|
# Logger.debug(inspect ical_event)
|
|
|
|
# TODO : refactor me !
|
2018-07-27 10:45:35 +02:00
|
|
|
category =
|
|
|
|
if is_nil(ical_event.categories) do
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
ical_category = ical_event.categories |> hd() |> String.downcase()
|
|
|
|
|
|
|
|
case ical_category |> Events.get_category_by_title() do
|
|
|
|
nil ->
|
|
|
|
case Events.create_category(%{"title" => ical_category}) do
|
|
|
|
{:ok, %Category{} = category} -> category
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
|
|
|
|
category ->
|
|
|
|
category
|
|
|
|
end
|
2018-05-30 14:27:21 +02:00
|
|
|
end
|
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
{:ok, event} =
|
|
|
|
Events.create_event(%{
|
|
|
|
begins_on: ical_event.start,
|
|
|
|
ends_on: ical_event.end,
|
|
|
|
inserted_at: ical_event.stamp,
|
|
|
|
updated_at: ical_event.stamp,
|
|
|
|
description: ical_event.description |> sanitize_ical_event_strings,
|
|
|
|
title: ical_event.summary |> sanitize_ical_event_strings,
|
|
|
|
organizer_actor: actor,
|
|
|
|
category: category
|
|
|
|
})
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
event_to_activity(event, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp sanitize_ical_event_strings(string) when is_binary(string) do
|
|
|
|
string
|
|
|
|
|> String.replace(~s"\r\n", "")
|
|
|
|
|> String.replace(~s"\\,", ",")
|
|
|
|
end
|
|
|
|
|
|
|
|
defp sanitize_ical_event_strings(nil) do
|
|
|
|
nil
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|