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
22 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
24 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
25 def validate(object, meta)
27 def validate(%{"type" => "Update"} = object, meta) do
30 |> UpdateValidator.cast_and_validate()
31 |> Ecto.Changeset.apply_action(:insert) do
32 object = stringify_keys(object)
37 def validate(%{"type" => "Undo"} = object, meta) do
40 |> UndoValidator.cast_and_validate()
41 |> Ecto.Changeset.apply_action(:insert) do
42 object = stringify_keys(object)
47 def validate(%{"type" => "Delete"} = object, meta) do
48 with cng <- DeleteValidator.cast_and_validate(object),
49 do_not_federate <- DeleteValidator.do_not_federate?(cng),
50 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
51 object = stringify_keys(object)
52 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
57 def validate(%{"type" => "Like"} = object, meta) do
60 |> LikeValidator.cast_and_validate()
61 |> Ecto.Changeset.apply_action(:insert) do
62 object = stringify_keys(object)
67 def validate(%{"type" => "ChatMessage"} = object, meta) do
70 |> ChatMessageValidator.cast_and_validate()
71 |> Ecto.Changeset.apply_action(:insert) do
72 object = stringify_keys(object)
77 def validate(%{"type" => "EmojiReact"} = object, meta) do
80 |> EmojiReactValidator.cast_and_validate()
81 |> Ecto.Changeset.apply_action(:insert) do
82 object = stringify_keys(object |> Map.from_struct())
87 def validate(%{"type" => "Create", "object" => object} = create_activity, meta) do
88 with {:ok, object_data} <- cast_and_apply(object),
89 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
90 {:ok, create_activity} <-
92 |> CreateChatMessageValidator.cast_and_validate(meta)
93 |> Ecto.Changeset.apply_action(:insert) do
94 create_activity = stringify_keys(create_activity)
95 {:ok, create_activity, meta}
99 def validate(%{"type" => "Announce"} = object, meta) do
100 with {:ok, object} <-
102 |> AnnounceValidator.cast_and_validate()
103 |> Ecto.Changeset.apply_action(:insert) do
104 object = stringify_keys(object |> Map.from_struct())
109 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
110 ChatMessageValidator.cast_and_apply(object)
113 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
115 def stringify_keys(%{__struct__: _} = object) do
121 def stringify_keys(object) when is_map(object) do
123 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
126 def stringify_keys(object) when is_list(object) do
128 |> Enum.map(&stringify_keys/1)
131 def stringify_keys(object), do: object
133 def fetch_actor(object) do
134 with {:ok, actor} <- ObjectValidators.ObjectID.cast(object["actor"]) do
135 User.get_or_fetch_by_ap_id(actor)
139 def fetch_actor_and_object(object) do
141 Object.normalize(object["object"], true)