Merge branch 'length-limit-bio' 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.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 select: %Activity{a | thread_muted?: not is_nil(tm.id)}
100 )
101 end
102
103 def with_set_thread_muted_field(query, _), do: query
104
105 def get_by_ap_id(ap_id) do
106 Repo.one(
107 from(
108 activity in Activity,
109 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
110 )
111 )
112 end
113
114 def get_bookmark(%Activity{} = activity, %User{} = user) do
115 if Ecto.assoc_loaded?(activity.bookmark) do
116 activity.bookmark
117 else
118 Bookmark.get(user.id, activity.id)
119 end
120 end
121
122 def get_bookmark(_, _), do: nil
123
124 def change(struct, params \\ %{}) do
125 struct
126 |> cast(params, [:data, :recipients])
127 |> validate_required([:data])
128 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
129 end
130
131 def get_by_ap_id_with_object(ap_id) do
132 Repo.one(
133 from(
134 activity in Activity,
135 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
136 left_join: o in Object,
137 on:
138 fragment(
139 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
140 o.data,
141 activity.data,
142 activity.data
143 ),
144 preload: [object: o]
145 )
146 )
147 end
148
149 def get_by_id(id) do
150 Activity
151 |> where([a], a.id == ^id)
152 |> restrict_deactivated_users()
153 |> Repo.one()
154 end
155
156 def get_by_id_with_object(id) do
157 from(activity in Activity,
158 where: activity.id == ^id,
159 inner_join: o in Object,
160 on:
161 fragment(
162 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
163 o.data,
164 activity.data,
165 activity.data
166 ),
167 preload: [object: o]
168 )
169 |> Repo.one()
170 end
171
172 def by_object_ap_id(ap_id) do
173 from(
174 activity in Activity,
175 where:
176 fragment(
177 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
178 activity.data,
179 activity.data,
180 ^to_string(ap_id)
181 )
182 )
183 end
184
185 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
186 from(
187 activity in Activity,
188 where:
189 fragment(
190 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
191 activity.data,
192 activity.data,
193 ^ap_ids
194 ),
195 where: fragment("(?)->>'type' = 'Create'", activity.data)
196 )
197 end
198
199 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
200 from(
201 activity in Activity,
202 where:
203 fragment(
204 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
205 activity.data,
206 activity.data,
207 ^to_string(ap_id)
208 ),
209 where: fragment("(?)->>'type' = 'Create'", activity.data)
210 )
211 end
212
213 def create_by_object_ap_id(_), do: nil
214
215 def get_all_create_by_object_ap_id(ap_id) do
216 Repo.all(create_by_object_ap_id(ap_id))
217 end
218
219 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
220 create_by_object_ap_id(ap_id)
221 |> restrict_deactivated_users()
222 |> Repo.one()
223 end
224
225 def get_create_by_object_ap_id(_), do: nil
226
227 def create_by_object_ap_id_with_object(ap_ids) when is_list(ap_ids) do
228 from(
229 activity in Activity,
230 where:
231 fragment(
232 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
233 activity.data,
234 activity.data,
235 ^ap_ids
236 ),
237 where: fragment("(?)->>'type' = 'Create'", activity.data),
238 inner_join: o in Object,
239 on:
240 fragment(
241 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
242 o.data,
243 activity.data,
244 activity.data
245 ),
246 preload: [object: o]
247 )
248 end
249
250 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
251 from(
252 activity in Activity,
253 where:
254 fragment(
255 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
256 activity.data,
257 activity.data,
258 ^to_string(ap_id)
259 ),
260 where: fragment("(?)->>'type' = 'Create'", activity.data),
261 inner_join: o in Object,
262 on:
263 fragment(
264 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
265 o.data,
266 activity.data,
267 activity.data
268 ),
269 preload: [object: o]
270 )
271 end
272
273 def create_by_object_ap_id_with_object(_), do: nil
274
275 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
276 ap_id
277 |> create_by_object_ap_id_with_object()
278 |> Repo.one()
279 end
280
281 def get_create_by_object_ap_id_with_object(_), do: nil
282
283 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
284 get_create_by_object_ap_id_with_object(ap_id)
285 end
286
287 defp get_in_reply_to_activity_from_object(_), do: nil
288
289 def get_in_reply_to_activity(%Activity{} = activity) do
290 get_in_reply_to_activity_from_object(Object.normalize(activity))
291 end
292
293 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
294 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
295 def normalize(_), do: nil
296
297 def delete_by_ap_id(id) when is_binary(id) do
298 by_object_ap_id(id)
299 |> select([u], u)
300 |> Repo.delete_all()
301 |> elem(1)
302 |> Enum.find(fn
303 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
304 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
305 _ -> nil
306 end)
307 end
308
309 def delete_by_ap_id(_), do: nil
310
311 for {ap_type, type} <- @mastodon_notification_types do
312 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
313 do: unquote(type)
314 end
315
316 def mastodon_notification_type(%Activity{}), do: nil
317
318 def from_mastodon_notification_type(type) do
319 Map.get(@mastodon_to_ap_notification_types, type)
320 end
321
322 def all_by_actor_and_id(actor, status_ids \\ [])
323 def all_by_actor_and_id(_actor, []), do: []
324
325 def all_by_actor_and_id(actor, status_ids) do
326 Activity
327 |> where([s], s.id in ^status_ids)
328 |> where([s], s.actor == ^actor)
329 |> Repo.all()
330 end
331
332 def follow_requests_for_actor(%Pleroma.User{ap_id: ap_id}) do
333 from(
334 a in Activity,
335 where:
336 fragment(
337 "? ->> 'type' = 'Follow'",
338 a.data
339 ),
340 where:
341 fragment(
342 "? ->> 'state' = 'pending'",
343 a.data
344 ),
345 where:
346 fragment(
347 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
348 a.data,
349 a.data,
350 ^ap_id
351 )
352 )
353 end
354
355 @spec query_by_actor(actor()) :: Ecto.Query.t()
356 def query_by_actor(actor) do
357 from(a in Activity, where: a.actor == ^actor)
358 end
359
360 def restrict_deactivated_users(query) do
361 from(activity in query,
362 where:
363 fragment(
364 "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
365 activity.actor
366 )
367 )
368 end
369
370 defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search
371 end