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.Relay
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.ActivityPub.Visibility
14 require Pleroma.Constants
16 @spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()}
17 def emoji_react(actor, object, emoji) do
18 with {:ok, data, meta} <- object_action(actor, object) do
21 |> Map.put("content", emoji)
22 |> Map.put("type", "EmojiReact")
28 @spec undo(User.t(), Activity.t()) :: {:ok, map(), keyword()}
29 def undo(actor, object) do
32 "id" => Utils.generate_activity_id(),
33 "actor" => actor.ap_id,
35 "object" => object.data["id"],
36 "to" => object.data["to"] || [],
37 "cc" => object.data["cc"] || []
41 @spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
42 def delete(actor, object_id) do
43 object = Object.normalize(object_id, false)
45 user = !object && User.get_cached_by_ap_id(object_id)
48 case {object, user} do
50 # We are deleting an object, address everyone who was originally mentioned
51 (object.data["to"] || []) ++ (object.data["cc"] || [])
53 {_, %User{follower_address: follower_address}} ->
54 # We are deleting a user, address the followers of that user
60 "id" => Utils.generate_activity_id(),
61 "actor" => actor.ap_id,
62 "object" => object_id,
68 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
69 def tombstone(actor, id) do
78 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
79 def like(actor, object) do
80 with {:ok, data, meta} <- object_action(actor, object) do
83 |> Map.put("type", "Like")
89 @spec announce(User.t(), Object.t(), keyword()) :: {:ok, map(), keyword()}
90 def announce(actor, object, options \\ []) do
91 public? = Keyword.get(options, :public, false)
95 actor.ap_id == Relay.relay_ap_id() ->
96 [actor.follower_address]
99 [actor.follower_address, object.data["actor"], Pleroma.Constants.as_public()]
102 [actor.follower_address, object.data["actor"]]
107 "id" => Utils.generate_activity_id(),
108 "actor" => actor.ap_id,
109 "object" => object.data["id"],
111 "context" => object.data["context"],
112 "type" => "Announce",
113 "published" => Utils.make_date()
117 @spec object_action(User.t(), Object.t()) :: {:ok, map(), keyword()}
118 defp object_action(actor, object) do
119 object_actor = User.get_cached_by_ap_id(object.data["actor"])
121 # Address the actor of the object, and our actor's follower collection if the post is public.
123 if Visibility.is_public?(object) do
124 [actor.follower_address, object.data["actor"]]
126 [object.data["actor"]]
129 # CC everyone who's been addressed in the object, except ourself and the object actor's
130 # follower collection
132 (object.data["to"] ++ (object.data["cc"] || []))
133 |> List.delete(actor.ap_id)
134 |> List.delete(object_actor.follower_address)
138 "id" => Utils.generate_activity_id(),
139 "actor" => actor.ap_id,
140 "object" => object.data["id"],
143 "context" => object.data["context"]