1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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.AddRemoveValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator
24 alias Pleroma.Web.ActivityPub.ObjectValidators.AudioImageVideoValidator
25 alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
26 alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
27 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
28 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator
29 alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
30 alias Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator
31 alias Pleroma.Web.ActivityPub.ObjectValidators.EventValidator
32 alias Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator
33 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
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" => "Block"} = block_activity, meta) do
42 with {:ok, block_activity} <-
44 |> BlockValidator.cast_and_validate()
45 |> Ecto.Changeset.apply_action(:insert) do
46 block_activity = stringify_keys(block_activity)
47 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
50 if !outgoing_blocks do
51 Keyword.put(meta, :do_not_federate, true)
56 {:ok, block_activity, meta}
60 def validate(%{"type" => "Undo"} = object, meta) do
63 |> UndoValidator.cast_and_validate()
64 |> Ecto.Changeset.apply_action(:insert) do
65 object = stringify_keys(object)
66 undone_object = Activity.get_by_ap_id(object["object"])
70 |> Keyword.put(:object_data, undone_object.data)
76 def validate(%{"type" => "Delete"} = object, meta) do
77 with cng <- DeleteValidator.cast_and_validate(object),
78 do_not_federate <- DeleteValidator.do_not_federate?(cng),
79 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
80 object = stringify_keys(object)
81 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
87 %{"type" => "Create", "object" => %{"type" => "ChatMessage"} = object} = create_activity,
90 with {:ok, object_data} <- cast_and_apply(object),
91 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
92 {:ok, create_activity} <-
94 |> CreateChatMessageValidator.cast_and_validate(meta)
95 |> Ecto.Changeset.apply_action(:insert) do
96 create_activity = stringify_keys(create_activity)
97 {:ok, create_activity, meta}
102 %{"type" => "Create", "object" => %{"type" => objtype} = object} = create_activity,
105 when objtype in ~w[Question Answer Audio Video Image Event Article Note Page] do
106 with {:ok, object_data} <- cast_and_apply_and_stringify_with_history(object),
107 meta = Keyword.put(meta, :object_data, object_data),
108 {:ok, create_activity} <-
110 |> CreateGenericValidator.cast_and_validate(meta)
111 |> Ecto.Changeset.apply_action(:insert) do
112 create_activity = stringify_keys(create_activity)
113 {:ok, create_activity, meta}
117 def validate(%{"type" => type} = object, meta)
118 when type in ~w[Event Question Audio Video Image Article Note Page] do
121 "Event" -> EventValidator
122 "Question" -> QuestionValidator
123 "Audio" -> AudioImageVideoValidator
124 "Video" -> AudioImageVideoValidator
125 "Image" -> AudioImageVideoValidator
126 "Article" -> ArticleNotePageValidator
127 "Note" -> ArticleNotePageValidator
128 "Page" -> ArticleNotePageValidator
131 with {:ok, object} <-
132 do_separate_with_history(object, fn object ->
133 with {:ok, object} <-
135 |> validator.cast_and_validate()
136 |> Ecto.Changeset.apply_action(:insert) do
137 object = stringify_keys(object)
139 # Insert copy of hashtags as strings for the non-hashtag table indexing
140 tag = (object["tag"] || []) ++ Object.hashtags(%Object{data: object})
141 object = Map.put(object, "tag", tag)
151 %{"type" => "Update", "object" => %{"type" => objtype} = object} = update_activity,
154 when objtype in ~w[Question Answer Audio Video Event Article Note Page] do
155 with {_, false} <- {:local, Access.get(meta, :local, false)},
156 {_, {:ok, object_data, _}} <- {:object_validation, validate(object, meta)},
157 meta = Keyword.put(meta, :object_data, object_data),
158 {:ok, update_activity} <-
160 |> UpdateValidator.cast_and_validate()
161 |> Ecto.Changeset.apply_action(:insert) do
162 update_activity = stringify_keys(update_activity)
163 {:ok, update_activity, meta}
166 with {:ok, object} <-
168 |> UpdateValidator.cast_and_validate()
169 |> Ecto.Changeset.apply_action(:insert) do
170 object = stringify_keys(object)
174 {:object_validation, e} ->
177 {:error, %Ecto.Changeset{} = e} ->
182 def validate(%{"type" => type} = object, meta)
183 when type in ~w[Accept Reject Follow Update Like EmojiReact Announce
184 ChatMessage Answer] do
187 "Accept" -> AcceptRejectValidator
188 "Reject" -> AcceptRejectValidator
189 "Follow" -> FollowValidator
190 "Update" -> UpdateValidator
191 "Like" -> LikeValidator
192 "EmojiReact" -> EmojiReactValidator
193 "Announce" -> AnnounceValidator
194 "ChatMessage" -> ChatMessageValidator
195 "Answer" -> AnswerValidator
198 with {:ok, object} <-
200 |> validator.cast_and_validate()
201 |> Ecto.Changeset.apply_action(:insert) do
202 object = stringify_keys(object)
207 def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
208 with {:ok, object} <-
210 |> AddRemoveValidator.cast_and_validate()
211 |> Ecto.Changeset.apply_action(:insert) do
212 object = stringify_keys(object)
217 def validate(o, m), do: {:error, {:validator_not_set, {o, m}}}
219 def cast_and_apply_and_stringify_with_history(object) do
220 do_separate_with_history(object, fn object ->
221 with {:ok, object_data} <- cast_and_apply(object),
222 object_data <- object_data |> stringify_keys() do
228 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
229 ChatMessageValidator.cast_and_apply(object)
232 def cast_and_apply(%{"type" => "Question"} = object) do
233 QuestionValidator.cast_and_apply(object)
236 def cast_and_apply(%{"type" => "Answer"} = object) do
237 AnswerValidator.cast_and_apply(object)
240 def cast_and_apply(%{"type" => type} = object) when type in ~w[Audio Image Video] do
241 AudioImageVideoValidator.cast_and_apply(object)
244 def cast_and_apply(%{"type" => "Event"} = object) do
245 EventValidator.cast_and_apply(object)
248 def cast_and_apply(%{"type" => type} = object) when type in ~w[Article Note Page] do
249 ArticleNotePageValidator.cast_and_apply(object)
252 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
254 def stringify_keys(object) when is_struct(object) do
260 def stringify_keys(object) when is_map(object) do
262 |> Enum.filter(fn {_, v} -> v != nil end)
263 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
266 def stringify_keys(object) when is_list(object) do
268 |> Enum.map(&stringify_keys/1)
271 def stringify_keys(object), do: object
273 def fetch_actor(object) do
274 with actor <- Containment.get_actor(object),
275 {:ok, actor} <- ObjectValidators.ObjectID.cast(actor) do
276 User.get_or_fetch_by_ap_id(actor)
280 def fetch_actor_and_object(object) do
282 Object.normalize(object["object"], fetch: true)
286 defp for_each_history_item(
287 %{"type" => "OrderedCollection", "orderedItems" => items} = history,
292 Enum.map(items, fn item ->
293 with item <- Map.put(item, "id", object["id"]),
294 {:ok, item} <- fun.(item) do
301 if Enum.all?(processed_items, &(not is_nil(&1))) do
302 {:ok, Map.put(history, "orderedItems", processed_items)}
304 {:error, :invalid_history}
308 defp for_each_history_item(nil, _object, _fun) do
312 defp for_each_history_item(_, _object, _fun) do
313 {:error, :invalid_history}
316 # fun is (object -> {:ok, validated_object_with_string_keys})
317 defp do_separate_with_history(object, fun) do
318 with history <- object["formerRepresentations"],
319 object <- Map.drop(object, ["formerRepresentations"]),
320 {_, {:ok, object}} <- {:main_body, fun.(object)},
321 {_, {:ok, history}} <- {:history_items, for_each_history_item(history, object, fun)} do
324 Map.put(object, "formerRepresentations", history)
332 {:history_items, e} -> e