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