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