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