Chat API: Align more to Pleroma/Mastodon API.
authorlain <lain@soykaf.club>
Mon, 27 Apr 2020 15:48:34 +0000 (17:48 +0200)
committerlain <lain@soykaf.club>
Mon, 27 Apr 2020 15:48:34 +0000 (17:48 +0200)
lib/pleroma/web/api_spec/operations/chat_operation.ex
lib/pleroma/web/api_spec/schemas/chat_message_response.ex
lib/pleroma/web/api_spec/schemas/chat_response.ex
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
lib/pleroma/web/pleroma_api/views/chat_message_view.ex
lib/pleroma/web/pleroma_api/views/chat_view.ex
lib/pleroma/web/router.ex
test/web/pleroma_api/controllers/chat_controller_test.exs
test/web/pleroma_api/views/chat_message_view_test.exs
test/web/pleroma_api/views/chat_view_test.exs

index 546bc4d9b9f38df90efd1d520dc0bb8ed37b5477..59539e8902c94365a97aa6c4b948ca45849f4e52 100644 (file)
@@ -23,12 +23,12 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
       operationId: "ChatController.create",
       parameters: [
         Operation.parameter(
-          :ap_id,
+          :id,
           :path,
           :string,
-          "The ActivityPub id of the recipient of this chat.",
+          "The account id of the recipient of this chat",
           required: true,
-          example: "https://lain.com/users/lain"
+          example: "someflakeid"
         )
       ],
       responses: %{
@@ -128,8 +128,7 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
       items: ChatResponse,
       example: [
         %{
-          "recipient" => "https://dontbulling.me/users/lain",
-          "recipient_account" => %{
+          "account" => %{
             "pleroma" => %{
               "is_admin" => false,
               "confirmation_pending" => false,
@@ -202,10 +201,10 @@ defmodule Pleroma.Web.ApiSpec.ChatOperation do
           "content" => "Check this out :firefox:",
           "id" => "13",
           "chat_id" => "1",
-          "actor" => "https://dontbulling.me/users/lain"
+          "actor_id" => "someflakeid"
         },
         %{
-          "actor" => "https://dontbulling.me/users/lain",
+          "actor_id" => "someflakeid",
           "content" => "Whats' up?",
           "id" => "12",
           "chat_id" => "1",
index 9459d210b5c82165c68c36cfe469535990732eb0..b7a662cbb9d5216cc4d6dc92c1bb21ff3d9c6f1e 100644 (file)
@@ -13,16 +13,14 @@ defmodule Pleroma.Web.ApiSpec.Schemas.ChatMessageResponse do
     type: :object,
     properties: %{
       id: %Schema{type: :string},
-      actor: %Schema{type: :string, description: "The ActivityPub id of the actor"},
-      actor_account_id: %Schema{type: :string, description: "The Mastodon API id of the actor"},
+      account_id: %Schema{type: :string, description: "The Mastodon API id of the actor"},
       chat_id: %Schema{type: :string},
       content: %Schema{type: :string},
       created_at: %Schema{type: :string, format: :datetime},
       emojis: %Schema{type: :array}
     },
     example: %{
-      "actor" => "https://dontbulling.me/users/lain",
-      "actor_account_id" => "someflakeid",
+      "account_id" => "someflakeid",
       "chat_id" => "1",
       "content" => "hey you again",
       "created_at" => "2020-04-21T15:06:45.000Z",
index a80f4d1737367f2330685606ec93e3e73a6b6d21..aa435165daa54d6d173638ea9195756df351b39c 100644 (file)
@@ -12,15 +12,12 @@ defmodule Pleroma.Web.ApiSpec.Schemas.ChatResponse do
     description: "Response schema for a Chat",
     type: :object,
     properties: %{
-      id: %Schema{type: :string},
-      recipient: %Schema{type: :string},
-      # TODO: Make this reference the account structure.
-      recipient_account: %Schema{type: :object},
-      unread: %Schema{type: :integer}
+      id: %Schema{type: :string, nullable: false},
+      account: %Schema{type: :object, nullable: false},
+      unread: %Schema{type: :integer, nullable: false}
     },
     example: %{
-      "recipient" => "https://dontbulling.me/users/lain",
-      "recipient_account" => %{
+      "account" => %{
         "pleroma" => %{
           "is_admin" => false,
           "confirmation_pending" => false,
index 771ad621797bbc206e113330718f80def779c853..8654f4295926a64b420571a9a8b3fe98df3ea45f 100644 (file)
@@ -99,9 +99,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
   end
 
   def create(%{assigns: %{user: user}} = conn, params) do
-    recipient = params[:ap_id]
-
-    with {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
+    with %User{ap_id: recipient} <- User.get_by_id(params[:id]),
+         {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
       conn
       |> put_view(ChatView)
       |> render("show.json", chat: chat)
index 5b740cc44c08ef7b4adde41926611d86c3c1d4eb..28f12d9b066921f47d8af8fdda0846f7cfd349c2 100644 (file)
@@ -21,8 +21,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageView do
       id: id |> to_string(),
       content: chat_message["content"],
       chat_id: chat_id |> to_string(),
-      actor: chat_message["actor"],
-      actor_account_id: User.get_cached_by_ap_id(chat_message["actor"]).id,
+      account_id: User.get_cached_by_ap_id(chat_message["actor"]).id,
       created_at: Utils.to_masto_date(chat_message["published"]),
       emojis: StatusView.build_emojis(chat_message["emoji"])
     }
index 1e9ef4356d9ca473282940146e75a29bf6af346b..bc3af5ef511853f9dfe10986b0d27e1090b7e062 100644 (file)
@@ -14,8 +14,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatView do
 
     %{
       id: chat.id |> to_string(),
-      recipient: chat.recipient,
-      recipient_account: AccountView.render("show.json", Map.put(opts, :user, recipient)),
+      account: AccountView.render("show.json", Map.put(opts, :user, recipient)),
       unread: chat.unread
     }
   end
index 0c56318ee8c8528c238d89ebec674baef551f45d..aad2e3b98ab0ff8ded7872b27d67f7d0c267b56c 100644 (file)
@@ -275,7 +275,7 @@ defmodule Pleroma.Web.Router do
     scope [] do
       pipe_through(:authenticated_api)
 
-      post("/chats/by-ap-id/:ap_id", ChatController, :create)
+      post("/chats/by-account-id/:id", ChatController, :create)
       get("/chats", ChatController, :index)
       get("/chats/:id/messages", ChatController, :messages)
       post("/chats/:id/messages", ChatController, :post_chat_message)
index 84d7b543e5b84a84adb0edae29458093e4f4efff..b1044574b91be3a20466dffa7ae9d267b476fa7e 100644 (file)
@@ -88,7 +88,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
     end
   end
 
-  describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
+  describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
     setup do: oauth_access(["write:statuses"])
 
     test "it creates or returns a chat", %{conn: conn} do
@@ -96,7 +96,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       result =
         conn
-        |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
+        |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
         |> json_response_and_validate_schema(200)
 
       assert result["id"]
index 7e3aeefab1a724ffff685af3f23ecc8e7c643f3e..5c4c8b0d59839f58406f7f3e45a0e5011aea0761 100644 (file)
@@ -25,8 +25,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageViewTest do
 
     assert chat_message[:id] == object.id |> to_string()
     assert chat_message[:content] == "kippis :firefox:"
-    assert chat_message[:actor] == user.ap_id
-    assert chat_message[:actor_account_id] == user.id
+    assert chat_message[:account_id] == user.id
     assert chat_message[:chat_id]
     assert chat_message[:created_at]
     assert match?([%{shortcode: "firefox"}], chat_message[:emojis])
@@ -39,8 +38,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageViewTest do
 
     assert chat_message_two[:id] == object.id |> to_string()
     assert chat_message_two[:content] == "gkgkgk"
-    assert chat_message_two[:actor] == recipient.ap_id
-    assert chat_message_two[:actor_account_id] == recipient.id
+    assert chat_message_two[:account_id] == recipient.id
     assert chat_message_two[:chat_id] == chat_message[:chat_id]
   end
 end
index 725da5ff80e8fd61ea73e4c4e6217800319460a2..1ac3483d115306a86a81112fa0b60dc3b7ec1875 100644 (file)
@@ -21,8 +21,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatViewTest do
 
     assert represented_chat == %{
              id: "#{chat.id}",
-             recipient: recipient.ap_id,
-             recipient_account: AccountView.render("show.json", user: recipient),
+             account: AccountView.render("show.json", user: recipient),
              unread: 0
            }
   end