From 12e036190bd95601d2dc268726adbdd8b881b917 Mon Sep 17 00:00:00 2001 From: Mohammed S Date: Wed, 12 Jun 2024 20:34:01 +0530 Subject: [PATCH] fix: support chatId for one to one in send message --- .../src/lib/chat/helpers/payloadHelper.ts | 2 +- packages/restapi/src/lib/chat/send.ts | 52 +++++++++++++++---- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/packages/restapi/src/lib/chat/helpers/payloadHelper.ts b/packages/restapi/src/lib/chat/helpers/payloadHelper.ts index 6ab762e1e..d22d2ba16 100644 --- a/packages/restapi/src/lib/chat/helpers/payloadHelper.ts +++ b/packages/restapi/src/lib/chat/helpers/payloadHelper.ts @@ -110,7 +110,7 @@ export const sendMessagePayloadCore = async ( env: ENV, pgpHelper: IPGPHelper ): Promise => { - const isGroup = !isValidPushCAIP(receiverAddress); + const isGroup = group !== null; let secretKey: string; if (isGroup && group?.encryptedSecret && group.sessionKey) { diff --git a/packages/restapi/src/lib/chat/send.ts b/packages/restapi/src/lib/chat/send.ts index 741e1e423..55838da14 100644 --- a/packages/restapi/src/lib/chat/send.ts +++ b/packages/restapi/src/lib/chat/send.ts @@ -1,4 +1,9 @@ -import { convertToValidDID, getAPIBaseUrls, isValidPushCAIP } from '../helpers'; +import { + convertToValidDID, + getAPIBaseUrls, + isValidPushCAIP, + walletToPCAIP10, +} from '../helpers'; import Constants, { MessageType, ENV } from '../constants'; import { ChatSendOptionsType, MessageWithCID, SignerType } from '../types'; import { @@ -15,6 +20,7 @@ import { validateMessageObj } from '../validations/messageObject'; import { axiosPost } from '../utils/axiosUtil'; import { getGroupInfo } from './getGroupInfo'; import { handleError } from '../errors/validationError'; +import * as PUSH_CHAT from '../chat'; /** * SENDS A PUSH CHAT MESSAGE @@ -36,7 +42,7 @@ export const sendCore = async ( * 2. Takes care of deprecated fields */ const computedOptions = computeOptions(options); - const { messageType, messageObj, account, to, signer, pgpPrivateKey, env } = + let { messageType, messageObj, account, to, signer, pgpPrivateKey, env } = computedOptions; /** * Validate Input Options @@ -50,16 +56,40 @@ export const sendCore = async ( env, pgpHelper ); - const receiver = await convertToValidDID(to, env); + let receiver = await convertToValidDID(to, env); const API_BASE_URL = getAPIBaseUrls(env); - const isGroup = isValidPushCAIP(to) ? false : true; - - const group = isGroup - ? await getGroupInfo({ - chatId: to, - env: env, - }) - : null; + + const isChatId = isValidPushCAIP(to) ? false : true; + let isGroup = false; + let group = null; + + if (isChatId) { + const request: PUSH_CHAT.GetChatInfoType = { + recipient: to, + account: account!, + env: env, + }; + + const chatInfo = await PUSH_CHAT.getChatInfo(request); + isGroup = chatInfo?.meta?.group ?? false; + + group = isGroup + ? await getGroupInfo({ + chatId: to, + env: env, + }) + : null; + + if (!isGroup) { + const participants = chatInfo.participants ?? []; + // Find the participant that is not the account being used + const messageSentTo = participants.find( + (participant) => participant !== walletToPCAIP10(account!) + ); + to = messageSentTo!; + receiver = to; + } + } // Not supported by legacy sdk versions, need to override messageContent to avoid parsing errors on legacy sdk versions let messageContent: string;