1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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(),
47 is_moderator: boolean(),
48 super_users: boolean(),
52 recipients_from_activity: [String.t()],
53 nickname: [String.t()] | String.t(),
61 @ilike_criteria [:nickname, :name, :query]
62 @equal_criteria [:email]
63 @contains_criteria [:ap_id, :nickname]
65 @spec build(Query.t(), criteria()) :: Query.t()
66 def build(query \\ base_query(), criteria) do
67 prepare_query(query, criteria)
70 @spec paginate(Ecto.Query.t(), pos_integer(), pos_integer()) :: Ecto.Query.t()
71 def paginate(query, page, page_size) do
74 offset: ^((page - 1) * page_size)
82 defp prepare_query(query, criteria) do
83 Enum.reduce(criteria, query, &compose_query/2)
86 defp compose_query({key, value}, query)
87 when key in @ilike_criteria and not_empty_string(value) do
89 key = if key == :query, do: :nickname, else: key
90 where(query, [u], ilike(field(u, ^key), ^"%#{value}%"))
93 defp compose_query({:invisible, bool}, query) when is_boolean(bool) do
94 where(query, [u], u.invisible == ^bool)
97 defp compose_query({key, value}, query)
98 when key in @equal_criteria and not_empty_string(value) do
99 where(query, [u], ^[{key, value}])
102 defp compose_query({key, values}, query) when key in @contains_criteria and is_list(values) do
103 where(query, [u], field(u, ^key) in ^values)
106 defp compose_query({:tags, tags}, query) when is_list(tags) and length(tags) > 0 do
107 where(query, [u], fragment("? && ?", u.tags, ^tags))
110 defp compose_query({:is_admin, _}, query) do
111 where(query, [u], u.is_admin)
114 defp compose_query({:is_moderator, _}, query) do
115 where(query, [u], u.is_moderator)
118 defp compose_query({:super_users, _}, query) do
122 u.is_admin or u.is_moderator
126 defp compose_query({:local, _}, query), do: location_query(query, true)
128 defp compose_query({:external, _}, query), do: location_query(query, false)
130 defp compose_query({:active, _}, query) do
131 User.restrict_deactivated(query)
132 |> where([u], not is_nil(u.nickname))
133 |> where([u], u.approval_pending == false)
136 defp compose_query({:legacy_active, _}, query) do
138 |> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
139 |> where([u], not is_nil(u.nickname))
142 defp compose_query({:deactivated, false}, query) do
143 User.restrict_deactivated(query)
146 defp compose_query({:deactivated, true}, query) do
147 where(query, [u], u.deactivated == ^true)
148 |> where([u], not is_nil(u.nickname))
151 defp compose_query({:need_approval, _}, query) do
152 where(query, [u], u.approval_pending)
155 defp compose_query({:followers, %User{id: id}}, query) do
157 |> where([u], u.id != ^id)
158 |> join(:inner, [u], r in FollowingRelationship,
160 on: r.following_id == ^id and r.follower_id == u.id
162 |> where([relationships: r], r.state == ^:follow_accept)
165 defp compose_query({:friends, %User{id: id}}, query) do
167 |> where([u], u.id != ^id)
168 |> join(:inner, [u], r in FollowingRelationship,
170 on: r.following_id == u.id and r.follower_id == ^id
172 |> where([relationships: r], r.state == ^:follow_accept)
175 defp compose_query({:recipients_from_activity, to}, query) do
178 join: f in FollowingRelationship,
179 on: u.id == f.following_id,
180 where: f.state == ^:follow_accept,
181 where: u.follower_address in ^to,
182 select: f.follower_id
186 where: u.ap_id in ^to or u.id in subquery(following_query)
190 defp compose_query({:order_by, key}, query) do
191 order_by(query, [u], field(u, ^key))
194 defp compose_query({:select, keys}, query) do
195 select(query, [u], ^keys)
198 defp compose_query({:limit, limit}, query) do
202 defp compose_query(_unsupported_param, query), do: query
204 defp location_query(query, local) do
205 where(query, [u], u.local == ^local)
206 |> where([u], not is_nil(u.nickname))