Merge remote-tracking branch 'pleroma/develop' into cycles-router-api-routes
[akkoma] / lib / pleroma / web / activity_pub / object_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 @behaviour Pleroma.Web.ActivityPub.ObjectValidator.Validating
13
14 alias Pleroma.Activity
15 alias Pleroma.EctoType.ActivityPub.ObjectValidators
16 alias Pleroma.Object
17 alias Pleroma.Object.Containment
18 alias Pleroma.User
19 alias Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator
20 alias Pleroma.Web.ActivityPub.ObjectValidators.AddRemoveValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNoteValidator
24 alias Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator
25 alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
26 alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
27 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
28 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator
29 alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
30 alias Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator
31 alias Pleroma.Web.ActivityPub.ObjectValidators.EventValidator
32 alias Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator
33 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
34 alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator
35 alias Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator
36 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
37
38 @impl true
39 def validate(object, meta)
40
41 def validate(%{"type" => "Block"} = block_activity, meta) do
42 with {:ok, block_activity} <-
43 block_activity
44 |> BlockValidator.cast_and_validate()
45 |> Ecto.Changeset.apply_action(:insert) do
46 block_activity = stringify_keys(block_activity)
47 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
48
49 meta =
50 if !outgoing_blocks do
51 Keyword.put(meta, :do_not_federate, true)
52 else
53 meta
54 end
55
56 {:ok, block_activity, meta}
57 end
58 end
59
60 def validate(%{"type" => "Undo"} = object, meta) do
61 with {:ok, object} <-
62 object
63 |> UndoValidator.cast_and_validate()
64 |> Ecto.Changeset.apply_action(:insert) do
65 object = stringify_keys(object)
66 undone_object = Activity.get_by_ap_id(object["object"])
67
68 meta =
69 meta
70 |> Keyword.put(:object_data, undone_object.data)
71
72 {:ok, object, meta}
73 end
74 end
75
76 def validate(%{"type" => "Delete"} = object, meta) do
77 with cng <- DeleteValidator.cast_and_validate(object),
78 do_not_federate <- DeleteValidator.do_not_federate?(cng),
79 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
80 object = stringify_keys(object)
81 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
82 {:ok, object, meta}
83 end
84 end
85
86 def validate(
87 %{"type" => "Create", "object" => %{"type" => "ChatMessage"} = object} = create_activity,
88 meta
89 ) do
90 with {:ok, object_data} <- cast_and_apply(object),
91 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
92 {:ok, create_activity} <-
93 create_activity
94 |> CreateChatMessageValidator.cast_and_validate(meta)
95 |> Ecto.Changeset.apply_action(:insert) do
96 create_activity = stringify_keys(create_activity)
97 {:ok, create_activity, meta}
98 end
99 end
100
101 def validate(
102 %{"type" => "Create", "object" => %{"type" => objtype} = object} = create_activity,
103 meta
104 )
105 when objtype in ~w[Question Answer Audio Video Event Article] do
106 with {:ok, object_data} <- cast_and_apply(object),
107 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
108 {:ok, create_activity} <-
109 create_activity
110 |> CreateGenericValidator.cast_and_validate(meta)
111 |> Ecto.Changeset.apply_action(:insert) do
112 create_activity = stringify_keys(create_activity)
113 {:ok, create_activity, meta}
114 end
115 end
116
117 def validate(%{"type" => type} = object, meta)
118 when type in ~w[Event Question Audio Video Article] do
119 validator =
120 case type do
121 "Event" -> EventValidator
122 "Question" -> QuestionValidator
123 "Audio" -> AudioVideoValidator
124 "Video" -> AudioVideoValidator
125 "Article" -> ArticleNoteValidator
126 end
127
128 with {:ok, object} <-
129 object
130 |> validator.cast_and_validate()
131 |> Ecto.Changeset.apply_action(:insert) do
132 object = stringify_keys(object)
133
134 # Insert copy of hashtags as strings for the non-hashtag table indexing
135 tag = (object["tag"] || []) ++ Object.hashtags(%Object{data: object})
136 object = Map.put(object, "tag", tag)
137
138 {:ok, object, meta}
139 end
140 end
141
142 def validate(%{"type" => type} = object, meta)
143 when type in ~w[Accept Reject Follow Update Like EmojiReact Announce
144 ChatMessage Answer] do
145 validator =
146 case type do
147 "Accept" -> AcceptRejectValidator
148 "Reject" -> AcceptRejectValidator
149 "Follow" -> FollowValidator
150 "Update" -> UpdateValidator
151 "Like" -> LikeValidator
152 "EmojiReact" -> EmojiReactValidator
153 "Announce" -> AnnounceValidator
154 "ChatMessage" -> ChatMessageValidator
155 "Answer" -> AnswerValidator
156 end
157
158 with {:ok, object} <-
159 object
160 |> validator.cast_and_validate()
161 |> Ecto.Changeset.apply_action(:insert) do
162 object = stringify_keys(object)
163 {:ok, object, meta}
164 end
165 end
166
167 def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
168 with {:ok, object} <-
169 object
170 |> AddRemoveValidator.cast_and_validate()
171 |> Ecto.Changeset.apply_action(:insert) do
172 object = stringify_keys(object)
173 {:ok, object, meta}
174 end
175 end
176
177 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
178 ChatMessageValidator.cast_and_apply(object)
179 end
180
181 def cast_and_apply(%{"type" => "Question"} = object) do
182 QuestionValidator.cast_and_apply(object)
183 end
184
185 def cast_and_apply(%{"type" => "Answer"} = object) do
186 AnswerValidator.cast_and_apply(object)
187 end
188
189 def cast_and_apply(%{"type" => type} = object) when type in ~w[Audio Video] do
190 AudioVideoValidator.cast_and_apply(object)
191 end
192
193 def cast_and_apply(%{"type" => "Event"} = object) do
194 EventValidator.cast_and_apply(object)
195 end
196
197 def cast_and_apply(%{"type" => "Article"} = object) do
198 ArticleNoteValidator.cast_and_apply(object)
199 end
200
201 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
202
203 # is_struct/1 appears in Elixir 1.11
204 def stringify_keys(%{__struct__: _} = object) do
205 object
206 |> Map.from_struct()
207 |> stringify_keys
208 end
209
210 def stringify_keys(object) when is_map(object) do
211 object
212 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
213 end
214
215 def stringify_keys(object) when is_list(object) do
216 object
217 |> Enum.map(&stringify_keys/1)
218 end
219
220 def stringify_keys(object), do: object
221
222 def fetch_actor(object) do
223 with actor <- Containment.get_actor(object),
224 {:ok, actor} <- ObjectValidators.ObjectID.cast(actor) do
225 User.get_or_fetch_by_ap_id(actor)
226 end
227 end
228
229 def fetch_actor_and_object(object) do
230 fetch_actor(object)
231 Object.normalize(object["object"], fetch: true)
232 :ok
233 end
234 end