Add ability to set a default post expiry (#321)
[akkoma] / lib / pleroma / web / mastodon_api / controllers / directory_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.DirectoryController do
6 use Pleroma.Web, :controller
7
8 import Ecto.Query
9 alias Pleroma.Pagination
10 alias Pleroma.User
11 alias Pleroma.UserRelationship
12 alias Pleroma.Web.MastodonAPI.AccountView
13
14 require Logger
15
16 plug(Pleroma.Web.ApiSpec.CastAndValidate)
17
18 plug(:skip_auth when action == "index")
19
20 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.DirectoryOperation
21
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)
26
27 users =
28 User.Query.build(%{is_discoverable: true, invisible: false, limit: limit})
29 |> order_by_creation_date(params)
30 |> exclude_remote(params)
31 |> exclude_user(user)
32 |> exclude_relationships(user, [:block, :mute])
33 |> Pagination.fetch_paginated(params, :offset)
34
35 conn
36 |> put_view(AccountView)
37 |> render("index.json", for: user, users: users, as: :user)
38 else
39 _ -> json(conn, [])
40 end
41 end
42
43 defp order_by_creation_date(query, %{order: "new"}) do
44 query
45 end
46
47 defp order_by_creation_date(query, _params) do
48 query
49 |> order_by([u], desc_nulls_last: u.last_status_at)
50 end
51
52 defp exclude_remote(query, %{local: true}) do
53 where(query, [u], u.local == true)
54 end
55
56 defp exclude_remote(query, _params) do
57 query
58 end
59
60 defp exclude_user(query, %User{id: user_id}) do
61 where(query, [u], u.id != ^user_id)
62 end
63
64 defp exclude_user(query, _user) do
65 query
66 end
67
68 defp exclude_relationships(query, %User{id: user_id}, relationship_types) do
69 query
70 |> join(:left, [u], r in UserRelationship,
71 as: :user_relationships,
72 on:
73 r.target_id == u.id and r.source_id == ^user_id and
74 r.relationship_type in ^relationship_types
75 )
76 |> where([user_relationships: r], is_nil(r.target_id))
77 end
78
79 defp exclude_relationships(query, _user, _relationship_types) do
80 query
81 end
82 end