Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / user / query.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.AdminAPI.Search, 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 User.restrict_deactivated(query)
141 |> where([u], u.approval_pending == false)
142 end
143
144 defp compose_query({:legacy_active, _}, query) do
145 query
146 |> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
147 end
148
149 defp compose_query({:deactivated, false}, query) do
150 User.restrict_deactivated(query)
151 end
152
153 defp compose_query({:deactivated, true}, query) do
154 where(query, [u], u.deactivated == ^true)
155 end
156
157 defp compose_query({:confirmation_pending, bool}, query) do
158 where(query, [u], u.confirmation_pending == ^bool)
159 end
160
161 defp compose_query({:need_approval, _}, query) do
162 where(query, [u], u.approval_pending)
163 end
164
165 defp compose_query({:unconfirmed, _}, query) do
166 where(query, [u], u.confirmation_pending)
167 end
168
169 defp compose_query({:followers, %User{id: id}}, query) do
170 query
171 |> where([u], u.id != ^id)
172 |> join(:inner, [u], r in FollowingRelationship,
173 as: :relationships,
174 on: r.following_id == ^id and r.follower_id == u.id
175 )
176 |> where([relationships: r], r.state == ^:follow_accept)
177 end
178
179 defp compose_query({:friends, %User{id: id}}, query) do
180 query
181 |> where([u], u.id != ^id)
182 |> join(:inner, [u], r in FollowingRelationship,
183 as: :relationships,
184 on: r.following_id == u.id and r.follower_id == ^id
185 )
186 |> where([relationships: r], r.state == ^:follow_accept)
187 end
188
189 defp compose_query({:recipients_from_activity, to}, query) do
190 following_query =
191 from(u in User,
192 join: f in FollowingRelationship,
193 on: u.id == f.following_id,
194 where: f.state == ^:follow_accept,
195 where: u.follower_address in ^to,
196 select: f.follower_id
197 )
198
199 from(u in query,
200 where: u.ap_id in ^to or u.id in subquery(following_query)
201 )
202 end
203
204 defp compose_query({:order_by, key}, query) do
205 order_by(query, [u], field(u, ^key))
206 end
207
208 defp compose_query({:select, keys}, query) do
209 select(query, [u], ^keys)
210 end
211
212 defp compose_query({:limit, limit}, query) do
213 limit(query, ^limit)
214 end
215
216 defp compose_query({:internal, false}, query) do
217 query
218 |> where([u], not is_nil(u.nickname))
219 |> where([u], not like(u.nickname, "internal.%"))
220 end
221
222 defp compose_query(_unsupported_param, query), do: query
223
224 defp location_query(query, local) do
225 where(query, [u], u.local == ^local)
226 end
227 end