Let pins federate
[akkoma] / lib / pleroma / web / activity_pub / object_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidator do
6 @moduledoc """
7 This module is responsible for validating an object (which can be an activity)
8 and checking if it is both well formed and also compatible with our view of
9 the system.
10 """
11
12 @behaviour Pleroma.Web.ActivityPub.ObjectValidator.Validating
13
14 alias Pleroma.Activity
15 alias Pleroma.EctoType.ActivityPub.ObjectValidators
16 alias Pleroma.Object
17 alias Pleroma.Object.Containment
18 alias Pleroma.User
19 alias Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator
20 alias Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator
24 alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
25 alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
26 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
27 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator
28 alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
29 alias Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator
30 alias Pleroma.Web.ActivityPub.ObjectValidators.EventValidator
31 alias Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator
32 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
33 alias Pleroma.Web.ActivityPub.ObjectValidators.PinValidator
34 alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator
35 alias Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator
36 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
37
38 @impl true
39 def validate(object, meta)
40
41 def validate(%{"type" => type} = object, meta)
42 when type in ~w[Accept Reject] do
43 with {:ok, object} <-
44 object
45 |> AcceptRejectValidator.cast_and_validate()
46 |> Ecto.Changeset.apply_action(:insert) do
47 object = stringify_keys(object)
48 {:ok, object, meta}
49 end
50 end
51
52 def validate(%{"type" => "Event"} = object, meta) do
53 with {:ok, object} <-
54 object
55 |> EventValidator.cast_and_validate()
56 |> Ecto.Changeset.apply_action(:insert) do
57 object = stringify_keys(object)
58 {:ok, object, meta}
59 end
60 end
61
62 def validate(%{"type" => "Follow"} = object, meta) do
63 with {:ok, object} <-
64 object
65 |> FollowValidator.cast_and_validate()
66 |> Ecto.Changeset.apply_action(:insert) do
67 object = stringify_keys(object)
68 {:ok, object, meta}
69 end
70 end
71
72 def validate(%{"type" => "Block"} = block_activity, meta) do
73 with {:ok, block_activity} <-
74 block_activity
75 |> BlockValidator.cast_and_validate()
76 |> Ecto.Changeset.apply_action(:insert) do
77 block_activity = stringify_keys(block_activity)
78 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
79
80 meta =
81 if !outgoing_blocks do
82 Keyword.put(meta, :do_not_federate, true)
83 else
84 meta
85 end
86
87 {:ok, block_activity, meta}
88 end
89 end
90
91 def validate(%{"type" => "Update"} = update_activity, meta) do
92 with {:ok, update_activity} <-
93 update_activity
94 |> UpdateValidator.cast_and_validate()
95 |> Ecto.Changeset.apply_action(:insert) do
96 update_activity = stringify_keys(update_activity)
97 {:ok, update_activity, meta}
98 end
99 end
100
101 def validate(%{"type" => "Undo"} = object, meta) do
102 with {:ok, object} <-
103 object
104 |> UndoValidator.cast_and_validate()
105 |> Ecto.Changeset.apply_action(:insert) do
106 object = stringify_keys(object)
107 undone_object = Activity.get_by_ap_id(object["object"])
108
109 meta =
110 meta
111 |> Keyword.put(:object_data, undone_object.data)
112
113 {:ok, object, meta}
114 end
115 end
116
117 def validate(%{"type" => "Delete"} = object, meta) do
118 with cng <- DeleteValidator.cast_and_validate(object),
119 do_not_federate <- DeleteValidator.do_not_federate?(cng),
120 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
121 object = stringify_keys(object)
122 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
123 {:ok, object, meta}
124 end
125 end
126
127 def validate(%{"type" => "Like"} = object, meta) do
128 with {:ok, object} <-
129 object
130 |> LikeValidator.cast_and_validate()
131 |> Ecto.Changeset.apply_action(:insert) do
132 object = stringify_keys(object)
133 {:ok, object, meta}
134 end
135 end
136
137 def validate(%{"type" => "ChatMessage"} = object, meta) do
138 with {:ok, object} <-
139 object
140 |> ChatMessageValidator.cast_and_validate()
141 |> Ecto.Changeset.apply_action(:insert) do
142 object = stringify_keys(object)
143 {:ok, object, meta}
144 end
145 end
146
147 def validate(%{"type" => "Question"} = object, meta) do
148 with {:ok, object} <-
149 object
150 |> QuestionValidator.cast_and_validate()
151 |> Ecto.Changeset.apply_action(:insert) do
152 object = stringify_keys(object)
153 {:ok, object, meta}
154 end
155 end
156
157 def validate(%{"type" => type} = object, meta) when type in ~w[Audio Video] do
158 with {:ok, object} <-
159 object
160 |> AudioVideoValidator.cast_and_validate()
161 |> Ecto.Changeset.apply_action(:insert) do
162 object = stringify_keys(object)
163 {:ok, object, meta}
164 end
165 end
166
167 def validate(%{"type" => "Article"} = object, meta) do
168 with {:ok, object} <-
169 object
170 |> ArticleNoteValidator.cast_and_validate()
171 |> Ecto.Changeset.apply_action(:insert) do
172 object = stringify_keys(object)
173 {:ok, object, meta}
174 end
175 end
176
177 def validate(%{"type" => "Answer"} = object, meta) do
178 with {:ok, object} <-
179 object
180 |> AnswerValidator.cast_and_validate()
181 |> Ecto.Changeset.apply_action(:insert) do
182 object = stringify_keys(object)
183 {:ok, object, meta}
184 end
185 end
186
187 def validate(%{"type" => "EmojiReact"} = object, meta) do
188 with {:ok, object} <-
189 object
190 |> EmojiReactValidator.cast_and_validate()
191 |> Ecto.Changeset.apply_action(:insert) do
192 object = stringify_keys(object)
193 {:ok, object, meta}
194 end
195 end
196
197 def validate(
198 %{"type" => "Create", "object" => %{"type" => "ChatMessage"} = object} = create_activity,
199 meta
200 ) do
201 with {:ok, object_data} <- cast_and_apply(object),
202 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
203 {:ok, create_activity} <-
204 create_activity
205 |> CreateChatMessageValidator.cast_and_validate(meta)
206 |> Ecto.Changeset.apply_action(:insert) do
207 create_activity = stringify_keys(create_activity)
208 {:ok, create_activity, meta}
209 end
210 end
211
212 def validate(
213 %{"type" => "Create", "object" => %{"type" => objtype} = object} = create_activity,
214 meta
215 )
216 when objtype in ~w[Question Answer Audio Video Event Article] do
217 with {:ok, object_data} <- cast_and_apply(object),
218 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
219 {:ok, create_activity} <-
220 create_activity
221 |> CreateGenericValidator.cast_and_validate(meta)
222 |> Ecto.Changeset.apply_action(:insert) do
223 create_activity = stringify_keys(create_activity)
224 {:ok, create_activity, meta}
225 end
226 end
227
228 def validate(%{"type" => "Announce"} = object, meta) do
229 with {:ok, object} <-
230 object
231 |> AnnounceValidator.cast_and_validate()
232 |> Ecto.Changeset.apply_action(:insert) do
233 object = stringify_keys(object)
234 {:ok, object, meta}
235 end
236 end
237
238 def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
239 with {:ok, object} <-
240 object
241 |> PinValidator.cast_and_validate()
242 |> Ecto.Changeset.apply_action(:insert) do
243 object = stringify_keys(object)
244 {:ok, object, meta}
245 end
246 end
247
248 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
249 ChatMessageValidator.cast_and_apply(object)
250 end
251
252 def cast_and_apply(%{"type" => "Question"} = object) do
253 QuestionValidator.cast_and_apply(object)
254 end
255
256 def cast_and_apply(%{"type" => "Answer"} = object) do
257 AnswerValidator.cast_and_apply(object)
258 end
259
260 def cast_and_apply(%{"type" => type} = object) when type in ~w[Audio Video] do
261 AudioVideoValidator.cast_and_apply(object)
262 end
263
264 def cast_and_apply(%{"type" => "Event"} = object) do
265 EventValidator.cast_and_apply(object)
266 end
267
268 def cast_and_apply(%{"type" => "Article"} = object) do
269 ArticleNoteValidator.cast_and_apply(object)
270 end
271
272 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
273
274 # is_struct/1 isn't present in Elixir 1.8.x
275 def stringify_keys(%{__struct__: _} = object) do
276 object
277 |> Map.from_struct()
278 |> stringify_keys
279 end
280
281 def stringify_keys(object) when is_map(object) do
282 object
283 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
284 end
285
286 def stringify_keys(object) when is_list(object) do
287 object
288 |> Enum.map(&stringify_keys/1)
289 end
290
291 def stringify_keys(object), do: object
292
293 def fetch_actor(object) do
294 with actor <- Containment.get_actor(object),
295 {:ok, actor} <- ObjectValidators.ObjectID.cast(actor) do
296 User.get_or_fetch_by_ap_id(actor)
297 end
298 end
299
300 def fetch_actor_and_object(object) do
301 fetch_actor(object)
302 Object.normalize(object["object"], fetch: true)
303 :ok
304 end
305 end