Speed up deletion and related queries.
[akkoma] / lib / pleroma / activity.ex
1 defmodule Pleroma.Activity do
2 use Ecto.Schema
3 alias Pleroma.{Repo, Activity, Notification}
4 import Ecto.Query
5
6 schema "activities" do
7 field :data, :map
8 field :local, :boolean, default: true
9 has_many :notifications, Notification
10
11 timestamps()
12 end
13
14 def get_by_ap_id(ap_id) do
15 Repo.one(from activity in Activity,
16 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)))
17 end
18
19 # TODO:
20 # Go through these and fix them everywhere.
21 # Wrong name, only returns create activities
22 def all_by_object_ap_id_q(ap_id) do
23 from activity in Activity,
24 where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^to_string(ap_id)),
25 where: fragment("(?)->>'type' = 'Create'", activity.data)
26 end
27
28 # Wrong name, returns all.
29 def all_non_create_by_object_ap_id_q(ap_id) do
30 from activity in Activity,
31 where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^to_string(ap_id))
32 end
33
34 # Wrong name plz fix thx
35 def all_by_object_ap_id(ap_id) do
36 Repo.all(all_by_object_ap_id_q(ap_id))
37 end
38
39 def get_create_activity_by_object_ap_id(ap_id) do
40 Repo.one(from activity in Activity,
41 where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id))
42 and fragment("(?)->>'type' = 'Create'", activity.data))
43 end
44 end