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.AddRemoveValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidator
24 alias Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator
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 Event Article] do
106 with {:ok, object_data} <- cast_and_apply(object),
107 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
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 Article] do
121 "Event" -> EventValidator
122 "Question" -> QuestionValidator
123 "Audio" -> AudioVideoValidator
124 "Video" -> AudioVideoValidator
125 "Article" -> ArticleNoteValidator
128 with {:ok, object} <-
130 |> validator.cast_and_validate()
131 |> Ecto.Changeset.apply_action(:insert) do
132 object = stringify_keys(object)
134 # Insert copy of hashtags as strings for the non-hashtag table indexing
135 tag = (object["tag"] || []) ++ Object.hashtags(%Object{data: object})
136 object = Map.put(object, "tag", tag)
142 def validate(%{"type" => type} = object, meta)
143 when type in ~w[Accept Reject Follow Update Like EmojiReact Announce
144 ChatMessage Answer] do
147 "Accept" -> AcceptRejectValidator
148 "Reject" -> AcceptRejectValidator
149 "Follow" -> FollowValidator
150 "Update" -> UpdateValidator
151 "Like" -> LikeValidator
152 "EmojiReact" -> EmojiReactValidator
153 "Announce" -> AnnounceValidator
154 "ChatMessage" -> ChatMessageValidator
155 "Answer" -> AnswerValidator
158 with {:ok, object} <-
160 |> validator.cast_and_validate()
161 |> Ecto.Changeset.apply_action(:insert) do
162 object = stringify_keys(object)
167 def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
168 with {:ok, object} <-
170 |> AddRemoveValidator.cast_and_validate()
171 |> Ecto.Changeset.apply_action(:insert) do
172 object = stringify_keys(object)
177 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
178 ChatMessageValidator.cast_and_apply(object)
181 def cast_and_apply(%{"type" => "Question"} = object) do
182 QuestionValidator.cast_and_apply(object)
185 def cast_and_apply(%{"type" => "Answer"} = object) do
186 AnswerValidator.cast_and_apply(object)
189 def cast_and_apply(%{"type" => type} = object) when type in ~w[Audio Video] do
190 AudioVideoValidator.cast_and_apply(object)
193 def cast_and_apply(%{"type" => "Event"} = object) do
194 EventValidator.cast_and_apply(object)
197 def cast_and_apply(%{"type" => "Article"} = object) do
198 ArticleNoteValidator.cast_and_apply(object)
201 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
203 # is_struct/1 appears in Elixir 1.11
204 def stringify_keys(%{__struct__: _} = object) do
210 def stringify_keys(object) when is_map(object) do
212 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
215 def stringify_keys(object) when is_list(object) do
217 |> Enum.map(&stringify_keys/1)
220 def stringify_keys(object), do: object
222 def fetch_actor(object) do
223 with actor <- Containment.get_actor(object),
224 {:ok, actor} <- ObjectValidators.ObjectID.cast(actor) do
225 User.get_or_fetch_by_ap_id(actor)
229 def fetch_actor_and_object(object) do
231 Object.normalize(object["object"], fetch: true)