2019-10-16 14:16:39 +00:00
|
|
|
defmodule Pleroma.Web.ActivityPub.Builder do
|
|
|
|
@moduledoc """
|
|
|
|
This module builds the objects. Meant to be used for creating local objects.
|
|
|
|
|
|
|
|
This module encodes our addressing policies and general shape of our objects.
|
|
|
|
"""
|
|
|
|
|
2020-04-20 12:08:54 +00:00
|
|
|
alias Pleroma.Emoji
|
2019-10-23 10:18:05 +00:00
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.User
|
2019-10-16 14:16:39 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
|
|
|
|
2020-04-29 17:09:51 +00:00
|
|
|
@spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
|
|
|
|
def delete(actor, object_id) do
|
2020-04-30 13:42:30 +00:00
|
|
|
object = Object.normalize(object_id, false)
|
2020-04-29 17:09:51 +00:00
|
|
|
|
2020-04-30 13:42:30 +00:00
|
|
|
user = !object && User.get_cached_by_ap_id(object_id)
|
|
|
|
|
|
|
|
to =
|
|
|
|
case {object, user} do
|
|
|
|
{%Object{}, _} ->
|
|
|
|
# We are deleting an object, address everyone who was originally mentioned
|
|
|
|
(object.data["to"] || []) ++ (object.data["cc"] || [])
|
|
|
|
|
|
|
|
{_, %User{follower_address: follower_address}} ->
|
|
|
|
# We are deleting a user, address the followers of that user
|
|
|
|
[follower_address]
|
|
|
|
end
|
2020-04-29 17:09:51 +00:00
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
"id" => Utils.generate_activity_id(),
|
|
|
|
"actor" => actor.ap_id,
|
|
|
|
"object" => object_id,
|
|
|
|
"to" => to,
|
|
|
|
"type" => "Delete"
|
|
|
|
}, []}
|
|
|
|
end
|
|
|
|
|
2020-04-28 15:29:54 +00:00
|
|
|
def create(actor, object, recipients) do
|
2020-04-09 10:44:20 +00:00
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
"id" => Utils.generate_activity_id(),
|
|
|
|
"actor" => actor.ap_id,
|
|
|
|
"to" => recipients,
|
2020-04-28 15:29:54 +00:00
|
|
|
"object" => object,
|
2020-04-20 11:14:59 +00:00
|
|
|
"type" => "Create",
|
|
|
|
"published" => DateTime.utc_now() |> DateTime.to_iso8601()
|
2020-04-09 10:44:20 +00:00
|
|
|
}, []}
|
|
|
|
end
|
|
|
|
|
2020-05-06 14:12:36 +00:00
|
|
|
def chat_message(actor, recipient, content, opts \\ []) do
|
|
|
|
basic = %{
|
|
|
|
"id" => Utils.generate_object_id(),
|
|
|
|
"actor" => actor.ap_id,
|
|
|
|
"type" => "ChatMessage",
|
|
|
|
"to" => [recipient],
|
|
|
|
"content" => content,
|
|
|
|
"published" => DateTime.utc_now() |> DateTime.to_iso8601(),
|
|
|
|
"emoji" => Emoji.Formatter.get_emoji_map(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
case opts[:attachment] do
|
|
|
|
%Object{data: attachment_data} ->
|
|
|
|
{
|
|
|
|
:ok,
|
|
|
|
Map.put(basic, "attachment", attachment_data),
|
|
|
|
[]
|
|
|
|
}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:ok, basic, []}
|
|
|
|
end
|
2020-04-09 10:44:20 +00:00
|
|
|
end
|
|
|
|
|
2019-10-16 14:16:39 +00:00
|
|
|
@spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
|
|
|
|
def like(actor, object) do
|
|
|
|
object_actor = User.get_cached_by_ap_id(object.data["actor"])
|
|
|
|
|
|
|
|
# Address the actor of the object, and our actor's follower collection if the post is public.
|
|
|
|
to =
|
|
|
|
if Visibility.is_public?(object) do
|
|
|
|
[actor.follower_address, object.data["actor"]]
|
|
|
|
else
|
|
|
|
[object.data["actor"]]
|
|
|
|
end
|
|
|
|
|
|
|
|
# CC everyone who's been addressed in the object, except ourself and the object actor's
|
|
|
|
# follower collection
|
|
|
|
cc =
|
|
|
|
(object.data["to"] ++ (object.data["cc"] || []))
|
|
|
|
|> List.delete(actor.ap_id)
|
|
|
|
|> List.delete(object_actor.follower_address)
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
"id" => Utils.generate_activity_id(),
|
|
|
|
"actor" => actor.ap_id,
|
|
|
|
"type" => "Like",
|
|
|
|
"object" => object.data["id"],
|
|
|
|
"to" => to,
|
|
|
|
"cc" => cc,
|
|
|
|
"context" => object.data["context"]
|
|
|
|
}, []}
|
|
|
|
end
|
|
|
|
end
|