2019-05-08 08:34:36 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2019-05-08 08:34:36 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.User.Query do
|
|
|
|
@moduledoc """
|
|
|
|
User query builder module. Builds query from new query or another user query.
|
|
|
|
|
|
|
|
## Example:
|
2019-07-09 11:36:35 -06:00
|
|
|
query = Pleroma.User.Query.build(%{nickname: "nickname"})
|
2019-05-08 08:34:36 -06:00
|
|
|
another_query = Pleroma.User.Query.build(query, %{email: "email@example.com"})
|
|
|
|
Pleroma.Repo.all(query)
|
|
|
|
Pleroma.Repo.all(another_query)
|
|
|
|
|
|
|
|
Adding new rules:
|
|
|
|
- *ilike criteria*
|
|
|
|
- add field to @ilike_criteria list
|
|
|
|
- pass non empty string
|
|
|
|
- e.g. Pleroma.User.Query.build(%{nickname: "nickname"})
|
|
|
|
- *equal criteria*
|
|
|
|
- add field to @equal_criteria list
|
|
|
|
- pass non empty string
|
|
|
|
- e.g. Pleroma.User.Query.build(%{email: "email@example.com"})
|
|
|
|
- *contains criteria*
|
|
|
|
- add field to @containns_criteria list
|
|
|
|
- pass values list
|
|
|
|
- e.g. Pleroma.User.Query.build(%{ap_id: ["http://ap_id1", "http://ap_id2"]})
|
|
|
|
"""
|
|
|
|
import Ecto.Query
|
2021-06-07 14:51:52 -06:00
|
|
|
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
|
2019-10-10 13:35:32 -06:00
|
|
|
|
2022-06-19 03:05:09 -06:00
|
|
|
alias Pleroma.Config
|
2019-10-10 13:35:32 -06:00
|
|
|
alias Pleroma.FollowingRelationship
|
2019-05-08 08:34:36 -06:00
|
|
|
alias Pleroma.User
|
|
|
|
|
|
|
|
@type criteria ::
|
|
|
|
%{
|
|
|
|
query: String.t(),
|
|
|
|
tags: [String.t()],
|
|
|
|
name: String.t(),
|
|
|
|
email: String.t(),
|
|
|
|
local: boolean(),
|
|
|
|
external: boolean(),
|
|
|
|
active: boolean(),
|
|
|
|
deactivated: boolean(),
|
2020-07-14 13:14:43 -06:00
|
|
|
need_approval: boolean(),
|
2020-09-25 00:39:49 -06:00
|
|
|
unconfirmed: boolean(),
|
2019-05-08 08:34:36 -06:00
|
|
|
is_admin: boolean(),
|
|
|
|
is_moderator: boolean(),
|
2021-11-26 20:02:14 -07:00
|
|
|
is_suggested: boolean(),
|
2021-12-25 19:35:17 -07:00
|
|
|
is_discoverable: boolean(),
|
2019-05-08 08:34:36 -06:00
|
|
|
super_users: boolean(),
|
2022-06-19 03:05:09 -06:00
|
|
|
is_privileged: atom(),
|
2020-06-01 13:18:20 -06:00
|
|
|
invisible: boolean(),
|
2020-09-22 06:55:40 -06:00
|
|
|
internal: boolean(),
|
2019-05-08 08:34:36 -06:00
|
|
|
followers: User.t(),
|
|
|
|
friends: User.t(),
|
|
|
|
recipients_from_activity: [String.t()],
|
2020-02-25 07:34:56 -07:00
|
|
|
nickname: [String.t()] | String.t(),
|
2019-07-09 11:36:35 -06:00
|
|
|
ap_id: [String.t()],
|
|
|
|
order_by: term(),
|
|
|
|
select: term(),
|
2020-09-25 07:04:01 -06:00
|
|
|
limit: pos_integer(),
|
2022-01-18 06:57:48 -07:00
|
|
|
actor_types: [String.t()],
|
2022-01-22 12:21:55 -07:00
|
|
|
birthday_day: pos_integer(),
|
|
|
|
birthday_month: pos_integer()
|
2019-05-08 08:34:36 -06:00
|
|
|
}
|
2019-12-03 23:49:17 -07:00
|
|
|
| map()
|
2019-05-08 08:34:36 -06:00
|
|
|
|
|
|
|
@ilike_criteria [:nickname, :name, :query]
|
|
|
|
@equal_criteria [:email]
|
|
|
|
@contains_criteria [:ap_id, :nickname]
|
|
|
|
|
2019-12-03 23:49:17 -07:00
|
|
|
@spec build(Query.t(), criteria()) :: Query.t()
|
2019-05-08 08:34:36 -06:00
|
|
|
def build(query \\ base_query(), criteria) do
|
|
|
|
prepare_query(query, criteria)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec paginate(Ecto.Query.t(), pos_integer(), pos_integer()) :: Ecto.Query.t()
|
|
|
|
def paginate(query, page, page_size) do
|
|
|
|
from(u in query,
|
|
|
|
limit: ^page_size,
|
|
|
|
offset: ^((page - 1) * page_size)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp base_query do
|
|
|
|
from(u in User)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp prepare_query(query, criteria) do
|
2020-09-22 06:55:40 -06:00
|
|
|
criteria
|
|
|
|
|> Map.put_new(:internal, false)
|
|
|
|
|> Enum.reduce(query, &compose_query/2)
|
2019-05-08 08:34:36 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({key, value}, query)
|
|
|
|
when key in @ilike_criteria and not_empty_string(value) do
|
|
|
|
# hack for :query key
|
|
|
|
key = if key == :query, do: :nickname, else: key
|
|
|
|
where(query, [u], ilike(field(u, ^key), ^"%#{value}%"))
|
|
|
|
end
|
|
|
|
|
2020-06-01 13:18:20 -06:00
|
|
|
defp compose_query({:invisible, bool}, query) when is_boolean(bool) do
|
|
|
|
where(query, [u], u.invisible == ^bool)
|
2020-05-01 05:31:24 -06:00
|
|
|
end
|
|
|
|
|
2019-05-08 08:34:36 -06:00
|
|
|
defp compose_query({key, value}, query)
|
|
|
|
when key in @equal_criteria and not_empty_string(value) do
|
|
|
|
where(query, [u], ^[{key, value}])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({key, values}, query) when key in @contains_criteria and is_list(values) do
|
|
|
|
where(query, [u], field(u, ^key) in ^values)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:tags, tags}, query) when is_list(tags) and length(tags) > 0 do
|
2020-05-01 05:31:24 -06:00
|
|
|
where(query, [u], fragment("? && ?", u.tags, ^tags))
|
2019-05-08 08:34:36 -06:00
|
|
|
end
|
|
|
|
|
2020-09-08 15:39:41 -06:00
|
|
|
defp compose_query({:is_admin, bool}, query) do
|
|
|
|
where(query, [u], u.is_admin == ^bool)
|
2019-10-16 12:59:21 -06:00
|
|
|
end
|
|
|
|
|
2020-09-25 07:04:01 -06:00
|
|
|
defp compose_query({:actor_types, actor_types}, query) when is_list(actor_types) do
|
|
|
|
where(query, [u], u.actor_type in ^actor_types)
|
|
|
|
end
|
|
|
|
|
2020-09-08 15:39:41 -06:00
|
|
|
defp compose_query({:is_moderator, bool}, query) do
|
|
|
|
where(query, [u], u.is_moderator == ^bool)
|
2019-05-08 08:34:36 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:super_users, _}, query) do
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
[u],
|
2019-10-16 12:59:21 -06:00
|
|
|
u.is_admin or u.is_moderator
|
2019-05-08 08:34:36 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-06-19 03:05:09 -06:00
|
|
|
defp compose_query({:is_privileged, privilege}, query) do
|
|
|
|
moderator_privileged = privilege in Config.get([:instance, :moderator_privileges])
|
|
|
|
admin_privileged = privilege in Config.get([:instance, :admin_privileges])
|
|
|
|
|
|
|
|
query = compose_query({:active, true}, query)
|
|
|
|
query = compose_query({:local, true}, query)
|
|
|
|
|
|
|
|
case {admin_privileged, moderator_privileged} do
|
|
|
|
{false, false} ->
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
|
|
|
|
{true, true} ->
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
[u],
|
|
|
|
u.is_admin or u.is_moderator
|
|
|
|
)
|
|
|
|
|
|
|
|
{true, false} ->
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
[u],
|
|
|
|
u.is_admin
|
|
|
|
)
|
|
|
|
|
|
|
|
{false, true} ->
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
[u],
|
|
|
|
u.is_moderator
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-08 08:34:36 -06:00
|
|
|
defp compose_query({:local, _}, query), do: location_query(query, true)
|
|
|
|
|
|
|
|
defp compose_query({:external, _}, query), do: location_query(query, false)
|
|
|
|
|
|
|
|
defp compose_query({:active, _}, query) do
|
2020-10-12 17:21:46 -06:00
|
|
|
where(query, [u], u.is_active == true)
|
2020-10-16 15:44:25 -06:00
|
|
|
|> where([u], u.is_approved == true)
|
2021-01-19 15:39:55 -07:00
|
|
|
|> where([u], u.is_confirmed == true)
|
2019-10-16 12:59:21 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:legacy_active, _}, query) do
|
|
|
|
query
|
|
|
|
|> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
|
2019-05-08 08:34:36 -06:00
|
|
|
end
|
|
|
|
|
2019-05-14 05:15:56 -06:00
|
|
|
defp compose_query({:deactivated, false}, query) do
|
2020-10-12 17:21:46 -06:00
|
|
|
where(query, [u], u.is_active == true)
|
2019-05-14 05:15:56 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:deactivated, true}, query) do
|
2020-10-12 16:42:27 -06:00
|
|
|
where(query, [u], u.is_active == false)
|
2019-05-08 08:34:36 -06:00
|
|
|
end
|
|
|
|
|
2020-09-08 15:39:08 -06:00
|
|
|
defp compose_query({:confirmation_pending, bool}, query) do
|
2020-10-13 13:29:34 -06:00
|
|
|
where(query, [u], u.is_confirmed != ^bool)
|
2020-09-08 15:39:08 -06:00
|
|
|
end
|
|
|
|
|
2020-07-14 13:14:43 -06:00
|
|
|
defp compose_query({:need_approval, _}, query) do
|
2020-10-16 15:44:25 -06:00
|
|
|
where(query, [u], u.is_approved == false)
|
2020-07-14 13:14:43 -06:00
|
|
|
end
|
|
|
|
|
2020-09-25 00:39:49 -06:00
|
|
|
defp compose_query({:unconfirmed, _}, query) do
|
2020-10-13 13:29:34 -06:00
|
|
|
where(query, [u], u.is_confirmed == false)
|
2020-09-21 00:33:51 -06:00
|
|
|
end
|
|
|
|
|
2021-11-26 13:33:27 -07:00
|
|
|
defp compose_query({:is_suggested, bool}, query) do
|
|
|
|
where(query, [u], u.is_suggested == ^bool)
|
|
|
|
end
|
|
|
|
|
2021-12-25 19:35:17 -07:00
|
|
|
defp compose_query({:is_discoverable, bool}, query) do
|
|
|
|
where(query, [u], u.is_discoverable == ^bool)
|
|
|
|
end
|
|
|
|
|
2019-10-10 13:35:32 -06:00
|
|
|
defp compose_query({:followers, %User{id: id}}, query) do
|
|
|
|
query
|
2019-05-08 08:34:36 -06:00
|
|
|
|> where([u], u.id != ^id)
|
2019-10-10 13:35:32 -06:00
|
|
|
|> join(:inner, [u], r in FollowingRelationship,
|
|
|
|
as: :relationships,
|
|
|
|
on: r.following_id == ^id and r.follower_id == u.id
|
|
|
|
)
|
2020-03-28 09:49:03 -06:00
|
|
|
|> where([relationships: r], r.state == ^:follow_accept)
|
2019-05-08 08:34:36 -06:00
|
|
|
end
|
|
|
|
|
2019-10-10 13:35:32 -06:00
|
|
|
defp compose_query({:friends, %User{id: id}}, query) do
|
|
|
|
query
|
2019-05-08 08:34:36 -06:00
|
|
|
|> where([u], u.id != ^id)
|
2019-10-10 13:35:32 -06:00
|
|
|
|> join(:inner, [u], r in FollowingRelationship,
|
|
|
|
as: :relationships,
|
|
|
|
on: r.following_id == u.id and r.follower_id == ^id
|
|
|
|
)
|
2020-03-28 09:49:03 -06:00
|
|
|
|> where([relationships: r], r.state == ^:follow_accept)
|
2019-05-08 08:34:36 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:recipients_from_activity, to}, query) do
|
2020-05-19 06:11:32 -06:00
|
|
|
following_query =
|
|
|
|
from(u in User,
|
|
|
|
join: f in FollowingRelationship,
|
|
|
|
on: u.id == f.following_id,
|
|
|
|
where: f.state == ^:follow_accept,
|
|
|
|
where: u.follower_address in ^to,
|
|
|
|
select: f.follower_id
|
|
|
|
)
|
|
|
|
|
|
|
|
from(u in query,
|
2020-05-19 06:36:34 -06:00
|
|
|
where: u.ap_id in ^to or u.id in subquery(following_query)
|
2019-10-10 13:35:32 -06:00
|
|
|
)
|
2019-05-08 08:34:36 -06:00
|
|
|
end
|
|
|
|
|
2019-07-09 11:36:35 -06:00
|
|
|
defp compose_query({:order_by, key}, query) do
|
|
|
|
order_by(query, [u], field(u, ^key))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:select, keys}, query) do
|
|
|
|
select(query, [u], ^keys)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:limit, limit}, query) do
|
|
|
|
limit(query, ^limit)
|
|
|
|
end
|
|
|
|
|
2020-09-22 06:55:40 -06:00
|
|
|
defp compose_query({:internal, false}, query) do
|
|
|
|
query
|
|
|
|
|> where([u], not is_nil(u.nickname))
|
|
|
|
|> where([u], not like(u.nickname, "internal.%"))
|
|
|
|
end
|
|
|
|
|
2022-01-22 12:21:55 -07:00
|
|
|
defp compose_query({:birthday_day, day}, query) do
|
2022-01-18 06:57:48 -07:00
|
|
|
query
|
2022-01-23 00:42:18 -07:00
|
|
|
|> where([u], u.show_birthday == true)
|
2022-01-22 12:21:55 -07:00
|
|
|
|> where([u], not is_nil(u.birthday))
|
|
|
|
|> where([u], fragment("date_part('day', ?)", u.birthday) == ^day)
|
2022-01-18 06:57:48 -07:00
|
|
|
end
|
|
|
|
|
2022-01-22 12:21:55 -07:00
|
|
|
defp compose_query({:birthday_month, month}, query) do
|
2022-01-18 06:57:48 -07:00
|
|
|
query
|
2022-01-23 00:42:18 -07:00
|
|
|
|> where([u], u.show_birthday == true)
|
2022-01-22 12:21:55 -07:00
|
|
|
|> where([u], not is_nil(u.birthday))
|
|
|
|
|> where([u], fragment("date_part('month', ?)", u.birthday) == ^month)
|
2022-01-18 06:57:48 -07:00
|
|
|
end
|
|
|
|
|
2019-05-08 08:34:36 -06:00
|
|
|
defp compose_query(_unsupported_param, query), do: query
|
|
|
|
|
|
|
|
defp location_query(query, local) do
|
|
|
|
where(query, [u], u.local == ^local)
|
|
|
|
end
|
|
|
|
end
|