1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 @containns_criteria list
27 - e.g. Pleroma.User.Query.build(%{ap_id: ["http://ap_id1", "http://ap_id2"]})
30 import Pleroma.Web.AdminAPI.Search, only: [not_empty_string: 1]
32 alias Pleroma.FollowingRelationship
44 deactivated: boolean(),
45 need_approval: boolean(),
46 unconfirmed: boolean(),
48 is_moderator: boolean(),
49 super_users: boolean(),
54 recipients_from_activity: [String.t()],
55 nickname: [String.t()] | String.t(),
60 actor_types: [String.t()]
64 @ilike_criteria [:nickname, :name, :query]
65 @equal_criteria [:email]
66 @contains_criteria [:ap_id, :nickname]
68 @spec build(Query.t(), criteria()) :: Query.t()
69 def build(query \\ base_query(), criteria) do
70 prepare_query(query, criteria)
73 @spec paginate(Ecto.Query.t(), pos_integer(), pos_integer()) :: Ecto.Query.t()
74 def paginate(query, page, page_size) do
77 offset: ^((page - 1) * page_size)
85 defp prepare_query(query, criteria) do
87 |> Map.put_new(:internal, false)
88 |> Enum.reduce(query, &compose_query/2)
91 defp compose_query({key, value}, query)
92 when key in @ilike_criteria and not_empty_string(value) do
94 key = if key == :query, do: :nickname, else: key
95 where(query, [u], ilike(field(u, ^key), ^"%#{value}%"))
98 defp compose_query({:invisible, bool}, query) when is_boolean(bool) do
99 where(query, [u], u.invisible == ^bool)
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}])
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)
111 defp compose_query({:tags, tags}, query) when is_list(tags) and length(tags) > 0 do
112 where(query, [u], fragment("? && ?", u.tags, ^tags))
115 defp compose_query({:is_admin, bool}, query) do
116 where(query, [u], u.is_admin == ^bool)
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)
123 defp compose_query({:is_moderator, bool}, query) do
124 where(query, [u], u.is_moderator == ^bool)
127 defp compose_query({:super_users, _}, query) do
131 u.is_admin or u.is_moderator
135 defp compose_query({:local, _}, query), do: location_query(query, true)
137 defp compose_query({:external, _}, query), do: location_query(query, false)
139 defp compose_query({:active, _}, query) do
140 User.restrict_deactivated(query)
141 |> where([u], u.approval_pending == false)
144 defp compose_query({:legacy_active, _}, query) do
146 |> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
149 defp compose_query({:deactivated, false}, query) do
150 User.restrict_deactivated(query)
153 defp compose_query({:deactivated, true}, query) do
154 where(query, [u], u.deactivated == ^true)
157 defp compose_query({:confirmation_pending, bool}, query) do
158 where(query, [u], u.confirmation_pending == ^bool)
161 defp compose_query({:need_approval, _}, query) do
162 where(query, [u], u.approval_pending)
165 defp compose_query({:unconfirmed, _}, query) do
166 where(query, [u], u.confirmation_pending)
169 defp compose_query({:followers, %User{id: id}}, query) do
171 |> where([u], u.id != ^id)
172 |> join(:inner, [u], r in FollowingRelationship,
174 on: r.following_id == ^id and r.follower_id == u.id
176 |> where([relationships: r], r.state == ^:follow_accept)
179 defp compose_query({:friends, %User{id: id}}, query) do
181 |> where([u], u.id != ^id)
182 |> join(:inner, [u], r in FollowingRelationship,
184 on: r.following_id == u.id and r.follower_id == ^id
186 |> where([relationships: r], r.state == ^:follow_accept)
189 defp compose_query({:recipients_from_activity, to}, query) do
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
200 where: u.ap_id in ^to or u.id in subquery(following_query)
204 defp compose_query({:order_by, key}, query) do
205 order_by(query, [u], field(u, ^key))
208 defp compose_query({:select, keys}, query) do
209 select(query, [u], ^keys)
212 defp compose_query({:limit, limit}, query) do
216 defp compose_query({:internal, false}, query) do
218 |> where([u], not is_nil(u.nickname))
219 |> where([u], not like(u.nickname, "internal.%"))
222 defp compose_query(_unsupported_param, query), do: query
224 defp location_query(query, local) do
225 where(query, [u], u.local == ^local)