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