From c01798af23f4f8cc2215fa9a529145d0c3bf349d Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Mon, 18 Mar 2024 13:41:59 +0530 Subject: [PATCH] fix: fix channel notifications response (#1166) --- .../lib/channels/getChannelNotifications.ts | 29 +++------- packages/restapi/src/lib/constantsV2.ts | 32 +++++----- .../pushNotification/PushNotificationTypes.ts | 18 +++--- .../src/lib/pushNotification/channel.ts | 47 +++++++-------- .../tests/lib/notification/channel.test.ts | 58 +++++++++++-------- 5 files changed, 89 insertions(+), 95 deletions(-) diff --git a/packages/restapi/src/lib/channels/getChannelNotifications.ts b/packages/restapi/src/lib/channels/getChannelNotifications.ts index 8e813668b..b934999d7 100644 --- a/packages/restapi/src/lib/channels/getChannelNotifications.ts +++ b/packages/restapi/src/lib/channels/getChannelNotifications.ts @@ -1,17 +1,7 @@ import { getCAIPAddress, getAPIBaseUrls, getQueryParams } from '../helpers'; import Constants, { ENV } from '../constants'; import { axiosGet } from '../utils/axiosUtil'; -import { parseApiResponse } from '../utils'; - -/** - * GET /v1/channels/{addressinCAIP} - */ - -enum NotifictaionType { - BROADCAT = 1, - TARGETTED = 3, - SUBSET = 4 - } +import { NotifictaionType } from '../types'; type GetChannelOptionsType = { channel: string; @@ -36,28 +26,23 @@ export const getChannelNotifications = async ( const _channel = await getCAIPAddress(env, channel, 'Channel'); const API_BASE_URL = getAPIBaseUrls(env); - const apiEndpoint = `${API_BASE_URL}/v1/channels`; + const apiEndpoint = `${API_BASE_URL}/v2/channels`; const query = getQueryParams( filter ? { page, limit, notificationType: filter, + raw, } : { page, limit, + raw, } ); const requestUrl = `${apiEndpoint}/${_channel}/notifications?${query}`; - return await axiosGet(requestUrl) - .then((response) => { - if (raw) return {feeds: response.data?.feeds ?? [], itemcount:response.data?.itemcount} ; - else - return { feeds: parseApiResponse(response.data?.feeds?? []), itemcount: response.data?.itemcount} - ; - }) - .catch((err) => { - console.error(`[Push SDK] - API ${requestUrl}: `, err); - }); + return await axiosGet(requestUrl).then((response) => { + return response.data; + }); }; diff --git a/packages/restapi/src/lib/constantsV2.ts b/packages/restapi/src/lib/constantsV2.ts index a1c98d81e..c7662e75f 100644 --- a/packages/restapi/src/lib/constantsV2.ts +++ b/packages/restapi/src/lib/constantsV2.ts @@ -1,7 +1,15 @@ import { ALPHA_FEATURES, ENCRYPTION_TYPE, ENV, MessageType } from './constants'; -import { ChannelListOrderType, ChannelListSortType, ChannelListType } from './pushNotification/PushNotificationTypes'; +import { + ChannelListOrderType, + ChannelListSortType, + ChannelListType, +} from './pushNotification/PushNotificationTypes'; import { ChatListType } from './pushapi/pushAPITypes'; -import { STREAM, SpaceEventType, VideoEventType } from './pushstream/pushStreamTypes'; +import { + STREAM, + SpaceEventType, + VideoEventType, +} from './pushstream/pushStreamTypes'; import { initSpaceData } from './space'; import { ConditionType, @@ -9,6 +17,7 @@ import { GROUP_RULES_CATEGORY, GROUP_RULES_PERMISSION, GROUP_RULES_SUB_CATEGORY, + NotifictaionType, VideoCallStatus, } from './types'; import { initVideoCallData } from './video'; @@ -36,30 +45,27 @@ const CONSTANTS = { VIDEO: { EVENT: VideoEventType, STATUS: VideoCallStatus, - INITIAL_DATA: initVideoCallData + INITIAL_DATA: initVideoCallData, }, SPACE: { EVENT: SpaceEventType, - INITIAL_DATA: initSpaceData + INITIAL_DATA: initSpaceData, }, ALPHA_FEATURES: ALPHA_FEATURES, USER: { ENCRYPTION_TYPE: ENCRYPTION_TYPE }, NOTIFICATION: { - TYPE: { - BROADCAST: 1, - SUBSET: 4, - TARGETTED: 3 - }, + TYPE: NotifictaionType, CHANNEL: { LIST_TYPE: ChannelListType, - } + }, }, FILTER: { CHANNEL_LIST: { SORT: ChannelListSortType, - ORDER: ChannelListOrderType - } - } + ORDER: ChannelListOrderType, + }, + NOTIFICATION_TYPE: NotifictaionType, + }, }; export default CONSTANTS; diff --git a/packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts b/packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts index 9eca06d8e..aee7b9955 100644 --- a/packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts +++ b/packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts @@ -7,7 +7,7 @@ export type SubscriptionOptions = { page?: number; limit?: number; channel?: string; - raw?: boolean + raw?: boolean; }; export type ChannelInfoOptions = { channel?: string; @@ -15,7 +15,7 @@ export type ChannelInfoOptions = { limit?: number; category?: number; setting?: boolean; - raw?: boolean + raw?: boolean; }; export type SubscribeUnsubscribeOptions = { @@ -26,7 +26,7 @@ export type SubscribeUnsubscribeOptions = { export type UserSetting = { enabled: boolean; - value?: number | {lower: number, upper: number}; + value?: number | { lower: number; upper: number }; }; export type AliasOptions = Omit; @@ -121,11 +121,11 @@ export type ChannelFeedsOptions = { page?: number; limit?: number; raw?: boolean; - filter?: NotifictaionType + filter?: NotifictaionType; }; export type ChannelOptions = { - raw: boolean -} + raw: boolean; +}; export type ChannelListOptions = { page?: number; @@ -138,13 +138,13 @@ export enum ChannelListType { ALL = 'all', VERIFIED = 'verified', UNVERIFIED = 'unverified', -}; +} export enum ChannelListSortType { SUBSCRIBER = 'subscribers', -}; +} export enum ChannelListOrderType { ASCENDING = 'asc', DESCENDING = 'desc', -}; \ No newline at end of file +} diff --git a/packages/restapi/src/lib/pushNotification/channel.ts b/packages/restapi/src/lib/pushNotification/channel.ts index abe037cd1..e18a27476 100644 --- a/packages/restapi/src/lib/pushNotification/channel.ts +++ b/packages/restapi/src/lib/pushNotification/channel.ts @@ -26,7 +26,7 @@ import { ChannelSearchOptions, CreateChannelOptions, NotificationOptions, - NotificationSettings + NotificationSettings, } from './PushNotificationTypes'; import { Alias } from './alias'; @@ -47,15 +47,15 @@ export class Channel extends PushNotificationBaseClass { * @param {string} [options.channel] - channel address in caip, defaults to eth caip address * @returns information about the channel if it exists */ - info = async (channel?: string, options?:ChannelOptions ) => { + info = async (channel?: string, options?: ChannelOptions) => { try { - const {raw = true} = options || {}; + const { raw = true } = options || {}; this.checkUserAddressExists(channel); channel = channel ?? getFallbackETHCAIPAddress(this.env!, this.account!); return await PUSH_CHANNEL.getChannel({ channel: channel as string, env: this.env, - raw: raw + raw: raw, }); } catch (error) { throw new Error(`Push SDK Error: API : channel::info : ${error}`); @@ -109,7 +109,7 @@ export class Channel extends PushNotificationBaseClass { limit: options.limit ?? 10, setting: options.setting ?? false, category: options.category, - raw: options.raw + raw: options.raw, }); } else { /** @dev - Fallback to deprecated method when page is not provided ( to ensure backward compatibility ) */ @@ -417,38 +417,32 @@ export class Channel extends PushNotificationBaseClass { } }; - notifications = async(account: string, options?:ChannelFeedsOptions) => { - try{ - const { - page, - limit, - filter = null, - raw = true - } = options || {} - if(account.split(":").length == 2){ - account = pCAIP10ToWallet(account) - } + notifications = async (account: string, options?: ChannelFeedsOptions) => { + try { + const { page, limit, filter = null, raw = true } = options || {}; return await PUSH_CHANNEL.getChannelNotifications({ channel: account as string, env: this.env, filter, raw, page, - limit - }) - } catch(error){ - throw new Error(`Push SDK Error: Contract : channel::notifications : ${error}`); + limit, + }); + } catch (error) { + throw new Error( + `Push SDK Error: Contract : channel::notifications : ${error}` + ); } - } + }; list = async (options?: ChannelListOptions) => { - try{ + try { const { page, limit, sort = ChannelListSortType.SUBSCRIBER, order = ChannelListOrderType.DESCENDING, - } = options || {} + } = options || {}; return await PUSH_CHANNEL.getChannels({ env: this.env, @@ -456,10 +450,9 @@ export class Channel extends PushNotificationBaseClass { limit, sort, order, - }) - - } catch(error){ + }); + } catch (error) { throw new Error(`Push SDK Error: Contract : channel::list : ${error}`); } - } + }; } diff --git a/packages/restapi/tests/lib/notification/channel.test.ts b/packages/restapi/tests/lib/notification/channel.test.ts index 6fdd8dab8..5fd633d2d 100644 --- a/packages/restapi/tests/lib/notification/channel.test.ts +++ b/packages/restapi/tests/lib/notification/channel.test.ts @@ -14,6 +14,7 @@ import { } from 'viem'; import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; import CONSTANTS from '../../../src/lib/constantsV2'; +import { inspect } from 'util'; describe('PushAPI.channel functionality', () => { let userAlice: PushAPI; @@ -63,7 +64,7 @@ describe('PushAPI.channel functionality', () => { // initialisation with signer and provider userKate = await PushAPI.initialize(signer2, { env: ENV.DEV }); // initialisation with signer - userAlice = await PushAPI.initialize(signer2); + userAlice = await PushAPI.initialize(signer2, { env: ENV.DEV }); // TODO: remove signer1 after chat makes signer as optional //initialisation without signer userBob = await PushAPI.initialize(signer1, { env: ENV.DEV }); @@ -91,7 +92,7 @@ describe('PushAPI.channel functionality', () => { const res = await userBob.channel.info( 'eip155:11155111:0xD8634C39BBFd4033c0d3289C4515275102423681', { - raw: false + raw: false, } ); console.log(res.channel_settings); @@ -128,7 +129,7 @@ describe('PushAPI.channel functionality', () => { const res = await userBob.channel.subscribers({ channel: 'eip155:11155111:0x93A829d16DE51745Db0530A0F8E8A9B8CA5370E5', }); - console.log(res) + console.log(res); expect(res).not.null; }); @@ -175,7 +176,7 @@ describe('PushAPI.channel functionality', () => { limit: 10, setting: true, }); - console.log(res) + console.log(res); expect(res).not.null; }); @@ -559,28 +560,37 @@ describe('PushAPI.channel functionality', () => { }, }, ]); - // console.log(res) + // console.log(res) expect(res).not.null; }, 10000000000); }); - describe("notifications", async()=>{ - it("Should fetch channel specific feeds", async()=>{ - const res = await userAlice.channel.notifications("eip155:11155111:0xD8634C39BBFd4033c0d3289C4515275102423681",{raw: false}) - console.log(res) - expect(res).not.null - }) - - it("Should fetch channel specific feeds in raw fomrta", async()=>{ - const res = await userAlice.channel.notifications("eip155:0xD8634C39BBFd4033c0d3289C4515275102423681",{raw: true}) - console.log(res) - expect(res).not.null - }) - - it("Should fetch channel specific feeds broadcast type", async()=>{ - const res = await userAlice.channel.notifications("0xD8634C39BBFd4033c0d3289C4515275102423681",{raw: false, filter: CONSTANTS.NOTIFICATION.TYPE.TARGETTED}) - console.log(res) - expect(res).not.null - }) - }) + describe('notifications', async () => { + it('Should fetch channel specific feeds', async () => { + const res = await userAlice.channel.notifications( + 'eip155:11155111:0xD8634C39BBFd4033c0d3289C4515275102423681', + { raw: false } + ); + console.log(inspect(res, { depth: null })); + expect(res).not.null; + }); + + it('Should fetch channel specific feeds in raw format', async () => { + const res = await userAlice.channel.notifications( + 'eip155:11155111:0xD8634C39BBFd4033c0d3289C4515275102423681', + { raw: true } + ); + console.log(inspect(res, { depth: null })); + expect(res).not.null; + }); + + it('Should fetch channel specific feeds broadcast type', async () => { + const res = await userAlice.channel.notifications( + '0xD8634C39BBFd4033c0d3289C4515275102423681', + { raw: false, filter: CONSTANTS.NOTIFICATION.TYPE.TARGETTED } + ); + console.log(inspect(res, { depth: null })); + expect(res).not.null; + }); + }); });