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