1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidator do
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
12 @behaviour Pleroma.Web.ActivityPub.ObjectValidator.Validating
14 alias Pleroma.Activity
15 alias Pleroma.EctoType.ActivityPub.ObjectValidators
17 alias Pleroma.Object.Containment
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
39 def validate(object, meta)
41 def validate(%{"type" => type} = object, meta)
42 when type in ~w[Accept Reject] do
45 |> AcceptRejectValidator.cast_and_validate()
46 |> Ecto.Changeset.apply_action(:insert) do
47 object = stringify_keys(object)
52 def validate(%{"type" => "Event"} = object, meta) do
55 |> EventValidator.cast_and_validate()
56 |> Ecto.Changeset.apply_action(:insert) do
57 object = stringify_keys(object)
62 def validate(%{"type" => "Follow"} = object, meta) do
65 |> FollowValidator.cast_and_validate()
66 |> Ecto.Changeset.apply_action(:insert) do
67 object = stringify_keys(object)
72 def validate(%{"type" => "Block"} = block_activity, meta) do
73 with {:ok, 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])
81 if !outgoing_blocks do
82 Keyword.put(meta, :do_not_federate, true)
87 {:ok, block_activity, meta}
91 def validate(%{"type" => "Update"} = update_activity, meta) do
92 with {:ok, 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}
101 def validate(%{"type" => "Undo"} = object, meta) do
102 with {:ok, 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"])
111 |> Keyword.put(:object_data, undone_object.data)
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)
127 def validate(%{"type" => "Like"} = object, meta) do
128 with {:ok, object} <-
130 |> LikeValidator.cast_and_validate()
131 |> Ecto.Changeset.apply_action(:insert) do
132 object = stringify_keys(object)
137 def validate(%{"type" => "ChatMessage"} = object, meta) do
138 with {:ok, object} <-
140 |> ChatMessageValidator.cast_and_validate()
141 |> Ecto.Changeset.apply_action(:insert) do
142 object = stringify_keys(object)
147 def validate(%{"type" => "Question"} = object, meta) do
148 with {:ok, object} <-
150 |> QuestionValidator.cast_and_validate()
151 |> Ecto.Changeset.apply_action(:insert) do
152 object = stringify_keys(object)
157 def validate(%{"type" => type} = object, meta) when type in ~w[Audio Video] do
158 with {:ok, object} <-
160 |> AudioVideoValidator.cast_and_validate()
161 |> Ecto.Changeset.apply_action(:insert) do
162 object = stringify_keys(object)
167 def validate(%{"type" => "Article"} = object, meta) do
168 with {:ok, object} <-
170 |> ArticleNoteValidator.cast_and_validate()
171 |> Ecto.Changeset.apply_action(:insert) do
172 object = stringify_keys(object)
177 def validate(%{"type" => "Answer"} = object, meta) do
178 with {:ok, object} <-
180 |> AnswerValidator.cast_and_validate()
181 |> Ecto.Changeset.apply_action(:insert) do
182 object = stringify_keys(object)
187 def validate(%{"type" => "EmojiReact"} = object, meta) do
188 with {:ok, object} <-
190 |> EmojiReactValidator.cast_and_validate()
191 |> Ecto.Changeset.apply_action(:insert) do
192 object = stringify_keys(object)
198 %{"type" => "Create", "object" => %{"type" => "ChatMessage"} = object} = create_activity,
201 with {:ok, object_data} <- cast_and_apply(object),
202 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
203 {:ok, 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}
213 %{"type" => "Create", "object" => %{"type" => objtype} = object} = create_activity,
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} <-
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}
228 def validate(%{"type" => "Announce"} = object, meta) do
229 with {:ok, object} <-
231 |> AnnounceValidator.cast_and_validate()
232 |> Ecto.Changeset.apply_action(:insert) do
233 object = stringify_keys(object)
238 def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
239 with {:ok, object} <-
241 |> PinValidator.cast_and_validate()
242 |> Ecto.Changeset.apply_action(:insert) do
243 object = stringify_keys(object)
248 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
249 ChatMessageValidator.cast_and_apply(object)
252 def cast_and_apply(%{"type" => "Question"} = object) do
253 QuestionValidator.cast_and_apply(object)
256 def cast_and_apply(%{"type" => "Answer"} = object) do
257 AnswerValidator.cast_and_apply(object)
260 def cast_and_apply(%{"type" => type} = object) when type in ~w[Audio Video] do
261 AudioVideoValidator.cast_and_apply(object)
264 def cast_and_apply(%{"type" => "Event"} = object) do
265 EventValidator.cast_and_apply(object)
268 def cast_and_apply(%{"type" => "Article"} = object) do
269 ArticleNoteValidator.cast_and_apply(object)
272 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
274 # is_struct/1 isn't present in Elixir 1.8.x
275 def stringify_keys(%{__struct__: _} = object) do
281 def stringify_keys(object) when is_map(object) do
283 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
286 def stringify_keys(object) when is_list(object) do
288 |> Enum.map(&stringify_keys/1)
291 def stringify_keys(object), do: object
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)
300 def fetch_actor_and_object(object) do
302 Object.normalize(object["object"], fetch: true)