field(:local, :boolean, default: true)
field(:follower_address, :string)
field(:search_rank, :float, virtual: true)
+ field(:search_type, :integer, virtual: true)
field(:tags, {:array, :string}, default: [])
field(:bookmarks, {:array, :string}, default: [])
field(:last_refreshed_at, :naive_datetime_usec)
timestamps()
end
- def auth_active?(%User{local: false}), do: true
-
- def auth_active?(%User{info: %User.Info{confirmation_pending: false}}), do: true
-
def auth_active?(%User{info: %User.Info{confirmation_pending: true}}),
do: !Pleroma.Config.get([:instance, :account_activation_required])
- def auth_active?(_), do: false
+ def auth_active?(%User{}), do: true
def visible_for?(user, for_user \\ nil)
def superuser?(%User{local: true, info: %User.Info{is_moderator: true}}), do: true
def superuser?(_), do: false
- def avatar_url(user) do
+ def avatar_url(user, options \\ []) do
case user.avatar do
%{"url" => [%{"href" => href} | _]} -> href
- _ -> "#{Web.base_url()}/images/avi.png"
+ _ -> !options[:no_default] && "#{Web.base_url()}/images/avi.png"
end
end
- def banner_url(user) do
+ def banner_url(user, options \\ []) do
case user.info.banner do
%{"url" => [%{"href" => href} | _]} -> href
- _ -> "#{Web.base_url()}/images/banner.png"
+ _ -> !options[:no_default] && "#{Web.base_url()}/images/banner.png"
end
end
Repo.all(query)
end
- @spec search_for_admin(%{
- local: boolean(),
- page: number(),
- page_size: number()
- }) :: {:ok, [Pleroma.User.t()], number()}
- def search_for_admin(%{query: nil, local: local, page: page, page_size: page_size}) do
- query =
- from(u in User, order_by: u.nickname)
- |> maybe_local_user_query(local)
-
- paginated_query =
- query
- |> paginate(page, page_size)
-
- count =
- query
- |> Repo.aggregate(:count, :id)
-
- {:ok, Repo.all(paginated_query), count}
- end
-
- @spec search_for_admin(%{
- query: binary(),
- local: boolean(),
- page: number(),
- page_size: number()
- }) :: {:ok, [Pleroma.User.t()], number()}
- def search_for_admin(%{
- query: term,
- local: local,
- page: page,
- page_size: page_size
- }) do
- maybe_local_query = User |> maybe_local_user_query(local)
-
- search_query = from(u in maybe_local_query, where: ilike(u.nickname, ^"%#{term}%"))
- count = search_query |> Repo.aggregate(:count, :id)
-
- results =
- search_query
- |> paginate(page, page_size)
- |> Repo.all()
-
- {:ok, results, count}
- end
-
def search(query, resolve \\ false, for_user \\ nil) do
# Strip the beginning @ off if there is a query
query = String.trim_leading(query, "@")
if resolve, do: get_or_fetch(query)
- fts_results = do_search(fts_search_subquery(query), for_user)
-
- {:ok, trigram_results} =
+ {:ok, results} =
Repo.transaction(fn ->
Ecto.Adapters.SQL.query(Repo, "select set_limit(0.25)", [])
- do_search(trigram_search_subquery(query), for_user)
+ Repo.all(search_query(query, for_user))
end)
- Enum.uniq_by(fts_results ++ trigram_results, & &1.id)
+ results
end
- defp do_search(subquery, for_user, options \\ []) do
- q =
- from(
- s in subquery(subquery),
- order_by: [desc: s.search_rank],
- limit: ^(options[:limit] || 20)
- )
+ def search_query(query, for_user) do
+ fts_subquery = fts_search_subquery(query)
+ trigram_subquery = trigram_search_subquery(query)
+ union_query = from(s in trigram_subquery, union_all: ^fts_subquery)
+ distinct_query = from(s in subquery(union_query), order_by: s.search_type, distinct: s.id)
- results =
- q
- |> Repo.all()
- |> Enum.filter(&(&1.search_rank > 0))
+ from(s in subquery(boost_search_rank_query(distinct_query, for_user)),
+ order_by: [desc: s.search_rank],
+ limit: 20
+ )
+ end
+
+ defp boost_search_rank_query(query, nil), do: query
- boost_search_results(results, for_user)
+ defp boost_search_rank_query(query, for_user) do
+ friends_ids = get_friends_ids(for_user)
+ followers_ids = get_followers_ids(for_user)
+
+ from(u in subquery(query),
+ select_merge: %{
+ search_rank:
+ fragment(
+ """
+ CASE WHEN (?) THEN (?) * 1.3
+ WHEN (?) THEN (?) * 1.2
+ WHEN (?) THEN (?) * 1.1
+ ELSE (?) END
+ """,
+ u.id in ^friends_ids and u.id in ^followers_ids,
+ u.search_rank,
+ u.id in ^friends_ids,
+ u.search_rank,
+ u.id in ^followers_ids,
+ u.search_rank,
+ u.search_rank
+ )
+ }
+ )
end
defp fts_search_subquery(term, query \\ User) do
from(
u in query,
select_merge: %{
+ search_type: ^0,
search_rank:
fragment(
"""
from(
u in User,
select_merge: %{
+ # ^1 gives 'Postgrex expected a binary, got 1' for some weird reason
+ search_type: fragment("?", 1),
search_rank:
fragment(
"similarity(?, trim(? || ' ' || coalesce(?, '')))",
)
end
- defp boost_search_results(results, nil), do: results
-
- defp boost_search_results(results, for_user) do
- friends_ids = get_friends_ids(for_user)
- followers_ids = get_followers_ids(for_user)
-
- Enum.map(
- results,
- fn u ->
- search_rank_coef =
- cond do
- u.id in friends_ids ->
- 1.2
-
- u.id in followers_ids ->
- 1.1
-
- true ->
- 1
- end
-
- Map.put(u, :search_rank, u.search_rank * search_rank_coef)
- end
- )
- |> Enum.sort_by(&(-&1.search_rank))
- end
-
def blocks_import(%User{} = blocker, blocked_identifiers) when is_list(blocked_identifiers) do
Enum.map(
blocked_identifiers,
)
end
+ def maybe_external_user_query(query, external) do
+ if external, do: external_user_query(query), else: query
+ end
+
+ def external_user_query(query \\ User) do
+ from(
+ u in query,
+ where: u.local == false,
+ where: not is_nil(u.nickname)
+ )
+ end
+
+ def maybe_active_user_query(query, active) do
+ if active, do: active_user_query(query), else: query
+ end
+
+ def active_user_query(query \\ User) do
+ from(
+ u in query,
+ where: fragment("not (?->'deactivated' @> 'true')", u.info),
+ where: not is_nil(u.nickname)
+ )
+ end
+
+ def maybe_deactivated_user_query(query, deactivated) do
+ if deactivated, do: deactivated_user_query(query), else: query
+ end
+
+ def deactivated_user_query(query \\ User) do
+ from(
+ u in query,
+ where: fragment("(?->'deactivated' @> 'true')", u.info),
+ where: not is_nil(u.nickname)
+ )
+ end
+
def active_local_user_query do
from(
u in local_user_query(),
# Remove all relationships
{:ok, followers} = User.get_followers(user)
- followers
- |> Enum.each(fn follower -> User.unfollow(follower, user) end)
+ Enum.each(followers, fn follower -> User.unfollow(follower, user) end)
{:ok, friends} = User.get_friends(user)
- friends
- |> Enum.each(fn followed -> User.unfollow(user, followed) end)
+ Enum.each(friends, fn followed -> User.unfollow(user, followed) end)
- query = from(a in Activity, where: a.actor == ^user.ap_id)
+ delete_user_activities(user)
+ end
- Repo.all(query)
- |> Enum.each(fn activity ->
- case activity.data["type"] do
- "Create" ->
- ActivityPub.delete(Object.normalize(activity.data["object"]))
+ def delete_user_activities(%User{ap_id: ap_id} = user) do
+ Activity
+ |> where(actor: ^ap_id)
+ |> Activity.with_preloaded_object()
+ |> Repo.all()
+ |> Enum.each(fn
+ %{data: %{"type" => "Create"}} = activity ->
+ activity |> Object.normalize() |> ActivityPub.delete()
- # TODO: Do something with likes, follows, repeats.
- _ ->
- "Doing nothing"
- end
+ # TODO: Do something with likes, follows, repeats.
+ _ ->
+ "Doing nothing"
end)
{:ok, user}
# this is because we have synchronous follow APIs and need to simulate them
# with an async handshake
def wait_and_refresh(_, %User{local: true} = a, %User{local: true} = b) do
- with %User{} = a <- Repo.get(User, a.id),
- %User{} = b <- Repo.get(User, b.id) do
+ with %User{} = a <- User.get_by_id(a.id),
+ %User{} = b <- User.get_by_id(b.id) do
{:ok, a, b}
else
_e ->
def wait_and_refresh(timeout, %User{} = a, %User{} = b) do
with :ok <- :timer.sleep(timeout),
- %User{} = a <- Repo.get(User, a.id),
- %User{} = b <- Repo.get(User, b.id) do
+ %User{} = a <- User.get_by_id(a.id),
+ %User{} = b <- User.get_by_id(b.id) do
{:ok, a, b}
else
_e ->