v2 Suggestions: dismiss a suggestion
authorAlex Gleason <alex@alexgleason.me>
Sat, 27 Nov 2021 02:19:29 +0000 (20:19 -0600)
committerAlex Gleason <alex@alexgleason.me>
Sat, 27 Nov 2021 02:19:29 +0000 (20:19 -0600)
lib/pleroma/ecto_enums.ex
lib/pleroma/web/mastodon_api/controllers/suggestion_controller.ex
lib/pleroma/web/router.ex
test/pleroma/web/admin_api/controllers/user_controller_test.exs
test/pleroma/web/mastodon_api/controllers/suggestion_controller_test.exs

index 2a9addabcecff4553bff21c483e9db83c8beb270..0e3e1e5deaee1762268239d5615699b8fa7a6199 100644 (file)
@@ -9,7 +9,8 @@ defenum(Pleroma.UserRelationship.Type,
   mute: 2,
   reblog_mute: 3,
   notification_mute: 4,
-  inverse_subscription: 5
+  inverse_subscription: 5,
+  suggestion_dismiss: 6
 )
 
 defenum(Pleroma.FollowingRelationship.State,
index 4f92c1f46e610c7fbed0201f85a9071380a009f3..4ebfc737c74a0d4892e985e2d9cb6f56bd99582b 100644 (file)
@@ -5,11 +5,13 @@
 defmodule Pleroma.Web.MastodonAPI.SuggestionController do
   use Pleroma.Web, :controller
   alias Pleroma.User
+  alias Pleroma.UserRelationship
 
   require Logger
 
   plug(Pleroma.Web.ApiSpec.CastAndValidate)
   plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["read"]} when action in [:index, :index2])
+  plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["write"]} when action in [:dismiss])
 
   def open_api_operation(action) do
     operation = String.to_existing_atom("#{action}_operation")
@@ -38,6 +40,26 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do
     }
   end
 
+  def dismiss_operation do
+    %OpenApiSpex.Operation{
+      tags: ["Suggestions"],
+      summary: "Remove a suggestion",
+      operationId: "SuggestionController.dismiss",
+      parameters: [
+        OpenApiSpex.Operation.parameter(
+          :account_id,
+          :path,
+          %OpenApiSpex.Schema{type: :string},
+          "Account to dismiss",
+          required: true
+        )
+      ],
+      responses: %{
+        200 => Pleroma.Web.ApiSpec.Helpers.empty_object_response()
+      }
+    }
+  end
+
   @doc "GET /api/v1/suggestions"
   def index(conn, params),
     do: Pleroma.Web.MastodonAPI.MastodonAPIController.empty_array(conn, params)
@@ -53,4 +75,12 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionController do
 
     render(conn, "index.json", %{users: users, source: :staff, for: user})
   end
+
+  @doc "DELETE /api/v1/suggestions/:account_id"
+  def dismiss(%{assigns: %{user: source}} = conn, %{account_id: user_id}) do
+    with %User{} = target <- User.get_cached_by_id(user_id),
+         {:ok, _} <- UserRelationship.create(:suggestion_dismiss, source, target) do
+      json(conn, %{})
+    end
+  end
 end
index 1f51bf45633d898a8b9a0fe43b39766f7e462b95..5d3a17b987f829f9ea0291457678bb5e0eef30a7 100644 (file)
@@ -539,6 +539,7 @@ defmodule Pleroma.Web.Router do
     delete("/push/subscription", SubscriptionController, :delete)
 
     get("/suggestions", SuggestionController, :index)
+    delete("/suggestions/:account_id", SuggestionController, :dismiss)
 
     get("/timelines/home", TimelineController, :home)
     get("/timelines/direct", TimelineController, :direct)
index df13f00e6f13adba05fcdb287dac8eb5d192a938..1580ca448895df23ccb14ab0b31ab4478773dbdd 100644 (file)
@@ -894,9 +894,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
     log_entry = Repo.one(ModerationLog)
 
     assert ModerationLog.get_log_entry_message(log_entry) ==
-             "@#{admin.nickname} added suggested users: @#{user1.nickname}, @#{
-               user2.nickname
-             }"
+             "@#{admin.nickname} added suggested users: @#{user1.nickname}, @#{user2.nickname}"
   end
 
   test "PATCH /api/pleroma/admin/users/unsuggest", %{admin: admin, conn: conn} do
@@ -920,9 +918,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
     log_entry = Repo.one(ModerationLog)
 
     assert ModerationLog.get_log_entry_message(log_entry) ==
-             "@#{admin.nickname} removed suggested users: @#{user1.nickname}, @#{
-               user2.nickname
-             }"
+             "@#{admin.nickname} removed suggested users: @#{user1.nickname}, @#{user2.nickname}"
   end
 
   test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admin, conn: conn} do
index 407063fa15fec068a62d10eb9eda3227a627aaec..803a38c676eecd49c032cd4dffd8037223b83a3d 100644 (file)
@@ -4,9 +4,10 @@
 
 defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do
   use Pleroma.Web.ConnCase, async: true
+  alias Pleroma.UserRelationship
   import Pleroma.Factory
 
-  setup do: oauth_access(["read"])
+  setup do: oauth_access(["read", "write"])
 
   test "returns empty result", %{conn: conn} do
     res =
@@ -27,4 +28,16 @@ defmodule Pleroma.Web.MastodonAPI.SuggestionControllerTest do
 
     assert [%{"source" => "staff", "account" => %{"id" => ^user_id}}] = res
   end
+
+  test "dismiss suggestion", %{conn: conn, user: source} do
+    target = insert(:user, is_suggested: true)
+
+    res =
+      conn
+      |> delete("/api/v1/suggestions/#{target.id}")
+      |> json_response_and_validate_schema(200)
+
+    assert res == %{}
+    assert UserRelationship.exists?(:suggestion_dismiss, source, target)
+  end
 end