Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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.ArticleNotePageValidator
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 Note Page] 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 Note Page] do
119 validator =
120 case type do
121 "Event" -> EventValidator
122 "Question" -> QuestionValidator
123 "Audio" -> AudioVideoValidator
124 "Video" -> AudioVideoValidator
125 "Article" -> ArticleNotePageValidator
126 "Note" -> ArticleNotePageValidator
127 "Page" -> ArticleNotePageValidator
128 end
129
130 with {:ok, object} <-
131 object
132 |> validator.cast_and_validate()
133 |> Ecto.Changeset.apply_action(:insert) do
134 object = stringify_keys(object)
135
136 # Insert copy of hashtags as strings for the non-hashtag table indexing
137 tag = (object["tag"] || []) ++ Object.hashtags(%Object{data: object})
138 object = Map.put(object, "tag", tag)
139
140 {:ok, object, meta}
141 end
142 end
143
144 def validate(%{"type" => type} = object, meta)
145 when type in ~w[Accept Reject Follow Update Like EmojiReact Announce
146 ChatMessage Answer] do
147 validator =
148 case type do
149 "Accept" -> AcceptRejectValidator
150 "Reject" -> AcceptRejectValidator
151 "Follow" -> FollowValidator
152 "Update" -> UpdateValidator
153 "Like" -> LikeValidator
154 "EmojiReact" -> EmojiReactValidator
155 "Announce" -> AnnounceValidator
156 "ChatMessage" -> ChatMessageValidator
157 "Answer" -> AnswerValidator
158 end
159
160 with {:ok, object} <-
161 object
162 |> validator.cast_and_validate()
163 |> Ecto.Changeset.apply_action(:insert) do
164 object = stringify_keys(object)
165 {:ok, object, meta}
166 end
167 end
168
169 def validate(%{"type" => type} = object, meta) when type in ~w(Add Remove) do
170 with {:ok, object} <-
171 object
172 |> AddRemoveValidator.cast_and_validate()
173 |> Ecto.Changeset.apply_action(:insert) do
174 object = stringify_keys(object)
175 {:ok, object, meta}
176 end
177 end
178
179 def validate(o, m), do: {:error, {:validator_not_set, {o, m}}}
180
181 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
182 ChatMessageValidator.cast_and_apply(object)
183 end
184
185 def cast_and_apply(%{"type" => "Question"} = object) do
186 QuestionValidator.cast_and_apply(object)
187 end
188
189 def cast_and_apply(%{"type" => "Answer"} = object) do
190 AnswerValidator.cast_and_apply(object)
191 end
192
193 def cast_and_apply(%{"type" => type} = object) when type in ~w[Audio Video] do
194 AudioVideoValidator.cast_and_apply(object)
195 end
196
197 def cast_and_apply(%{"type" => "Event"} = object) do
198 EventValidator.cast_and_apply(object)
199 end
200
201 def cast_and_apply(%{"type" => type} = object) when type in ~w[Article Note Page] do
202 ArticleNotePageValidator.cast_and_apply(object)
203 end
204
205 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
206
207 # is_struct/1 appears in Elixir 1.11
208 def stringify_keys(%{__struct__: _} = object) do
209 object
210 |> Map.from_struct()
211 |> stringify_keys
212 end
213
214 def stringify_keys(object) when is_map(object) do
215 object
216 |> Enum.filter(fn {_, v} -> v != nil end)
217 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
218 end
219
220 def stringify_keys(object) when is_list(object) do
221 object
222 |> Enum.map(&stringify_keys/1)
223 end
224
225 def stringify_keys(object), do: object
226
227 def fetch_actor(object) do
228 with actor <- Containment.get_actor(object),
229 {:ok, actor} <- ObjectValidators.ObjectID.cast(actor) do
230 User.get_or_fetch_by_ap_id(actor)
231 end
232 end
233
234 def fetch_actor_and_object(object) do
235 fetch_actor(object)
236 Object.normalize(object["object"], fetch: true)
237 :ok
238 end
239 end