user: use preloads when deleting accounts
[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})
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'::text) ->> 'id'::text)",
45 # o.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'::text) ->> 'id'::text)",
65 o.data,
66 activity.data
67 )
68 )
69 |> preload([activity, object], object: object)
70 end
71
72 def get_by_ap_id(ap_id) do
73 Repo.one(
74 from(
75 activity in Activity,
76 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
77 )
78 )
79 end
80
81 def get_by_ap_id_with_object(ap_id) do
82 Repo.one(
83 from(
84 activity in Activity,
85 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
86 inner_join: o in Object,
87 on:
88 fragment(
89 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
90 o.data,
91 activity.data
92 ),
93 preload: [object: o]
94 )
95 )
96 end
97
98 def get_by_id(id) do
99 Repo.get(Activity, id)
100 end
101
102 def get_by_id_with_object(id) do
103 from(activity in Activity,
104 where: activity.id == ^id,
105 inner_join: o in Object,
106 on:
107 fragment(
108 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
109 o.data,
110 activity.data
111 ),
112 preload: [object: o]
113 )
114 |> Repo.one()
115 end
116
117 def by_object_ap_id(ap_id) do
118 from(
119 activity in Activity,
120 where:
121 fragment(
122 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
123 activity.data,
124 activity.data,
125 ^to_string(ap_id)
126 )
127 )
128 end
129
130 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
131 from(
132 activity in Activity,
133 where:
134 fragment(
135 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
136 activity.data,
137 activity.data,
138 ^ap_ids
139 ),
140 where: fragment("(?)->>'type' = 'Create'", activity.data)
141 )
142 end
143
144 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
145 from(
146 activity in Activity,
147 where:
148 fragment(
149 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
150 activity.data,
151 activity.data,
152 ^to_string(ap_id)
153 ),
154 where: fragment("(?)->>'type' = 'Create'", activity.data)
155 )
156 end
157
158 def create_by_object_ap_id(_), do: nil
159
160 def get_all_create_by_object_ap_id(ap_id) do
161 Repo.all(create_by_object_ap_id(ap_id))
162 end
163
164 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
165 create_by_object_ap_id(ap_id)
166 |> Repo.one()
167 end
168
169 def get_create_by_object_ap_id(_), do: nil
170
171 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
172 from(
173 activity in Activity,
174 where:
175 fragment(
176 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
177 activity.data,
178 activity.data,
179 ^to_string(ap_id)
180 ),
181 where: fragment("(?)->>'type' = 'Create'", activity.data),
182 inner_join: o in Object,
183 on:
184 fragment(
185 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
186 o.data,
187 activity.data
188 ),
189 preload: [object: o]
190 )
191 end
192
193 def create_by_object_ap_id_with_object(_), do: nil
194
195 def get_create_by_object_ap_id_with_object(ap_id) do
196 ap_id
197 |> create_by_object_ap_id_with_object()
198 |> Repo.one()
199 end
200
201 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
202 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
203 def normalize(_), do: nil
204
205 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
206 get_create_by_object_ap_id(ap_id)
207 end
208
209 def get_in_reply_to_activity(_), do: nil
210
211 def delete_by_ap_id(id) when is_binary(id) do
212 by_object_ap_id(id)
213 |> select([u], u)
214 |> Repo.delete_all()
215 |> elem(1)
216 |> Enum.find(fn
217 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
218 _ -> nil
219 end)
220 end
221
222 def delete_by_ap_id(_), do: nil
223
224 for {ap_type, type} <- @mastodon_notification_types do
225 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
226 do: unquote(type)
227 end
228
229 def mastodon_notification_type(%Activity{}), do: nil
230
231 def from_mastodon_notification_type(type) do
232 Map.get(@mastodon_to_ap_notification_types, type)
233 end
234
235 def all_by_actor_and_id(actor, status_ids \\ [])
236 def all_by_actor_and_id(_actor, []), do: []
237
238 def all_by_actor_and_id(actor, status_ids) do
239 Activity
240 |> where([s], s.id in ^status_ids)
241 |> where([s], s.actor == ^actor)
242 |> Repo.all()
243 end
244 end