Add specs for AccountController.mute and AccountController.unmute
authorEgor Kislitsyn <egor@kislitsyn.com>
Thu, 9 Apr 2020 14:28:14 +0000 (18:28 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Mon, 13 Apr 2020 14:17:08 +0000 (18:17 +0400)
lib/pleroma/web/api_spec/operations/account_operation.ex
lib/pleroma/web/api_spec/schemas/account_mute_request.ex [new file with mode: 0644]
lib/pleroma/web/mastodon_api/controllers/account_controller.ex
test/web/mastodon_api/controllers/account_controller_test.exs

index 8925ebefd9a6caf5e07cd659e1ba800bd5cc7b1a..62ae2eead3f4efaeb174e67462d376fea8d9ead5 100644 (file)
@@ -10,6 +10,7 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
   alias Pleroma.Web.ApiSpec.Schemas.Account
   alias Pleroma.Web.ApiSpec.Schemas.AccountCreateRequest
   alias Pleroma.Web.ApiSpec.Schemas.AccountCreateResponse
+  alias Pleroma.Web.ApiSpec.Schemas.AccountMuteRequest
   alias Pleroma.Web.ApiSpec.Schemas.AccountRelationship
   alias Pleroma.Web.ApiSpec.Schemas.AccountRelationshipsResponse
   alias Pleroma.Web.ApiSpec.Schemas.AccountsResponse
@@ -239,8 +240,44 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
     }
   end
 
-  def mute_operation, do: :ok
-  def unmute_operation, do: :ok
+  def mute_operation do
+    %Operation{
+      tags: ["accounts"],
+      summary: "Mute",
+      operationId: "AccountController.mute",
+      security: [%{"oAuth" => ["follow", "write:mutes"]}],
+      requestBody: Helpers.request_body("Parameters", AccountMuteRequest),
+      description:
+        "Mute the given account. Clients should filter statuses and notifications from this account, if received (e.g. due to a boost in the Home timeline).",
+      parameters: [
+        %Reference{"$ref": "#/components/parameters/accountIdOrNickname"},
+        Operation.parameter(
+          :notifications,
+          :query,
+          %Schema{allOf: [BooleanLike], default: true},
+          "Mute notifications in addition to statuses? Defaults to `true`."
+        )
+      ],
+      responses: %{
+        200 => Operation.response("Relationship", "application/json", AccountRelationship)
+      }
+    }
+  end
+
+  def unmute_operation do
+    %Operation{
+      tags: ["accounts"],
+      summary: "Unmute",
+      operationId: "AccountController.unmute",
+      security: [%{"oAuth" => ["follow", "write:mutes"]}],
+      description: "Unmute the given account.",
+      parameters: [%Reference{"$ref": "#/components/parameters/accountIdOrNickname"}],
+      responses: %{
+        200 => Operation.response("Relationship", "application/json", AccountRelationship)
+      }
+    }
+  end
+
   def block_operation, do: :ok
   def unblock_operation, do: :ok
   def follows_operation, do: :ok
diff --git a/lib/pleroma/web/api_spec/schemas/account_mute_request.ex b/lib/pleroma/web/api_spec/schemas/account_mute_request.ex
new file mode 100644 (file)
index 0000000..a61f6d0
--- /dev/null
@@ -0,0 +1,24 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ApiSpec.Schemas.AccountMuteRequest do
+  alias OpenApiSpex.Schema
+  require OpenApiSpex
+
+  OpenApiSpex.schema(%{
+    title: "AccountMuteRequest",
+    description: "POST body for muting an account",
+    type: :object,
+    properties: %{
+      notifications: %Schema{
+        type: :boolean,
+        description: "Mute notifications in addition to statuses? Defaults to true.",
+        default: true
+      }
+    },
+    example: %{
+      "notifications" => true
+    }
+  })
+end
index 1ecce292871f5ddebd458f5d3909a255d1ff614d..9aba2e094f6237406b909d1d79d4525762a4c6b3 100644 (file)
@@ -94,7 +94,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
            :following,
            :lists,
            :follow,
-           :unfollow
+           :unfollow,
+           :mute,
+           :unmute
          ]
   )
 
@@ -359,10 +361,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
   end
 
   @doc "POST /api/v1/accounts/:id/mute"
-  def mute(%{assigns: %{user: muter, account: muted}} = conn, params) do
-    notifications? = params |> Map.get("notifications", true) |> truthy_param?()
-
-    with {:ok, _user_relationships} <- User.mute(muter, muted, notifications?) do
+  def mute(%{assigns: %{user: muter, account: muted}, body_params: params} = conn, _params) do
+    with {:ok, _user_relationships} <- User.mute(muter, muted, params.notifications) do
       render(conn, "relationship.json", user: muter, target: muted)
     else
       {:error, message} -> json_response(conn, :forbidden, %{error: message})
index d56e7fb4af1bde1e40c1fd56451f4e2bdc9e64e5..91d4685cb68b030e2d7cdb780d7354c61dcf0d1a 100644 (file)
@@ -751,32 +751,41 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
     test "with notifications", %{conn: conn} do
       other_user = insert(:user)
 
-      ret_conn = post(conn, "/api/v1/accounts/#{other_user.id}/mute")
+      ret_conn =
+        conn
+        |> put_req_header("content-type", "application/json")
+        |> post("/api/v1/accounts/#{other_user.id}/mute")
 
       response = json_response(ret_conn, 200)
 
       assert %{"id" => _id, "muting" => true, "muting_notifications" => true} = response
+      assert_schema(response, "AccountRelationship", ApiSpec.spec())
 
       conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
 
       response = json_response(conn, 200)
       assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = response
+      assert_schema(response, "AccountRelationship", ApiSpec.spec())
     end
 
     test "without notifications", %{conn: conn} do
       other_user = insert(:user)
 
       ret_conn =
-        post(conn, "/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
+        conn
+        |> put_req_header("content-type", "multipart/form-data")
+        |> post("/api/v1/accounts/#{other_user.id}/mute", %{"notifications" => "false"})
 
       response = json_response(ret_conn, 200)
 
       assert %{"id" => _id, "muting" => true, "muting_notifications" => false} = response
+      assert_schema(response, "AccountRelationship", ApiSpec.spec())
 
       conn = post(conn, "/api/v1/accounts/#{other_user.id}/unmute")
 
       response = json_response(conn, 200)
       assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = response
+      assert_schema(response, "AccountRelationship", ApiSpec.spec())
     end
   end