ChatController: Add GET /chats/:id
authorlain <lain@soykaf.club>
Sun, 10 May 2020 11:26:14 +0000 (13:26 +0200)
committerlain <lain@soykaf.club>
Sun, 10 May 2020 11:26:14 +0000 (13:26 +0200)
lib/pleroma/web/api_spec/operations/chat_operation.ex
lib/pleroma/web/api_spec/schemas/chat.ex
lib/pleroma/web/api_spec/schemas/chat_message.ex
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
lib/pleroma/web/router.ex
test/web/pleroma_api/controllers/chat_controller_test.exs

index 16d3d5e22bbbc79432ac41453a1b2246ededdf40..fe6c2f52f3f568aa53dad02f269adeafeb2743fe 100644 (file)
@@ -38,6 +38,37 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
     }
   end
 
+  def show_operation do
+    %Operation{
+      tags: ["chat"],
+      summary: "Create a chat",
+      operationId: "ChatController.show",
+      parameters: [
+        Operation.parameter(
+          :id,
+          :path,
+          :string,
+          "The id of the chat",
+          required: true,
+          example: "1234"
+        )
+      ],
+      responses: %{
+        200 =>
+          Operation.response(
+            "The existing chat",
+            "application/json",
+            Chat
+          )
+      },
+      security: [
+        %{
+          "oAuth" => ["read"]
+        }
+      ]
+    }
+  end
+
   def create_operation do
     %Operation{
       tags: ["chat"],
index 8aaa4a79203b0fba8ac10980f23982a2fca42ca2..c6ec07c885c0e3435b893f37733902c01483589e 100644 (file)
@@ -16,7 +16,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Chat do
       id: %Schema{type: :string},
       account: %Schema{type: :object},
       unread: %Schema{type: :integer},
-      last_message: %Schema{type: ChatMessage, nullable: true}
+      last_message: ChatMessage
     },
     example: %{
       "account" => %{
index 89e062dddd09f480b9a4f0f543b80e551efcac0e..6e8f1a10ad87a499db5d24bc0a9654e6622bdf77 100644 (file)
@@ -10,6 +10,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.ChatMessage do
   OpenApiSpex.schema(%{
     title: "ChatMessage",
     description: "Response schema for a ChatMessage",
+    nullable: true,
     type: :object,
     properties: %{
       id: %Schema{type: :string},
index 1ef3477c8874bc01753b26a7bfb32d7195414217..04f136dcd77009bd92d312157966c71c735a5c48 100644 (file)
@@ -27,7 +27,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
 
   plug(
     OAuthScopesPlug,
-    %{scopes: ["read:statuses"]} when action in [:messages, :index]
+    %{scopes: ["read:statuses"]} when action in [:messages, :index, :show]
   )
 
   plug(OpenApiSpex.Plug.CastAndValidate, render_error: Pleroma.Web.ApiSpec.RenderError)
@@ -100,4 +100,12 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
       |> render("show.json", chat: chat)
     end
   end
+
+  def show(%{assigns: %{user: user}} = conn, params) do
+    with %Chat{} = chat <- Repo.get_by(Chat, user_id: user.id, id: params[:id]) do
+      conn
+      |> put_view(ChatView)
+      |> render("show.json", chat: chat)
+    end
+  end
 end
index 4b264c43ec1bea39a6b6f2297925457e7f39d3fe..3b1834d97ff2e1c0c0390a841faf805b67e93b46 100644 (file)
@@ -307,6 +307,7 @@ defmodule Pleroma.Web.Router do
 
       post("/chats/by-account-id/:id", ChatController, :create)
       get("/chats", ChatController, :index)
+      get("/chats/:id", ChatController, :show)
       get("/chats/:id/messages", ChatController, :messages)
       post("/chats/:id/messages", ChatController, :post_chat_message)
       post("/chats/:id/read", ChatController, :mark_as_read)
index b4b73da90ec010d2f7955dc8ce5c0bb3d52d1e08..dda4f9e5b212d7a1005f7ead920c00bb559af883 100644 (file)
@@ -153,6 +153,23 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
     end
   end
 
+  describe "GET /api/v1/pleroma/chats/:id" do
+    setup do: oauth_access(["read:statuses"])
+
+    test "it returns a chat", %{conn: conn, user: user} do
+      other_user = insert(:user)
+
+      {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+
+      result =
+        conn
+        |> get("/api/v1/pleroma/chats/#{chat.id}")
+        |> json_response_and_validate_schema(200)
+
+      assert result["id"] == to_string(chat.id)
+    end
+  end
+
   describe "GET /api/v1/pleroma/chats" do
     setup do: oauth_access(["read:statuses"])