diff --git a/packages/restapi/src/lib/constantsV2.ts b/packages/restapi/src/lib/constantsV2.ts new file mode 100644 index 000000000..30839ba2f --- /dev/null +++ b/packages/restapi/src/lib/constantsV2.ts @@ -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; diff --git a/packages/restapi/src/lib/index.ts b/packages/restapi/src/lib/index.ts index cbb11f040..5136174e8 100644 --- a/packages/restapi/src/lib/index.ts +++ b/packages/restapi/src/lib/index.ts @@ -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, diff --git a/packages/restapi/src/lib/pushNotification/notification.ts b/packages/restapi/src/lib/pushNotification/notification.ts index ebd7ade15..962ce5b74 100644 --- a/packages/restapi/src/lib/pushNotification/notification.ts +++ b/packages/restapi/src/lib/pushNotification/notification.ts @@ -15,8 +15,7 @@ import { validateCAIP, getFallbackETHCAIPAddress, } from '../helpers'; -import PROGRESSHOOK from '../progressHook'; -import { ethers } from 'ethers'; + import { PushNotificationBaseClass } from './pushNotificationBase'; // ERROR CONSTANTS diff --git a/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts b/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts index 30a06c6c4..756da351e 100644 --- a/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts +++ b/packages/restapi/src/lib/pushNotification/pushNotificationBase.ts @@ -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'; diff --git a/packages/restapi/src/lib/pushapi/PushAPI.ts b/packages/restapi/src/lib/pushapi/PushAPI.ts index 429b3aac3..ab5a3a7d9 100644 --- a/packages/restapi/src/lib/pushapi/PushAPI.ts +++ b/packages/restapi/src/lib/pushapi/PushAPI.ts @@ -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; @@ -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; @@ -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 @@ -100,10 +99,6 @@ export class PushAPI { options?.autoUpgrade !== undefined ? options?.autoUpgrade : defaultOptions.autoUpgrade, - streamOptions: { - ...defaultOptions.streamOptions, - ...(options?.streamOptions ?? {}), - }, }; // Get account @@ -161,24 +156,6 @@ 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); @@ -186,6 +163,27 @@ export class PushAPI { } } + async stream( + listen: STREAM[], + options?: PushStreamInitializeProps + ): Promise { + 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, diff --git a/packages/restapi/src/lib/pushapi/pushAPITypes.ts b/packages/restapi/src/lib/pushapi/pushAPITypes.ts index 094b16488..6431800d4 100644 --- a/packages/restapi/src/lib/pushapi/pushAPITypes.ts +++ b/packages/restapi/src/lib/pushapi/pushAPITypes.ts @@ -14,7 +14,6 @@ export interface PushAPIInitializeProps { versionMeta?: { NFTPGP_V1?: { password: string } }; autoUpgrade?: boolean; origin?: string; - streamOptions?: PushStreamInitializeProps; } export interface GroupCreationOptions { diff --git a/packages/restapi/src/lib/pushstream/DataModifier.ts b/packages/restapi/src/lib/pushstream/DataModifier.ts index 8dc1a7946..e167fc823 100644 --- a/packages/restapi/src/lib/pushstream/DataModifier.ts +++ b/packages/restapi/src/lib/pushstream/DataModifier.ts @@ -6,7 +6,7 @@ import { MessageRawData, MessageEvent, MessageEventType, - Member, + GroupMember, GroupEventType, LeaveGroupEvent, JoinGroupEvent, diff --git a/packages/restapi/src/lib/pushstream/PushStream.ts b/packages/restapi/src/lib/pushstream/PushStream.ts index f7102175e..686c99deb 100644 --- a/packages/restapi/src/lib/pushstream/PushStream.ts +++ b/packages/restapi/src/lib/pushstream/PushStream.ts @@ -22,46 +22,23 @@ export class PushStream extends EventEmitter { private raw: boolean; private options: PushStreamInitializeProps; private chatInstance: Chat; + private listen: STREAM[]; constructor( account: string, private decryptedPgpPvtKey: string, private signer: SignerType, + private _listen: STREAM[], options: PushStreamInitializeProps, private progressHook?: (progress: ProgressHookType) => void ) { super(); this.account = account; - this.pushChatSocket = createSocketConnection({ - user: walletToPCAIP10(account), - socketType: 'chat', - socketOptions: { - autoConnect: options.connection?.auto ?? true, - reconnectionAttempts: options.connection?.retries ?? 3, - }, - env: options.env as ENV, - }); - - this.pushNotificationSocket = createSocketConnection({ - user: pCAIP10ToWallet(this.account), - env: options.env as ENV, - socketOptions: { - autoConnect: options.connection?.auto ?? true, - reconnectionAttempts: options.connection?.retries ?? 3, - }, - }); - - if (!this.pushNotificationSocket) { - throw new Error('Push notification socket not connected'); - } - - if (!this.pushChatSocket) { - throw new Error('Push chat socket not connected'); - } this.raw = options.raw ?? false; this.options = options; + this.listen = _listen; this.chatInstance = new Chat( this.account, @@ -76,32 +53,45 @@ export class PushStream extends EventEmitter { account: string, decryptedPgpPvtKey: string, signer: SignerType, + listen: STREAM[], + env: ENV, progressHook?: (progress: ProgressHookType) => void, options?: PushStreamInitializeProps ): Promise { const defaultOptions: PushStreamInitializeProps = { - listen: [], - env: ENV.LOCAL, raw: false, connection: { auto: true, retries: 3, }, + env: env, }; + if (!listen || listen.length === 0) { + throw new Error( + 'The listen property must have at least one STREAM type.' + ); + } + const settings = { ...defaultOptions, ...options, }; + const accountToUse = settings.overrideAccount || account; + const stream = new PushStream( - account, + accountToUse, decryptedPgpPvtKey, signer, + listen, settings, progressHook ); - await stream.init(); + + if (settings.connection?.auto) { + await stream.connect(); + } return stream; } @@ -150,7 +140,11 @@ export class PushStream extends EventEmitter { } private shouldEmitChat(dataChatId: string): boolean { - if (!this.options.filter?.chats || this.options.filter.chats.length === 0) { + if ( + !this.options.filter?.chats || + this.options.filter.chats.length === 0 || + this.options.filter.chats.includes('*') + ) { return true; } return this.options.filter.chats.includes(dataChatId); @@ -159,122 +153,201 @@ export class PushStream extends EventEmitter { private shouldEmitChannel(dataChannelId: string): boolean { if ( !this.options.filter?.channels || - this.options.filter.channels.length === 0 + this.options.filter.channels.length === 0 || + this.options.filter.channels.includes('*') ) { return true; } return this.options.filter.channels.includes(dataChannelId); } - public async init(): Promise { + public async connect(): Promise { + const shouldInitializeChatSocket = + !this.listen || + this.listen.length === 0 || + this.listen.includes(STREAM.CHAT) || + this.listen.includes(STREAM.CHAT_OPS); + const shouldInitializeNotifSocket = + !this.listen || + this.listen.length === 0 || + this.listen.includes(STREAM.NOTIF) || + this.listen.includes(STREAM.NOTIF_OPS); + + if (shouldInitializeChatSocket && !this.pushChatSocket) { + this.pushChatSocket = createSocketConnection({ + user: walletToPCAIP10(this.account), + socketType: 'chat', + socketOptions: { + autoConnect: true, + reconnectionAttempts: this.options?.connection?.retries ?? 3, + }, + env: this.options?.env as ENV, + }); + + if (!this.pushChatSocket) { + throw new Error('Push chat socket not connected'); + } + } + + if (shouldInitializeNotifSocket && !this.pushNotificationSocket) { + this.pushNotificationSocket = createSocketConnection({ + user: pCAIP10ToWallet(this.account), + env: this.options?.env as ENV, + socketOptions: { + autoConnect: true, + reconnectionAttempts: this.options?.connection?.retries ?? 3, + }, + }); + + if (!this.pushNotificationSocket) { + throw new Error('Push notification socket not connected'); + } + } + const shouldEmit = (eventType: STREAM): boolean => { - if (!this.options.listen || this.options.listen.length === 0) { + if (!this.listen || this.listen.length === 0) { return true; } - return this.options.listen.includes(eventType); + return this.listen.includes(eventType); }; - this.pushChatSocket.on(EVENTS.CHAT_GROUPS, (data: any) => { - try { - const modifiedData = DataModifier.handleChatGroupEvent(data, this.raw); - modifiedData.event = this.convertToProposedName(modifiedData.event); - this.handleToField(modifiedData); - if (this.shouldEmitChat(data.chatId)) { - if ( - data.eventType === GroupEventType.JoinGroup || - data.eventType === GroupEventType.LeaveGroup || - data.eventType === MessageEventType.Request || - data.eventType === GroupEventType.Remove - ) { - if (shouldEmit(STREAM.CHAT)) { - this.emit(STREAM.CHAT, modifiedData); - } - } else { - if (shouldEmit(STREAM.CHAT_OPS)) { - this.emit(STREAM.CHAT_OPS, modifiedData); + if (this.pushChatSocket) { + this.pushChatSocket.on(EVENTS.CHAT_GROUPS, (data: any) => { + try { + console.log(data); + const modifiedData = DataModifier.handleChatGroupEvent( + data, + this.raw + ); + modifiedData.event = this.convertToProposedName(modifiedData.event); + this.handleToField(modifiedData); + if (this.shouldEmitChat(data.chatId)) { + if ( + data.eventType === GroupEventType.JoinGroup || + data.eventType === GroupEventType.LeaveGroup || + data.eventType === MessageEventType.Request || + data.eventType === GroupEventType.Remove + ) { + if (shouldEmit(STREAM.CHAT)) { + this.emit(STREAM.CHAT, modifiedData); + } + } else { + if (shouldEmit(STREAM.CHAT_OPS)) { + this.emit(STREAM.CHAT_OPS, modifiedData); + } } } + } catch (error) { + console.error( + 'Error handling CHAT_GROUPS event:', + error, + 'Data:', + data + ); } - } catch (error) { - console.error( - 'Error handling CHAT_GROUPS event:', - error, - 'Data:', - data - ); - } - }); - - this.pushChatSocket.on(EVENTS.CHAT_RECEIVED_MESSAGE, async (data: any) => { - try { - if ( - data.messageCategory == 'Chat' || - data.messageCategory == 'Request' - ) { - data = await this.chatInstance.decrypt([data]); - data = data[0]; - } + }); - const modifiedData = DataModifier.handleChatEvent(data, this.raw); - modifiedData.event = this.convertToProposedName(modifiedData.event); - this.handleToField(modifiedData); - if (this.shouldEmitChat(data.chatId)) { - if (shouldEmit(STREAM.CHAT)) { - this.emit(STREAM.CHAT, modifiedData); + this.pushChatSocket.on( + EVENTS.CHAT_RECEIVED_MESSAGE, + async (data: any) => { + try { + if ( + data.messageCategory == 'Chat' || + data.messageCategory == 'Request' + ) { + data = await this.chatInstance.decrypt([data]); + data = data[0]; + } + + const modifiedData = DataModifier.handleChatEvent(data, this.raw); + modifiedData.event = this.convertToProposedName(modifiedData.event); + this.handleToField(modifiedData); + if (this.shouldEmitChat(data.chatId)) { + if (shouldEmit(STREAM.CHAT)) { + this.emit(STREAM.CHAT, modifiedData); + } + } + } catch (error) { + console.error( + 'Error handling CHAT_RECEIVED_MESSAGE event:', + error, + 'Data:', + data + ); } } - } catch (error) { - console.error( - 'Error handling CHAT_RECEIVED_MESSAGE event:', - error, - 'Data:', - data - ); - } - }); - - this.pushNotificationSocket.on(EVENTS.USER_FEEDS, (data: any) => { - try { - const modifiedData = DataModifier.mapToNotificationEvent( - data, - NotificationEventType.INBOX, - this.account === data.sender ? 'self' : 'other', - this.raw - ); - - if (this.shouldEmitChannel(modifiedData.from)) { - if (shouldEmit(STREAM.NOTIF)) { - this.emit(STREAM.NOTIF, modifiedData); + ); + } + + if (this.pushNotificationSocket) { + this.pushNotificationSocket.on(EVENTS.USER_FEEDS, (data: any) => { + try { + const modifiedData = DataModifier.mapToNotificationEvent( + data, + NotificationEventType.INBOX, + this.account === data.sender ? 'self' : 'other', + this.raw + ); + + if (this.shouldEmitChannel(modifiedData.from)) { + if (shouldEmit(STREAM.NOTIF)) { + this.emit(STREAM.NOTIF, modifiedData); + } } + } catch (error) { + console.error( + 'Error handling USER_FEEDS event:', + error, + 'Data:', + data + ); } - } catch (error) { - console.error('Error handling USER_FEEDS event:', error, 'Data:', data); - } - }); - - this.pushNotificationSocket.on(EVENTS.USER_SPAM_FEEDS, (data: any) => { - try { - const modifiedData = DataModifier.mapToNotificationEvent( - data, - NotificationEventType.SPAM, - this.account === data.sender ? 'self' : 'other', - this.raw - ); - modifiedData.origin = - this.account === modifiedData.from ? 'self' : 'other'; - if (this.shouldEmitChannel(modifiedData.from)) { - if (shouldEmit(STREAM.NOTIF)) { - this.emit(STREAM.NOTIF, modifiedData); + }); + + this.pushNotificationSocket.on(EVENTS.USER_SPAM_FEEDS, (data: any) => { + try { + const modifiedData = DataModifier.mapToNotificationEvent( + data, + NotificationEventType.SPAM, + this.account === data.sender ? 'self' : 'other', + this.raw + ); + modifiedData.origin = + this.account === modifiedData.from ? 'self' : 'other'; + if (this.shouldEmitChannel(modifiedData.from)) { + if (shouldEmit(STREAM.NOTIF)) { + this.emit(STREAM.NOTIF, modifiedData); + } } + } catch (error) { + console.error( + 'Error handling USER_SPAM_FEEDS event:', + error, + 'Data:', + data + ); } - } catch (error) { - console.error( - 'Error handling USER_SPAM_FEEDS event:', - error, - 'Data:', - data - ); - } - }); + }); + } + } + + public async disconnect(): Promise { + // Disconnect push chat socket if connected + if (this.pushChatSocket) { + this.pushChatSocket.disconnect(); + this.pushChatSocket = null; + console.log('Push chat socket disconnected.'); + } else { + console.log('Push chat socket was not connected.'); + } + + // Disconnect push notification socket if connected + if (this.pushNotificationSocket) { + this.pushNotificationSocket.disconnect(); + this.pushNotificationSocket = null; + console.log('Push notification socket disconnected.'); + } else { + console.log('Push notification socket was not connected.'); + } } } diff --git a/packages/restapi/src/lib/pushstream/pushStreamTypes.ts b/packages/restapi/src/lib/pushstream/pushStreamTypes.ts index 7a499dcaf..bcbbabbe3 100644 --- a/packages/restapi/src/lib/pushstream/pushStreamTypes.ts +++ b/packages/restapi/src/lib/pushstream/pushStreamTypes.ts @@ -2,7 +2,6 @@ import { Rules } from "../types"; import Constants, { ENV } from '../constants'; export type PushStreamInitializeProps = { - listen?: STREAM[]; filter?: { channels?: string[]; chats?: string[]; @@ -13,7 +12,7 @@ export type PushStreamInitializeProps = { }; raw?: boolean; env?: ENV; - enabled?: boolean; + overrideAccount?: string; }; export enum STREAM { @@ -69,14 +68,14 @@ export interface Profile { publicKey: string; } -export interface Member { +export interface GroupMember { address: string; profile: Profile; } export interface Pending { - members: Member[]; - admins: Member[]; + members: GroupMember[]; + admins: GroupMember[]; } export interface GroupMeta { diff --git a/packages/restapi/src/lib/types/index.ts b/packages/restapi/src/lib/types/index.ts index 3438d8292..d0f2a045b 100644 --- a/packages/restapi/src/lib/types/index.ts +++ b/packages/restapi/src/lib/types/index.ts @@ -313,6 +313,32 @@ export enum ConditionType { GUILD = 'GUILD', } +export enum GROUP_RULES_CATEGORY { + PUSH = 'PUSH', + GUILD = 'GUILD', + ERC721 = 'ERC721', + ERC20 = 'ERC20', + CUSTOM_ENDPOINT = 'CustomEndpoint', + INVITE = 'INVITE', +} + +export enum GROUP_RULES_SUB_CATEGORY { + DEFAULT = 'DEFAULT', + HOLDER = 'holder', + GET = 'GET', +} + + +export enum GROUP_RULES_PERMISSION { + ENTRY = 'Entry', + CHAT = 'Chat', +} + +export enum GROUP_INVITER_ROLE { + ADMIN = 'ADMIN', + OWNER = 'OWNER', +} + export type Data = { contract?: string; amount?: number; diff --git a/packages/restapi/tests/lib/channel/sendNotification.test.ts b/packages/restapi/tests/lib/channel/sendNotification.test.ts index 692e8e740..017a33cdb 100644 --- a/packages/restapi/tests/lib/channel/sendNotification.test.ts +++ b/packages/restapi/tests/lib/channel/sendNotification.test.ts @@ -3,7 +3,6 @@ import * as dotenv from 'dotenv'; dotenv.config({ path: path.resolve(__dirname, '../../.env') }); import * as PUSH_PAYLOAD from '../../../src/lib/payloads'; import { expect } from 'chai'; -import Constants from '../../../src/lib/constants'; import { ethers } from 'ethers'; describe('PUSH_PAYLOAD.sendNotification functionality', () => { diff --git a/packages/restapi/tests/lib/channel/subscribeV2.test.ts b/packages/restapi/tests/lib/channel/subscribeV2.test.ts index b1bd3f0b4..6535b322e 100644 --- a/packages/restapi/tests/lib/channel/subscribeV2.test.ts +++ b/packages/restapi/tests/lib/channel/subscribeV2.test.ts @@ -3,8 +3,8 @@ import * as dotenv from 'dotenv'; dotenv.config({ path: path.resolve(__dirname, '../../.env') }); import * as PUSH_CHANNEL from '../../../src/lib/channels/'; import { expect } from 'chai'; -import Constants from '../../../src/lib/constants'; import { ethers } from 'ethers'; +import CONSTANTS from '../../../src/lib/constantsV2'; describe('PUSH_CHANNEL.subscribeV2 functionality', () => { let signer1: any; @@ -27,7 +27,7 @@ describe('PUSH_CHANNEL.subscribeV2 functionality', () => { signer: signer1, channelAddress: 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681', userAddress: `eip155:5:${account1}`, - env: Constants.ENV.STAGING, + env: CONSTANTS.ENV.STAGING, }); expect(res.status).to.be.equal('success'); }); @@ -37,7 +37,7 @@ describe('PUSH_CHANNEL.subscribeV2 functionality', () => { signer: signer1, channelAddress: 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681', userAddress: `eip155:5:${account1}`, - env: Constants.ENV.STAGING, + env: CONSTANTS.ENV.STAGING, }); console.log(res) expect(res.status).to.be.equal('success'); @@ -48,7 +48,7 @@ describe('PUSH_CHANNEL.subscribeV2 functionality', () => { signer: signer1, channelAddress: 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681', userAddress: `eip155:5:${account1}`, - env: Constants.ENV.STAGING, + env: CONSTANTS.ENV.STAGING, userSetting: '2-1-0+2-1', }); expect(res.status).to.be.equal('success'); diff --git a/packages/restapi/tests/lib/chatLowLevel/createGroup.test.ts b/packages/restapi/tests/lib/chatLowLevel/createGroup.test.ts index d9ea55a2e..08dd7cfde 100644 --- a/packages/restapi/tests/lib/chatLowLevel/createGroup.test.ts +++ b/packages/restapi/tests/lib/chatLowLevel/createGroup.test.ts @@ -1,8 +1,6 @@ -import * as chai from 'chai'; import { expect } from 'chai'; -import * as chaiAsPromised from 'chai-as-promised'; import { ethers } from 'ethers'; -import Constants from '../../../src/lib/constants'; +import CONSTANTS from '../../../src/lib/constantsV2'; import { createGroup } from '../../../src/lib/chat'; import { GroupDTO } from '../../../src/lib/types'; import { @@ -11,7 +9,7 @@ import { colors, uniqueNamesGenerator, } from 'unique-names-generator'; -const _env = Constants.ENV.DEV; +const _env = CONSTANTS.ENV.DEV; let account: string; let signer: any; let groupName: string; diff --git a/packages/restapi/tests/lib/chatLowLevel/updateGroup.test.ts b/packages/restapi/tests/lib/chatLowLevel/updateGroup.test.ts index 3d2e32dd1..445e585f3 100644 --- a/packages/restapi/tests/lib/chatLowLevel/updateGroup.test.ts +++ b/packages/restapi/tests/lib/chatLowLevel/updateGroup.test.ts @@ -1,8 +1,6 @@ -import * as chai from 'chai'; import { expect } from 'chai'; -import * as chaiAsPromised from 'chai-as-promised'; import { ethers } from 'ethers'; -import Constants from '../../../src/lib/constants'; +import CONSTANTS from '../../../src/lib/constantsV2'; import { createGroup, updateGroup, @@ -16,7 +14,7 @@ import { colors, uniqueNamesGenerator, } from 'unique-names-generator'; -const _env = Constants.ENV.DEV; +const _env = CONSTANTS.ENV.DEV; let account: string; let signer: any; let account2: string; diff --git a/packages/restapi/tests/lib/progressHook/progressHook.test.ts b/packages/restapi/tests/lib/progressHook/progressHook.test.ts index 4ea5a4155..67eb2fc58 100644 --- a/packages/restapi/tests/lib/progressHook/progressHook.test.ts +++ b/packages/restapi/tests/lib/progressHook/progressHook.test.ts @@ -7,6 +7,8 @@ import * as chaiAsPromised from 'chai-as-promised'; import { create, decryptAuth, get } from '../../../src/lib/user'; import { ethers } from 'ethers'; import Constants, { ENCRYPTION_TYPE } from '../../../src/lib/constants'; +import CONSTANTS from '../../../src/lib/constantsV2'; + import { ProgressHookType, ProgressHookTypeFunction } from '../../../src'; chai.use(chaiAsPromised); import PROGRESSHOOK from './progressHookData'; @@ -15,7 +17,7 @@ import { profileUpdate } from '../../../src/lib/user/profile.updateUser'; import { authUpdate } from '../../../src/lib/user/auth.updateUser'; describe('ProgressHook Tests', () => { - const _env = Constants.ENV.DEV; + const _env = CONSTANTS.ENV.DEV; let provider = ethers.getDefaultProvider(5); let _signer: any; let walletAddress: string; diff --git a/packages/restapi/tests/lib/pushapi/chat.test.ts b/packages/restapi/tests/lib/pushapi/chat.test.ts new file mode 100644 index 000000000..10b88c9d1 --- /dev/null +++ b/packages/restapi/tests/lib/pushapi/chat.test.ts @@ -0,0 +1,66 @@ +import * as path from 'path'; +import * as dotenv from 'dotenv'; +dotenv.config({ path: path.resolve(__dirname, '../../.env') }); + +import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; // Ensure correct import path +import { expect } from 'chai'; +import { ethers } from 'ethers'; +import CONSTANTS from '../../../src/lib/constantsV2'; + +describe('PushAPI.chat functionality', () => { + let userAlice: PushAPI; + let userBob: PushAPI; + let signer1: any; + let account1: string; + let signer2: any; + let account2: string; + const MESSAGE = 'Hey There!!!'; + + beforeEach(async () => { + const WALLET1 = ethers.Wallet.createRandom(); + signer1 = new ethers.Wallet(WALLET1.privateKey); + account1 = WALLET1.address; + + const WALLET2 = ethers.Wallet.createRandom(); + signer2 = new ethers.Wallet(WALLET2.privateKey); + account2 = WALLET2.address; + + userAlice = await PushAPI.initialize(signer1); + userBob = await PushAPI.initialize(signer2); + }); + + it('Should list request ', async () => { + await userAlice.chat.send(account2, { content: MESSAGE }); + const response = await userBob.chat.list('REQUESTS', { + page: 1, + limit: 10, + }); + expect(response).to.be.an('array'); + expect(response.length).to.equal(1); + }); + it('Should list chats ', async () => { + const response = await userAlice.chat.list('CHATS', { + page: 1, + limit: 10, + }); + expect(response).to.be.an('array'); + }); + it('Should send message ', async () => { + const response = await userAlice.chat.send(account2, { + content: 'Hello', + type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT, + }); + expect(response).to.be.an('object'); + }); + it('Should decrypt message ', async () => { + await userAlice.chat.send(account2, { + content: 'Hello', + type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT, + }); + const messagePayloads = await userAlice.chat.history(account2); + const decryptedMessagePayloads = await userBob.chat.decrypt( + messagePayloads + ); + expect(decryptedMessagePayloads).to.be.an('array'); + }); +}); diff --git a/packages/restapi/tests/lib/stream/initialize.test.ts b/packages/restapi/tests/lib/stream/initialize.test.ts index 9ae1527f6..cd250c0ca 100644 --- a/packages/restapi/tests/lib/stream/initialize.test.ts +++ b/packages/restapi/tests/lib/stream/initialize.test.ts @@ -6,11 +6,9 @@ import { ethers } from 'ethers'; import { PushAPI } from '../../../src/lib/pushapi/PushAPI'; import { sendNotification } from '../../../src/lib/payloads/sendNotifications'; import { subscribe, unsubscribe } from '../../../src/lib/channels'; +import CONSTANTS from '../../../src/lib/constantsV2'; -import { ENV } from '../../../src/lib/constants'; -import { STREAM } from '../../../src/lib/pushstream/pushStreamTypes'; import * as util from 'util'; -import { ConditionType } from '../../../src/lib'; describe('PushStream.initialize functionality', () => { it('Should initialize new stream and listen to events', async () => { @@ -21,26 +19,25 @@ describe('PushStream.initialize functionality', () => { const WALLET = ethers.Wallet.createRandom(); const signer = new ethers.Wallet(WALLET.privateKey, provider); const user = await PushAPI.initialize(signer, { - env: ENV.LOCAL, - streamOptions: { raw: true }, + env: CONSTANTS.ENV.LOCAL, }); const WALLET2 = ethers.Wallet.createRandom(); const signer2 = new ethers.Wallet(WALLET2.privateKey, provider); const user2 = await PushAPI.initialize(signer2, { - env: ENV.LOCAL, + env: CONSTANTS.ENV.LOCAL, }); const WALLET3 = ethers.Wallet.createRandom(); const signer3 = new ethers.Wallet(WALLET3.privateKey, provider); const user3 = await PushAPI.initialize(signer3, { - env: ENV.LOCAL, + env: CONSTANTS.ENV.LOCAL, }); const WALLET4 = ethers.Wallet.createRandom(); const signer4 = new ethers.Wallet(WALLET4.privateKey, provider); const user4 = await PushAPI.initialize(signer4, { - env: ENV.LOCAL, + env: CONSTANTS.ENV.LOCAL, }); const GROUP_RULES = { @@ -49,9 +46,9 @@ describe('PushStream.initialize functionality', () => { { any: [ { - type: 'PUSH', - category: 'CustomEndpoint', - subcategory: 'GET', + type: CONSTANTS.CHAT.GROUP.RULES.CONDITION_TYPE.PUSH, + category: CONSTANTS.CHAT.GROUP.RULES.CATEGORY.CUSTOM_ENDPOINT, + subcategory: CONSTANTS.CHAT.GROUP.RULES.SUBCATEGORY.GET, data: { url: 'https://api.ud-staging.com/profile/badges/dead_pixel/validate/{{user_address}}?rule=join', }, @@ -65,9 +62,9 @@ describe('PushStream.initialize functionality', () => { { any: [ { - type: 'PUSH', - category: 'CustomEndpoint', - subcategory: 'GET', + type: CONSTANTS.CHAT.GROUP.RULES.CONDITION_TYPE.PUSH, + category: CONSTANTS.CHAT.GROUP.RULES.CATEGORY.CUSTOM_ENDPOINT, + subcategory: CONSTANTS.CHAT.GROUP.RULES.SUBCATEGORY.GET, data: { url: 'https://api.ud-staging.com/profile/badges/dead_pixel/validate/{{user_address}}?rule=chat', }, @@ -89,9 +86,9 @@ describe('PushStream.initialize functionality', () => { conditions: { any: [ { - type: ConditionType.PUSH, - category: 'ERC20', - subcategory: 'holder', + type: CONSTANTS.CHAT.GROUP.RULES.CONDITION_TYPE.PUSH, + category: CONSTANTS.CHAT.GROUP.RULES.CATEGORY.ERC20, + subcategory: CONSTANTS.CHAT.GROUP.RULES.SUBCATEGORY.HOLDER, data: { contract: 'eip155:1:0xf418588522d5dd018b425E472991E52EBBeEEEEE', @@ -100,11 +97,14 @@ describe('PushStream.initialize functionality', () => { }, }, { - type: ConditionType.PUSH, - category: 'INVITE', - subcategory: 'DEFAULT', + type: CONSTANTS.CHAT.GROUP.RULES.CONDITION_TYPE.PUSH, + category: CONSTANTS.CHAT.GROUP.RULES.CATEGORY.INVITE, + subcategory: CONSTANTS.CHAT.GROUP.RULES.SUBCATEGORY.DEFAULT, data: { - inviterRoles: ['ADMIN', 'OWNER'], + inviterRoles: [ + CONSTANTS.CHAT.GROUP.RULES.INVITER_ROLE.ADMIN, + CONSTANTS.CHAT.GROUP.RULES.INVITER_ROLE.OWNER, + ], }, }, ], @@ -122,7 +122,25 @@ describe('PushStream.initialize functionality', () => { rules: {}, }; - const stream = user.stream; + const stream = await user.stream( + [CONSTANTS.STREAM.CHAT, CONSTANTS.STREAM.CHAT_OPS], + { + // stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS + // more info can be found at push.org/docs/chat + + filter: { + channels: ['*'], + chats: ['*'], + }, + connection: { + auto: false, // should connection be automatic, else need to call stream.connect(); + retries: 3, // number of retries in case of error + }, + raw: true, // enable true to show all data + } + ); + + await stream.connect(); const createEventPromise = ( expectedEvent: string, @@ -164,9 +182,21 @@ describe('PushStream.initialize functionality', () => { // leave admin bug // group creator check remove add - const onDataReceived = createEventPromise('CHAT_OPS', STREAM.CHAT_OPS, 5); - const onMessageReceived = createEventPromise('CHAT', STREAM.CHAT, 4); - const onNoitificationsReceived = createEventPromise('NOTIF', STREAM.NOTIF, 4); + const onDataReceived = createEventPromise( + 'CHAT_OPS', + CONSTANTS.STREAM.CHAT_OPS, + 5 + ); + const onMessageReceived = createEventPromise( + 'CHAT', + CONSTANTS.STREAM.CHAT, + 4 + ); + const onNoitificationsReceived = createEventPromise( + 'NOTIF', + CONSTANTS.STREAM.NOTIF, + 4 + ); // Create and update group const createdGroup = await user.chat.group.create( @@ -174,20 +204,16 @@ describe('PushStream.initialize functionality', () => { CREATE_GROUP_REQUEST_2 ); - const updatedGroup = await user.chat.group.update(createdGroup.chatId, { - description: 'Updated Description', - }); - - const updatedGroup2 = await user.chat.group.add(createdGroup.chatId, { - role: 'ADMIN', - accounts: [signer2.address, signer3.address, signer4.address], - }); - - const w2wRejectRequest = await user2.chat.group.join( - createdGroup.chatId - ); + const updatedGroup = await user.chat.group.update(createdGroup.chatId, { + description: 'Updated Description', + }); + const updatedGroup2 = await user.chat.group.add(createdGroup.chatId, { + role: 'ADMIN', + accounts: [signer2.address, signer3.address, signer4.address], + }); + const w2wRejectRequest = await user2.chat.group.join(createdGroup.chatId); /*const w2wMessageResponse = await user2.chat.send(signer.address, { content: MESSAGE, diff --git a/packages/restapi/tests/lib/userLowLevel/createUser.test.ts b/packages/restapi/tests/lib/userLowLevel/createUser.test.ts index ade9220be..b9551f9ef 100644 --- a/packages/restapi/tests/lib/userLowLevel/createUser.test.ts +++ b/packages/restapi/tests/lib/userLowLevel/createUser.test.ts @@ -7,10 +7,12 @@ import * as chaiAsPromised from 'chai-as-promised'; import { create } from '../../../src/lib/user'; import { ethers } from 'ethers'; import Constants from '../../../src/lib/constants'; +import CONSTANTS from '../../../src/lib/constantsV2'; + chai.use(chaiAsPromised); describe('Create Push Profile', () => { - const _env = Constants.ENV.DEV; + const _env = CONSTANTS.ENV.DEV; let provider = ethers.getDefaultProvider(5); let _signer: any; let walletAddress: string; diff --git a/packages/restapi/tests/lib/userLowLevel/createUserWithProfile.test.ts b/packages/restapi/tests/lib/userLowLevel/createUserWithProfile.test.ts index c160cf25f..2ea321e61 100644 --- a/packages/restapi/tests/lib/userLowLevel/createUserWithProfile.test.ts +++ b/packages/restapi/tests/lib/userLowLevel/createUserWithProfile.test.ts @@ -7,10 +7,12 @@ import * as chaiAsPromised from 'chai-as-promised'; import { createUserWithProfile } from '../../../src/lib/user'; import { ethers } from 'ethers'; import Constants from '../../../src/lib/constants'; +import CONSTANTS from '../../../src/lib/constantsV2'; + chai.use(chaiAsPromised); describe('Create Push Profile with Profile update', () => { - const _env = Constants.ENV.DEV; + const _env = CONSTANTS.ENV.DEV; let provider = ethers.getDefaultProvider(5); let _signer: any; let walletAddress: string; @@ -29,7 +31,7 @@ describe('Create Push Profile with Profile update', () => { it('Push Profile V3', async () => { const name = 'John'; const desc = 'He is web3 dev' - const picture = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAvUlEQVR4AcXBsa3CMBiF0Y+rFNarM0Jqi1EoU2QIj+IxPACTuGYEBnAH7Q+FI/SKe87lflxfGAkzYSbMFr7kMoh6TUTb2ph5PHeiXAZRr4lImAkzYbbkMoh6TUTb2vjFtjaiXneiXAaRMBNmwmzhxN9xI+o1MZPL4ENNzAgzYSbMFk70mohyGcz0mviFMBNmwmzhxLY2Pt2Y2dZG9HjuzAgzYSbMLvfj+mIil8F/9JqYEWbCTJgJM2EmzITZG0wkJb/EapQdAAAAAElFTkSuQmCC', + const picture = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAvUlEQVR4AcXBsa3CMBiF0Y+rFNarM0Jqi1EoU2QIj+IxPACTuGYEBnAH7Q+FI/SKe87lflxfGAkzYSbMFr7kMoh6TUTb2ph5PHeiXAZRr4lImAkzYbbkMoh6TUTb2vjFtjaiXneiXAaRMBNmwmzhxN9xI+o1MZPL4ENNzAgzYSbMFk70mohyGcz0mviFMBNmwmzhxLY2Pt2Y2dZG9HjuzAgzYSbMLvfj+mIil8F/9JqYEWbCTJgJM2EmzITZG0wkJb/EapQdAAAAAElFTkSuQmCC' const user = await createUserWithProfile({ account: account, env: _env, diff --git a/packages/restapi/tests/lib/userLowLevel/getUser.test.ts b/packages/restapi/tests/lib/userLowLevel/getUser.test.ts index ca8fcbd9a..444c7b8a6 100644 --- a/packages/restapi/tests/lib/userLowLevel/getUser.test.ts +++ b/packages/restapi/tests/lib/userLowLevel/getUser.test.ts @@ -2,9 +2,10 @@ import { expect } from 'chai'; import { create, get } from '../../../src/lib/user'; import { ethers } from 'ethers'; import Constants from '../../../src/lib/constants'; +import CONSTANTS from '../../../src/lib/constantsV2'; describe('Get user', () => { - const _env = Constants.ENV.DEV; + const _env = CONSTANTS.ENV.DEV; let provider = ethers.getDefaultProvider(5); let Pkey: string; let _signer: any; diff --git a/packages/restapi/tests/lib/userLowLevel/upgradeUser.test.ts b/packages/restapi/tests/lib/userLowLevel/upgradeUser.test.ts index 1648f369d..2d80a191d 100644 --- a/packages/restapi/tests/lib/userLowLevel/upgradeUser.test.ts +++ b/packages/restapi/tests/lib/userLowLevel/upgradeUser.test.ts @@ -4,13 +4,15 @@ import * as chaiAsPromised from 'chai-as-promised'; import { create, get } from '../../../src/lib/user'; import { ethers } from 'ethers'; import Constants from '../../../src/lib/constants'; +import CONSTANTS from '../../../src/lib/constantsV2'; + import { upgrade } from '../../../src/lib/user/upgradeUser'; import { decryptPGPKey } from '../../../src/lib/helpers'; chai.use(chaiAsPromised); describe('Upgrade user keys', () => { const upgradationVersion = Constants.ENC_TYPE_V3; - const _env = Constants.ENV.DEV; + const _env = CONSTANTS.ENV.DEV; let provider = ethers.getDefaultProvider(5); let _signer: any; let walletAddress: string;