Chat API: Align more to Pleroma/Mastodon API.

This commit is contained in:
lain 2020-04-27 17:48:34 +02:00
parent 49e673dfea
commit ad82a216ff
10 changed files with 22 additions and 34 deletions

View File

@ -23,12 +23,12 @@ def create_operation do
operationId: "ChatController.create", operationId: "ChatController.create",
parameters: [ parameters: [
Operation.parameter( Operation.parameter(
:ap_id, :id,
:path, :path,
:string, :string,
"The ActivityPub id of the recipient of this chat.", "The account id of the recipient of this chat",
required: true, required: true,
example: "https://lain.com/users/lain" example: "someflakeid"
) )
], ],
responses: %{ responses: %{
@ -128,8 +128,7 @@ def chats_response do
items: ChatResponse, items: ChatResponse,
example: [ example: [
%{ %{
"recipient" => "https://dontbulling.me/users/lain", "account" => %{
"recipient_account" => %{
"pleroma" => %{ "pleroma" => %{
"is_admin" => false, "is_admin" => false,
"confirmation_pending" => false, "confirmation_pending" => false,
@ -202,10 +201,10 @@ def chat_messages_response do
"content" => "Check this out :firefox:", "content" => "Check this out :firefox:",
"id" => "13", "id" => "13",
"chat_id" => "1", "chat_id" => "1",
"actor" => "https://dontbulling.me/users/lain" "actor_id" => "someflakeid"
}, },
%{ %{
"actor" => "https://dontbulling.me/users/lain", "actor_id" => "someflakeid",
"content" => "Whats' up?", "content" => "Whats' up?",
"id" => "12", "id" => "12",
"chat_id" => "1", "chat_id" => "1",

View File

@ -13,16 +13,14 @@ defmodule Pleroma.Web.ApiSpec.Schemas.ChatMessageResponse do
type: :object, type: :object,
properties: %{ properties: %{
id: %Schema{type: :string}, id: %Schema{type: :string},
actor: %Schema{type: :string, description: "The ActivityPub id of the actor"}, account_id: %Schema{type: :string, description: "The Mastodon API id of the actor"},
actor_account_id: %Schema{type: :string, description: "The Mastodon API id of the actor"},
chat_id: %Schema{type: :string}, chat_id: %Schema{type: :string},
content: %Schema{type: :string}, content: %Schema{type: :string},
created_at: %Schema{type: :string, format: :datetime}, created_at: %Schema{type: :string, format: :datetime},
emojis: %Schema{type: :array} emojis: %Schema{type: :array}
}, },
example: %{ example: %{
"actor" => "https://dontbulling.me/users/lain", "account_id" => "someflakeid",
"actor_account_id" => "someflakeid",
"chat_id" => "1", "chat_id" => "1",
"content" => "hey you again", "content" => "hey you again",
"created_at" => "2020-04-21T15:06:45.000Z", "created_at" => "2020-04-21T15:06:45.000Z",

View File

@ -12,15 +12,12 @@ defmodule Pleroma.Web.ApiSpec.Schemas.ChatResponse do
description: "Response schema for a Chat", description: "Response schema for a Chat",
type: :object, type: :object,
properties: %{ properties: %{
id: %Schema{type: :string}, id: %Schema{type: :string, nullable: false},
recipient: %Schema{type: :string}, account: %Schema{type: :object, nullable: false},
# TODO: Make this reference the account structure. unread: %Schema{type: :integer, nullable: false}
recipient_account: %Schema{type: :object},
unread: %Schema{type: :integer}
}, },
example: %{ example: %{
"recipient" => "https://dontbulling.me/users/lain", "account" => %{
"recipient_account" => %{
"pleroma" => %{ "pleroma" => %{
"is_admin" => false, "is_admin" => false,
"confirmation_pending" => false, "confirmation_pending" => false,

View File

@ -99,9 +99,8 @@ def index(%{assigns: %{user: %{id: user_id}}} = conn, params) do
end end
def create(%{assigns: %{user: user}} = conn, params) do def create(%{assigns: %{user: user}} = conn, params) do
recipient = params[:ap_id] with %User{ap_id: recipient} <- User.get_by_id(params[:id]),
{:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
with {:ok, %Chat{} = chat} <- Chat.get_or_create(user.id, recipient) do
conn conn
|> put_view(ChatView) |> put_view(ChatView)
|> render("show.json", chat: chat) |> render("show.json", chat: chat)

View File

@ -21,8 +21,7 @@ def render(
id: id |> to_string(), id: id |> to_string(),
content: chat_message["content"], content: chat_message["content"],
chat_id: chat_id |> to_string(), chat_id: chat_id |> to_string(),
actor: chat_message["actor"], account_id: User.get_cached_by_ap_id(chat_message["actor"]).id,
actor_account_id: User.get_cached_by_ap_id(chat_message["actor"]).id,
created_at: Utils.to_masto_date(chat_message["published"]), created_at: Utils.to_masto_date(chat_message["published"]),
emojis: StatusView.build_emojis(chat_message["emoji"]) emojis: StatusView.build_emojis(chat_message["emoji"])
} }

View File

@ -14,8 +14,7 @@ def render("show.json", %{chat: %Chat{} = chat} = opts) do
%{ %{
id: chat.id |> to_string(), id: chat.id |> to_string(),
recipient: chat.recipient, account: AccountView.render("show.json", Map.put(opts, :user, recipient)),
recipient_account: AccountView.render("show.json", Map.put(opts, :user, recipient)),
unread: chat.unread unread: chat.unread
} }
end end

View File

@ -275,7 +275,7 @@ defmodule Pleroma.Web.Router do
scope [] do scope [] do
pipe_through(:authenticated_api) 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", ChatController, :index)
get("/chats/:id/messages", ChatController, :messages) get("/chats/:id/messages", ChatController, :messages)
post("/chats/:id/messages", ChatController, :post_chat_message) post("/chats/:id/messages", ChatController, :post_chat_message)

View File

@ -88,7 +88,7 @@ test "it returns the messages for a given chat", %{conn: conn, user: user} do
end end
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"]) setup do: oauth_access(["write:statuses"])
test "it creates or returns a chat", %{conn: conn} do test "it creates or returns a chat", %{conn: conn} do
@ -96,7 +96,7 @@ test "it creates or returns a chat", %{conn: conn} do
result = result =
conn 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) |> json_response_and_validate_schema(200)
assert result["id"] assert result["id"]

View File

@ -25,8 +25,7 @@ test "it displays a chat message" do
assert chat_message[:id] == object.id |> to_string() assert chat_message[:id] == object.id |> to_string()
assert chat_message[:content] == "kippis :firefox:" assert chat_message[:content] == "kippis :firefox:"
assert chat_message[:actor] == user.ap_id assert chat_message[:account_id] == user.id
assert chat_message[:actor_account_id] == user.id
assert chat_message[:chat_id] assert chat_message[:chat_id]
assert chat_message[:created_at] assert chat_message[:created_at]
assert match?([%{shortcode: "firefox"}], chat_message[:emojis]) assert match?([%{shortcode: "firefox"}], chat_message[:emojis])
@ -39,8 +38,7 @@ test "it displays a chat message" do
assert chat_message_two[:id] == object.id |> to_string() assert chat_message_two[:id] == object.id |> to_string()
assert chat_message_two[:content] == "gkgkgk" assert chat_message_two[:content] == "gkgkgk"
assert chat_message_two[:actor] == recipient.ap_id assert chat_message_two[:account_id] == recipient.id
assert chat_message_two[:actor_account_id] == recipient.id
assert chat_message_two[:chat_id] == chat_message[:chat_id] assert chat_message_two[:chat_id] == chat_message[:chat_id]
end end
end end

View File

@ -21,8 +21,7 @@ test "it represents a chat" do
assert represented_chat == %{ assert represented_chat == %{
id: "#{chat.id}", id: "#{chat.id}",
recipient: recipient.ap_id, account: AccountView.render("show.json", user: recipient),
recipient_account: AccountView.render("show.json", user: recipient),
unread: 0 unread: 0
} }
end end