1 defmodule Pleroma.Web.ActivityPub.Builder do
3 This module builds the objects. Meant to be used for creating local objects.
5 This module encodes our addressing policies and general shape of our objects.
10 alias Pleroma.Web.ActivityPub.Utils
11 alias Pleroma.Web.ActivityPub.Visibility
13 require Pleroma.Constants
15 @spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()}
16 def emoji_react(actor, object, emoji) do
17 with {:ok, data, meta} <- object_action(actor, object) do
20 |> Map.put("content", emoji)
21 |> Map.put("type", "EmojiReact")
27 @spec undo(User.t(), Activity.t()) :: {:ok, map(), keyword()}
28 def undo(actor, object) do
31 "id" => Utils.generate_activity_id(),
32 "actor" => actor.ap_id,
34 "object" => object.data["id"],
35 "to" => object.data["to"] || [],
36 "cc" => object.data["cc"] || []
40 @spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
41 def delete(actor, object_id) do
42 object = Object.normalize(object_id, false)
44 user = !object && User.get_cached_by_ap_id(object_id)
47 case {object, user} do
49 # We are deleting an object, address everyone who was originally mentioned
50 (object.data["to"] || []) ++ (object.data["cc"] || [])
52 {_, %User{follower_address: follower_address}} ->
53 # We are deleting a user, address the followers of that user
59 "id" => Utils.generate_activity_id(),
60 "actor" => actor.ap_id,
61 "object" => object_id,
67 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
68 def tombstone(actor, id) do
77 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
78 def like(actor, object) do
79 with {:ok, data, meta} <- object_action(actor, object) do
82 |> Map.put("type", "Like")
88 def announce(actor, object, options \\ []) do
89 public? = Keyword.get(options, :public, false)
90 to = [actor.follower_address, object.data["actor"]]
94 [Pleroma.Constants.as_public() | to]
101 "id" => Utils.generate_activity_id(),
102 "actor" => actor.ap_id,
103 "object" => object.data["id"],
105 "context" => object.data["context"],
106 "type" => "Announce",
107 "published" => Utils.make_date()
111 @spec object_action(User.t(), Object.t()) :: {:ok, map(), keyword()}
112 defp object_action(actor, object) do
113 object_actor = User.get_cached_by_ap_id(object.data["actor"])
115 # Address the actor of the object, and our actor's follower collection if the post is public.
117 if Visibility.is_public?(object) do
118 [actor.follower_address, object.data["actor"]]
120 [object.data["actor"]]
123 # CC everyone who's been addressed in the object, except ourself and the object actor's
124 # follower collection
126 (object.data["to"] ++ (object.data["cc"] || []))
127 |> List.delete(actor.ap_id)
128 |> List.delete(object_actor.follower_address)
132 "id" => Utils.generate_activity_id(),
133 "actor" => actor.ap_id,
134 "object" => object.data["id"],
137 "context" => object.data["context"]