First
[anni] / lib / pleroma / web / mastodon_api / controllers / suggestion_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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.FollowingRelationship
9   alias Pleroma.User
10   alias Pleroma.UserRelationship
11
12   require Logger
13
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])
17
18   def open_api_operation(action) do
19     operation = String.to_existing_atom("#{action}_operation")
20     apply(__MODULE__, operation, [])
21   end
22
23   def index_operation do
24     %OpenApiSpex.Operation{
25       tags: ["Suggestions"],
26       summary: "Follow suggestions (Not implemented)",
27       operationId: "SuggestionController.index",
28       responses: %{
29         200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response()
30       }
31     }
32   end
33
34   def index2_operation do
35     %OpenApiSpex.Operation{
36       tags: ["Suggestions"],
37       summary: "Follow suggestions",
38       operationId: "SuggestionController.index2",
39       responses: %{
40         200 => Pleroma.Web.ApiSpec.Helpers.empty_array_response()
41       }
42     }
43   end
44
45   def dismiss_operation do
46     %OpenApiSpex.Operation{
47       tags: ["Suggestions"],
48       summary: "Remove a suggestion",
49       operationId: "SuggestionController.dismiss",
50       parameters: [
51         OpenApiSpex.Operation.parameter(
52           :account_id,
53           :path,
54           %OpenApiSpex.Schema{type: :string},
55           "Account to dismiss",
56           required: true
57         )
58       ],
59       responses: %{
60         200 => Pleroma.Web.ApiSpec.Helpers.empty_object_response()
61       }
62     }
63   end
64
65   @doc "GET /api/v1/suggestions"
66   def index(conn, params),
67     do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params)
68
69   @doc "GET /api/v2/suggestions"
70   def index2(%{assigns: %{user: user}} = conn, params) do
71     limit = Map.get(params, :limit, 40) |> min(80)
72
73     users =
74       %{is_suggested: true, invisible: false, limit: limit}
75       |> User.Query.build()
76       |> exclude_user(user)
77       |> exclude_relationships(user, [:block, :mute, :suggestion_dismiss])
78       |> exclude_following(user)
79       |> Pleroma.Repo.all()
80
81     render(conn, "index.json", %{
82       users: users,
83       source: :staff,
84       for: user,
85       skip_visibility_check: true
86     })
87   end
88
89   defp exclude_user(query, %User{id: user_id}) do
90     where(query, [u], u.id != ^user_id)
91   end
92
93   defp exclude_relationships(query, %User{id: user_id}, relationship_types) do
94     query
95     |> join(:left, [u], r in UserRelationship,
96       as: :user_relationships,
97       on:
98         r.target_id == u.id and r.source_id == ^user_id and
99           r.relationship_type in ^relationship_types
100     )
101     |> where([user_relationships: r], is_nil(r.target_id))
102   end
103
104   defp exclude_following(query, %User{id: user_id}) do
105     query
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
109     )
110     |> where([following_relationships: r], is_nil(r.following_id))
111   end
112
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
117       json(conn, %{})
118     end
119   end
120 end