Skip to content

Commit

Permalink
fix: fix channel notifications response (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 authored Mar 18, 2024
1 parent 4026d87 commit c01798a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 95 deletions.
29 changes: 7 additions & 22 deletions packages/restapi/src/lib/channels/getChannelNotifications.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
});
};
32 changes: 19 additions & 13 deletions packages/restapi/src/lib/constantsV2.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
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,
GROUP_INVITER_ROLE,
GROUP_RULES_CATEGORY,
GROUP_RULES_PERMISSION,
GROUP_RULES_SUB_CATEGORY,
NotifictaionType,
VideoCallStatus,
} from './types';
import { initVideoCallData } from './video';
Expand Down Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export type SubscriptionOptions = {
page?: number;
limit?: number;
channel?: string;
raw?: boolean
raw?: boolean;
};
export type ChannelInfoOptions = {
channel?: string;
page?: number;
limit?: number;
category?: number;
setting?: boolean;
raw?: boolean
raw?: boolean;
};

export type SubscribeUnsubscribeOptions = {
Expand All @@ -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<GetAliasInfoOptionsType, 'env'>;
Expand Down Expand Up @@ -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;
Expand All @@ -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',
};
}
47 changes: 20 additions & 27 deletions packages/restapi/src/lib/pushNotification/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
ChannelSearchOptions,
CreateChannelOptions,
NotificationOptions,
NotificationSettings
NotificationSettings,
} from './PushNotificationTypes';

import { Alias } from './alias';
Expand All @@ -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}`);
Expand Down Expand Up @@ -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 ) */
Expand Down Expand Up @@ -417,49 +417,42 @@ 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,
page,
limit,
sort,
order,
})

} catch(error){
});
} catch (error) {
throw new Error(`Push SDK Error: Contract : channel::list : ${error}`);
}
}
};
}
58 changes: 34 additions & 24 deletions packages/restapi/tests/lib/notification/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
});

Expand Down Expand Up @@ -175,7 +176,7 @@ describe('PushAPI.channel functionality', () => {
limit: 10,
setting: true,
});
console.log(res)
console.log(res);
expect(res).not.null;
});

Expand Down Expand Up @@ -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;
});
});
});

0 comments on commit c01798a

Please sign in to comment.