SideEffects: port ones from ActivityPub.do_create and ActivityPub.insert
[akkoma] / lib / pleroma / web / activity_pub / builder.ex
1 defmodule Pleroma.Web.ActivityPub.Builder do
2 @moduledoc """
3 This module builds the objects. Meant to be used for creating local objects.
4
5 This module encodes our addressing policies and general shape of our objects.
6 """
7
8 alias Pleroma.Emoji
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.Relay
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.ActivityPub.Visibility
14
15 require Pleroma.Constants
16
17 @spec follow(User.t(), User.t()) :: {:ok, map(), keyword()}
18 def follow(follower, followed) do
19 data = %{
20 "id" => Utils.generate_activity_id(),
21 "actor" => follower.ap_id,
22 "type" => "Follow",
23 "object" => followed.ap_id,
24 "to" => [followed.ap_id]
25 }
26
27 {:ok, data, []}
28 end
29
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
33 data =
34 data
35 |> Map.put("content", emoji)
36 |> Map.put("type", "EmojiReact")
37
38 {:ok, data, meta}
39 end
40 end
41
42 @spec undo(User.t(), Activity.t()) :: {:ok, map(), keyword()}
43 def undo(actor, object) do
44 {:ok,
45 %{
46 "id" => Utils.generate_activity_id(),
47 "actor" => actor.ap_id,
48 "type" => "Undo",
49 "object" => object.data["id"],
50 "to" => object.data["to"] || [],
51 "cc" => object.data["cc"] || []
52 }, []}
53 end
54
55 @spec delete(User.t(), String.t()) :: {:ok, map(), keyword()}
56 def delete(actor, object_id) do
57 object = Object.normalize(object_id, false)
58
59 user = !object && User.get_cached_by_ap_id(object_id)
60
61 to =
62 case {object, user} do
63 {%Object{}, _} ->
64 # We are deleting an object, address everyone who was originally mentioned
65 (object.data["to"] || []) ++ (object.data["cc"] || [])
66
67 {_, %User{follower_address: follower_address}} ->
68 # We are deleting a user, address the followers of that user
69 [follower_address]
70 end
71
72 {:ok,
73 %{
74 "id" => Utils.generate_activity_id(),
75 "actor" => actor.ap_id,
76 "object" => object_id,
77 "to" => to,
78 "type" => "Delete"
79 }, []}
80 end
81
82 def create(actor, object, recipients) do
83 {:ok,
84 %{
85 "id" => Utils.generate_activity_id(),
86 "actor" => actor.ap_id,
87 "to" => recipients,
88 "object" => object,
89 "type" => "Create",
90 "published" => DateTime.utc_now() |> DateTime.to_iso8601()
91 }, []}
92 end
93
94 def chat_message(actor, recipient, content, opts \\ []) do
95 basic = %{
96 "id" => Utils.generate_object_id(),
97 "actor" => actor.ap_id,
98 "type" => "ChatMessage",
99 "to" => [recipient],
100 "content" => content,
101 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
102 "emoji" => Emoji.Formatter.get_emoji_map(content)
103 }
104
105 case opts[:attachment] do
106 %Object{data: attachment_data} ->
107 {
108 :ok,
109 Map.put(basic, "attachment", attachment_data),
110 []
111 }
112
113 _ ->
114 {:ok, basic, []}
115 end
116 end
117
118 def answer(user, object, name) do
119 {:ok,
120 %{
121 "type" => "Answer",
122 "actor" => user.ap_id,
123 "cc" => [object.data["actor"]],
124 "to" => [],
125 "name" => name,
126 "inReplyTo" => object.data["id"],
127 "context" => object.data["context"],
128 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
129 "id" => Utils.generate_object_id()
130 }, []}
131 end
132
133 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
134 def tombstone(actor, id) do
135 {:ok,
136 %{
137 "id" => id,
138 "actor" => actor,
139 "type" => "Tombstone"
140 }, []}
141 end
142
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
146 data =
147 data
148 |> Map.put("type", "Like")
149
150 {:ok, data, meta}
151 end
152 end
153
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]
158
159 {:ok,
160 %{
161 "id" => Utils.generate_activity_id(),
162 "type" => "Update",
163 "actor" => actor.ap_id,
164 "object" => object,
165 "to" => to
166 }, []}
167 end
168
169 @spec block(User.t(), User.t()) :: {:ok, map(), keyword()}
170 def block(blocker, blocked) do
171 {:ok,
172 %{
173 "id" => Utils.generate_activity_id(),
174 "type" => "Block",
175 "actor" => blocker.ap_id,
176 "object" => blocked.ap_id,
177 "to" => [blocked.ap_id]
178 }, []}
179 end
180
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)
184
185 to =
186 cond do
187 actor.ap_id == Relay.relay_ap_id() ->
188 [actor.follower_address]
189
190 public? ->
191 [actor.follower_address, object.data["actor"], Pleroma.Constants.as_public()]
192
193 true ->
194 [actor.follower_address, object.data["actor"]]
195 end
196
197 {:ok,
198 %{
199 "id" => Utils.generate_activity_id(),
200 "actor" => actor.ap_id,
201 "object" => object.data["id"],
202 "to" => to,
203 "context" => object.data["context"],
204 "type" => "Announce",
205 "published" => Utils.make_date()
206 }, []}
207 end
208
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"])
212
213 # Address the actor of the object, and our actor's follower collection if the post is public.
214 to =
215 if Visibility.is_public?(object) do
216 [actor.follower_address, object.data["actor"]]
217 else
218 [object.data["actor"]]
219 end
220
221 # CC everyone who's been addressed in the object, except ourself and the object actor's
222 # follower collection
223 cc =
224 (object.data["to"] ++ (object.data["cc"] || []))
225 |> List.delete(actor.ap_id)
226 |> List.delete(object_actor.follower_address)
227
228 {:ok,
229 %{
230 "id" => Utils.generate_activity_id(),
231 "actor" => actor.ap_id,
232 "object" => object.data["id"],
233 "to" => to,
234 "cc" => cc,
235 "context" => object.data["context"]
236 }, []}
237 end
238 end