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

Stream changes #830

Merged
merged 4 commits into from
Nov 12, 2023
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
26 changes: 26 additions & 0 deletions packages/restapi/src/lib/constantsV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ENV, MessageType } from "./constants";
import { ChatListType } from "./pushapi/pushAPITypes";
import { STREAM } from "./pushstream/pushStreamTypes";
import { ConditionType, GROUP_INVITER_ROLE, GROUP_RULES_CATEGORY, GROUP_RULES_PERMISSION, GROUP_RULES_SUB_CATEGORY } from "./types";



const CONSTANTS = {
ENV: ENV,
STREAM: STREAM,
CHAT: {
LIST_TYPE: ChatListType,
MESSAGE_TYPE: MessageType,
GROUP: {
RULES: {
CONDITION_TYPE: ConditionType,
CATEGORY: GROUP_RULES_CATEGORY,
SUBCATEGORY: GROUP_RULES_SUB_CATEGORY,
PERMISSION: GROUP_RULES_PERMISSION,
INVITER_ROLE: GROUP_INVITER_ROLE,
},
},
},
};

export default CONSTANTS;
6 changes: 6 additions & 0 deletions packages/restapi/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import * as payloads from './payloads';
import * as chat from './chat';
import * as space from './space';
import * as video from "./video"
import CONSTANTS from './constantsV2';

export * from './types';
export * from './pushNotification/PushNotificationTypes';
export * from './pushstream/pushStreamTypes';
export * from './pushapi/pushAPITypes';
export { CONSTANTS };

export { PushAPI } from './pushapi/PushAPI';
export {
alias,
Expand Down
3 changes: 1 addition & 2 deletions packages/restapi/src/lib/pushNotification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
validateCAIP,
getFallbackETHCAIPAddress,
} from '../helpers';
import PROGRESSHOOK from '../progressHook';
import { ethers } from 'ethers';


import { PushNotificationBaseClass } from './pushNotificationBase';
// ERROR CONSTANTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
NotificationSettings,
UserSetting,
} from './PushNotificationTypes';
import CONFIG, * as config from '../config';
import * as config from '../config';
import { getAccountAddress } from '../chat/helpers';
import { IDENTITY_TYPE, NOTIFICATION_TYPE } from '../payloads/constants';
import { ethers, Signer, BigNumber } from 'ethers';
Expand Down
54 changes: 26 additions & 28 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { User } from './user';
import { PushStream } from '../pushstream/PushStream';
import { Channel } from '../pushNotification/channel';
import { Notification } from '../pushNotification/notification';
import {
PushStreamInitializeProps,
STREAM,
} from '../pushstream/pushStreamTypes';

export class PushAPI {
private signer: SignerType;
Expand All @@ -24,7 +28,7 @@ export class PushAPI {
public profile: Profile;
public encryption: Encryption;
private user: User;
public stream!: PushStream;
public _stream!: PushStream;
// Notification
public channel!: Channel;
public notification!: Notification;
Expand Down Expand Up @@ -79,14 +83,9 @@ export class PushAPI {
// Default options
const defaultOptions: PushAPIInitializeProps = {
env: ENV.STAGING,

version: Constants.ENC_TYPE_V3,
autoUpgrade: true,

account: null,
streamOptions: {
enabled: true, // Default value
},
};

// Settings object
Expand All @@ -100,10 +99,6 @@ export class PushAPI {
options?.autoUpgrade !== undefined
? options?.autoUpgrade
: defaultOptions.autoUpgrade,
streamOptions: {
...defaultOptions.streamOptions,
...(options?.streamOptions ?? {}),
},
};

// Get account
Expand Down Expand Up @@ -161,31 +156,34 @@ export class PushAPI {
settings.progressHook
);

if (settings.streamOptions.enabled) {
const streamInstance = await PushStream.initialize(
api.account,
decryptedPGPPrivateKey,
signer,
settings.progressHook,
{
...settings.streamOptions,
env: settings.env, // Use the env from the top-level PushAPIInitializeProps
}
);
if (streamInstance) {
api.stream = streamInstance;
} else {
throw new Error('Failed to initialize PushStream.');
}
}

return api;
} catch (error) {
console.error('Error initializing PushAPI:', error);
throw error; // or handle it more gracefully if desired
}
}

async stream(
listen: STREAM[],
options?: PushStreamInitializeProps
): Promise<PushStream> {
if (this._stream) {
throw new Error('Stream is already initialized.');
}

this._stream = await PushStream.initialize(
this.account,
this.decryptedPgpPvtKey,
this.signer,
listen,
this.env,
this.progressHook,
options
);

return this._stream;
}

async info() {
return await PUSH_USER.get({
account: this.account,
Expand Down
1 change: 0 additions & 1 deletion packages/restapi/src/lib/pushapi/pushAPITypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface PushAPIInitializeProps {
versionMeta?: { NFTPGP_V1?: { password: string } };
autoUpgrade?: boolean;
origin?: string;
streamOptions?: PushStreamInitializeProps;
}

export interface GroupCreationOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/pushstream/DataModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
MessageRawData,
MessageEvent,
MessageEventType,
Member,
GroupMember,
GroupEventType,
LeaveGroupEvent,
JoinGroupEvent,
Expand Down
Loading