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.Utils.Guards, 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 where(query, [u], u.is_active == true)
141 |> where([u], u.is_approved == true)
142 |> where([u], u.is_confirmed == true)
145 defp compose_query({:legacy_active, _}, query) do
147 |> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
150 defp compose_query({:deactivated, false}, query) do
151 where(query, [u], u.is_active == true)
154 defp compose_query({:deactivated, true}, query) do
155 where(query, [u], u.is_active == false)
158 defp compose_query({:confirmation_pending, bool}, query) do
159 where(query, [u], u.is_confirmed != ^bool)
162 defp compose_query({:need_approval, _}, query) do
163 where(query, [u], u.is_approved == false)
166 defp compose_query({:unconfirmed, _}, query) do
167 where(query, [u], u.is_confirmed == false)
170 defp compose_query({:followers, %User{id: id}}, query) do
172 |> where([u], u.id != ^id)
173 |> join(:inner, [u], r in FollowingRelationship,
175 on: r.following_id == ^id and r.follower_id == u.id
177 |> where([relationships: r], r.state == ^:follow_accept)
180 defp compose_query({:friends, %User{id: id}}, query) do
182 |> where([u], u.id != ^id)
183 |> join(:inner, [u], r in FollowingRelationship,
185 on: r.following_id == u.id and r.follower_id == ^id
187 |> where([relationships: r], r.state == ^:follow_accept)
190 defp compose_query({:recipients_from_activity, to}, query) do
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
201 where: u.ap_id in ^to or u.id in subquery(following_query)
205 defp compose_query({:order_by, key}, query) do
206 order_by(query, [u], field(u, ^key))
209 defp compose_query({:select, keys}, query) do
210 select(query, [u], ^keys)
213 defp compose_query({:limit, limit}, query) do
217 defp compose_query({:internal, false}, query) do
219 |> where([u], not is_nil(u.nickname))
220 |> where([u], not like(u.nickname, "internal.%"))
223 defp compose_query(_unsupported_param, query), do: query
225 defp location_query(query, local) do
226 where(query, [u], u.local == ^local)