Reduce OP fetching 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 field :actor, :string
10 field :recipients, {:array, :string}
11 has_many :notifications, Notification, on_delete: :delete_all
12
13 timestamps()
14 end
15
16 def get_by_ap_id(ap_id) do
17 Repo.one(from activity in Activity,
18 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id)))
19 end
20
21 # TODO:
22 # Go through these and fix them everywhere.
23 # Wrong name, only returns create activities
24 def all_by_object_ap_id_q(ap_id) do
25 from activity in Activity,
26 where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^to_string(ap_id)),
27 where: fragment("(?)->>'type' = 'Create'", activity.data)
28 end
29
30 # Wrong name, returns all.
31 def all_non_create_by_object_ap_id_q(ap_id) do
32 from activity in Activity,
33 where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^to_string(ap_id))
34 end
35
36 # Wrong name plz fix thx
37 def all_by_object_ap_id(ap_id) do
38 Repo.all(all_by_object_ap_id_q(ap_id))
39 end
40
41 def create_activity_by_object_id_query(ap_ids) do
42 from activity in Activity,
43 where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)", activity.data, activity.data, ^ap_ids),
44 where: fragment("(?)->>'type' = 'Create'", activity.data)
45 end
46
47 def get_create_activity_by_object_ap_id(ap_id) do
48 create_activity_by_object_id_query([ap_id])
49 |> Repo.one
50 end
51 end