Merge branch 'poll-notification' into 'develop'
[akkoma] / lib / pleroma / user / query.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.User.Query do
6 @moduledoc """
7 User query builder module. Builds query from new query or another user query.
8
9 ## Example:
10 query = Pleroma.User.Query.build(%{nickname: "nickname"})
11 another_query = Pleroma.User.Query.build(query, %{email: "email@example.com"})
12 Pleroma.Repo.all(query)
13 Pleroma.Repo.all(another_query)
14
15 Adding new rules:
16 - *ilike criteria*
17 - add field to @ilike_criteria list
18 - pass non empty string
19 - e.g. Pleroma.User.Query.build(%{nickname: "nickname"})
20 - *equal criteria*
21 - add field to @equal_criteria list
22 - pass non empty string
23 - e.g. Pleroma.User.Query.build(%{email: "email@example.com"})
24 - *contains criteria*
25 - add field to @containns_criteria list
26 - pass values list
27 - e.g. Pleroma.User.Query.build(%{ap_id: ["http://ap_id1", "http://ap_id2"]})
28 """
29 import Ecto.Query
30 import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
31
32 alias Pleroma.FollowingRelationship
33 alias Pleroma.User
34
35 @type criteria ::
36 %{
37 query: String.t(),
38 tags: [String.t()],
39 name: String.t(),
40 email: String.t(),
41 local: boolean(),
42 external: boolean(),
43 active: boolean(),
44 deactivated: boolean(),
45 need_approval: boolean(),
46 unconfirmed: boolean(),
47 is_admin: boolean(),
48 is_moderator: boolean(),
49 super_users: boolean(),
50 invisible: boolean(),
51 internal: boolean(),
52 followers: User.t(),
53 friends: User.t(),
54 recipients_from_activity: [String.t()],
55 nickname: [String.t()] | String.t(),
56 ap_id: [String.t()],
57 order_by: term(),
58 select: term(),
59 limit: pos_integer(),
60 actor_types: [String.t()]
61 }
62 | map()
63
64 @ilike_criteria [:nickname, :name, :query]
65 @equal_criteria [:email]
66 @contains_criteria [:ap_id, :nickname]
67
68 @spec build(Query.t(), criteria()) :: Query.t()
69 def build(query \\ base_query(), criteria) do
70 prepare_query(query, criteria)
71 end
72
73 @spec paginate(Ecto.Query.t(), pos_integer(), pos_integer()) :: Ecto.Query.t()
74 def paginate(query, page, page_size) do
75 from(u in query,
76 limit: ^page_size,
77 offset: ^((page - 1) * page_size)
78 )
79 end
80
81 defp base_query do
82 from(u in User)
83 end
84
85 defp prepare_query(query, criteria) do
86 criteria
87 |> Map.put_new(:internal, false)
88 |> Enum.reduce(query, &compose_query/2)
89 end
90
91 defp compose_query({key, value}, query)
92 when key in @ilike_criteria and not_empty_string(value) do
93 # hack for :query key
94 key = if key == :query, do: :nickname, else: key
95 where(query, [u], ilike(field(u, ^key), ^"%#{value}%"))
96 end
97
98 defp compose_query({:invisible, bool}, query) when is_boolean(bool) do
99 where(query, [u], u.invisible == ^bool)
100 end
101
102 defp compose_query({key, value}, query)
103 when key in @equal_criteria and not_empty_string(value) do
104 where(query, [u], ^[{key, value}])
105 end
106
107 defp compose_query({key, values}, query) when key in @contains_criteria and is_list(values) do
108 where(query, [u], field(u, ^key) in ^values)
109 end
110
111 defp compose_query({:tags, tags}, query) when is_list(tags) and length(tags) > 0 do
112 where(query, [u], fragment("? && ?", u.tags, ^tags))
113 end
114
115 defp compose_query({:is_admin, bool}, query) do
116 where(query, [u], u.is_admin == ^bool)
117 end
118
119 defp compose_query({:actor_types, actor_types}, query) when is_list(actor_types) do
120 where(query, [u], u.actor_type in ^actor_types)
121 end
122
123 defp compose_query({:is_moderator, bool}, query) do
124 where(query, [u], u.is_moderator == ^bool)
125 end
126
127 defp compose_query({:super_users, _}, query) do
128 where(
129 query,
130 [u],
131 u.is_admin or u.is_moderator
132 )
133 end
134
135 defp compose_query({:local, _}, query), do: location_query(query, true)
136
137 defp compose_query({:external, _}, query), do: location_query(query, false)
138
139 defp compose_query({:active, _}, query) do
140 where(query, [u], u.is_active == true)
141 |> where([u], u.is_approved == true)
142 |> where([u], u.is_confirmed == true)
143 end
144
145 defp compose_query({:legacy_active, _}, query) do
146 query
147 |> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
148 end
149
150 defp compose_query({:deactivated, false}, query) do
151 where(query, [u], u.is_active == true)
152 end
153
154 defp compose_query({:deactivated, true}, query) do
155 where(query, [u], u.is_active == false)
156 end
157
158 defp compose_query({:confirmation_pending, bool}, query) do
159 where(query, [u], u.is_confirmed != ^bool)
160 end
161
162 defp compose_query({:need_approval, _}, query) do
163 where(query, [u], u.is_approved == false)
164 end
165
166 defp compose_query({:unconfirmed, _}, query) do
167 where(query, [u], u.is_confirmed == false)
168 end
169
170 defp compose_query({:followers, %User{id: id}}, query) do
171 query
172 |> where([u], u.id != ^id)
173 |> join(:inner, [u], r in FollowingRelationship,
174 as: :relationships,
175 on: r.following_id == ^id and r.follower_id == u.id
176 )
177 |> where([relationships: r], r.state == ^:follow_accept)
178 end
179
180 defp compose_query({:friends, %User{id: id}}, query) do
181 query
182 |> where([u], u.id != ^id)
183 |> join(:inner, [u], r in FollowingRelationship,
184 as: :relationships,
185 on: r.following_id == u.id and r.follower_id == ^id
186 )
187 |> where([relationships: r], r.state == ^:follow_accept)
188 end
189
190 defp compose_query({:recipients_from_activity, to}, query) do
191 following_query =
192 from(u in User,
193 join: f in FollowingRelationship,
194 on: u.id == f.following_id,
195 where: f.state == ^:follow_accept,
196 where: u.follower_address in ^to,
197 select: f.follower_id
198 )
199
200 from(u in query,
201 where: u.ap_id in ^to or u.id in subquery(following_query)
202 )
203 end
204
205 defp compose_query({:order_by, key}, query) do
206 order_by(query, [u], field(u, ^key))
207 end
208
209 defp compose_query({:select, keys}, query) do
210 select(query, [u], ^keys)
211 end
212
213 defp compose_query({:limit, limit}, query) do
214 limit(query, ^limit)
215 end
216
217 defp compose_query({:internal, false}, query) do
218 query
219 |> where([u], not is_nil(u.nickname))
220 |> where([u], not like(u.nickname, "internal.%"))
221 end
222
223 defp compose_query(_unsupported_param, query), do: query
224
225 defp location_query(query, local) do
226 where(query, [u], u.local == ^local)
227 end
228 end