Skip to content

Commit

Permalink
fix: some changes on stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Sep 29, 2023
1 parent c4d2cff commit 92a2202
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 57 deletions.
2 changes: 0 additions & 2 deletions packages/restapi/src/lib/pushNotification/alias.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ENV } from '../constants';
import { SignerType } from '../types';
import { AliasOptions } from './PushNotificationTypes';

import * as PUSH_ALIAS from '../alias';

import { PushNotificationBaseClass } from './pushNotificationBase';

export class Alias {
private env: ENV
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/pushNotification/delegate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Constants, { ENV } from '../constants';
import { ENV } from '../constants';
import { SignerType } from '../types';
import { ChannelInfoOptions } from './PushNotificationTypes';
import CONFIG, * as config from '../config';
Expand Down
11 changes: 0 additions & 11 deletions packages/restapi/src/lib/pushNotification/notification.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import Constants, { ENV } from '../constants';
import {
SignerType,
ProgressHookType,
ProgressHookTypeFunction,
} from '../types';
import {
ChannelInfoOptions,
SubscribeUnsubscribeOptions,
SubscriptionOptions,
AliasOptions,
FeedType,
FeedsOptions,
ChannelSearchOptions,
NotificationOptions,
CreateChannelOptions,
NotificationSettings,
} from './PushNotificationTypes';
import CONFIG, * as config from '../config';
import * as PUSH_USER from '../user';
import * as PUSH_PAYLOAD from '../payloads';
import * as PUSH_CHANNEL from '../channels';
import * as PUSH_ALIAS from '../alias';

import {
getCAIPDetails,
Expand Down
71 changes: 36 additions & 35 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,10 @@ export class PushAPI {
this.pgpPublicKey = pgpPublicKey;
this.progressHook = progressHook;
// Instantiate the notification classes
this.channel = new Channel(
this.signer,
this.env,
this.account
)
this.alias = new Alias(
this.env
)
this.delegate = new Delegate(
this.signer,
this.env,
this.account
)
this.notification = new Notification(
this.signer,
this.env,
this.account
)
this.channel = new Channel(this.signer, this.env, this.account);
this.alias = new Alias(this.env);
this.delegate = new Delegate(this.signer, this.env, this.account);
this.notification = new Notification(this.signer, this.env, this.account);
// Initialize the instances of the four classes
this.chat = new Chat(
this.account,
Expand Down Expand Up @@ -98,16 +84,29 @@ export class PushAPI {
// Default options
const defaultOptions: PushAPIInitializeProps = {
env: ENV.STAGING,
version: Constants.ENC_TYPE_V3,
autoUpgrade: true,
chatOptions: {
version: Constants.ENC_TYPE_V3,
autoUpgrade: true,
},
account: null,
streamOptions: {
socketEnabled: true, // Default value
},
};

// Settings object
// Default options are overwritten by the options passed in the initialize method
const settings = {
...defaultOptions,
...options,
chatOptions: {
...defaultOptions.chatOptions,
...(options?.chatOptions ?? {}),
},
streamOptions: {
...defaultOptions.streamOptions,
...(options?.streamOptions ?? {}),
},
};

// Get account
Expand Down Expand Up @@ -135,8 +134,8 @@ export class PushAPI {
decryptedPGPPrivateKey = await PUSH_CHAT.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: signer,
toUpgrade: settings.autoUpgrade,
additionalMeta: settings.versionMeta,
toUpgrade: settings.chatOptions.autoUpgrade,
additionalMeta: settings.chatOptions.versionMeta,
progressHook: settings.progressHook,
env: settings.env,
});
Expand All @@ -146,8 +145,8 @@ export class PushAPI {
env: settings.env,
account: derivedAccount,
signer,
version: settings.version,
additionalMeta: settings.versionMeta,
version: settings.chatOptions.version,
additionalMeta: settings.chatOptions.versionMeta,
origin: settings.origin,
progressHook: settings.progressHook,
});
Expand All @@ -165,17 +164,19 @@ export class PushAPI {
settings.progressHook
);

const streamInstance = await PushStream.initialize(
api.account,
decryptedPGPPrivateKey,
signer,
settings.progressHook,
settings.streamOptions
);
if (streamInstance) {
api.stream = streamInstance;
} else {
throw new Error('Failed to initialize PushStream.');
if (settings.streamOptions.socketEnabled) {
const streamInstance = await PushStream.initialize(
api.account,
decryptedPGPPrivateKey,
signer,
settings.progressHook,
settings.streamOptions
);
if (streamInstance) {
api.stream = streamInstance;
} else {
throw new Error('Failed to initialize PushStream.');
}
}

return api;
Expand Down
8 changes: 3 additions & 5 deletions packages/restapi/src/lib/pushapi/pushAPITypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Constants, { ENV } from '../constants';
import { PushStreamInitializeProps } from '../pushstream/pushStreamTypes';
import { ENV } from '../constants';
import { PushChatInitializeProps, PushStreamInitializeProps } from '../pushstream/pushStreamTypes';
import { ChatStatus, ProgressHookType, Rules } from '../types';

export enum ChatListType {
Expand All @@ -10,9 +10,7 @@ export interface PushAPIInitializeProps {
env?: ENV;
progressHook?: (progress: ProgressHookType) => void;
account?: string | null;
version?: typeof Constants.ENC_TYPE_V1 | typeof Constants.ENC_TYPE_V3;
versionMeta?: { NFTPGP_V1?: { password: string } };
autoUpgrade?: boolean;
chatOptions?: PushChatInitializeProps;
origin?: string;
streamOptions?: PushStreamInitializeProps;
}
Expand Down
1 change: 0 additions & 1 deletion packages/restapi/src/lib/pushstream/DataModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
GroupEventType,
LeaveGroupEvent,
JoinGroupEvent,
ProposedEventNames,
RequestEvent,
RemoveEvent,
} from './pushStreamTypes';
Expand Down
2 changes: 2 additions & 0 deletions packages/restapi/src/lib/pushstream/PushStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ export class PushStream extends EventEmitter {
data = await this.chatInstance.decrypt([data]);
}

console.log(data);

const modifiedData = DataModifier.handleChatEvent(data[0], this.raw);
modifiedData.event = this.convertToProposedName(modifiedData.event);
this.handleToField(modifiedData);
Expand Down
9 changes: 8 additions & 1 deletion packages/restapi/src/lib/pushstream/pushStreamTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ENV } from "../constants";
import { Rules } from "../types";
import Constants, { ENV } from '../constants';

export type PushStreamInitializeProps = {
listen?: STREAM[];
Expand All @@ -13,8 +13,15 @@ export type PushStreamInitializeProps = {
};
raw?: boolean;
env?: ENV;
socketEnabled?: boolean;
};

export type PushChatInitializeProps = {
version?: typeof Constants.ENC_TYPE_V1 | typeof Constants.ENC_TYPE_V3;
versionMeta?: { NFTPGP_V1?: { password: string } };
autoUpgrade?: boolean;
}

export enum STREAM {
PROFILE = 'STREAM.PROFILE',
ENCRYPTION = 'STREAM.ENCRYPTION',
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/tests/lib/pushstream/initialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe.only('PushStream.initialize functionality', () => {
accounts: [signer2.address, signer3.address, signer4.address],
});

const w2wRejectRequest = await user2.chat.group.join(
const w2wRejectRequest = await user2.chat.group.join(
createdGroup.chatId
);

Expand Down

0 comments on commit 92a2202

Please sign in to comment.