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