1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.User.Query do
7 User query builder module. Builds query from new query or another user query.
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)
17 - add field to @ilike_criteria list
18 - pass non empty string
19 - e.g. Pleroma.User.Query.build(%{nickname: "nickname"})
21 - add field to @equal_criteria list
22 - pass non empty string
23 - e.g. Pleroma.User.Query.build(%{email: "email@example.com"})
25 - add field to @contains_criteria list
27 - e.g. Pleroma.User.Query.build(%{ap_id: ["http://ap_id1", "http://ap_id2"]})
30 import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
33 alias Pleroma.FollowingRelationship
45 deactivated: boolean(),
46 need_approval: boolean(),
47 unconfirmed: boolean(),
49 is_moderator: boolean(),
50 is_suggested: boolean(),
51 is_discoverable: boolean(),
52 super_users: boolean(),
53 is_privileged: atom(),
58 recipients_from_activity: [String.t()],
59 nickname: [String.t()] | String.t(),
64 actor_types: [String.t()],
65 birthday_day: pos_integer(),
66 birthday_month: pos_integer()
70 @ilike_criteria [:nickname, :name, :query]
71 @equal_criteria [:email]
72 @contains_criteria [:ap_id, :nickname]
74 @spec build(Ecto.Query.t(), criteria()) :: Ecto.Query.t()
75 def build(query \\ base_query(), criteria) do
76 prepare_query(query, criteria)
79 @spec paginate(Ecto.Query.t(), pos_integer(), pos_integer()) :: Ecto.Query.t()
80 def paginate(query, page, page_size) do
83 offset: ^((page - 1) * page_size)
91 defp prepare_query(query, criteria) do
93 |> Map.put_new(:internal, false)
94 |> Enum.reduce(query, &compose_query/2)
97 defp compose_query({key, value}, query)
98 when key in @ilike_criteria and not_empty_string(value) do
100 key = if key == :query, do: :nickname, else: key
101 where(query, [u], ilike(field(u, ^key), ^"%#{value}%"))
104 defp compose_query({:invisible, bool}, query) when is_boolean(bool) do
105 where(query, [u], u.invisible == ^bool)
108 defp compose_query({key, value}, query)
109 when key in @equal_criteria and not_empty_string(value) do
110 where(query, [u], ^[{key, value}])
113 defp compose_query({key, values}, query) when key in @contains_criteria and is_list(values) do
114 where(query, [u], field(u, ^key) in ^values)
117 defp compose_query({:tags, tags}, query) when is_list(tags) and length(tags) > 0 do
118 where(query, [u], fragment("? && ?", u.tags, ^tags))
121 defp compose_query({:is_admin, bool}, query) do
122 where(query, [u], u.is_admin == ^bool)
125 defp compose_query({:actor_types, actor_types}, query) when is_list(actor_types) do
126 where(query, [u], u.actor_type in ^actor_types)
129 defp compose_query({:is_moderator, bool}, query) do
130 where(query, [u], u.is_moderator == ^bool)
133 defp compose_query({:super_users, _}, query) do
137 u.is_admin or u.is_moderator
141 defp compose_query({:is_privileged, privilege}, query) do
142 moderator_privileged = privilege in Config.get([:instance, :moderator_privileges])
143 admin_privileged = privilege in Config.get([:instance, :admin_privileges])
145 query = compose_query({:active, true}, query)
146 query = compose_query({:local, true}, query)
148 case {admin_privileged, moderator_privileged} do
159 u.is_admin or u.is_moderator
178 defp compose_query({:local, _}, query), do: location_query(query, true)
180 defp compose_query({:external, _}, query), do: location_query(query, false)
182 defp compose_query({:active, _}, query) do
183 where(query, [u], u.is_active == true)
184 |> where([u], u.is_approved == true)
185 |> where([u], u.is_confirmed == true)
188 defp compose_query({:legacy_active, _}, query) do
190 |> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
193 defp compose_query({:deactivated, false}, query) do
194 where(query, [u], u.is_active == true)
197 defp compose_query({:deactivated, true}, query) do
198 where(query, [u], u.is_active == false)
201 defp compose_query({:confirmation_pending, bool}, query) do
202 where(query, [u], u.is_confirmed != ^bool)
205 defp compose_query({:need_approval, _}, query) do
206 where(query, [u], u.is_approved == false)
209 defp compose_query({:unconfirmed, _}, query) do
210 where(query, [u], u.is_confirmed == false)
213 defp compose_query({:is_suggested, bool}, query) do
214 where(query, [u], u.is_suggested == ^bool)
217 defp compose_query({:is_discoverable, bool}, query) do
218 where(query, [u], u.is_discoverable == ^bool)
221 defp compose_query({:followers, %User{id: id}}, query) do
223 |> where([u], u.id != ^id)
224 |> join(:inner, [u], r in FollowingRelationship,
226 on: r.following_id == ^id and r.follower_id == u.id
228 |> where([relationships: r], r.state == ^:follow_accept)
231 defp compose_query({:friends, %User{id: id}}, query) do
233 |> where([u], u.id != ^id)
234 |> join(:inner, [u], r in FollowingRelationship,
236 on: r.following_id == u.id and r.follower_id == ^id
238 |> where([relationships: r], r.state == ^:follow_accept)
241 defp compose_query({:recipients_from_activity, to}, query) do
244 join: f in FollowingRelationship,
245 on: u.id == f.following_id,
246 where: f.state == ^:follow_accept,
247 where: u.follower_address in ^to,
248 select: f.follower_id
252 where: u.ap_id in ^to or u.id in subquery(following_query)
256 defp compose_query({:order_by, key}, query) do
257 order_by(query, [u], field(u, ^key))
260 defp compose_query({:select, keys}, query) do
261 select(query, [u], ^keys)
264 defp compose_query({:limit, limit}, query) do
268 defp compose_query({:internal, false}, query) do
270 |> where([u], not is_nil(u.nickname))
271 |> where([u], not like(u.nickname, "internal.%"))
274 defp compose_query({:birthday_day, day}, query) do
276 |> where([u], u.show_birthday == true)
277 |> where([u], not is_nil(u.birthday))
278 |> where([u], fragment("date_part('day', ?)", u.birthday) == ^day)
281 defp compose_query({:birthday_month, month}, query) do
283 |> where([u], u.show_birthday == true)
284 |> where([u], not is_nil(u.birthday))
285 |> where([u], fragment("date_part('month', ?)", u.birthday) == ^month)
288 defp compose_query(_unsupported_param, query), do: query
290 defp location_query(query, local) do
291 where(query, [u], u.local == ^local)