From 75bacb7923a68b65a81c0eb9d9e5fe9fb25201a2 Mon Sep 17 00:00:00 2001 From: Yaroslav Pavlov Date: Thu, 10 Oct 2024 19:10:11 +0100 Subject: [PATCH] desktop: fix typescript sdk ability to send / receive messages (#4970) * typescript sdk: fix send messages * typescript sdk: fix send messages naming --- .../typescript/examples/squaring-bot.js | 17 +++++++++-------- .../simplex-chat-client/typescript/package.json | 2 +- .../typescript/src/client.ts | 10 +++++----- .../typescript/src/command.ts | 4 ++-- .../typescript/src/response.ts | 10 +++++----- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/simplex-chat-client/typescript/examples/squaring-bot.js b/packages/simplex-chat-client/typescript/examples/squaring-bot.js index 5a96dfb205..9651436ffc 100644 --- a/packages/simplex-chat-client/typescript/examples/squaring-bot.js +++ b/packages/simplex-chat-client/typescript/examples/squaring-bot.js @@ -35,15 +35,16 @@ async function run() { ) continue } - case "newChatItem": { + case "newChatItems": { // calculates the square of the number and sends the reply - const {chatInfo} = resp.chatItem - if (chatInfo.type !== ChatInfoType.Direct) continue - const msg = ciContentText(resp.chatItem.chatItem.content) - if (msg) { - const n = +msg - const reply = typeof n === "number" && !isNaN(n) ? `${n} * ${n} = ${n * n}` : `this is not a number` - await chat.apiSendTextMessage(ChatType.Direct, chatInfo.contact.contactId, reply) + for (const {chatInfo, chatItem} of resp.chatItems) { + if (chatInfo.type !== ChatInfoType.Direct) continue + const msg = ciContentText(chatItem.content) + if (msg) { + const n = +msg + const reply = typeof n === "number" && !isNaN(n) ? `${n} * ${n} = ${n * n}` : `this is not a number` + await chat.apiSendTextMessage(ChatType.Direct, chatInfo.contact.contactId, reply) + } } } } diff --git a/packages/simplex-chat-client/typescript/package.json b/packages/simplex-chat-client/typescript/package.json index c8aa6b4f1e..bb2fdda702 100644 --- a/packages/simplex-chat-client/typescript/package.json +++ b/packages/simplex-chat-client/typescript/package.json @@ -1,6 +1,6 @@ { "name": "simplex-chat", - "version": "0.2.0", + "version": "0.2.1", "description": "SimpleX Chat client", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/simplex-chat-client/typescript/src/client.ts b/packages/simplex-chat-client/typescript/src/client.ts index eb3e39f00a..3600147ba7 100644 --- a/packages/simplex-chat-client/typescript/src/client.ts +++ b/packages/simplex-chat-client/typescript/src/client.ts @@ -160,14 +160,14 @@ export class ChatClient { throw new ChatCommandError("error loading chat", r) } - async apiSendMessage(chatType: ChatType, chatId: number, message: CC.ComposedMessage): Promise { - const r = await this.sendChatCommand({type: "apiSendMessage", chatType, chatId, message}) - if (r.type === "newChatItem") return r.chatItem + async apiSendMessages(chatType: ChatType, chatId: number, messages: CC.ComposedMessage[]): Promise { + const r = await this.sendChatCommand({type: "apiSendMessage", chatType, chatId, messages}) + if (r.type === "newChatItems") return r.chatItems throw new ChatCommandError("unexpected response", r) } - apiSendTextMessage(chatType: ChatType, chatId: number, text: string): Promise { - return this.apiSendMessage(chatType, chatId, {msgContent: {type: "text", text}}) + async apiSendTextMessage(chatType: ChatType, chatId: number, text: string): Promise { + return this.apiSendMessages(chatType, chatId, [{msgContent: {type: "text", text}}]) } async apiUpdateChatItem(chatType: ChatType, chatId: number, chatItemId: CC.ChatItemId, msgContent: CC.MsgContent): Promise { diff --git a/packages/simplex-chat-client/typescript/src/command.ts b/packages/simplex-chat-client/typescript/src/command.ts index bd17a55926..e512e06672 100644 --- a/packages/simplex-chat-client/typescript/src/command.ts +++ b/packages/simplex-chat-client/typescript/src/command.ts @@ -277,7 +277,7 @@ export interface APISendMessage extends IChatCommand { type: "apiSendMessage" chatType: ChatType chatId: number - message: ComposedMessage + messages: ComposedMessage[] } export interface ComposedMessage { @@ -709,7 +709,7 @@ export function cmdString(cmd: ChatCommand): string { case "apiGetChat": return `/_get chat ${cmd.chatType}${cmd.chatId}${paginationStr(cmd.pagination)}` case "apiSendMessage": - return `/_send ${cmd.chatType}${cmd.chatId} json ${JSON.stringify(cmd.message)}` + return `/_send ${cmd.chatType}${cmd.chatId} json ${JSON.stringify(cmd.messages)}` case "apiUpdateChatItem": return `/_update item ${cmd.chatType}${cmd.chatId} ${cmd.chatItemId} json ${JSON.stringify(cmd.msgContent)}` case "apiDeleteChatItem": diff --git a/packages/simplex-chat-client/typescript/src/response.ts b/packages/simplex-chat-client/typescript/src/response.ts index b50b2e2943..2e92e335df 100644 --- a/packages/simplex-chat-client/typescript/src/response.ts +++ b/packages/simplex-chat-client/typescript/src/response.ts @@ -12,7 +12,7 @@ export type ChatResponse = | CRUserProtoServers | CRContactInfo | CRGroupMemberInfo - | CRNewChatItem + | CRNewChatItems | CRChatItemStatusUpdated | CRChatItemUpdated | CRChatItemDeleted @@ -109,7 +109,7 @@ type ChatResponseTag = | "userProtoServers" | "contactInfo" | "groupMemberInfo" - | "newChatItem" + | "newChatItems" | "chatItemStatusUpdated" | "chatItemUpdated" | "chatItemDeleted" @@ -255,10 +255,10 @@ export interface CRGroupMemberInfo extends CR { connectionStats_?: ConnectionStats } -export interface CRNewChatItem extends CR { - type: "newChatItem" +export interface CRNewChatItems extends CR { + type: "newChatItems" user: User - chatItem: AChatItem + chatItems: AChatItem[] } export interface CRChatItemStatusUpdated extends CR {