2019-05-08 14:34:36 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-05-08 14:34:36 +00: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 17:36:35 +00:00
|
|
|
query = Pleroma.User.Query.build(%{nickname: "nickname"})
|
2019-05-08 14:34:36 +00: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
|
|
|
|
import Pleroma.Web.AdminAPI.Search, only: [not_empty_string: 1]
|
2019-10-10 19:35:32 +00:00
|
|
|
|
|
|
|
alias Pleroma.FollowingRelationship
|
2019-05-08 14:34:36 +00: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(),
|
|
|
|
is_admin: boolean(),
|
|
|
|
is_moderator: boolean(),
|
|
|
|
super_users: boolean(),
|
2020-05-01 11:31:24 +00:00
|
|
|
exclude_service_users: boolean(),
|
2019-05-08 14:34:36 +00:00
|
|
|
followers: User.t(),
|
|
|
|
friends: User.t(),
|
|
|
|
recipients_from_activity: [String.t()],
|
2020-02-25 14:34:56 +00:00
|
|
|
nickname: [String.t()] | String.t(),
|
2019-07-09 17:36:35 +00:00
|
|
|
ap_id: [String.t()],
|
|
|
|
order_by: term(),
|
|
|
|
select: term(),
|
|
|
|
limit: pos_integer()
|
2019-05-08 14:34:36 +00:00
|
|
|
}
|
2019-12-04 06:49:17 +00:00
|
|
|
| map()
|
2019-05-08 14:34:36 +00:00
|
|
|
|
|
|
|
@ilike_criteria [:nickname, :name, :query]
|
|
|
|
@equal_criteria [:email]
|
|
|
|
@contains_criteria [:ap_id, :nickname]
|
|
|
|
|
2019-12-04 06:49:17 +00:00
|
|
|
@spec build(Query.t(), criteria()) :: Query.t()
|
2019-05-08 14:34:36 +00: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
|
|
|
|
Enum.reduce(criteria, query, &compose_query/2)
|
|
|
|
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-05-01 11:31:24 +00:00
|
|
|
defp compose_query({:exclude_service_users, _}, query) do
|
|
|
|
where(query, [u], not like(u.ap_id, "%/relay") and not like(u.ap_id, "%/internal/fetch"))
|
|
|
|
end
|
|
|
|
|
2019-05-08 14:34:36 +00: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 11:31:24 +00:00
|
|
|
where(query, [u], fragment("? && ?", u.tags, ^tags))
|
2019-05-08 14:34:36 +00:00
|
|
|
end
|
|
|
|
|
2019-10-16 18:59:21 +00:00
|
|
|
defp compose_query({:is_admin, _}, query) do
|
|
|
|
where(query, [u], u.is_admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:is_moderator, _}, query) do
|
|
|
|
where(query, [u], u.is_moderator)
|
2019-05-08 14:34:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:super_users, _}, query) do
|
|
|
|
where(
|
|
|
|
query,
|
|
|
|
[u],
|
2019-10-16 18:59:21 +00:00
|
|
|
u.is_admin or u.is_moderator
|
2019-05-08 14:34:36 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2019-10-16 18:59:21 +00:00
|
|
|
User.restrict_deactivated(query)
|
|
|
|
|> where([u], not is_nil(u.nickname))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:legacy_active, _}, query) do
|
|
|
|
query
|
|
|
|
|> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
|
2019-05-08 14:34:36 +00:00
|
|
|
|> where([u], not is_nil(u.nickname))
|
|
|
|
end
|
|
|
|
|
2019-05-14 11:15:56 +00:00
|
|
|
defp compose_query({:deactivated, false}, query) do
|
2019-05-14 11:29:10 +00:00
|
|
|
User.restrict_deactivated(query)
|
2019-05-14 11:15:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:deactivated, true}, query) do
|
2019-10-16 18:59:21 +00:00
|
|
|
where(query, [u], u.deactivated == ^true)
|
2019-05-08 14:34:36 +00:00
|
|
|
|> where([u], not is_nil(u.nickname))
|
|
|
|
end
|
|
|
|
|
2019-10-10 19:35:32 +00:00
|
|
|
defp compose_query({:followers, %User{id: id}}, query) do
|
|
|
|
query
|
2019-05-08 14:34:36 +00:00
|
|
|
|> where([u], u.id != ^id)
|
2019-10-10 19:35:32 +00:00
|
|
|
|> join(:inner, [u], r in FollowingRelationship,
|
|
|
|
as: :relationships,
|
|
|
|
on: r.following_id == ^id and r.follower_id == u.id
|
|
|
|
)
|
2020-03-28 15:49:03 +00:00
|
|
|
|> where([relationships: r], r.state == ^:follow_accept)
|
2019-05-08 14:34:36 +00:00
|
|
|
end
|
|
|
|
|
2019-10-10 19:35:32 +00:00
|
|
|
defp compose_query({:friends, %User{id: id}}, query) do
|
|
|
|
query
|
2019-05-08 14:34:36 +00:00
|
|
|
|> where([u], u.id != ^id)
|
2019-10-10 19:35:32 +00:00
|
|
|
|> join(:inner, [u], r in FollowingRelationship,
|
|
|
|
as: :relationships,
|
|
|
|
on: r.following_id == u.id and r.follower_id == ^id
|
|
|
|
)
|
2020-03-28 15:49:03 +00:00
|
|
|
|> where([relationships: r], r.state == ^:follow_accept)
|
2019-05-08 14:34:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp compose_query({:recipients_from_activity, to}, query) do
|
2020-05-19 12:11:32 +00: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,
|
|
|
|
where: u.ap_id in ^to or u.id in subquery(following_query),
|
|
|
|
distinct: true
|
2019-10-10 19:35:32 +00:00
|
|
|
)
|
2019-05-08 14:34:36 +00:00
|
|
|
end
|
|
|
|
|
2019-07-09 17:36:35 +00: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
|
|
|
|
|
2019-05-08 14:34:36 +00:00
|
|
|
defp compose_query(_unsupported_param, query), do: query
|
|
|
|
|
|
|
|
defp location_query(query, local) do
|
|
|
|
where(query, [u], u.local == ^local)
|
|
|
|
|> where([u], not is_nil(u.nickname))
|
|
|
|
end
|
|
|
|
end
|