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.SuggestionController do
6 use Pleroma.Web, :controller
9 alias Pleroma.UserRelationship
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])
17 def open_api_operation(action) do
18 operation = String.to_existing_atom("#{action}_operation")
19 apply(__MODULE__, operation, [])
22 def index_operation do
23 %OpenApiSpex.Operation{
24 tags: ["Suggestions"],
25 summary: "Follow suggestions (Not implemented)",
26 operationId: "SuggestionController.index",
28 200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response()
33 def index2_operation do
34 %OpenApiSpex.Operation{
35 tags: ["Suggestions"],
36 summary: "Follow suggestions",
37 operationId: "SuggestionController.index2",
39 200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response()
44 def dismiss_operation do
45 %OpenApiSpex.Operation{
46 tags: ["Suggestions"],
47 summary: "Remove a suggestion",
48 operationId: "SuggestionController.dismiss",
50 OpenApiSpex.Operation.parameter(
53 %OpenApiSpex.Schema{type: :string},
59 200 => Pleroma.Web.ApiSpec.Helpers.empty_object_response()
64 @doc "GET /api/v1/suggestions"
65 def index(conn, params),
66 do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params)
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)
73 %{is_suggested: true, invisible: false, limit: limit}
75 |> where([u], u.id != ^user_id)
76 |> join(:left, [u], r in UserRelationship,
79 r.target_id == u.id and r.source_id == ^user_id and
80 r.relationship_type in [:block, :mute, :suggestion_dismiss]
82 |> where([relationships: r], is_nil(r.target_id))
85 render(conn, "index.json", %{
89 skip_visibility_check: true
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