1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.DirectoryController do
6 use Pleroma.Web, :controller
9 alias Pleroma.Pagination
11 alias Pleroma.UserRelationship
12 alias Pleroma.Web.MastodonAPI.AccountView
16 plug(Pleroma.Web.ApiSpec.CastAndValidate)
18 plug(:skip_auth when action == "index")
20 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.DirectoryOperation
22 @doc "GET /api/v1/directory"
23 def index(%{assigns: %{user: user}} = conn, params) do
24 with true <- Pleroma.Config.get([:instance, :profile_directory]) do
25 limit = Map.get(params, :limit, 20) |> min(80)
28 User.Query.build(%{is_discoverable: true, invisible: false, limit: limit})
29 |> order_by_creation_date(params)
30 |> exclude_remote(params)
32 |> exclude_relationships(user, [:block, :mute])
33 |> Pagination.fetch_paginated(params, :offset)
36 |> put_view(AccountView)
37 |> render("index.json", for: user, users: users, as: :user)
43 defp order_by_creation_date(query, %{order: "new"}) do
47 defp order_by_creation_date(query, _params) do
49 |> order_by([u], desc_nulls_last: u.last_status_at)
52 defp exclude_remote(query, %{local: true}) do
53 where(query, [u], u.local == true)
56 defp exclude_remote(query, _params) do
60 defp exclude_user(query, %User{id: user_id}) do
61 where(query, [u], u.id != ^user_id)
64 defp exclude_user(query, _user) do
68 defp exclude_relationships(query, %User{id: user_id}, relationship_types) do
70 |> join(:left, [u], r in UserRelationship,
71 as: :user_relationships,
73 r.target_id == u.id and r.source_id == ^user_id and
74 r.relationship_type in ^relationship_types
76 |> where([user_relationships: r], is_nil(r.target_id))
79 defp exclude_relationships(query, _user, _relationship_types) do