Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support chatId for one to one in send message #1350

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading