question_validator: remove conversation field
[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.AnswerValidator
17 alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
18 alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
19 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
20 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateQuestionValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator
24 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
25 alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator
26 alias Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator
27 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
28
29 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
30 def validate(object, meta)
31
32 def validate(%{"type" => "Follow"} = object, meta) do
33 with {:ok, object} <-
34 object
35 |> FollowValidator.cast_and_validate()
36 |> Ecto.Changeset.apply_action(:insert) do
37 object = stringify_keys(object)
38 {:ok, object, meta}
39 end
40 end
41
42 def validate(%{"type" => "Block"} = block_activity, meta) do
43 with {:ok, block_activity} <-
44 block_activity
45 |> BlockValidator.cast_and_validate()
46 |> Ecto.Changeset.apply_action(:insert) do
47 block_activity = stringify_keys(block_activity)
48 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
49
50 meta =
51 if !outgoing_blocks do
52 Keyword.put(meta, :do_not_federate, true)
53 else
54 meta
55 end
56
57 {:ok, block_activity, meta}
58 end
59 end
60
61 def validate(%{"type" => "Update"} = update_activity, meta) do
62 with {:ok, update_activity} <-
63 update_activity
64 |> UpdateValidator.cast_and_validate()
65 |> Ecto.Changeset.apply_action(:insert) do
66 update_activity = stringify_keys(update_activity)
67 {:ok, update_activity, meta}
68 end
69 end
70
71 def validate(%{"type" => "Undo"} = object, meta) do
72 with {:ok, object} <-
73 object
74 |> UndoValidator.cast_and_validate()
75 |> Ecto.Changeset.apply_action(:insert) do
76 object = stringify_keys(object)
77 {:ok, object, meta}
78 end
79 end
80
81 def validate(%{"type" => "Delete"} = object, meta) do
82 with cng <- DeleteValidator.cast_and_validate(object),
83 do_not_federate <- DeleteValidator.do_not_federate?(cng),
84 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
85 object = stringify_keys(object)
86 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
87 {:ok, object, meta}
88 end
89 end
90
91 def validate(%{"type" => "Like"} = object, meta) do
92 with {:ok, object} <-
93 object
94 |> LikeValidator.cast_and_validate()
95 |> Ecto.Changeset.apply_action(:insert) do
96 object = stringify_keys(object)
97 {:ok, object, meta}
98 end
99 end
100
101 def validate(%{"type" => "ChatMessage"} = object, meta) do
102 with {:ok, object} <-
103 object
104 |> ChatMessageValidator.cast_and_validate()
105 |> Ecto.Changeset.apply_action(:insert) do
106 object = stringify_keys(object)
107 {:ok, object, meta}
108 end
109 end
110
111 def validate(%{"type" => "Question"} = object, meta) do
112 with {:ok, object} <-
113 object
114 |> QuestionValidator.cast_and_validate()
115 |> Ecto.Changeset.apply_action(:insert) do
116 object = stringify_keys(object)
117 {:ok, object, meta}
118 end
119 end
120
121 def validate(%{"type" => "Answer"} = object, meta) do
122 with {:ok, object} <-
123 object
124 |> AnswerValidator.cast_and_validate()
125 |> Ecto.Changeset.apply_action(:insert) do
126 object = stringify_keys(object)
127 {:ok, object, meta}
128 end
129 end
130
131 def validate(%{"type" => "EmojiReact"} = object, meta) do
132 with {:ok, object} <-
133 object
134 |> EmojiReactValidator.cast_and_validate()
135 |> Ecto.Changeset.apply_action(:insert) do
136 object = stringify_keys(object)
137 {:ok, object, meta}
138 end
139 end
140
141 def validate(
142 %{"type" => "Create", "object" => %{"type" => "ChatMessage"} = object} = create_activity,
143 meta
144 ) do
145 with {:ok, object_data} <- cast_and_apply(object),
146 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
147 {:ok, create_activity} <-
148 create_activity
149 |> CreateChatMessageValidator.cast_and_validate(meta)
150 |> Ecto.Changeset.apply_action(:insert) do
151 create_activity = stringify_keys(create_activity)
152 {:ok, create_activity, meta}
153 end
154 end
155
156 def validate(
157 %{"type" => "Create", "object" => %{"type" => objtype} = object} = create_activity,
158 meta
159 )
160 when objtype in ["Question", "Answer"] do
161 with {:ok, object_data} <- cast_and_apply(object),
162 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
163 {:ok, create_activity} <-
164 create_activity
165 |> CreateQuestionValidator.cast_and_validate(meta)
166 |> Ecto.Changeset.apply_action(:insert) do
167 create_activity = stringify_keys(create_activity)
168 {:ok, create_activity, meta}
169 end
170 end
171
172 def validate(%{"type" => "Announce"} = object, meta) do
173 with {:ok, object} <-
174 object
175 |> AnnounceValidator.cast_and_validate()
176 |> Ecto.Changeset.apply_action(:insert) do
177 object = stringify_keys(object)
178 {:ok, object, meta}
179 end
180 end
181
182 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
183 ChatMessageValidator.cast_and_apply(object)
184 end
185
186 def cast_and_apply(%{"type" => "Question"} = object) do
187 QuestionValidator.cast_and_apply(object)
188 end
189
190 def cast_and_apply(%{"type" => "Answer"} = object) do
191 QuestionValidator.cast_and_apply(object)
192 end
193
194 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
195
196 # is_struct/1 isn't present in Elixir 1.8.x
197 def stringify_keys(%{__struct__: _} = object) do
198 object
199 |> Map.from_struct()
200 |> stringify_keys
201 end
202
203 def stringify_keys(object) when is_map(object) do
204 object
205 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
206 end
207
208 def stringify_keys(object) when is_list(object) do
209 object
210 |> Enum.map(&stringify_keys/1)
211 end
212
213 def stringify_keys(object), do: object
214
215 def fetch_actor(object) do
216 with {:ok, actor} <- ObjectValidators.ObjectID.cast(object["actor"]) do
217 User.get_or_fetch_by_ap_id(actor)
218 end
219 end
220
221 def fetch_actor_and_object(object) do
222 fetch_actor(object)
223 Object.normalize(object["object"], true)
224 :ok
225 end
226 end