Merge branch 'update-validator' 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 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
23
24 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
25 def validate(object, meta)
26
27 def validate(%{"type" => "Update"} = update_activity, meta) do
28 with {:ok, update_activity} <-
29 update_activity
30 |> UpdateValidator.cast_and_validate()
31 |> Ecto.Changeset.apply_action(:insert) do
32 update_activity = stringify_keys(update_activity)
33 {:ok, update_activity, meta}
34 end
35 end
36
37 def validate(%{"type" => "Undo"} = object, meta) do
38 with {:ok, object} <-
39 object
40 |> UndoValidator.cast_and_validate()
41 |> Ecto.Changeset.apply_action(:insert) do
42 object = stringify_keys(object)
43 {:ok, object, meta}
44 end
45 end
46
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)
53 {:ok, object, meta}
54 end
55 end
56
57 def validate(%{"type" => "Like"} = object, meta) do
58 with {:ok, object} <-
59 object
60 |> LikeValidator.cast_and_validate()
61 |> Ecto.Changeset.apply_action(:insert) do
62 object = stringify_keys(object)
63 {:ok, object, meta}
64 end
65 end
66
67 def validate(%{"type" => "ChatMessage"} = object, meta) do
68 with {:ok, object} <-
69 object
70 |> ChatMessageValidator.cast_and_validate()
71 |> Ecto.Changeset.apply_action(:insert) do
72 object = stringify_keys(object)
73 {:ok, object, meta}
74 end
75 end
76
77 def validate(%{"type" => "EmojiReact"} = object, meta) do
78 with {:ok, object} <-
79 object
80 |> EmojiReactValidator.cast_and_validate()
81 |> Ecto.Changeset.apply_action(:insert) do
82 object = stringify_keys(object |> Map.from_struct())
83 {:ok, object, meta}
84 end
85 end
86
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} <-
91 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}
96 end
97 end
98
99 def validate(%{"type" => "Announce"} = object, meta) do
100 with {:ok, object} <-
101 object
102 |> AnnounceValidator.cast_and_validate()
103 |> Ecto.Changeset.apply_action(:insert) do
104 object = stringify_keys(object |> Map.from_struct())
105 {:ok, object, meta}
106 end
107 end
108
109 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
110 ChatMessageValidator.cast_and_apply(object)
111 end
112
113 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
114
115 def stringify_keys(%{__struct__: _} = object) do
116 object
117 |> Map.from_struct()
118 |> stringify_keys
119 end
120
121 def stringify_keys(object) when is_map(object) do
122 object
123 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
124 end
125
126 def stringify_keys(object) when is_list(object) do
127 object
128 |> Enum.map(&stringify_keys/1)
129 end
130
131 def stringify_keys(object), do: object
132
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)
136 end
137 end
138
139 def fetch_actor_and_object(object) do
140 fetch_actor(object)
141 Object.normalize(object["object"], true)
142 :ok
143 end
144 end