Skip to content

Commit

Permalink
fix: initialize method change
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Nov 27, 2023
1 parent 2dd6284 commit d0b0b62
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
5 changes: 5 additions & 0 deletions packages/restapi/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export enum MessageType {
PAYMENT = 'Payment',
}

export const ALPHA_FEATURES = {
GROUP_SCALABILITY: 'GROUP_SCALABILITY',
};

const Constants = {
ENV,
ENCRYPTION_TYPE,
Expand All @@ -60,6 +64,7 @@ const Constants = {
ENC_TYPE_V2: 'aes256GcmHkdfSha256',
ENC_TYPE_V3: 'eip191-aes256-gcm-hkdf-sha256',
ENC_TYPE_V4: 'pgpv1:nft',
ALPHA_FEATURES,
};

export default Constants;
17 changes: 10 additions & 7 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Constants, { ENV } from '../constants';
import { SignerType, ProgressHookType } from '../types';
import { FeatureTag, PushAPIInitializeProps } from './pushAPITypes';
import { PushAPIInitializeProps } from './pushAPITypes';
import * as PUSH_USER from '../user';
import * as PUSH_CHAT from '../chat';
import { getAccountAddress, getWallet } from '../chat/helpers';
Expand All @@ -19,7 +19,7 @@ import {
export class PushAPI {
private signer?: SignerType;
private readMode: boolean;
private featureTag: FeatureTag;
private alpha: { feature: string[] };
private account: string;
private decryptedPgpPvtKey?: string;
private pgpPublicKey?: string;
Expand All @@ -39,15 +39,15 @@ export class PushAPI {
env: ENV,
account: string,
readMode: boolean,
featureTag: FeatureTag,
alpha: { feature: string[] },
decryptedPgpPvtKey?: string,
pgpPublicKey?: string,
signer?: SignerType,
progressHook?: (progress: ProgressHookType) => void
) {
this.signer = signer;
this.readMode = readMode;
this.featureTag = featureTag;
this.alpha = alpha;
this.env = env;
this.account = account;
this.decryptedPgpPvtKey = decryptedPgpPvtKey;
Expand All @@ -60,7 +60,7 @@ export class PushAPI {
this.chat = new Chat(
this.account,
this.env,
this.featureTag,
this.alpha,
this.decryptedPgpPvtKey,
this.signer,
this.progressHook
Expand Down Expand Up @@ -134,7 +134,10 @@ export class PushAPI {
options?.autoUpgrade !== undefined
? options?.autoUpgrade
: defaultOptions.autoUpgrade,
featureTag: options?.featureTag || 'STABLE',
alpha:
options?.alpha && options.alpha.feature
? options.alpha
: { feature: [] },
};

const readMode = !signer;
Expand Down Expand Up @@ -202,7 +205,7 @@ export class PushAPI {
settings.env as ENV,
derivedAccount,
readMode,
settings.featureTag,
settings.alpha,
decryptedPGPPrivateKey,
pgpPublicKey,
signer,
Expand Down
46 changes: 25 additions & 21 deletions packages/restapi/src/lib/pushapi/chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ENV, MessageType } from '../constants';
import { ENV, MessageType, ALPHA_FEATURES } from '../constants';
import {
ChatSendOptionsType,
GroupAccess,
Expand All @@ -21,7 +21,6 @@ import {
ManageGroupOptions,
RemoveFromGroupOptions,
GetGroupParticipantsOptions,
FeatureTag,
} from './pushAPITypes';
import * as PUSH_USER from '../user';
import * as PUSH_CHAT from '../chat';
Expand All @@ -36,16 +35,22 @@ import { updateGroupConfig } from '../chat/updateGroupConfig';
import { PushAPI } from './PushAPI';
export class Chat {
private userInstance: User;
private groupScalabilityTag: 'ALPHA' | 'STABLE';

constructor(
private account: string,
private env: ENV,
private featureTag: FeatureTag,
private alpha: { feature: string[] },
private decryptedPgpPvtKey?: string,
private signer?: SignerType,
private progressHook?: (progress: ProgressHookType) => void
) {
this.userInstance = new User(this.account, this.env);
this.groupScalabilityTag = this.alpha.feature.includes(
ALPHA_FEATURES.GROUP_SCALABILITY
)
? 'ALPHA'
: 'STABLE';
}

async list(
Expand Down Expand Up @@ -165,7 +170,7 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
}

Expand Down Expand Up @@ -299,8 +304,7 @@ export class Chat {
admins: options?.admins ? options.admins : [],
};
const response = await PUSH_CHAT.createGroupV2(groupParams);

switch (this.featureTag) {
switch (this.groupScalabilityTag) {
case 'ALPHA':
return response;
case 'STABLE':
Expand Down Expand Up @@ -340,7 +344,7 @@ export class Chat {
},

info: async (chatId: string): Promise<GroupDTO | GroupInfoDTO> => {
switch (this.featureTag) {
switch (this.groupScalabilityTag) {
case 'ALPHA':
return await PUSH_CHAT.getGroupInfo({
chatId: chatId,
Expand Down Expand Up @@ -396,7 +400,7 @@ export class Chat {
await updateGroupProfile(updateGroupProfileOptions);
const response = await updateGroupConfig(updateGroupConfigOptions);

switch (this.featureTag) {
switch (this.groupScalabilityTag) {
case 'ALPHA':
return response;
case 'STABLE':
Expand Down Expand Up @@ -440,7 +444,7 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
} else {
response = await PUSH_CHAT.addMembers({
Expand All @@ -450,11 +454,11 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
}

switch (this.featureTag) {
switch (this.groupScalabilityTag) {
case 'ALPHA':
return response;
case 'STABLE':
Expand Down Expand Up @@ -509,7 +513,7 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
}

Expand All @@ -521,10 +525,10 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
}
switch (this.featureTag) {
switch (this.groupScalabilityTag) {
case 'ALPHA':
return await PUSH_CHAT.getGroupInfo({
chatId: chatId,
Expand Down Expand Up @@ -566,7 +570,7 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
},

Expand All @@ -587,7 +591,7 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
} else if (!status.isMember) {
await PUSH_CHAT.addMembers({
Expand All @@ -597,10 +601,10 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
}
switch (this.featureTag) {
switch (this.groupScalabilityTag) {
case 'ALPHA':
return await PUSH_CHAT.getGroupInfo({
chatId: target,
Expand Down Expand Up @@ -635,7 +639,7 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
} else {
response = await PUSH_CHAT.removeMembers({
Expand All @@ -645,11 +649,11 @@ export class Chat {
account: this.account,
signer: this.signer,
pgpPrivateKey: this.decryptedPgpPvtKey,
overrideSecretKeyGeneration: this.featureTag !== 'ALPHA',
overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA',
});
}

switch (this.featureTag) {
switch (this.groupScalabilityTag) {
case 'ALPHA':
return response;
case 'STABLE':
Expand Down
5 changes: 3 additions & 2 deletions packages/restapi/src/lib/pushapi/pushAPITypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export enum ChatListType {
CHATS = 'CHATS',
REQUESTS = 'REQUESTS',
}
export type FeatureTag = 'ALPHA' | 'STABLE';
export interface PushAPIInitializeProps {
env?: ENV;
progressHook?: (progress: ProgressHookType) => void;
Expand All @@ -14,7 +13,9 @@ export interface PushAPIInitializeProps {
versionMeta?: { NFTPGP_V1?: { password: string } };
autoUpgrade?: boolean;
origin?: string;
featureTag?: FeatureTag;
alpha?: {
feature: string[];
};
}

export interface GroupCreationOptions {
Expand Down
3 changes: 1 addition & 2 deletions packages/restapi/src/lib/pushstream/PushStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class PushStream extends EventEmitter {
this.chatInstance = new Chat(
this.account,
this.options.env as ENV,
this.options.featureTag,
{ feature: [] },
this.decryptedPgpPvtKey,
this.signer,
this.progressHook
Expand All @@ -65,7 +65,6 @@ export class PushStream extends EventEmitter {
retries: 3,
},
env: env,
featureTag: 'STABLE',
};

if (!listen || listen.length === 0) {
Expand Down
4 changes: 1 addition & 3 deletions packages/restapi/src/lib/pushstream/pushStreamTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Rules } from '../types';
import Constants, { ENV } from '../constants';
import { FeatureTag } from '../pushapi/pushAPITypes';
import { ENV } from '../constants';

export type PushStreamInitializeProps = {
filter?: {
Expand All @@ -14,7 +13,6 @@ export type PushStreamInitializeProps = {
raw?: boolean;
env?: ENV;
overrideAccount?: string;
featureTag: FeatureTag;
};

export enum STREAM {
Expand Down
6 changes: 3 additions & 3 deletions packages/restapi/tests/lib/chat/privateGroup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ describe('Private Groups', () => {
account1 = `eip155:${signer1.address}`;
userAlice = await PushAPI.initialize(signer1, {
env: _env,
featureTag: 'ALPHA',
alpha: { feature: [Constants.ALPHA_FEATURES.GROUP_SCALABILITY] },
});

const WALLET2 = ethers.Wallet.createRandom();
const signer2 = new ethers.Wallet(WALLET2.privateKey);
account2 = `eip155:${signer2.address}`;
userBob = await PushAPI.initialize(signer2, {
env: _env,
featureTag: 'ALPHA',
alpha: { feature: [Constants.ALPHA_FEATURES.GROUP_SCALABILITY] },
});

const WALLET3 = ethers.Wallet.createRandom();
const signer3 = new ethers.Wallet(WALLET3.privateKey);
account3 = `eip155:${signer3.address}`;
userJohn = await PushAPI.initialize(signer3, {
env: _env,
featureTag: 'ALPHA',
alpha: { feature: [Constants.ALPHA_FEATURES.GROUP_SCALABILITY] },
});

group = await userAlice.chat.group.create(groupName, {
Expand Down

0 comments on commit d0b0b62

Please sign in to comment.