1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 alias Pleroma.EctoType.ActivityPub.ObjectValidators
15 alias Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator
16 alias Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator
17 alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
18 alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
19 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
20 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateQuestionValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator
24 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
25 alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator
26 alias Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator
27 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
29 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
30 def validate(object, meta)
32 def validate(%{"type" => "Follow"} = object, meta) do
35 |> FollowValidator.cast_and_validate()
36 |> Ecto.Changeset.apply_action(:insert) do
37 object = stringify_keys(object)
42 def validate(%{"type" => "Block"} = block_activity, meta) do
43 with {:ok, block_activity} <-
45 |> BlockValidator.cast_and_validate()
46 |> Ecto.Changeset.apply_action(:insert) do
47 block_activity = stringify_keys(block_activity)
48 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
51 if !outgoing_blocks do
52 Keyword.put(meta, :do_not_federate, true)
57 {:ok, block_activity, meta}
61 def validate(%{"type" => "Update"} = update_activity, meta) do
62 with {:ok, update_activity} <-
64 |> UpdateValidator.cast_and_validate()
65 |> Ecto.Changeset.apply_action(:insert) do
66 update_activity = stringify_keys(update_activity)
67 {:ok, update_activity, meta}
71 def validate(%{"type" => "Undo"} = object, meta) do
74 |> UndoValidator.cast_and_validate()
75 |> Ecto.Changeset.apply_action(:insert) do
76 object = stringify_keys(object)
81 def validate(%{"type" => "Delete"} = object, meta) do
82 with cng <- DeleteValidator.cast_and_validate(object),
83 do_not_federate <- DeleteValidator.do_not_federate?(cng),
84 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
85 object = stringify_keys(object)
86 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
91 def validate(%{"type" => "Like"} = object, meta) do
94 |> LikeValidator.cast_and_validate()
95 |> Ecto.Changeset.apply_action(:insert) do
96 object = stringify_keys(object)
101 def validate(%{"type" => "ChatMessage"} = object, meta) do
102 with {:ok, object} <-
104 |> ChatMessageValidator.cast_and_validate()
105 |> Ecto.Changeset.apply_action(:insert) do
106 object = stringify_keys(object)
111 def validate(%{"type" => "Question"} = object, meta) do
112 with {:ok, object} <-
114 |> QuestionValidator.cast_and_validate()
115 |> Ecto.Changeset.apply_action(:insert) do
116 object = stringify_keys(object)
121 def validate(%{"type" => "Answer"} = object, meta) do
122 with {:ok, object} <-
124 |> AnswerValidator.cast_and_validate()
125 |> Ecto.Changeset.apply_action(:insert) do
126 object = stringify_keys(object)
131 def validate(%{"type" => "EmojiReact"} = object, meta) do
132 with {:ok, object} <-
134 |> EmojiReactValidator.cast_and_validate()
135 |> Ecto.Changeset.apply_action(:insert) do
136 object = stringify_keys(object)
142 %{"type" => "Create", "object" => %{"type" => "ChatMessage"} = object} = create_activity,
145 with {:ok, object_data} <- cast_and_apply(object),
146 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
147 {:ok, create_activity} <-
149 |> CreateChatMessageValidator.cast_and_validate(meta)
150 |> Ecto.Changeset.apply_action(:insert) do
151 create_activity = stringify_keys(create_activity)
152 {:ok, create_activity, meta}
157 %{"type" => "Create", "object" => %{"type" => objtype} = object} = create_activity,
160 when objtype in ["Question", "Answer"] do
161 with {:ok, object_data} <- cast_and_apply(object),
162 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
163 {:ok, create_activity} <-
165 |> CreateQuestionValidator.cast_and_validate(meta)
166 |> Ecto.Changeset.apply_action(:insert) do
167 create_activity = stringify_keys(create_activity)
168 {:ok, create_activity, meta}
172 def validate(%{"type" => "Announce"} = object, meta) do
173 with {:ok, object} <-
175 |> AnnounceValidator.cast_and_validate()
176 |> Ecto.Changeset.apply_action(:insert) do
177 object = stringify_keys(object)
182 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
183 ChatMessageValidator.cast_and_apply(object)
186 def cast_and_apply(%{"type" => "Question"} = object) do
187 QuestionValidator.cast_and_apply(object)
190 def cast_and_apply(%{"type" => "Answer"} = object) do
191 QuestionValidator.cast_and_apply(object)
194 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
196 # is_struct/1 isn't present in Elixir 1.8.x
197 def stringify_keys(%{__struct__: _} = object) do
203 def stringify_keys(object) when is_map(object) do
205 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
208 def stringify_keys(object) when is_list(object) do
210 |> Enum.map(&stringify_keys/1)
213 def stringify_keys(object), do: object
215 def fetch_actor(object) do
216 with {:ok, actor} <- ObjectValidators.ObjectID.cast(object["actor"]) do
217 User.get_or_fetch_by_ap_id(actor)
221 def fetch_actor_and_object(object) do
223 Object.normalize(object["object"], true)