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