Merge branch 'fix-recipients-from-activity' into 'develop'
[akkoma] / lib / pleroma / activity / queries.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.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_ap_id(query, String.t()) :: query
17 def by_ap_id(query \\ Activity, ap_id) do
18 from(
19 activity in query,
20 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
21 )
22 end
23
24 @spec by_actor(query, String.t()) :: query
25 def by_actor(query \\ Activity, actor) do
26 from(
27 activity in query,
28 where: fragment("(?)->>'actor' = ?", activity.data, ^actor)
29 )
30 end
31
32 @spec by_object_id(query, String.t() | [String.t()]) :: query
33 def by_object_id(query \\ Activity, object_id)
34
35 def by_object_id(query, object_ids) when is_list(object_ids) do
36 from(
37 activity in query,
38 where:
39 fragment(
40 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
41 activity.data,
42 activity.data,
43 ^object_ids
44 )
45 )
46 end
47
48 def by_object_id(query, object_id) when is_binary(object_id) do
49 from(activity in query,
50 where:
51 fragment(
52 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
53 activity.data,
54 activity.data,
55 ^object_id
56 )
57 )
58 end
59
60 @spec by_type(query, String.t()) :: query
61 def by_type(query \\ Activity, activity_type) do
62 from(
63 activity in query,
64 where: fragment("(?)->>'type' = ?", activity.data, ^activity_type)
65 )
66 end
67 end