Skip to content

Commit

Permalink
fix: minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Nov 9, 2023
1 parent 809b3ee commit f3c3d0c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
11 changes: 9 additions & 2 deletions packages/restapi/src/lib/chat/approveRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
import * as CryptoJS from 'crypto-js';
import { getGroup } from './getGroup';
import * as AES from '../chat/helpers/aes';
import { getGroupInfo } from './getGroupInfo';
import { getAllGroupMembersPublicKeys } from './getAllGroupMembersPublicKeys';

export interface ApproveRequestOptionsType extends EnvOptionsType {
/**
Expand Down Expand Up @@ -88,13 +90,18 @@ export const approveCore = async (
// pgpv2 is used for private grps
let sigType: 'pgp' | 'pgpv2' = 'pgp';
if (isGroup) {
const group = await getGroup({ chatId: senderAddress, env });
const group = await getGroupInfo({ chatId: senderAddress, env });

if (group && !group.isPublic) {
sigType = 'pgpv2';
const secretKey = AES.generateRandomSecret(15);

const groupMembers = await getAllGroupMembersPublicKeys({
chatId: group.chatId,
env,
});
// Encrypt secret key with group members public keys
const publicKeys: string[] = group.members.map(
const publicKeys: string[] = groupMembers.map(
(member) => member.publicKey
);
publicKeys.push(connectedUser.publicKey);
Expand Down
7 changes: 3 additions & 4 deletions packages/restapi/src/lib/chat/getAllGroupMembersPublicKeys.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { getGroupMembers } from './getGroupMembers';
import { getChatMemberCount } from './getChatMemberCount';
import { ChatMemberProfile, EnvOptionsType } from '../types';
import { EnvOptionsType } from '../types';
import { getGroupMembersPublicKeys } from './getGroupMembersPublicKeys';

export const getAllGroupMembersPublicKeys = async (options: {
chatId: string;
env: EnvOptionsType['env'];
}): Promise<ChatMemberProfile[]> => {
}): Promise<{ did: string; publicKey: string }[]> => {
const { chatId, env } = options;
const count = await getChatMemberCount({ chatId, env });
const overallCount = count.approvedCount;
const limit = 5000;
const totalPages = Math.ceil(overallCount / limit);
const pageNumbers = Array.from({ length: totalPages }, (_, i) => i + 1);
const groupMembers: ChatMemberProfile[] = [];
const groupMembers: { did: string; publicKey: string }[] = [];

const memberFetchPromises = pageNumbers.map((page) =>
getGroupMembersPublicKeys({ chatId, env, page, limit })
Expand Down
5 changes: 1 addition & 4 deletions packages/restapi/src/lib/chat/getGroupMembersPublicKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { FetchChatGroupInfoType } from './getGroupMembers';

export const getGroupMembersPublicKeys = async (
options: FetchChatGroupInfoType
): Promise<{
totalMembersCount: ChatMemberCounts;
members: ChatMemberProfile[];
}> => {
): Promise<{ members: [{ did: string; publicKey: string }] }> => {
const { chatId, page = 1, limit = 20, env = Constants.ENV.PROD } = options;

try {
Expand Down
10 changes: 7 additions & 3 deletions packages/restapi/src/lib/chat/helpers/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,11 @@ export const validateGroupMemberUpdateOptions = (
`Invalid role: ${role}. Allowed roles are ${allowedRoles.join(', ')}.`
);
}


if (upsert[role] && upsert[role].length > 1000) {
throw new Error(`${role} array cannot have more than 1000 addresses.`);
}

// Assuming you have a function `isValidETHAddress` to validate Ethereum addresses
upsert[role].forEach((address) => {
if (!isValidETHAddress(address)) {
Expand All @@ -237,8 +241,8 @@ export const validateGroupMemberUpdateOptions = (
});

// Validating remove array
if (remove && remove.length > 100) {
throw new Error('Remove array cannot have more than 100 addresses.');
if (remove && remove.length > 1000) {
throw new Error('Remove array cannot have more than 1000 addresses.');
}
remove.forEach((address) => {
if (!isValidETHAddress(address)) {
Expand Down
16 changes: 5 additions & 11 deletions packages/restapi/src/lib/chat/updateGroupMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { EnvOptionsType, GroupInfoDTO, SignerType } from '../types';
import { getGroupInfo } from './getGroupInfo';
import { getGroupMemberStatus } from './getGroupMemberStatus';
import * as AES from '../chat/helpers/aes';
import { getAllGroupMembers } from './getAllGroupMembers';
import { getAllGroupMembersPublicKeys } from './getAllGroupMembersPublicKeys';

export interface GroupMemberUpdateOptions extends EnvOptionsType {
chatId: string;
Expand Down Expand Up @@ -79,18 +79,15 @@ export const updateGroupMembers = async (
env,
});

const groupMembers = await getAllGroupMembers({ chatId, env });
const groupMembers = await getAllGroupMembersPublicKeys({ chatId, env });

const removeParticipantSet = new Set(
convertedRemove.map((participant) => participant.toLowerCase())
);
let sameMembers = true;

groupMembers.map((element) => {
if (
element.intent &&
removeParticipantSet.has(element.address.toLowerCase())
) {
if (removeParticipantSet.has(element.did.toLowerCase())) {
sameMembers = false;
}
});
Expand All @@ -101,11 +98,8 @@ export const updateGroupMembers = async (
const publicKeys: string[] = [];
// This will now only take keys of non-removed members
groupMembers.map((element) => {
if (
element.intent &&
!removeParticipantSet.has(element.address.toLowerCase())
) {
publicKeys.push(element.userInfo.publicKey as string);
if (!removeParticipantSet.has(element.did.toLowerCase())) {
publicKeys.push(element.publicKey as string);
}
});

Expand Down
29 changes: 15 additions & 14 deletions packages/restapi/tests/lib/benchmark/privateGroup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const _env = Constants.ENV.LOCAL;
* THIS TEST GROUP IS FOR BENCHMARKING SEND MESSAGE FOR PRIVATE GROUP
* These tests will be skipped
*/
describe('Private Groups', () => {
describe.skip('Private Groups', () => {
let account: string;
let account2: string;
let userAlice: PushAPI;
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('Private Groups', () => {
* STEP 3 - AUTOJOIN
* This is imp for generating session keys so , do skip this test
*/
describe.skip('Private Group AutoJoin', () => {
describe('Private Group AutoJoin', () => {
it('10 Members', async () => {
const chatId =
'9e8bea378b4e4860956c177146786c2e96a0db8aa7c4156299181b3e56290a57';
Expand Down Expand Up @@ -341,18 +341,21 @@ describe('Private Groups', () => {
console.log('Duration in ms : ', duration);
});
it('5000 Members', async () => {
await createGroupAndSendMessages(userAlice, 5000);
});
it('10000 Members', async () => {
await createGroupAndSendMessages(userAlice, 10000);
});
it('15000 Members', async () => {
await createGroupAndSendMessages(userAlice, 15000);
const chatId =
'33c9295913786a8c446ceca46af8ee29a3a7144ba63071c24e5f05a5407bccdf';
const startTime = new Date();
await userAlice.chat.send(chatId, {
content: 'Sending Message to Private Grp',
type: 'Text',
});
const endTime = new Date();
const duration = endTime.getTime() - startTime.getTime();
console.log('Duration in ms : ', duration);
});
});

describe('Update Group with Pending members', () => {
it.only('10 Members', async () => {
describe.skip('Update Group with Pending members', () => {
it('10 Members', async () => {
const chatId =
'd8892a41ccbb7d0c627d1e3976f3a0bd64540d1d535b1a339680f2ce5b0fbcf0';
await updateGroupWithPendingMembers(userAlice, chatId, 500);
Expand Down Expand Up @@ -470,7 +473,6 @@ const createGroupWithPendingMembers = async (
return createdGroup.chatId;
};


/**
* CREATE GROUP WITH GIVEN MEMBERS COUNT PENDING MEMBERS
* @dev - Added members are pending members
Expand All @@ -484,7 +486,7 @@ const updateGroupWithPendingMembers = async (
* STEP 1: Generate ENOUGH USERS
*/
console.log('Generating Users');
const users = await generateUsers(memberCount);
const users = await generateUsers(memberCount);

/**
* STEP 2: Add Members to Group
Expand Down Expand Up @@ -553,7 +555,6 @@ const generateUsers = async (memberCount: number): Promise<string[]> => {
return users; // 'users' is an array of strings representing accounts
};


/**
* CREATE GROUP WITH GIVEN MEMBERS COUNT NON-PENDING MEMBERS
* @dev - Added members are pending members
Expand Down

0 comments on commit f3c3d0c

Please sign in to comment.