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.ChatMessageValidator
17 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
18 alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
19 alias Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator
20 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator
23 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
24 def validate(object, meta)
26 def validate(%{"type" => "Undo"} = object, meta) do
29 |> UndoValidator.cast_and_validate()
30 |> Ecto.Changeset.apply_action(:insert) do
31 object = stringify_keys(object)
36 def validate(%{"type" => "Delete"} = object, meta) do
37 with cng <- DeleteValidator.cast_and_validate(object),
38 do_not_federate <- DeleteValidator.do_not_federate?(cng),
39 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
40 object = stringify_keys(object)
41 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
46 def validate(%{"type" => "Like"} = object, meta) do
49 |> LikeValidator.cast_and_validate()
50 |> Ecto.Changeset.apply_action(:insert) do
51 object = stringify_keys(object)
56 def validate(%{"type" => "ChatMessage"} = object, meta) do
59 |> ChatMessageValidator.cast_and_validate()
60 |> Ecto.Changeset.apply_action(:insert) do
61 object = stringify_keys(object)
66 def validate(%{"type" => "EmojiReact"} = object, meta) do
69 |> EmojiReactValidator.cast_and_validate()
70 |> Ecto.Changeset.apply_action(:insert) do
71 object = stringify_keys(object |> Map.from_struct())
76 def validate(%{"type" => "Create", "object" => object} = create_activity, meta) do
77 with {:ok, object_data} <- cast_and_apply(object),
78 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
79 {:ok, create_activity} <-
81 |> CreateChatMessageValidator.cast_and_validate(meta)
82 |> Ecto.Changeset.apply_action(:insert) do
83 create_activity = stringify_keys(create_activity)
84 {:ok, create_activity, meta}
88 def validate(%{"type" => "Announce"} = object, meta) do
91 |> AnnounceValidator.cast_and_validate()
92 |> Ecto.Changeset.apply_action(:insert) do
93 object = stringify_keys(object |> Map.from_struct())
98 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
99 ChatMessageValidator.cast_and_apply(object)
102 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
104 def stringify_keys(%{__struct__: _} = object) do
110 def stringify_keys(object) when is_map(object) do
112 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
115 def stringify_keys(object) when is_list(object) do
117 |> Enum.map(&stringify_keys/1)
120 def stringify_keys(object), do: object
122 def fetch_actor(object) do
123 with {:ok, actor} <- ObjectValidators.ObjectID.cast(object["actor"]) do
124 User.get_or_fetch_by_ap_id(actor)
128 def fetch_actor_and_object(object) do
130 Object.normalize(object["object"], true)