Merge branch 'feature/788-separate-email-addresses' 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.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 Repo.get(Activity, id)
102 end
103
104 def get_by_id_with_object(id) do
105 from(activity in Activity,
106 where: activity.id == ^id,
107 inner_join: o in Object,
108 on:
109 fragment(
110 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
111 o.data,
112 activity.data,
113 activity.data
114 ),
115 preload: [object: o]
116 )
117 |> Repo.one()
118 end
119
120 def by_object_ap_id(ap_id) do
121 from(
122 activity in Activity,
123 where:
124 fragment(
125 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
126 activity.data,
127 activity.data,
128 ^to_string(ap_id)
129 )
130 )
131 end
132
133 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
134 from(
135 activity in Activity,
136 where:
137 fragment(
138 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
139 activity.data,
140 activity.data,
141 ^ap_ids
142 ),
143 where: fragment("(?)->>'type' = 'Create'", activity.data)
144 )
145 end
146
147 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
148 from(
149 activity in Activity,
150 where:
151 fragment(
152 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
153 activity.data,
154 activity.data,
155 ^to_string(ap_id)
156 ),
157 where: fragment("(?)->>'type' = 'Create'", activity.data)
158 )
159 end
160
161 def create_by_object_ap_id(_), do: nil
162
163 def get_all_create_by_object_ap_id(ap_id) do
164 Repo.all(create_by_object_ap_id(ap_id))
165 end
166
167 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
168 create_by_object_ap_id(ap_id)
169 |> Repo.one()
170 end
171
172 def get_create_by_object_ap_id(_), do: nil
173
174 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
175 from(
176 activity in Activity,
177 where:
178 fragment(
179 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
180 activity.data,
181 activity.data,
182 ^to_string(ap_id)
183 ),
184 where: fragment("(?)->>'type' = 'Create'", activity.data),
185 inner_join: o in Object,
186 on:
187 fragment(
188 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
189 o.data,
190 activity.data,
191 activity.data
192 ),
193 preload: [object: o]
194 )
195 end
196
197 def create_by_object_ap_id_with_object(_), do: nil
198
199 def get_create_by_object_ap_id_with_object(ap_id) do
200 ap_id
201 |> create_by_object_ap_id_with_object()
202 |> Repo.one()
203 end
204
205 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
206 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
207 def normalize(_), do: nil
208
209 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
210 get_create_by_object_ap_id(ap_id)
211 end
212
213 def get_in_reply_to_activity(_), do: nil
214
215 def delete_by_ap_id(id) when is_binary(id) do
216 by_object_ap_id(id)
217 |> select([u], u)
218 |> Repo.delete_all()
219 |> elem(1)
220 |> Enum.find(fn
221 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
222 _ -> nil
223 end)
224 end
225
226 def delete_by_ap_id(_), do: nil
227
228 for {ap_type, type} <- @mastodon_notification_types do
229 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
230 do: unquote(type)
231 end
232
233 def mastodon_notification_type(%Activity{}), do: nil
234
235 def from_mastodon_notification_type(type) do
236 Map.get(@mastodon_to_ap_notification_types, type)
237 end
238
239 def all_by_actor_and_id(actor, status_ids \\ [])
240 def all_by_actor_and_id(_actor, []), do: []
241
242 def all_by_actor_and_id(actor, status_ids) do
243 Activity
244 |> where([s], s.id in ^status_ids)
245 |> where([s], s.actor == ^actor)
246 |> Repo.all()
247 end
248
249 def increase_replies_count(nil), do: nil
250
251 def increase_replies_count(object_ap_id) do
252 from(a in create_by_object_ap_id(object_ap_id),
253 update: [
254 set: [
255 data:
256 fragment(
257 """
258 jsonb_set(?, '{object, repliesCount}',
259 (coalesce((?->'object'->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
260 """,
261 a.data,
262 a.data
263 )
264 ]
265 ]
266 )
267 |> Repo.update_all([])
268 |> case do
269 {1, [activity]} -> activity
270 _ -> {:error, "Not found"}
271 end
272 end
273
274 def decrease_replies_count(nil), do: nil
275
276 def decrease_replies_count(object_ap_id) do
277 from(a in create_by_object_ap_id(object_ap_id),
278 update: [
279 set: [
280 data:
281 fragment(
282 """
283 jsonb_set(?, '{object, repliesCount}',
284 (greatest(0, (?->'object'->>'repliesCount')::int - 1))::varchar::jsonb, true)
285 """,
286 a.data,
287 a.data
288 )
289 ]
290 ]
291 )
292 |> Repo.update_all([])
293 |> case do
294 {1, [activity]} -> activity
295 _ -> {:error, "Not found"}
296 end
297 end
298 end