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.
11 alias Pleroma.Web.ActivityPub.Relay
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.ActivityPub.Visibility
15 require Pleroma.Constants
17 @spec follow(User.t(), User.t()) :: {:ok, map(), keyword()}
18 def follow(follower, followed) do
20 "id" => Utils.generate_activity_id(),
21 "actor" => follower.ap_id,
23 "object" => followed.ap_id,
24 "to" => [followed.ap_id]
30 @spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()}
31 def emoji_react(actor, object, emoji) do
32 with {:ok, data, meta} <- object_action(actor, object) do
35 |> Map.put("content", emoji)
36 |> Map.put("type", "EmojiReact")
42 @spec undo(User.t(), Activity.t()) :: {:ok, map(), keyword()}
43 def undo(actor, object) do
46 "id" => Utils.generate_activity_id(),
47 "actor" => actor.ap_id,
49 "object" => object.data["id"],
50 "to" => object.data["to"] || [],
51 "cc" => object.data["cc"] || []
55 @spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
56 def delete(actor, object_id) do
57 object = Object.normalize(object_id, false)
59 user = !object && User.get_cached_by_ap_id(object_id)
62 case {object, user} do
64 # We are deleting an object, address everyone who was originally mentioned
65 (object.data["to"] || []) ++ (object.data["cc"] || [])
67 {_, %User{follower_address: follower_address}} ->
68 # We are deleting a user, address the followers of that user
74 "id" => Utils.generate_activity_id(),
75 "actor" => actor.ap_id,
76 "object" => object_id,
82 def create(actor, object, recipients) do
85 "id" => Utils.generate_activity_id(),
86 "actor" => actor.ap_id,
90 "published" => DateTime.utc_now() |> DateTime.to_iso8601()
94 def chat_message(actor, recipient, content, opts \\ []) do
96 "id" => Utils.generate_object_id(),
97 "actor" => actor.ap_id,
98 "type" => "ChatMessage",
100 "content" => content,
101 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
102 "emoji" => Emoji.Formatter.get_emoji_map(content)
105 case opts[:attachment] do
106 %Object{data: attachment_data} ->
109 Map.put(basic, "attachment", attachment_data),
118 def answer(user, object, name) do
122 "actor" => user.ap_id,
123 "cc" => [object.data["actor"]],
126 "inReplyTo" => object.data["id"],
127 "context" => object.data["context"],
128 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
129 "id" => Utils.generate_object_id()
133 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
134 def tombstone(actor, id) do
139 "type" => "Tombstone"
143 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
144 def like(actor, object) do
145 with {:ok, data, meta} <- object_action(actor, object) do
148 |> Map.put("type", "Like")
154 # Retricted to user updates for now, always public
155 @spec update(User.t(), Object.t()) :: {:ok, map(), keyword()}
156 def update(actor, object) do
157 to = [Pleroma.Constants.as_public(), actor.follower_address]
161 "id" => Utils.generate_activity_id(),
163 "actor" => actor.ap_id,
169 @spec block(User.t(), User.t()) :: {:ok, map(), keyword()}
170 def block(blocker, blocked) do
173 "id" => Utils.generate_activity_id(),
175 "actor" => blocker.ap_id,
176 "object" => blocked.ap_id,
177 "to" => [blocked.ap_id]
181 @spec announce(User.t(), Object.t(), keyword()) :: {:ok, map(), keyword()}
182 def announce(actor, object, options \\ []) do
183 public? = Keyword.get(options, :public, false)
187 actor.ap_id == Relay.relay_ap_id() ->
188 [actor.follower_address]
191 [actor.follower_address, object.data["actor"], Pleroma.Constants.as_public()]
194 [actor.follower_address, object.data["actor"]]
199 "id" => Utils.generate_activity_id(),
200 "actor" => actor.ap_id,
201 "object" => object.data["id"],
203 "context" => object.data["context"],
204 "type" => "Announce",
205 "published" => Utils.make_date()
209 @spec object_action(User.t(), Object.t()) :: {:ok, map(), keyword()}
210 defp object_action(actor, object) do
211 object_actor = User.get_cached_by_ap_id(object.data["actor"])
213 # Address the actor of the object, and our actor's follower collection if the post is public.
215 if Visibility.is_public?(object) do
216 [actor.follower_address, object.data["actor"]]
218 [object.data["actor"]]
221 # CC everyone who's been addressed in the object, except ourself and the object actor's
222 # follower collection
224 (object.data["to"] ++ (object.data["cc"] || []))
225 |> List.delete(actor.ap_id)
226 |> List.delete(object_actor.follower_address)
230 "id" => Utils.generate_activity_id(),
231 "actor" => actor.ap_id,
232 "object" => object.data["id"],
235 "context" => object.data["context"]