activity: add with_preloaded_object() convenience
[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})
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 # fragment("(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)", o.data, activity.data))
45 # |> preload([activity, object], [object: object])
46 # ```
47 #
48 # As a convenience, Activity.with_preloaded_object() sets up an inner join and preload for the
49 # typical case.
50 has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
51
52 timestamps()
53 end
54
55 def with_preloaded_object(query) do
56 query
57 |> join(
58 :inner,
59 [activity],
60 o in Object,
61 fragment(
62 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
63 o.data,
64 activity.data
65 )
66 )
67 |> preload([activity, object], object: object)
68 end
69
70 def get_by_ap_id(ap_id) do
71 Repo.one(
72 from(
73 activity in Activity,
74 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
75 )
76 )
77 end
78
79 def get_by_id(id) do
80 Repo.get(Activity, id)
81 end
82
83 def get_by_id_with_object(id) do
84 from(activity in Activity,
85 where: activity.id == ^id,
86 inner_join: o in Object,
87 on:
88 fragment(
89 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
90 o.data,
91 activity.data
92 ),
93 preload: [object: o]
94 )
95 |> Repo.one()
96 end
97
98 def by_object_ap_id(ap_id) do
99 from(
100 activity in Activity,
101 where:
102 fragment(
103 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
104 activity.data,
105 activity.data,
106 ^to_string(ap_id)
107 )
108 )
109 end
110
111 def create_by_object_ap_id(ap_ids) when is_list(ap_ids) do
112 from(
113 activity in Activity,
114 where:
115 fragment(
116 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
117 activity.data,
118 activity.data,
119 ^ap_ids
120 ),
121 where: fragment("(?)->>'type' = 'Create'", activity.data)
122 )
123 end
124
125 def create_by_object_ap_id(ap_id) when is_binary(ap_id) do
126 from(
127 activity in Activity,
128 where:
129 fragment(
130 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
131 activity.data,
132 activity.data,
133 ^to_string(ap_id)
134 ),
135 where: fragment("(?)->>'type' = 'Create'", activity.data)
136 )
137 end
138
139 def create_by_object_ap_id(_), do: nil
140
141 def get_all_create_by_object_ap_id(ap_id) do
142 Repo.all(create_by_object_ap_id(ap_id))
143 end
144
145 def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
146 create_by_object_ap_id(ap_id)
147 |> Repo.one()
148 end
149
150 def get_create_by_object_ap_id(_), do: nil
151
152 def create_by_object_ap_id_with_object(ap_id) when is_binary(ap_id) do
153 from(
154 activity in Activity,
155 where:
156 fragment(
157 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
158 activity.data,
159 activity.data,
160 ^to_string(ap_id)
161 ),
162 where: fragment("(?)->>'type' = 'Create'", activity.data),
163 inner_join: o in Object,
164 on:
165 fragment(
166 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
167 o.data,
168 activity.data
169 ),
170 preload: [object: o]
171 )
172 end
173
174 def create_by_object_ap_id_with_object(_), do: nil
175
176 def get_create_by_object_ap_id_with_object(ap_id) do
177 ap_id
178 |> create_by_object_ap_id_with_object()
179 |> Repo.one()
180 end
181
182 def normalize(obj) when is_map(obj), do: Activity.get_by_ap_id(obj["id"])
183 def normalize(ap_id) when is_binary(ap_id), do: Activity.get_by_ap_id(ap_id)
184 def normalize(_), do: nil
185
186 def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
187 get_create_by_object_ap_id(ap_id)
188 end
189
190 def get_in_reply_to_activity(_), do: nil
191
192 def delete_by_ap_id(id) when is_binary(id) do
193 by_object_ap_id(id)
194 |> select([u], u)
195 |> Repo.delete_all()
196 |> elem(1)
197 |> Enum.find(fn
198 %{data: %{"type" => "Create", "object" => %{"id" => ap_id}}} -> ap_id == id
199 _ -> nil
200 end)
201 end
202
203 def delete_by_ap_id(_), do: nil
204
205 for {ap_type, type} <- @mastodon_notification_types do
206 def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}),
207 do: unquote(type)
208 end
209
210 def mastodon_notification_type(%Activity{}), do: nil
211
212 def from_mastodon_notification_type(type) do
213 Map.get(@mastodon_to_ap_notification_types, type)
214 end
215
216 def all_by_actor_and_id(actor, status_ids \\ [])
217 def all_by_actor_and_id(_actor, []), do: []
218
219 def all_by_actor_and_id(actor, status_ids) do
220 Activity
221 |> where([s], s.id in ^status_ids)
222 |> where([s], s.actor == ^actor)
223 |> Repo.all()
224 end
225 end