Remove updating reply count for embeded objects
[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 Repo.get(Activity, id)
110 end
111
112 def get_by_id_with_object(id) do
113 from(activity in Activity,
114 where: activity.id == ^id,
115 inner_join: o in Object,
116 on:
117 fragment(
118 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
119 o.data,
120 activity.data,
121 activity.data
122 ),
123 preload: [object: o]
124 )
125 |> Repo.one()
126 end
127
128 def by_object_ap_id(ap_id) do
129 from(
130 activity in Activity,
131 where:
132 fragment(
133 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
134 activity.data,
135 activity.data,
136 ^to_string(ap_id)
137 )
138 )
139 end
140
141 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
142 from(
143 activity in Activity,
144 where:
145 fragment(
146 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
147 activity.data,
148 activity.data,
149 ^ap_ids
150 ),
151 where: fragment("(?)->>'type' = 'Create'", activity.data)
152 )
153 end
154
155 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
156 from(
157 activity in Activity,
158 where:
159 fragment(
160 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
161 activity.data,
162 activity.data,
163 ^to_string(ap_id)
164 ),
165 where: fragment("(?)->>'type' = 'Create'", activity.data)
166 )
167 end
168
169 def create_by_object_ap_id(_), do: nil
170
171 def get_all_create_by_object_ap_id(ap_id) do
172 Repo.all(create_by_object_ap_id(ap_id))
173 end
174
175 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
176 create_by_object_ap_id(ap_id)
177 |> Repo.one()
178 end
179
180 def get_create_by_object_ap_id(_), do: nil
181
182 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
183 from(
184 activity in Activity,
185 where:
186 fragment(
187 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
188 activity.data,
189 activity.data,
190 ^to_string(ap_id)
191 ),
192 where: fragment("(?)->>'type' = 'Create'", activity.data),
193 inner_join: o in Object,
194 on:
195 fragment(
196 "(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')",
197 o.data,
198 activity.data,
199 activity.data
200 ),
201 preload: [object: o]
202 )
203 end
204
205 def create_by_object_ap_id_with_object(_), do: nil
206
207 def get_create_by_object_ap_id_with_object(ap_id) do
208 ap_id
209 |> create_by_object_ap_id_with_object()
210 |> Repo.one()
211 end
212
213 defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
214 get_create_by_object_ap_id_with_object(ap_id)
215 end
216
217 defp get_in_reply_to_activity_from_object(_), do: nil
218
219 def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
220 get_in_reply_to_activity_from_object(Object.normalize(object))
221 end
222
223 def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
224 def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
225 def normalize(_), do: nil
226
227 def delete_by_ap_id(id) when is_binary(id) do
228 by_object_ap_id(id)
229 |> select([u], u)
230 |> Repo.delete_all()
231 |> elem(1)
232 |> Enum.find(fn
233 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
234 _ -> nil
235 end)
236 end
237
238 def delete_by_ap_id(_), do: nil
239
240 for {ap_type, type} <- @mastodon_notification_types do
241 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
242 do: unquote(type)
243 end
244
245 def mastodon_notification_type(%Activity{}), do: nil
246
247 def from_mastodon_notification_type(type) do
248 Map.get(@mastodon_to_ap_notification_types, type)
249 end
250
251 def all_by_actor_and_id(actor, status_ids \\ [])
252 def all_by_actor_and_id(_actor, []), do: []
253
254 def all_by_actor_and_id(actor, status_ids) do
255 Activity
256 |> where([s], s.id in ^status_ids)
257 |> where([s], s.actor == ^actor)
258 |> Repo.all()
259 end
260 end