Merge branch '1925-muting-reblogs' into 'develop'
[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 @spec tombstone(String.t(), String.t()) :: {:ok, map(), keyword()}
119 def tombstone(actor, id) do
120 {:ok,
121 %{
122 "id" => id,
123 "actor" => actor,
124 "type" => "Tombstone"
125 }, []}
126 end
127
128 @spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
129 def like(actor, object) do
130 with {:ok, data, meta} <- object_action(actor, object) do
131 data =
132 data
133 |> Map.put("type", "Like")
134
135 {:ok, data, meta}
136 end
137 end
138
139 # Retricted to user updates for now, always public
140 @spec update(User.t(), Object.t()) :: {:ok, map(), keyword()}
141 def update(actor, object) do
142 to = [Pleroma.Constants.as_public(), actor.follower_address]
143
144 {:ok,
145 %{
146 "id" => Utils.generate_activity_id(),
147 "type" => "Update",
148 "actor" => actor.ap_id,
149 "object" => object,
150 "to" => to
151 }, []}
152 end
153
154 @spec block(User.t(), User.t()) :: {:ok, map(), keyword()}
155 def block(blocker, blocked) do
156 {:ok,
157 %{
158 "id" => Utils.generate_activity_id(),
159 "type" => "Block",
160 "actor" => blocker.ap_id,
161 "object" => blocked.ap_id,
162 "to" => [blocked.ap_id]
163 }, []}
164 end
165
166 @spec announce(User.t(), Object.t(), keyword()) :: {:ok, map(), keyword()}
167 def announce(actor, object, options \\ []) do
168 public? = Keyword.get(options, :public, false)
169
170 to =
171 cond do
172 actor.ap_id == Relay.relay_ap_id() ->
173 [actor.follower_address]
174
175 public? ->
176 [actor.follower_address, object.data["actor"], Pleroma.Constants.as_public()]
177
178 true ->
179 [actor.follower_address, object.data["actor"]]
180 end
181
182 {:ok,
183 %{
184 "id" => Utils.generate_activity_id(),
185 "actor" => actor.ap_id,
186 "object" => object.data["id"],
187 "to" => to,
188 "context" => object.data["context"],
189 "type" => "Announce",
190 "published" => Utils.make_date()
191 }, []}
192 end
193
194 @spec object_action(User.t(), Object.t()) :: {:ok, map(), keyword()}
195 defp object_action(actor, object) do
196 object_actor = User.get_cached_by_ap_id(object.data["actor"])
197
198 # Address the actor of the object, and our actor's follower collection if the post is public.
199 to =
200 if Visibility.is_public?(object) do
201 [actor.follower_address, object.data["actor"]]
202 else
203 [object.data["actor"]]
204 end
205
206 # CC everyone who's been addressed in the object, except ourself and the object actor's
207 # follower collection
208 cc =
209 (object.data["to"] ++ (object.data["cc"] || []))
210 |> List.delete(actor.ap_id)
211 |> List.delete(object_actor.follower_address)
212
213 {:ok,
214 %{
215 "id" => Utils.generate_activity_id(),
216 "actor" => actor.ap_id,
217 "object" => object.data["id"],
218 "to" => to,
219 "cc" => cc,
220 "context" => object.data["context"]
221 }, []}
222 end
223 end