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