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.BlockValidator
17 alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
18 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
19 alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
20 alias Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator
24 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
26 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
27 def validate(object, meta)
29 def validate(%{"type" => "Follow"} = object, meta) do
32 |> FollowValidator.cast_and_validate()
33 |> Ecto.Changeset.apply_action(:insert) do
34 object = stringify_keys(object)
39 def validate(%{"type" => "Block"} = block_activity, meta) do
40 with {:ok, block_activity} <-
42 |> BlockValidator.cast_and_validate()
43 |> Ecto.Changeset.apply_action(:insert) do
44 block_activity = stringify_keys(block_activity)
45 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
48 if !outgoing_blocks do
49 Keyword.put(meta, :do_not_federate, true)
54 {:ok, block_activity, meta}
58 def validate(%{"type" => "Update"} = update_activity, meta) do
59 with {:ok, update_activity} <-
61 |> UpdateValidator.cast_and_validate()
62 |> Ecto.Changeset.apply_action(:insert) do
63 update_activity = stringify_keys(update_activity)
64 {:ok, update_activity, meta}
68 def validate(%{"type" => "Undo"} = object, meta) do
71 |> UndoValidator.cast_and_validate()
72 |> Ecto.Changeset.apply_action(:insert) do
73 object = stringify_keys(object)
78 def validate(%{"type" => "Delete"} = object, meta) do
79 with cng <- DeleteValidator.cast_and_validate(object),
80 do_not_federate <- DeleteValidator.do_not_federate?(cng),
81 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
82 object = stringify_keys(object)
83 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
88 def validate(%{"type" => "Like"} = object, meta) do
91 |> LikeValidator.cast_and_validate()
92 |> Ecto.Changeset.apply_action(:insert) do
93 object = stringify_keys(object)
98 def validate(%{"type" => "ChatMessage"} = object, meta) do
101 |> ChatMessageValidator.cast_and_validate()
102 |> Ecto.Changeset.apply_action(:insert) do
103 object = stringify_keys(object)
108 def validate(%{"type" => "EmojiReact"} = object, meta) do
109 with {:ok, object} <-
111 |> EmojiReactValidator.cast_and_validate()
112 |> Ecto.Changeset.apply_action(:insert) do
113 object = stringify_keys(object |> Map.from_struct())
118 def validate(%{"type" => "Create", "object" => object} = create_activity, meta) do
119 with {:ok, object_data} <- cast_and_apply(object),
120 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
121 {:ok, create_activity} <-
123 |> CreateChatMessageValidator.cast_and_validate(meta)
124 |> Ecto.Changeset.apply_action(:insert) do
125 create_activity = stringify_keys(create_activity)
126 {:ok, create_activity, meta}
130 def validate(%{"type" => "Announce"} = object, meta) do
131 with {:ok, object} <-
133 |> AnnounceValidator.cast_and_validate()
134 |> Ecto.Changeset.apply_action(:insert) do
135 object = stringify_keys(object |> Map.from_struct())
140 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
141 ChatMessageValidator.cast_and_apply(object)
144 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
146 def stringify_keys(%{__struct__: _} = object) do
152 def stringify_keys(object) when is_map(object) do
154 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
157 def stringify_keys(object) when is_list(object) do
159 |> Enum.map(&stringify_keys/1)
162 def stringify_keys(object), do: object
164 def fetch_actor(object) do
165 with {:ok, actor} <- ObjectValidators.ObjectID.cast(object["actor"]) do
166 User.get_or_fetch_by_ap_id(actor)
170 def fetch_actor_and_object(object) do
172 Object.normalize(object["object"], true)