Merge branch 'fix/oom-parallel-rendering' into 'develop'
[akkoma] / lib / pleroma / activity.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity do
6 use Ecto.Schema
7
8 alias Pleroma.Activity
9 alias Pleroma.ActivityExpiration
10 alias Pleroma.Bookmark
11 alias Pleroma.Notification
12 alias Pleroma.Object
13 alias Pleroma.Repo
14 alias Pleroma.ThreadMute
15 alias Pleroma.User
16
17 import Ecto.Changeset
18 import Ecto.Query
19
20 @type t :: %__MODULE__{}
21 @type actor :: String.t()
22
23 @primary_key {:id, Pleroma.FlakeId, autogenerate: true}
24
25 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
26 @mastodon_notification_types %{
27 "Create" => "mention",
28 "Follow" => "follow",
29 "Announce" => "reblog",
30 "Like" => "favourite"
31 }
32
33 @mastodon_to_ap_notification_types for {k, v} <- @mastodon_notification_types,
34 into: %{},
35 do: {v, k}
36
37 schema "activities" do
38 field(:data, :map)
39 field(:local, :boolean, default: true)
40 field(:actor, :string)
41 field(:recipients, {:array, :string}, default: [])
42 field(:thread_muted?, :boolean, virtual: true)
43 # This is a fake relation, do not use outside of with_preloaded_bookmark/get_bookmark
44 has_one(:bookmark, Bookmark)
45 has_many(:notifications, Notification, on_delete: :delete_all)
46
47 # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
48 # The foreign key is embedded in a jsonb field.
49 #
50 # To use it, you probably want to do an inner join and a preload:
51 #
52 # ```
53 # |> join(:inner, [activity], o in Object,
54 # on: fragment("(?->>'id') = COALESCE((?)->'object'->> 'id', (?)->>'object')",
55 # o.data, activity.data, activity.data))
56 # |> preload([activity, object], [object: object])
57 # ```
58 #
59 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
60 # typical case.
61 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
62
63 has_one(:expiration, ActivityExpiration, on_delete: :delete_all)
64
65 timestamps()
66 end
67
68 def with_joined_object(query) do
69 join(query, :inner, [activity], o in Object,
70 on:
71 fragment(
72 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
73 o.data,
74 activity.data,
75 activity.data
76 ),
77 as: :object
78 )
79 end
80
81 def with_preloaded_object(query) do
82 query
83 |> has_named_binding?(:object)
84 |> if(do: query, else: with_joined_object(query))
85 |> preload([activity, object: object], object: object)
86 end
87
88 def with_preloaded_bookmark(query, %User{} = user) do
89 from([a] in query,
90 left_join: b in Bookmark,
91 on: b.user_id == ^user.id and b.activity_id == a.id,
92 preload: [bookmark: b]
93 )
94 end
95
96 def with_preloaded_bookmark(query, _), do: query
97
98 def with_set_thread_muted_field(query, %User{} = user) do
99 from([a] in query,
100 left_join: tm in ThreadMute,
101 on: tm.user_id == ^user.id and tm.context == fragment("?->>'context'", a.data),
102 as: :thread_mute,
103 select: %Activity{a | thread_muted?: not is_nil(tm.id)}
104 )
105 end
106
107 def with_set_thread_muted_field(query, _), do: query
108
109 def get_by_ap_id(ap_id) do
110 Repo.one(
111 from(
112 activity in Activity,
113 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
114 )
115 )
116 end
117
118 def get_bookmark(%Activity{} = activity, %User{} = user) do
119 if Ecto.assoc_loaded?(activity.bookmark) do
120 activity.bookmark
121 else
122 Bookmark.get(user.id, activity.id)
123 end
124 end
125
126 def get_bookmark(_, _), do: nil
127
128 def change(struct, params \\ %{}) do
129 struct
130 |> cast(params, [:data, :recipients])
131 |> validate_required([:data])
132 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
133 end
134
135 def get_by_ap_id_with_object(ap_id) do
136 Repo.one(
137 from(
138 activity in Activity,
139 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
140 left_join: o in Object,
141 on:
142 fragment(
143 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
144 o.data,
145 activity.data,
146 activity.data
147 ),
148 preload: [object: o]
149 )
150 )
151 end
152
153 def get_by_id(id) do
154 Activity
155 |> where([a], a.id == ^id)
156 |> restrict_deactivated_users()
157 |> Repo.one()
158 end
159
160 def get_by_id_with_object(id) do
161 from(activity in Activity,
162 where: activity.id == ^id,
163 inner_join: o in Object,
164 on:
165 fragment(
166 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
167 o.data,
168 activity.data,
169 activity.data
170 ),
171 preload: [object: o]
172 )
173 |> Repo.one()
174 end
175
176 def all_by_ids_with_object(ids) do
177 Activity
178 |> where([a], a.id in ^ids)
179 |> with_preloaded_object()
180 |> Repo.all()
181 end
182
183 def by_object_ap_id(ap_id) do
184 from(
185 activity in Activity,
186 where:
187 fragment(
188 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
189 activity.data,
190 activity.data,
191 ^to_string(ap_id)
192 )
193 )
194 end
195
196 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
197 from(
198 activity in Activity,
199 where:
200 fragment(
201 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
202 activity.data,
203 activity.data,
204 ^ap_ids
205 ),
206 where: fragment("(?)->>'type' = 'Create'", activity.data)
207 )
208 end
209
210 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
211 from(
212 activity in Activity,
213 where:
214 fragment(
215 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
216 activity.data,
217 activity.data,
218 ^to_string(ap_id)
219 ),
220 where: fragment("(?)->>'type' = 'Create'", activity.data)
221 )
222 end
223
224 def create_by_object_ap_id(_), do: nil
225
226 def get_all_create_by_object_ap_id(ap_id) do
227 Repo.all(create_by_object_ap_id(ap_id))
228 end
229
230 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
231 create_by_object_ap_id(ap_id)
232 |> restrict_deactivated_users()
233 |> Repo.one()
234 end
235
236 def get_create_by_object_ap_id(_), do: nil
237
238 def create_by_object_ap_id_with_object(ap_ids) when is_list(ap_ids) do
239 from(
240 activity in Activity,
241 where:
242 fragment(
243 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
244 activity.data,
245 activity.data,
246 ^ap_ids
247 ),
248 where: fragment("(?)->>'type' = 'Create'", activity.data),
249 inner_join: o in Object,
250 on:
251 fragment(
252 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
253 o.data,
254 activity.data,
255 activity.data
256 ),
257 preload: [object: o]
258 )
259 end
260
261 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
262 from(
263 activity in Activity,
264 where:
265 fragment(
266 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
267 activity.data,
268 activity.data,
269 ^to_string(ap_id)
270 ),
271 where: fragment("(?)->>'type' = 'Create'", activity.data),
272 inner_join: o in Object,
273 on:
274 fragment(
275 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
276 o.data,
277 activity.data,
278 activity.data
279 ),
280 preload: [object: o]
281 )
282 end
283
284 def create_by_object_ap_id_with_object(_), do: nil
285
286 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
287 ap_id
288 |> create_by_object_ap_id_with_object()
289 |> Repo.one()
290 end
291
292 def get_create_by_object_ap_id_with_object(_), do: nil
293
294 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
295 get_create_by_object_ap_id_with_object(ap_id)
296 end
297
298 defp get_in_reply_to_activity_from_object(_), do: nil
299
300 def get_in_reply_to_activity(%Activity{} = activity) do
301 get_in_reply_to_activity_from_object(Object.normalize(activity))
302 end
303
304 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
305 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
306 def normalize(_), do: nil
307
308 def delete_by_ap_id(id) when is_binary(id) do
309 by_object_ap_id(id)
310 |> select([u], u)
311 |> Repo.delete_all()
312 |> elem(1)
313 |> Enum.find(fn
314 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
315 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
316 _ -> nil
317 end)
318 |> purge_web_resp_cache()
319 end
320
321 def delete_by_ap_id(_), do: nil
322
323 defp purge_web_resp_cache(%Activity{} = activity) do
324 %{path: path} = URI.parse(activity.data["id"])
325 Cachex.del(:web_resp_cache, path)
326 activity
327 end
328
329 defp purge_web_resp_cache(nil), do: nil
330
331 for {ap_type, type} <- @mastodon_notification_types do
332 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
333 do: unquote(type)
334 end
335
336 def mastodon_notification_type(%Activity{}), do: nil
337
338 def from_mastodon_notification_type(type) do
339 Map.get(@mastodon_to_ap_notification_types, type)
340 end
341
342 def all_by_actor_and_id(actor, status_ids \\ [])
343 def all_by_actor_and_id(_actor, []), do: []
344
345 def all_by_actor_and_id(actor, status_ids) do
346 Activity
347 |> where([s], s.id in ^status_ids)
348 |> where([s], s.actor == ^actor)
349 |> Repo.all()
350 end
351
352 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
353 from(
354 a in Activity,
355 where:
356 fragment(
357 "? ->> 'type' = 'Follow'",
358 a.data
359 ),
360 where:
361 fragment(
362 "? ->> 'state' = 'pending'",
363 a.data
364 ),
365 where:
366 fragment(
367 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
368 a.data,
369 a.data,
370 ^ap_id
371 )
372 )
373 end
374
375 @spec query_by_actor(actor()) :: Ecto.Query.t()
376 def query_by_actor(actor) do
377 from(a in Activity, where: a.actor == ^actor)
378 end
379
380 def restrict_deactivated_users(query) do
381 deactivated_users =
382 from(u in User.Query.build(deactivated: true), select: u.ap_id)
383 |> Repo.all()
384
385 from(activity in query,
386 where: activity.actor not in ^deactivated_users
387 )
388 end
389
390 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search
391 end