Merge branch 'bugfix/mix.exs-version-dot-git-less' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / object_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidator do
6 @moduledoc """
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
9 the system.
10 """
11
12 alias Pleroma.EctoType.ActivityPub.ObjectValidators
13 alias Pleroma.Object
14 alias Pleroma.User
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
23 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
24 def validate(object, meta)
25
26 def validate(%{"type" => "Undo"} = object, meta) do
27 with {:ok, object} <-
28 object
29 |> UndoValidator.cast_and_validate()
30 |> Ecto.Changeset.apply_action(:insert) do
31 object = stringify_keys(object)
32 {:ok, object, meta}
33 end
34 end
35
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)
42 {:ok, object, meta}
43 end
44 end
45
46 def validate(%{"type" => "Like"} = object, meta) do
47 with {:ok, object} <-
48 object
49 |> LikeValidator.cast_and_validate()
50 |> Ecto.Changeset.apply_action(:insert) do
51 object = stringify_keys(object)
52 {:ok, object, meta}
53 end
54 end
55
56 def validate(%{"type" => "ChatMessage"} = object, meta) do
57 with {:ok, object} <-
58 object
59 |> ChatMessageValidator.cast_and_validate()
60 |> Ecto.Changeset.apply_action(:insert) do
61 object = stringify_keys(object)
62 {:ok, object, meta}
63 end
64 end
65
66 def validate(%{"type" => "EmojiReact"} = object, meta) do
67 with {:ok, object} <-
68 object
69 |> EmojiReactValidator.cast_and_validate()
70 |> Ecto.Changeset.apply_action(:insert) do
71 object = stringify_keys(object |> Map.from_struct())
72 {:ok, object, meta}
73 end
74 end
75
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} <-
80 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}
85 end
86 end
87
88 def validate(%{"type" => "Announce"} = object, meta) do
89 with {:ok, object} <-
90 object
91 |> AnnounceValidator.cast_and_validate()
92 |> Ecto.Changeset.apply_action(:insert) do
93 object = stringify_keys(object |> Map.from_struct())
94 {:ok, object, meta}
95 end
96 end
97
98 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
99 ChatMessageValidator.cast_and_apply(object)
100 end
101
102 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
103
104 def stringify_keys(%{__struct__: _} = object) do
105 object
106 |> Map.from_struct()
107 |> stringify_keys
108 end
109
110 def stringify_keys(object) when is_map(object) do
111 object
112 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
113 end
114
115 def stringify_keys(object) when is_list(object) do
116 object
117 |> Enum.map(&stringify_keys/1)
118 end
119
120 def stringify_keys(object), do: object
121
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)
125 end
126 end
127
128 def fetch_actor_and_object(object) do
129 fetch_actor(object)
130 Object.normalize(object["object"], true)
131 :ok
132 end
133 end