Add Pleroma.Plugs.Cache
[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 by_object_ap_id(ap_id) do
177 from(
178 activity in Activity,
179 where:
180 fragment(
181 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
182 activity.data,
183 activity.data,
184 ^to_string(ap_id)
185 )
186 )
187 end
188
189 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
190 from(
191 activity in Activity,
192 where:
193 fragment(
194 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
195 activity.data,
196 activity.data,
197 ^ap_ids
198 ),
199 where: fragment("(?)->>'type' = 'Create'", activity.data)
200 )
201 end
202
203 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
204 from(
205 activity in Activity,
206 where:
207 fragment(
208 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
209 activity.data,
210 activity.data,
211 ^to_string(ap_id)
212 ),
213 where: fragment("(?)->>'type' = 'Create'", activity.data)
214 )
215 end
216
217 def create_by_object_ap_id(_), do: nil
218
219 def get_all_create_by_object_ap_id(ap_id) do
220 Repo.all(create_by_object_ap_id(ap_id))
221 end
222
223 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
224 create_by_object_ap_id(ap_id)
225 |> restrict_deactivated_users()
226 |> Repo.one()
227 end
228
229 def get_create_by_object_ap_id(_), do: nil
230
231 def create_by_object_ap_id_with_object(ap_ids) when is_list(ap_ids) do
232 from(
233 activity in Activity,
234 where:
235 fragment(
236 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
237 activity.data,
238 activity.data,
239 ^ap_ids
240 ),
241 where: fragment("(?)->>'type' = 'Create'", activity.data),
242 inner_join: o in Object,
243 on:
244 fragment(
245 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
246 o.data,
247 activity.data,
248 activity.data
249 ),
250 preload: [object: o]
251 )
252 end
253
254 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
255 from(
256 activity in Activity,
257 where:
258 fragment(
259 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
260 activity.data,
261 activity.data,
262 ^to_string(ap_id)
263 ),
264 where: fragment("(?)->>'type' = 'Create'", activity.data),
265 inner_join: o in Object,
266 on:
267 fragment(
268 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
269 o.data,
270 activity.data,
271 activity.data
272 ),
273 preload: [object: o]
274 )
275 end
276
277 def create_by_object_ap_id_with_object(_), do: nil
278
279 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
280 ap_id
281 |> create_by_object_ap_id_with_object()
282 |> Repo.one()
283 end
284
285 def get_create_by_object_ap_id_with_object(_), do: nil
286
287 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
288 get_create_by_object_ap_id_with_object(ap_id)
289 end
290
291 defp get_in_reply_to_activity_from_object(_), do: nil
292
293 def get_in_reply_to_activity(%Activity{} = activity) do
294 get_in_reply_to_activity_from_object(Object.normalize(activity))
295 end
296
297 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
298 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
299 def normalize(_), do: nil
300
301 def delete_by_ap_id(id) when is_binary(id) do
302 by_object_ap_id(id)
303 |> select([u], u)
304 |> Repo.delete_all()
305 |> elem(1)
306 |> Enum.find(fn
307 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
308 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
309 _ -> nil
310 end)
311 |> purge_web_resp_cache()
312 end
313
314 def delete_by_ap_id(_), do: nil
315
316 defp purge_web_resp_cache(%Activity{} = activity) do
317 %{path: path} = URI.parse(activity.data["id"])
318 Cachex.del(:web_resp_cache, path)
319 activity
320 end
321
322 defp purge_web_resp_cache(nil), do: nil
323
324 for {ap_type, type} <- @mastodon_notification_types do
325 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
326 do: unquote(type)
327 end
328
329 def mastodon_notification_type(%Activity{}), do: nil
330
331 def from_mastodon_notification_type(type) do
332 Map.get(@mastodon_to_ap_notification_types, type)
333 end
334
335 def all_by_actor_and_id(actor, status_ids \\ [])
336 def all_by_actor_and_id(_actor, []), do: []
337
338 def all_by_actor_and_id(actor, status_ids) do
339 Activity
340 |> where([s], s.id in ^status_ids)
341 |> where([s], s.actor == ^actor)
342 |> Repo.all()
343 end
344
345 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
346 from(
347 a in Activity,
348 where:
349 fragment(
350 "? ->> 'type' = 'Follow'",
351 a.data
352 ),
353 where:
354 fragment(
355 "? ->> 'state' = 'pending'",
356 a.data
357 ),
358 where:
359 fragment(
360 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
361 a.data,
362 a.data,
363 ^ap_id
364 )
365 )
366 end
367
368 @spec query_by_actor(actor()) :: Ecto.Query.t()
369 def query_by_actor(actor) do
370 from(a in Activity, where: a.actor == ^actor)
371 end
372
373 def restrict_deactivated_users(query) do
374 deactivated_users =
375 from(u in User.Query.build(deactivated: true), select: u.ap_id)
376 |> Repo.all()
377
378 from(activity in query,
379 where: activity.actor not in ^deactivated_users
380 )
381 end
382
383 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search
384 end