fix formatting
[akkoma] / lib / pleroma / activity / queries.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity.Queries do
6 @moduledoc """
7 Contains queries for Activity.
8 """
9
10 import Ecto.Query, only: [from: 2]
11
12 @type query :: Ecto.Queryable.t() | Activity.t()
13
14 alias Pleroma.Activity
15
16 @spec by_actor(query, String.t()) :: query
17 def by_actor(query \\ Activity, actor) do
18 from(
19 activity in query,
20 where: fragment("(?)->>'actor' = ?", activity.data, ^actor)
21 )
22 end
23
24 @spec by_object_id(query, String.t()) :: query
25 def by_object_id(query \\ Activity, object_id) do
26 from(activity in query,
27 where:
28 fragment(
29 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
30 activity.data,
31 activity.data,
32 ^object_id
33 )
34 )
35 end
36
37 @spec by_type(query, String.t()) :: query
38 def by_type(query \\ Activity, activity_type) do
39 from(
40 activity in query,
41 where: fragment("(?)->>'type' = ?", activity.data, ^activity_type)
42 )
43 end
44
45 @spec limit(query, pos_integer()) :: query
46 def limit(query \\ Activity, limit) do
47 from(activity in query, limit: ^limit)
48 end
49 end