From f283c5f93c83d4053f0390df58762141432761f6 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Tue, 8 Jul 2025 12:22:20 +0100 Subject: [PATCH] core: fix API of websocket server, fix example in TS client API definition (#6051) --- apps/simplex-chat/Server.hs | 11 ++++++----- .../typescript/src/client.ts | 19 ++++++++++++++----- .../typescript/src/response.ts | 15 +++++++++------ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/apps/simplex-chat/Server.hs b/apps/simplex-chat/Server.hs index 0906d14536..0b18546609 100644 --- a/apps/simplex-chat/Server.hs +++ b/apps/simplex-chat/Server.hs @@ -18,7 +18,6 @@ import Control.Monad.Reader import Data.Aeson (FromJSON, ToJSON (..)) import qualified Data.Aeson as J import qualified Data.Aeson.TH as JQ -import Data.Bifunctor (first) import Data.Text (Text) import Data.Text.Encoding (encodeUtf8) import GHC.Generics (Generic) @@ -51,13 +50,15 @@ $(JQ.deriveToJSON (taggedObjectJSON $ dropPrefix "Obj") ''ObjChatCmdError) $(JQ.deriveToJSON (taggedObjectJSON $ dropPrefix "Obj") ''ObjChatError) +-- this encoding preserves the websocket API format when ChatError was sent either with type: "chatError" or type: "chatCmdError" instance ToJSON (CSRBody ChatResponse) where - toJSON = toJSON . first ObjChatCmdError . csrBody - toEncoding = toEncoding . first ObjChatCmdError . csrBody + toJSON = either (toJSON . ObjChatCmdError) toJSON . csrBody + toEncoding = either (toEncoding . ObjChatCmdError) toEncoding . csrBody +-- this encoding preserves the websocket API format when ChatError was sent either with type: "chatError" or type: "chatCmdError" instance ToJSON (CSRBody ChatEvent) where - toJSON = toJSON . first ObjChatError . csrBody - toEncoding = toEncoding . first ObjChatError . csrBody + toJSON = either (toJSON . ObjChatError) toJSON . csrBody + toEncoding = either (toEncoding . ObjChatError) toEncoding . csrBody data AChatSrvResponse = forall r. ToJSON (ChatSrvResponse r) => ACR (ChatSrvResponse r) diff --git a/packages/simplex-chat-client/typescript/src/client.ts b/packages/simplex-chat-client/typescript/src/client.ts index 3600147ba7..f908e6a7e3 100644 --- a/packages/simplex-chat-client/typescript/src/client.ts +++ b/packages/simplex-chat-client/typescript/src/client.ts @@ -79,8 +79,9 @@ export class ChatClient { sendChatCmdStr(cmd: string): Promise { const corrId = `${++this.clientCorrId}` const t: ChatSrvRequest = {corrId, cmd} + const p = new Promise((resolve, reject) => this.sentCommands.set(corrId, {resolve, reject})) this.transport.write(t).then(noop, noop) - return new Promise((resolve, reject) => this.sentCommands.set(corrId, {resolve, reject})) + return p } sendChatCommand(command: ChatCommand): Promise { @@ -189,7 +190,10 @@ export class ChatClient { async apiCreateLink(): Promise { const r = await this.sendChatCommand({type: "addContact"}) - if (r.type === "invitation") return r.connReqInvitation + if (r.type === "invitation") { + const link = r.connLinkInvitation + return link.connShortLink || link.connFullLink + } throw new ChatCommandError("error creating link", r) } @@ -247,7 +251,10 @@ export class ChatClient { async apiCreateUserAddress(): Promise { const r = await this.sendChatCommand({type: "createMyAddress"}) - if (r.type === "userContactLinkCreated") return r.connReqContact + if (r.type === "userContactLinkCreated") { + const link = r.connLinkContact + return link.connShortLink || link.connFullLink + } throw new ChatCommandError("error creating user address", r) } @@ -260,8 +267,10 @@ export class ChatClient { async apiGetUserAddress(): Promise { const r = await this.sendChatCommand({type: "showMyAddress"}) switch (r.type) { - case "userContactLink": - return r.contactLink.connReqContact + case "userContactLink": { + const link = r.contactLink.connLinkContact + return link.connShortLink || link.connFullLink + } default: if (r.type === "chatCmdError" && r.chatError.type === "errorStore" && r.chatError.storeError.type === "userContactLinkNotFound") { return undefined diff --git a/packages/simplex-chat-client/typescript/src/response.ts b/packages/simplex-chat-client/typescript/src/response.ts index 5f91baa7db..95eac89249 100644 --- a/packages/simplex-chat-client/typescript/src/response.ts +++ b/packages/simplex-chat-client/typescript/src/response.ts @@ -299,9 +299,7 @@ export interface CRUserContactLink extends CR { export interface CRUserContactLinkUpdated extends CR { type: "userContactLinkUpdated" user: User - connReqContact: string - autoAccept: boolean - autoReply?: MsgContent + contactLink: UserContactLink } export interface CRContactRequestRejected extends CR { @@ -337,7 +335,7 @@ export interface CRContactAliasUpdated extends CR { export interface CRInvitation extends CR { type: "invitation" user: User - connReqInvitation: string + connLinkInvitation: CreatedConnLink } export interface CRSentConfirmation extends CR { @@ -379,7 +377,7 @@ export interface CRChatCleared extends CR { export interface CRUserContactLinkCreated extends CR { type: "userContactLinkCreated" user: User - connReqContact: string + connLinkContact: CreatedConnLink } export interface CRUserContactLinkDeleted extends CR { @@ -994,10 +992,15 @@ interface FileTransferMeta { } interface UserContactLink { - connReqContact: string + connLinkContact: CreatedConnLink autoAccept?: AutoAccept } +interface CreatedConnLink { + connFullLink: string + connShortLink?: string +} + interface AutoAccept { acceptIncognito: boolean autoReply?: MsgContent