2018-12-24 12:34:45 +00: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 10:24:04 +00:00
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/activity_pub/utils.ex
|
2018-12-24 12:34:45 +00:00
|
|
|
|
2018-10-11 15:37:39 +00:00
|
|
|
defmodule Mobilizon.Service.ActivityPub.Utils do
|
2018-06-14 16:15:27 +00:00
|
|
|
@moduledoc """
|
|
|
|
# Utils
|
|
|
|
|
|
|
|
Various utils
|
|
|
|
"""
|
|
|
|
|
2018-10-11 15:37:39 +00:00
|
|
|
alias Mobilizon.Repo
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events.Event
|
2018-11-08 15:13:47 +00:00
|
|
|
alias Mobilizon.Events.Comment
|
2018-10-11 15:37:39 +00:00
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Activity
|
2018-11-08 14:57:46 +00:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-12-14 16:41:55 +00:00
|
|
|
alias Ecto.Changeset
|
2018-11-08 16:41:47 +00:00
|
|
|
require Logger
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-12-14 16:41:55 +00: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.
|
|
|
|
def get_url(object) do
|
|
|
|
case object do
|
|
|
|
%{"id" => id} -> id
|
|
|
|
id -> id
|
|
|
|
end
|
|
|
|
end
|
2018-11-12 08:05:31 +00:00
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
def make_json_ld_header do
|
|
|
|
%{
|
|
|
|
"@context" => [
|
|
|
|
"https://www.w3.org/ns/activitystreams",
|
2018-11-12 08:05:31 +00:00
|
|
|
"https://litepub.github.io/litepub/context.jsonld",
|
2018-05-17 09:32:23 +00:00
|
|
|
%{
|
2018-11-12 08:05:31 +00:00
|
|
|
"sc" => "http://schema.org#",
|
2018-05-17 09:32:23 +00:00
|
|
|
"Hashtag" => "as:Hashtag",
|
2018-11-12 08:05:31 +00:00
|
|
|
"category" => "sc:category",
|
|
|
|
"uuid" => "sc:identifier"
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_date do
|
2019-02-14 13:19:55 +00:00
|
|
|
DateTime.utc_now() |> DateTime.truncate(:second) |> DateTime.to_iso8601()
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Enqueues an activity for federation if it's local
|
|
|
|
"""
|
|
|
|
def maybe_federate(%Activity{local: true} = activity) do
|
2018-11-12 08:05:31 +00:00
|
|
|
Logger.debug("Maybe federate an activity")
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
priority =
|
|
|
|
case activity.data["type"] do
|
|
|
|
"Delete" -> 10
|
|
|
|
"Create" -> 1
|
|
|
|
_ -> 5
|
|
|
|
end
|
|
|
|
|
2018-10-11 15:37:39 +00:00
|
|
|
Mobilizon.Service.Federator.enqueue(:publish, activity, priority)
|
2018-05-17 09:32:23 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def maybe_federate(_), do: :ok
|
|
|
|
|
2018-11-12 08:05:31 +00:00
|
|
|
def remote_actors(%{data: %{"to" => to} = data}) do
|
|
|
|
to = to ++ (data["cc"] || [])
|
|
|
|
|
|
|
|
to
|
2018-11-12 17:17:53 +00:00
|
|
|
|> Enum.map(fn url -> Actors.get_actor_by_url(url) end)
|
|
|
|
|> Enum.map(fn {status, actor} ->
|
|
|
|
case status do
|
|
|
|
:ok ->
|
|
|
|
actor
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|> Enum.map(& &1)
|
2018-11-12 08:05:31 +00:00
|
|
|
|> Enum.filter(fn actor -> actor && !is_nil(actor.domain) end)
|
|
|
|
end
|
|
|
|
|
2018-05-17 09:32:23 +00: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
|
2018-11-12 22:30:47 +00:00
|
|
|
object = lazy_put_object_defaults(map["object"])
|
2018-05-17 09:32:23 +00:00
|
|
|
%{map | "object" => object}
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Adds an id and published date if they aren't there.
|
|
|
|
"""
|
2018-11-12 22:30:47 +00:00
|
|
|
def lazy_put_object_defaults(map) do
|
|
|
|
Map.put_new_lazy(map, "published", &make_date/0)
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
@doc """
|
|
|
|
Converts an AP object data to our internal data structure
|
|
|
|
"""
|
|
|
|
def object_to_event_data(object) do
|
|
|
|
{:ok, %Actor{id: actor_id}} = Actors.get_actor_by_url(object["actor"])
|
|
|
|
|
|
|
|
%{
|
|
|
|
"title" => object["name"],
|
|
|
|
"description" => object["content"],
|
|
|
|
"organizer_actor_id" => actor_id,
|
|
|
|
"begins_on" => object["begins_on"],
|
2019-02-22 15:54:01 +00:00
|
|
|
"category" => object["category"],
|
2019-01-21 14:08:22 +00:00
|
|
|
"url" => object["id"],
|
|
|
|
"uuid" => object["uuid"]
|
2018-12-14 16:41:55 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
2018-12-14 16:41:55 +00:00
|
|
|
def insert_full_object(object_data)
|
2018-11-12 08:05:31 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
2018-12-14 16:41:55 +00:00
|
|
|
def insert_full_object(%{"object" => %{"type" => "Event"} = object_data})
|
|
|
|
when is_map(object_data) do
|
|
|
|
with object_data <- object_to_event_data(object_data),
|
|
|
|
{:ok, _} <- Events.create_event(object_data) do
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def insert_full_object(%{"object" => %{"type" => "Group"} = object_data})
|
|
|
|
when is_map(object_data) do
|
|
|
|
with object_data <-
|
|
|
|
Map.put(object_data, "preferred_username", object_data["preferredUsername"]),
|
|
|
|
{:ok, _} <- Actors.create_group(object_data) do
|
2018-05-17 09:32:23 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Inserts a full object if it is contained in an activity.
|
|
|
|
"""
|
2018-12-14 16:41:55 +00:00
|
|
|
def insert_full_object(%{"object" => %{"type" => "Note"} = object_data})
|
|
|
|
when is_map(object_data) do
|
|
|
|
Logger.debug("Inserting full comment")
|
|
|
|
Logger.debug(inspect(object_data))
|
|
|
|
|
2018-08-24 09:34:00 +00: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 14:57:46 +00:00
|
|
|
"in_reply_to_comment_id" => nil,
|
|
|
|
"event_id" => nil,
|
2018-12-14 16:41:55 +00:00
|
|
|
"uuid" => object_data["uuid"]
|
2018-08-24 09:34:00 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 14:57:46 +00:00
|
|
|
# We fetch the parent object
|
2018-12-14 16:41:55 +00:00
|
|
|
Logger.debug("We're fetching the parent object")
|
|
|
|
|
2018-11-08 16:40:26 +00:00
|
|
|
data =
|
|
|
|
if Map.has_key?(object_data, "inReplyTo") && object_data["inReplyTo"] != nil &&
|
|
|
|
object_data["inReplyTo"] != "" do
|
2019-01-03 13:59:59 +00:00
|
|
|
Logger.debug(fn -> "Object has inReplyTo #{object_data["inReplyTo"]}" end)
|
2018-11-08 16:40:26 +00:00
|
|
|
|
2018-11-08 16:17:35 +00:00
|
|
|
case ActivityPub.fetch_object_from_url(object_data["inReplyTo"]) do
|
|
|
|
# Reply to an event (Comment)
|
|
|
|
{:ok, %Event{id: id}} ->
|
2018-11-08 16:40:26 +00:00
|
|
|
Logger.debug("Parent object is an event")
|
2018-11-08 16:17:35 +00:00
|
|
|
data |> Map.put("event_id", id)
|
|
|
|
|
|
|
|
# Reply to a comment (Comment)
|
|
|
|
{:ok, %Comment{id: id} = comment} ->
|
2018-11-08 16:40:26 +00:00
|
|
|
Logger.debug("Parent object is another comment")
|
|
|
|
|
2018-11-08 16:17:35 +00:00
|
|
|
data
|
|
|
|
|> Map.put("in_reply_to_comment_id", id)
|
|
|
|
|> Map.put("origin_comment_id", comment |> Comment.get_thread_id())
|
|
|
|
|
2019-02-22 15:11:57 +00:00
|
|
|
# Anything else is kind of a MP
|
2018-12-14 16:41:55 +00:00
|
|
|
{:error, object} ->
|
2018-11-08 16:40:26 +00:00
|
|
|
Logger.debug("Parent object is something we don't handle")
|
2018-11-08 16:48:05 +00:00
|
|
|
Logger.debug(inspect(object))
|
2018-11-08 16:17:35 +00:00
|
|
|
data
|
|
|
|
end
|
2018-11-08 16:40:26 +00:00
|
|
|
else
|
|
|
|
Logger.debug("No parent object for this comment")
|
|
|
|
data
|
|
|
|
end
|
2018-11-08 14:57:46 +00:00
|
|
|
|
2018-11-12 22:30:47 +00:00
|
|
|
with {:ok, _comment} <- Events.create_comment(data) do
|
2018-08-24 09:34:00 +00:00
|
|
|
:ok
|
2018-11-07 18:14:31 +00:00
|
|
|
else
|
|
|
|
err ->
|
|
|
|
Logger.error("Error while inserting a remote comment inside database")
|
2018-11-07 18:15:11 +00:00
|
|
|
Logger.error(inspect(err))
|
2018-11-07 18:14:31 +00:00
|
|
|
{:error, err}
|
2018-08-24 09:34:00 +00:00
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
def insert_full_object(_), do: :ok
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
#### Like-related helpers
|
|
|
|
|
2018-07-27 08:45:35 +00: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 09:32:23 +00:00
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
@doc """
|
|
|
|
Make an AP event object from an set of values
|
|
|
|
"""
|
2019-02-22 15:11:57 +00:00
|
|
|
@spec make_event_data(
|
|
|
|
String.t(),
|
|
|
|
String.t(),
|
|
|
|
String.t(),
|
|
|
|
String.t(),
|
|
|
|
list(),
|
|
|
|
list(),
|
|
|
|
map(),
|
|
|
|
String.t()
|
|
|
|
) :: map()
|
2018-12-14 16:41:55 +00:00
|
|
|
def make_event_data(
|
|
|
|
actor,
|
|
|
|
to,
|
|
|
|
title,
|
|
|
|
content_html,
|
|
|
|
# attachments,
|
|
|
|
tags \\ [],
|
|
|
|
# _cw \\ nil,
|
|
|
|
cc \\ [],
|
|
|
|
metadata \\ %{},
|
|
|
|
category \\ ""
|
|
|
|
) do
|
|
|
|
Logger.debug("Making event data")
|
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Event",
|
|
|
|
"to" => to,
|
|
|
|
"cc" => cc,
|
|
|
|
"content" => content_html,
|
|
|
|
"name" => title,
|
|
|
|
# "summary" => cw,
|
|
|
|
# "attachment" => attachments,
|
|
|
|
"begins_on" => metadata.begins_on,
|
|
|
|
"category" => category,
|
|
|
|
"actor" => actor,
|
|
|
|
"id" => "#{MobilizonWeb.Endpoint.url()}/events/#{uuid}",
|
|
|
|
"uuid" => uuid,
|
|
|
|
"tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-02-22 15:11:57 +00:00
|
|
|
@spec make_event_data(Event.t(), list(String.t())) :: map()
|
2018-11-12 17:17:53 +00:00
|
|
|
def make_event_data(
|
2019-03-01 17:30:46 +00:00
|
|
|
%Event{} = event,
|
2018-11-12 17:17:53 +00:00
|
|
|
to \\ ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
) do
|
|
|
|
%{
|
|
|
|
"type" => "Event",
|
|
|
|
"to" => to,
|
2019-03-01 17:30:46 +00:00
|
|
|
"title" => event.title,
|
|
|
|
"actor" => event.organizer_actor.url,
|
|
|
|
"uuid" => event.uuid,
|
|
|
|
"category" => event.category,
|
|
|
|
"summary" => event.description,
|
|
|
|
"publish_at" => (event.publish_at || event.inserted_at) |> DateTime.to_iso8601(),
|
|
|
|
"updated_at" => event.updated_at |> DateTime.to_iso8601(),
|
|
|
|
"id" => "#{MobilizonWeb.Endpoint.url()}/events/#{event.uuid}"
|
2018-11-12 17:17:53 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make an AP comment object from an existing `Comment` structure.
|
|
|
|
"""
|
|
|
|
def make_comment_data(
|
|
|
|
%Comment{
|
|
|
|
text: text,
|
|
|
|
actor: actor,
|
|
|
|
uuid: uuid,
|
|
|
|
in_reply_to_comment: reply_to,
|
|
|
|
event: event
|
|
|
|
},
|
|
|
|
to \\ ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
) do
|
|
|
|
object = %{
|
|
|
|
"type" => "Note",
|
|
|
|
"to" => to,
|
|
|
|
"content" => text,
|
|
|
|
"actor" => actor.url,
|
2018-12-14 16:41:55 +00:00
|
|
|
"attributedTo" => actor.url,
|
2018-11-12 17:17:53 +00:00
|
|
|
"uuid" => uuid,
|
|
|
|
"id" => "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}"
|
|
|
|
}
|
|
|
|
|
|
|
|
if reply_to do
|
|
|
|
object |> Map.put("inReplyTo", reply_to.url || event.url)
|
|
|
|
else
|
|
|
|
object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
@doc """
|
|
|
|
Make an AP comment object from an set of values
|
|
|
|
"""
|
2018-11-07 18:14:59 +00:00
|
|
|
def make_comment_data(
|
|
|
|
actor,
|
|
|
|
to,
|
|
|
|
content_html,
|
2018-11-12 08:05:31 +00:00
|
|
|
# attachments,
|
|
|
|
inReplyTo \\ nil,
|
2018-12-14 16:41:55 +00:00
|
|
|
tags \\ [],
|
|
|
|
# _cw \\ nil,
|
2018-11-07 18:14:59 +00:00
|
|
|
cc \\ []
|
|
|
|
) do
|
2018-11-12 08:05:31 +00:00
|
|
|
Logger.debug("Making comment data")
|
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
2018-11-07 18:14:59 +00:00
|
|
|
object = %{
|
|
|
|
"type" => "Note",
|
|
|
|
"to" => to,
|
2018-11-12 22:30:47 +00:00
|
|
|
"cc" => cc,
|
2018-11-07 18:14:59 +00:00
|
|
|
"content" => content_html,
|
2018-11-12 08:05:31 +00:00
|
|
|
# "summary" => cw,
|
|
|
|
# "attachment" => attachments,
|
2018-11-07 18:14:59 +00:00
|
|
|
"actor" => actor,
|
2018-11-12 08:06:44 +00:00
|
|
|
"id" => "#{MobilizonWeb.Endpoint.url()}/comments/#{uuid}",
|
2018-12-14 16:41:55 +00:00
|
|
|
"uuid" => uuid,
|
|
|
|
"tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
|
2018-11-07 18:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if inReplyTo do
|
|
|
|
object
|
2018-11-12 08:05:31 +00:00
|
|
|
|> Map.put("inReplyTo", inReplyTo)
|
2018-11-07 18:14:59 +00:00
|
|
|
else
|
|
|
|
object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
def make_group_data(
|
|
|
|
actor,
|
|
|
|
to,
|
|
|
|
preferred_username,
|
|
|
|
content_html,
|
|
|
|
# attachments,
|
|
|
|
tags \\ [],
|
|
|
|
# _cw \\ nil,
|
|
|
|
cc \\ []
|
|
|
|
) do
|
|
|
|
uuid = Ecto.UUID.generate()
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
%{
|
|
|
|
"type" => "Group",
|
|
|
|
"to" => to,
|
|
|
|
"cc" => cc,
|
|
|
|
"summary" => content_html,
|
|
|
|
"attributedTo" => actor,
|
|
|
|
"preferredUsername" => preferred_username,
|
|
|
|
"id" => "#{MobilizonWeb.Endpoint.url()}/~#{preferred_username}",
|
|
|
|
"uuid" => uuid,
|
|
|
|
"tag" => tags |> Enum.map(fn {_, tag} -> tag end) |> Enum.uniq()
|
|
|
|
}
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
#### Like-related helpers
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an existing like if a user already liked an object
|
|
|
|
"""
|
|
|
|
# @spec get_existing_like(Actor.t, map()) :: nil
|
|
|
|
# def get_existing_like(%Actor{url: url} = actor, %{data: %{"id" => id}}) do
|
|
|
|
# nil
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def make_like_data(%Actor{url: url} = actor, %{data: %{"id" => id}} = object, activity_id) do
|
|
|
|
# data = %{
|
|
|
|
# "type" => "Like",
|
|
|
|
# "actor" => url,
|
|
|
|
# "object" => id,
|
|
|
|
# "to" => [actor.followers_url, 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
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
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 08:45:35 +00: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 09:32:23 +00:00
|
|
|
|
|
|
|
#### Follow-related helpers
|
|
|
|
|
|
|
|
@doc """
|
2018-12-14 16:41:55 +00:00
|
|
|
Makes a follow activity data for the given followed and follower
|
2018-05-17 09:32:23 +00:00
|
|
|
"""
|
2018-12-14 16:41:55 +00:00
|
|
|
def make_follow_data(%Actor{url: followed_id}, %Actor{url: follower_id}, activity_id) do
|
2018-11-12 08:05:31 +00:00
|
|
|
Logger.debug("Make follow data")
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
data = %{
|
|
|
|
"type" => "Follow",
|
|
|
|
"actor" => follower_id,
|
|
|
|
"to" => [followed_id],
|
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"object" => followed_id
|
|
|
|
}
|
|
|
|
|
2018-11-12 08:05:31 +00:00
|
|
|
Logger.debug(inspect(data))
|
|
|
|
|
|
|
|
if activity_id,
|
2018-12-14 16:41:55 +00:00
|
|
|
do: Map.put(data, "id", activity_id),
|
2018-11-12 08:05:31 +00:00
|
|
|
else: data
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#### Announce-related helpers
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make announce activity data for the given actor and object
|
|
|
|
"""
|
|
|
|
def make_announce_data(
|
2018-12-14 16:41:55 +00:00
|
|
|
%Actor{url: actor_url} = actor,
|
|
|
|
%Event{url: event_url} = object,
|
2018-05-17 09:32:23 +00:00
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
|
|
|
"type" => "Announce",
|
2018-12-14 16:41:55 +00:00
|
|
|
"actor" => actor_url,
|
|
|
|
"object" => event_url,
|
|
|
|
"to" => [actor.followers_url, object.actor.url],
|
|
|
|
"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
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Make announce activity data for the given actor and object
|
|
|
|
"""
|
|
|
|
def make_announce_data(
|
|
|
|
%Actor{url: actor_url} = actor,
|
|
|
|
%Comment{url: comment_url} = object,
|
|
|
|
activity_id
|
|
|
|
) do
|
|
|
|
data = %{
|
|
|
|
"type" => "Announce",
|
|
|
|
"actor" => actor_url,
|
|
|
|
"object" => comment_url,
|
|
|
|
"to" => [actor.followers_url, object.actor.url],
|
|
|
|
"cc" => ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
# "context" => object.data["context"]
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-12-14 16:41:55 +00: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 09:32:23 +00:00
|
|
|
"type" => "Undo",
|
2018-12-14 16:41:55 +00:00
|
|
|
"actor" => follower_url,
|
|
|
|
"to" => [followed_url],
|
|
|
|
"object" => follow_activity.data
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
2018-12-14 16:41:55 +00:00
|
|
|
|
|
|
|
if activity_id, do: Map.put(data, "id", activity_id), else: data
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#### Create-related helpers
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
@doc """
|
|
|
|
Make create activity data
|
|
|
|
"""
|
|
|
|
@spec make_create_data(map(), map()) :: map()
|
2018-11-12 17:17:53 +00:00
|
|
|
def make_create_data(params, additional \\ %{}) do
|
2018-12-14 16:41:55 +00:00
|
|
|
Logger.debug("Making create data")
|
|
|
|
Logger.debug(inspect(params))
|
2018-05-17 09:32:23 +00:00
|
|
|
published = params.published || make_date()
|
|
|
|
|
|
|
|
%{
|
|
|
|
"type" => "Create",
|
|
|
|
"to" => params.to |> Enum.uniq(),
|
|
|
|
"actor" => params.actor.url,
|
|
|
|
"object" => params.object,
|
2018-11-12 08:05:31 +00:00
|
|
|
"published" => published
|
2018-05-17 09:32:23 +00:00
|
|
|
}
|
|
|
|
|> Map.merge(additional)
|
|
|
|
end
|
2018-06-14 15:25:55 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts PEM encoded keys to a public key representation
|
|
|
|
"""
|
|
|
|
def pem_to_public_key(pem) do
|
2018-08-03 08:16:22 +00:00
|
|
|
[key_code] = :public_key.pem_decode(pem)
|
|
|
|
key = :public_key.pem_entry_decode(key_code)
|
2018-08-03 08:19:28 +00:00
|
|
|
|
2018-08-03 08:16:22 +00:00
|
|
|
case key do
|
|
|
|
{:RSAPrivateKey, _, modulus, exponent, _, _, _, _, _, _, _} ->
|
|
|
|
{:RSAPublicKey, modulus, exponent}
|
2018-08-03 08:19:28 +00:00
|
|
|
|
|
|
|
{:RSAPublicKey, modulus, exponent} ->
|
|
|
|
{:RSAPublicKey, modulus, exponent}
|
2018-08-03 08:16:22 +00:00
|
|
|
end
|
2018-06-14 15:25:55 +00: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 09:32:23 +00:00
|
|
|
end
|