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.LikeValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
25 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
26 def validate(object, meta)
28 def validate(%{"type" => "Block"} = block_activity, meta) do
29 with {:ok, block_activity} <-
31 |> BlockValidator.cast_and_validate()
32 |> Ecto.Changeset.apply_action(:insert) do
33 block_activity = stringify_keys(block_activity)
34 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
37 if !outgoing_blocks do
38 Keyword.put(meta, :do_not_federate, true)
43 {:ok, block_activity, meta}
47 def validate(%{"type" => "Update"} = update_activity, meta) do
48 with {:ok, update_activity} <-
50 |> UpdateValidator.cast_and_validate()
51 |> Ecto.Changeset.apply_action(:insert) do
52 update_activity = stringify_keys(update_activity)
53 {:ok, update_activity, meta}
57 def validate(%{"type" => "Undo"} = object, meta) do
60 |> UndoValidator.cast_and_validate()
61 |> Ecto.Changeset.apply_action(:insert) do
62 object = stringify_keys(object)
67 def validate(%{"type" => "Delete"} = object, meta) do
68 with cng <- DeleteValidator.cast_and_validate(object),
69 do_not_federate <- DeleteValidator.do_not_federate?(cng),
70 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
71 object = stringify_keys(object)
72 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
77 def validate(%{"type" => "Like"} = object, meta) do
80 |> LikeValidator.cast_and_validate()
81 |> Ecto.Changeset.apply_action(:insert) do
82 object = stringify_keys(object)
87 def validate(%{"type" => "ChatMessage"} = object, meta) do
90 |> ChatMessageValidator.cast_and_validate()
91 |> Ecto.Changeset.apply_action(:insert) do
92 object = stringify_keys(object)
97 def validate(%{"type" => "EmojiReact"} = object, meta) do
100 |> EmojiReactValidator.cast_and_validate()
101 |> Ecto.Changeset.apply_action(:insert) do
102 object = stringify_keys(object |> Map.from_struct())
107 def validate(%{"type" => "Create", "object" => object} = create_activity, meta) do
108 with {:ok, object_data} <- cast_and_apply(object),
109 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
110 {:ok, create_activity} <-
112 |> CreateChatMessageValidator.cast_and_validate(meta)
113 |> Ecto.Changeset.apply_action(:insert) do
114 create_activity = stringify_keys(create_activity)
115 {:ok, create_activity, meta}
119 def validate(%{"type" => "Announce"} = object, meta) do
120 with {:ok, object} <-
122 |> AnnounceValidator.cast_and_validate()
123 |> Ecto.Changeset.apply_action(:insert) do
124 object = stringify_keys(object |> Map.from_struct())
129 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
130 ChatMessageValidator.cast_and_apply(object)
133 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
135 def stringify_keys(%{__struct__: _} = object) do
141 def stringify_keys(object) when is_map(object) do
143 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
146 def stringify_keys(object) when is_list(object) do
148 |> Enum.map(&stringify_keys/1)
151 def stringify_keys(object), do: object
153 def fetch_actor(object) do
154 with {:ok, actor} <- ObjectValidators.ObjectID.cast(object["actor"]) do
155 User.get_or_fetch_by_ap_id(actor)
159 def fetch_actor_and_object(object) do
161 Object.normalize(object["object"], true)