Skip to content

Commit

Permalink
fix: support chatId for one to one in send message
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Jun 12, 2024
1 parent 72beb73 commit 12e0361
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/chat/helpers/payloadHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const sendMessagePayloadCore = async (
env: ENV,
pgpHelper: IPGPHelper
): Promise<ISendMessagePayload> => {
const isGroup = !isValidPushCAIP(receiverAddress);
const isGroup = group !== null;

let secretKey: string;
if (isGroup && group?.encryptedSecret && group.sessionKey) {
Expand Down
52 changes: 41 additions & 11 deletions packages/restapi/src/lib/chat/send.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 12e0361

Please sign in to comment.