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