Use with_preloaded_bookmark in create_by_object_ap_id_with_object
[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 |> with_preloaded_bookmarks()
77 end
78
79 def with_preloaded_bookmarks(query) do
80 query
81 |> preload(:bookmarks)
82 end
83
84 def get_by_ap_id(ap_id) do
85 Repo.one(
86 from(
87 activity in Activity,
88 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
89 )
90 )
91 end
92
93 def change(struct, params \\ %{}) do
94 struct
95 |> cast(params, [:data])
96 |> validate_required([:data])
97 |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
98 end
99
100 def get_by_ap_id_with_object(ap_id) do
101 Repo.one(
102 from(
103 activity in Activity,
104 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)),
105 left_join: o in Object,
106 on:
107 fragment(
108 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
109 o.data,
110 activity.data,
111 activity.data
112 ),
113 preload: [object: o]
114 )
115 |> with_preloaded_bookmarks()
116 )
117 end
118
119 def get_by_id(id) do
120 Repo.get(Activity, id)
121 end
122
123 def get_by_id_with_object(id) do
124 from(activity in Activity,
125 where: activity.id == ^id,
126 inner_join: o in Object,
127 on:
128 fragment(
129 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
130 o.data,
131 activity.data,
132 activity.data
133 ),
134 preload: [object: o]
135 )
136 |> with_preloaded_bookmarks()
137 |> Repo.one()
138 end
139
140 def by_object_ap_id(ap_id) do
141 from(
142 activity in Activity,
143 where:
144 fragment(
145 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
146 activity.data,
147 activity.data,
148 ^to_string(ap_id)
149 )
150 )
151 end
152
153 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
154 from(
155 activity in Activity,
156 where:
157 fragment(
158 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
159 activity.data,
160 activity.data,
161 ^ap_ids
162 ),
163 where: fragment("(?)->>'type' = 'Create'", activity.data)
164 )
165 end
166
167 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
168 from(
169 activity in Activity,
170 where:
171 fragment(
172 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
173 activity.data,
174 activity.data,
175 ^to_string(ap_id)
176 ),
177 where: fragment("(?)->>'type' = 'Create'", activity.data)
178 )
179 end
180
181 def create_by_object_ap_id(_), do: nil
182
183 def get_all_create_by_object_ap_id(ap_id) do
184 Repo.all(create_by_object_ap_id(ap_id))
185 end
186
187 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
188 create_by_object_ap_id(ap_id)
189 |> Repo.one()
190 end
191
192 def get_create_by_object_ap_id(_), do: nil
193
194 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
195 from(
196 activity in Activity,
197 where:
198 fragment(
199 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
200 activity.data,
201 activity.data,
202 ^to_string(ap_id)
203 ),
204 where: fragment("(?)->>'type' = 'Create'", activity.data),
205 inner_join: o in Object,
206 on:
207 fragment(
208 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
209 o.data,
210 activity.data,
211 activity.data
212 ),
213 preload: [object: o]
214 )
215 |> with_preloaded_bookmarks()
216 end
217
218 def create_by_object_ap_id_with_object(_), do: nil
219
220 def get_create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
221 ap_id
222 |> create_by_object_ap_id_with_object()
223 |> Repo.one()
224 end
225
226 def get_create_by_object_ap_id_with_object(_), do: nil
227
228 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
229 get_create_by_object_ap_id_with_object(ap_id)
230 end
231
232 defp get_in_reply_to_activity_from_object(_), do: nil
233
234 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
235 get_in_reply_to_activity_from_object(Object.normalize(object))
236 end
237
238 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
239 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
240 def normalize(_), do: nil
241
242 def delete_by_ap_id(id) when is_binary(id) do
243 by_object_ap_id(id)
244 |> select([u], u)
245 |> Repo.delete_all()
246 |> elem(1)
247 |> Enum.find(fn
248 %{data: %{"type" => "Create", "object" => ap_id}} when is_binary(ap_id) -> ap_id == id
249 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
250 _ -> nil
251 end)
252 end
253
254 def delete_by_ap_id(_), do: nil
255
256 for {ap_type, type} <- @mastodon_notification_types do
257 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
258 do: unquote(type)
259 end
260
261 def mastodon_notification_type(%Activity{}), do: nil
262
263 def from_mastodon_notification_type(type) do
264 Map.get(@mastodon_to_ap_notification_types, type)
265 end
266
267 def all_by_actor_and_id(actor, status_ids \\ [])
268 def all_by_actor_and_id(_actor, []), do: []
269
270 def all_by_actor_and_id(actor, status_ids) do
271 Activity
272 |> where([s], s.id in ^status_ids)
273 |> where([s], s.actor == ^actor)
274 |> Repo.all()
275 end
276
277 @spec query_by_actor(actor()) :: Ecto.Query.t()
278 def query_by_actor(actor) do
279 from(a in Activity, where: a.actor == ^actor)
280 end
281 end