1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
8 alias Pleroma.FollowingRelationship
10 alias Pleroma.UserRelationship
14 plug(Pleroma.Web.ApiSpec.CastAndValidate)
15 plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["read"]} when action in [:index, :index2])
16 plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["write"]} when action in [:dismiss])
18 def open_api_operation(action) do
19 operation = String.to_existing_atom("#{action}_operation")
20 apply(__MODULE__, operation, [])
23 def index_operation do
24 %OpenApiSpex.Operation{
25 tags: ["Suggestions"],
26 summary: "Follow suggestions (Not implemented)",
27 operationId: "SuggestionController.index",
29 200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response()
34 def index2_operation do
35 %OpenApiSpex.Operation{
36 tags: ["Suggestions"],
37 summary: "Follow suggestions",
38 operationId: "SuggestionController.index2",
40 200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response()
45 def dismiss_operation do
46 %OpenApiSpex.Operation{
47 tags: ["Suggestions"],
48 summary: "Remove a suggestion",
49 operationId: "SuggestionController.dismiss",
51 OpenApiSpex.Operation.parameter(
54 %OpenApiSpex.Schema{type: :string},
60 200 => Pleroma.Web.ApiSpec.Helpers.empty_object_response()
65 @doc "GET /api/v1/suggestions"
66 def index(conn, params),
67 do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params)
69 @doc "GET /api/v2/suggestions"
70 def index2(%{assigns: %{user: user}} = conn, params) do
71 limit = Map.get(params, :limit, 40) |> min(80)
74 %{is_suggested: true, invisible: false, limit: limit}
77 |> exclude_relationships(user, [:block, :mute, :suggestion_dismiss])
78 |> exclude_following(user)
81 render(conn, "index.json", %{
85 skip_visibility_check: true
89 defp exclude_user(query, %User{id: user_id}) do
90 where(query, [u], u.id != ^user_id)
93 defp exclude_relationships(query, %User{id: user_id}, relationship_types) do
95 |> join(:left, [u], r in UserRelationship,
96 as: :user_relationships,
98 r.target_id == u.id and r.source_id == ^user_id and
99 r.relationship_type in ^relationship_types
101 |> where([user_relationships: r], is_nil(r.target_id))
104 defp exclude_following(query, %User{id: user_id}) do
106 |> join(:left, [u], r in FollowingRelationship,
107 as: :following_relationships,
108 on: r.following_id == u.id and r.follower_id == ^user_id and r.state == :follow_accept
110 |> where([following_relationships: r], is_nil(r.following_id))
113 @doc "DELETE /api/v1/suggestions/:account_id"
114 def dismiss(%{assigns: %{user: source}} = conn, %{account_id: user_id}) do
115 with %User{} = target <- User.get_cached_by_id(user_id),
116 {:ok, _} <- UserRelationship.create(:suggestion_dismiss, source, target) do