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