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
92 "id" => Utils.generate_activity_id(),
93 "actor" => actor.ap_id,
97 "published" => DateTime.utc_now() |> DateTime.to_iso8601()
99 |> Pleroma.Maps.put_if_present("context", context), []}
102 def chat_message(actor, recipient, content, opts \\ []) do
104 "id" => Utils.generate_object_id(),
105 "actor" => actor.ap_id,
106 "type" => "ChatMessage",
108 "content" => content,
109 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
110 "emoji" => Emoji.Formatter.get_emoji_map(content)
113 case opts[:attachment] do
114 %Object{data: attachment_data} ->
117 Map.put(basic, "attachment", attachment_data),
126 def answer(user, object, name) do
130 "actor" => user.ap_id,
131 "attributedTo" => user.ap_id,
132 "cc" => [object.data["actor"]],
135 "inReplyTo" => object.data["id"],
136 "context" => object.data["context"],
137 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
138 "id" => Utils.generate_object_id()
142 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
143 def tombstone(actor, id) do
148 "type" => "Tombstone"
152 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
153 def like(actor, object) do
154 with {:ok, data, meta} <- object_action(actor, object) do
157 |> Map.put("type", "Like")
163 # Retricted to user updates for now, always public
164 @spec update(User.t(), Object.t()) :: {:ok, map(), keyword()}
165 def update(actor, object) do
166 to = [Pleroma.Constants.as_public(), actor.follower_address]
170 "id" => Utils.generate_activity_id(),
172 "actor" => actor.ap_id,
178 @spec block(User.t(), User.t()) :: {:ok, map(), keyword()}
179 def block(blocker, blocked) do
182 "id" => Utils.generate_activity_id(),
184 "actor" => blocker.ap_id,
185 "object" => blocked.ap_id,
186 "to" => [blocked.ap_id]
190 @spec announce(User.t(), Object.t(), keyword()) :: {:ok, map(), keyword()}
191 def announce(actor, object, options \\ []) do
192 public? = Keyword.get(options, :public, false)
196 actor.ap_id == Relay.relay_ap_id() ->
197 [actor.follower_address]
200 [actor.follower_address, object.data["actor"], Pleroma.Constants.as_public()]
203 [actor.follower_address, object.data["actor"]]
208 "id" => Utils.generate_activity_id(),
209 "actor" => actor.ap_id,
210 "object" => object.data["id"],
212 "context" => object.data["context"],
213 "type" => "Announce",
214 "published" => Utils.make_date()
218 @spec object_action(User.t(), Object.t()) :: {:ok, map(), keyword()}
219 defp object_action(actor, object) do
220 object_actor = User.get_cached_by_ap_id(object.data["actor"])
222 # Address the actor of the object, and our actor's follower collection if the post is public.
224 if Visibility.is_public?(object) do
225 [actor.follower_address, object.data["actor"]]
227 [object.data["actor"]]
230 # CC everyone who's been addressed in the object, except ourself and the object actor's
231 # follower collection
233 (object.data["to"] ++ (object.data["cc"] || []))
234 |> List.delete(actor.ap_id)
235 |> List.delete(object_actor.follower_address)
239 "id" => Utils.generate_activity_id(),
240 "actor" => actor.ap_id,
241 "object" => object.data["id"],
244 "context" => object.data["context"]