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