Merge branch '1031-parent-visible' 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.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
24
25 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
26 def validate(object, meta)
27
28 def validate(%{"type" => "Block"} = block_activity, meta) do
29 with {:ok, block_activity} <-
30 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])
35
36 meta =
37 if !outgoing_blocks do
38 Keyword.put(meta, :do_not_federate, true)
39 else
40 meta
41 end
42
43 {:ok, block_activity, meta}
44 end
45 end
46
47 def validate(%{"type" => "Update"} = update_activity, meta) do
48 with {:ok, update_activity} <-
49 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}
54 end
55 end
56
57 def validate(%{"type" => "Undo"} = object, meta) do
58 with {:ok, object} <-
59 object
60 |> UndoValidator.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" => "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)
73 {:ok, object, meta}
74 end
75 end
76
77 def validate(%{"type" => "Like"} = object, meta) do
78 with {:ok, object} <-
79 object
80 |> LikeValidator.cast_and_validate()
81 |> Ecto.Changeset.apply_action(:insert) do
82 object = stringify_keys(object)
83 {:ok, object, meta}
84 end
85 end
86
87 def validate(%{"type" => "ChatMessage"} = object, meta) do
88 with {:ok, object} <-
89 object
90 |> ChatMessageValidator.cast_and_validate()
91 |> Ecto.Changeset.apply_action(:insert) do
92 object = stringify_keys(object)
93 {:ok, object, meta}
94 end
95 end
96
97 def validate(%{"type" => "EmojiReact"} = object, meta) do
98 with {:ok, object} <-
99 object
100 |> EmojiReactValidator.cast_and_validate()
101 |> Ecto.Changeset.apply_action(:insert) do
102 object = stringify_keys(object |> Map.from_struct())
103 {:ok, object, meta}
104 end
105 end
106
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} <-
111 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}
116 end
117 end
118
119 def validate(%{"type" => "Announce"} = object, meta) do
120 with {:ok, object} <-
121 object
122 |> AnnounceValidator.cast_and_validate()
123 |> Ecto.Changeset.apply_action(:insert) do
124 object = stringify_keys(object |> Map.from_struct())
125 {:ok, object, meta}
126 end
127 end
128
129 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
130 ChatMessageValidator.cast_and_apply(object)
131 end
132
133 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
134
135 def stringify_keys(%{__struct__: _} = object) do
136 object
137 |> Map.from_struct()
138 |> stringify_keys
139 end
140
141 def stringify_keys(object) when is_map(object) do
142 object
143 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
144 end
145
146 def stringify_keys(object) when is_list(object) do
147 object
148 |> Enum.map(&stringify_keys/1)
149 end
150
151 def stringify_keys(object), do: object
152
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)
156 end
157 end
158
159 def fetch_actor_and_object(object) do
160 fetch_actor(object)
161 Object.normalize(object["object"], true)
162 :ok
163 end
164 end