3c5a07b7d4ce8e04c78145d5d8a354b5661c6b7e
[akkoma] / lib / pleroma / web / mastodon_api / controllers / suggestion_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.SuggestionController do
6 use Pleroma.Web, :controller
7 import Ecto.Query
8 alias Pleroma.User
9 alias Pleroma.UserRelationship
10
11 require Logger
12
13 plug(Pleroma.Web.ApiSpec.CastAndValidate)
14 plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["read"]} when action in [:index, :index2])
15 plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["write"]} when action in [:dismiss])
16
17 def open_api_operation(action) do
18 operation = String.to_existing_atom("#{action}_operation")
19 apply(__MODULE__, operation, [])
20 end
21
22 def index_operation do
23 %OpenApiSpex.Operation{
24 tags: ["Suggestions"],
25 summary: "Follow suggestions (Not implemented)",
26 operationId: "SuggestionController.index",
27 responses: %{
28 200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response()
29 }
30 }
31 end
32
33 def index2_operation do
34 %OpenApiSpex.Operation{
35 tags: ["Suggestions"],
36 summary: "Follow suggestions",
37 operationId: "SuggestionController.index2",
38 responses: %{
39 200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response()
40 }
41 }
42 end
43
44 def dismiss_operation do
45 %OpenApiSpex.Operation{
46 tags: ["Suggestions"],
47 summary: "Remove a suggestion",
48 operationId: "SuggestionController.dismiss",
49 parameters: [
50 OpenApiSpex.Operation.parameter(
51 :account_id,
52 :path,
53 %OpenApiSpex.Schema{type: :string},
54 "Account to dismiss",
55 required: true
56 )
57 ],
58 responses: %{
59 200 => Pleroma.Web.ApiSpec.Helpers.empty_object_response()
60 }
61 }
62 end
63
64 @doc "GET /api/v1/suggestions"
65 def index(conn, params),
66 do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params)
67
68 @doc "GET /api/v2/suggestions"
69 def index2(%{assigns: %{user: %{id: user_id} = user}} = conn, params) do
70 limit = Map.get(params, :limit, 40) |> min(80)
71
72 users =
73 %{is_suggested: true, invisible: false, limit: limit}
74 |> User.Query.build()
75 |> where([u], u.id != ^user_id)
76 |> join(:left, [u], r in UserRelationship,
77 as: :relationships,
78 on:
79 r.target_id == u.id and r.source_id == ^user_id and
80 r.relationship_type in [:block, :mute, :suggestion_dismiss]
81 )
82 |> where([relationships: r], is_nil(r.target_id))
83 |> Pleroma.Repo.all()
84
85 render(conn, "index.json", %{
86 users: users,
87 source: :staff,
88 for: user,
89 skip_visibility_check: true
90 })
91 end
92
93 @doc "DELETE /api/v1/suggestions/:account_id"
94 def dismiss(%{assigns: %{user: source}} = conn, %{account_id: user_id}) do
95 with %User{} = target <- User.get_cached_by_id(user_id),
96 {:ok, _} <- UserRelationship.create(:suggestion_dismiss, source, target) do
97 json(conn, %{})
98 end
99 end
100 end