Merge branch 'release/2.1.0' into 'stable'
[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 is_admin: boolean(),
47 is_moderator: boolean(),
48 super_users: boolean(),
49 invisible: boolean(),
50 followers: User.t(),
51 friends: User.t(),
52 recipients_from_activity: [String.t()],
53 nickname: [String.t()] | String.t(),
54 ap_id: [String.t()],
55 order_by: term(),
56 select: term(),
57 limit: pos_integer()
58 }
59 | map()
60
61 @ilike_criteria [:nickname, :name, :query]
62 @equal_criteria [:email]
63 @contains_criteria [:ap_id, :nickname]
64
65 @spec build(Query.t(), criteria()) :: Query.t()
66 def build(query \\ base_query(), criteria) do
67 prepare_query(query, criteria)
68 end
69
70 @spec paginate(Ecto.Query.t(), pos_integer(), pos_integer()) :: Ecto.Query.t()
71 def paginate(query, page, page_size) do
72 from(u in query,
73 limit: ^page_size,
74 offset: ^((page - 1) * page_size)
75 )
76 end
77
78 defp base_query do
79 from(u in User)
80 end
81
82 defp prepare_query(query, criteria) do
83 Enum.reduce(criteria, query, &compose_query/2)
84 end
85
86 defp compose_query({key, value}, query)
87 when key in @ilike_criteria and not_empty_string(value) do
88 # hack for :query key
89 key = if key == :query, do: :nickname, else: key
90 where(query, [u], ilike(field(u, ^key), ^"%#{value}%"))
91 end
92
93 defp compose_query({:invisible, bool}, query) when is_boolean(bool) do
94 where(query, [u], u.invisible == ^bool)
95 end
96
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}])
100 end
101
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)
104 end
105
106 defp compose_query({:tags, tags}, query) when is_list(tags) and length(tags) > 0 do
107 where(query, [u], fragment("? && ?", u.tags, ^tags))
108 end
109
110 defp compose_query({:is_admin, _}, query) do
111 where(query, [u], u.is_admin)
112 end
113
114 defp compose_query({:is_moderator, _}, query) do
115 where(query, [u], u.is_moderator)
116 end
117
118 defp compose_query({:super_users, _}, query) do
119 where(
120 query,
121 [u],
122 u.is_admin or u.is_moderator
123 )
124 end
125
126 defp compose_query({:local, _}, query), do: location_query(query, true)
127
128 defp compose_query({:external, _}, query), do: location_query(query, false)
129
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)
134 end
135
136 defp compose_query({:legacy_active, _}, query) do
137 query
138 |> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
139 |> where([u], not is_nil(u.nickname))
140 end
141
142 defp compose_query({:deactivated, false}, query) do
143 User.restrict_deactivated(query)
144 end
145
146 defp compose_query({:deactivated, true}, query) do
147 where(query, [u], u.deactivated == ^true)
148 |> where([u], not is_nil(u.nickname))
149 end
150
151 defp compose_query({:need_approval, _}, query) do
152 where(query, [u], u.approval_pending)
153 end
154
155 defp compose_query({:followers, %User{id: id}}, query) do
156 query
157 |> where([u], u.id != ^id)
158 |> join(:inner, [u], r in FollowingRelationship,
159 as: :relationships,
160 on: r.following_id == ^id and r.follower_id == u.id
161 )
162 |> where([relationships: r], r.state == ^:follow_accept)
163 end
164
165 defp compose_query({:friends, %User{id: id}}, query) do
166 query
167 |> where([u], u.id != ^id)
168 |> join(:inner, [u], r in FollowingRelationship,
169 as: :relationships,
170 on: r.following_id == u.id and r.follower_id == ^id
171 )
172 |> where([relationships: r], r.state == ^:follow_accept)
173 end
174
175 defp compose_query({:recipients_from_activity, to}, query) do
176 following_query =
177 from(u in User,
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
183 )
184
185 from(u in query,
186 where: u.ap_id in ^to or u.id in subquery(following_query)
187 )
188 end
189
190 defp compose_query({:order_by, key}, query) do
191 order_by(query, [u], field(u, ^key))
192 end
193
194 defp compose_query({:select, keys}, query) do
195 select(query, [u], ^keys)
196 end
197
198 defp compose_query({:limit, limit}, query) do
199 limit(query, ^limit)
200 end
201
202 defp compose_query(_unsupported_param, query), do: query
203
204 defp location_query(query, local) do
205 where(query, [u], u.local == ^local)
206 |> where([u], not is_nil(u.nickname))
207 end
208 end