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