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.ArticleNotePageValidator
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 Note Page] 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 Note Page] do
121 "Event" -> EventValidator
122 "Question" -> QuestionValidator
123 "Audio" -> AudioVideoValidator
124 "Video" -> AudioVideoValidator
125 "Article" -> ArticleNotePageValidator
126 "Note" -> ArticleNotePageValidator
127 "Page" -> ArticleNotePageValidator
130 with {:ok, object} <-
132 |> validator.cast_and_validate()
133 |> Ecto.Changeset.apply_action(:insert) do
134 object = stringify_keys(object)
136 # Insert copy of hashtags as strings for the non-hashtag table indexing
137 tag = (object["tag"] || []) ++ Object.hashtags(%Object{data: object})
138 object = Map.put(object, "tag", tag)
144 def validate(%{"type" => type} = object, meta)
145 when type in ~w[Accept Reject Follow Update Like EmojiReact Announce
146 ChatMessage Answer] do
149 "Accept" -> AcceptRejectValidator
150 "Reject" -> AcceptRejectValidator
151 "Follow" -> FollowValidator
152 "Update" -> UpdateValidator
153 "Like" -> LikeValidator
154 "EmojiReact" -> EmojiReactValidator
155 "Announce" -> AnnounceValidator
156 "ChatMessage" -> ChatMessageValidator
157 "Answer" -> AnswerValidator
160 with {:ok, object} <-
162 |> validator.cast_and_validate()
163 |> Ecto.Changeset.apply_action(:insert) do
164 object = stringify_keys(object)
169 def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
170 with {:ok, object} <-
172 |> AddRemoveValidator.cast_and_validate()
173 |> Ecto.Changeset.apply_action(:insert) do
174 object = stringify_keys(object)
179 def validate(o, m), do: {:error, {:validator_not_set, {o, m}}}
181 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
182 ChatMessageValidator.cast_and_apply(object)
185 def cast_and_apply(%{"type" => "Question"} = object) do
186 QuestionValidator.cast_and_apply(object)
189 def cast_and_apply(%{"type" => "Answer"} = object) do
190 AnswerValidator.cast_and_apply(object)
193 def cast_and_apply(%{"type" => type} = object) when type in ~w[Audio Video] do
194 AudioVideoValidator.cast_and_apply(object)
197 def cast_and_apply(%{"type" => "Event"} = object) do
198 EventValidator.cast_and_apply(object)
201 def cast_and_apply(%{"type" => type} = object) when type in ~w[Article Note Page] do
202 ArticleNotePageValidator.cast_and_apply(object)
205 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
207 # is_struct/1 appears in Elixir 1.11
208 def stringify_keys(%{__struct__: _} = object) do
214 def stringify_keys(object) when is_map(object) do
216 |> Enum.filter(fn {_, v} -> v != nil end)
217 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
220 def stringify_keys(object) when is_list(object) do
222 |> Enum.map(&stringify_keys/1)
225 def stringify_keys(object), do: object
227 def fetch_actor(object) do
228 with actor <- Containment.get_actor(object),
229 {:ok, actor} <- ObjectValidators.ObjectID.cast(actor) do
230 User.get_or_fetch_by_ap_id(actor)
234 def fetch_actor_and_object(object) do
236 Object.normalize(object["object"], fetch: true)