[#1505] Fixed @spec for Queries.by_object_in_reply_to_id/3
[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, where: 3]
11
12 @type query :: Ecto.Queryable.t() | Activity.t()
13
14 alias Pleroma.Activity
15 alias Pleroma.User
16
17 @spec by_ap_id(query, String.t()) :: query
18 def by_ap_id(query \\ Activity, ap_id) do
19 from(
20 activity in query,
21 where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))
22 )
23 end
24
25 @spec by_actor(query, String.t()) :: query
26 def by_actor(query \\ Activity, actor) do
27 from(
28 activity in query,
29 where: fragment("(?)->>'actor' = ?", activity.data, ^actor)
30 )
31 end
32
33 @spec by_author(query, String.t()) :: query
34 def by_author(query \\ Activity, %User{ap_id: ap_id}) do
35 from(a in query, where: a.actor == ^ap_id)
36 end
37
38 @spec by_object_id(query, String.t() | [String.t()]) :: query
39 def by_object_id(query \\ Activity, object_id)
40
41 def by_object_id(query, object_ids) when is_list(object_ids) do
42 from(
43 activity in query,
44 where:
45 fragment(
46 "coalesce((?)->'object'->>'id', (?)->>'object') = ANY(?)",
47 activity.data,
48 activity.data,
49 ^object_ids
50 )
51 )
52 end
53
54 def by_object_id(query, object_id) when is_binary(object_id) do
55 from(activity in query,
56 where:
57 fragment(
58 "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
59 activity.data,
60 activity.data,
61 ^object_id
62 )
63 )
64 end
65
66 @spec by_object_in_reply_to_id(query, String.t(), keyword()) :: query
67 def by_object_in_reply_to_id(query, in_reply_to_id, opts \\ []) do
68 query =
69 if opts[:skip_preloading] do
70 Activity.with_joined_object(query)
71 else
72 Activity.with_preloaded_object(query)
73 end
74
75 where(
76 query,
77 [activity, object: o],
78 fragment("(?)->>'inReplyTo' = ?", o.data, ^to_string(in_reply_to_id))
79 )
80 end
81
82 @spec by_type(query, String.t()) :: query
83 def by_type(query \\ Activity, activity_type) do
84 from(
85 activity in query,
86 where: fragment("(?)->>'type' = ?", activity.data, ^activity_type)
87 )
88 end
89
90 @spec exclude_type(query, String.t()) :: query
91 def exclude_type(query \\ Activity, activity_type) do
92 from(
93 activity in query,
94 where: fragment("(?)->>'type' != ?", activity.data, ^activity_type)
95 )
96 end
97
98 def exclude_authors(query \\ Activity, actors) do
99 from(activity in query, where: activity.actor not in ^actors)
100 end
101 end