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

fix: added settings #762

Merged
merged 1 commit into from
Oct 8, 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
7 changes: 4 additions & 3 deletions packages/restapi/src/lib/channels/subscribeV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
} from './signature.helpers';
import Constants, { ENV } from '../constants';
import { SignerType } from '../types';

export type SubscribeOptionsV2Type = {
signer: SignerType;
channelAddress: string;
userAddress: string;
userSetting?: string;
settings?: string;
verifyingContractAddress?: string;
env?: ENV;
onSuccess?: () => void;
Expand All @@ -28,7 +29,7 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
signer,
channelAddress,
userAddress,
userSetting = undefined,
settings = undefined,
verifyingContractAddress,
env = Constants.ENV.PROD,
onSuccess,
Expand Down Expand Up @@ -72,7 +73,7 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
channelCAIPDetails.address,
userCAIPDetails.address,
'Subscribe',
userSetting
settings
),
};
// sign a message using EIP712
Expand Down
26 changes: 17 additions & 9 deletions packages/restapi/src/lib/pushNotification/PushNotificationTypes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ProgressHookType } from '../types';
import { GetAliasInfoOptionsType } from '../alias';
import {
ADDITIONAL_META_TYPE,
} from '../../lib/payloads/constants';
import { ADDITIONAL_META_TYPE } from '../../lib/payloads/constants';

export type SubscriptionOptions = {
account?: string;
Expand All @@ -16,6 +14,12 @@ export type ChannelInfoOptions = {
export type SubscribeUnsubscribeOptions = {
onSuccess?: () => void;
onError?: (err: Error) => void;
settings?: UserSetting[];
};

export type UserSetting = {
enabled: boolean;
value?: number;
};

export type AliasOptions = Omit<GetAliasInfoOptionsType, 'env'>;
Expand Down Expand Up @@ -50,6 +54,10 @@ export type IPayload = {
body?: string;
cta?: string;
embed?: string;
index?: {
index: number;
value?: number;
};
meta?: {
domain?: string;
type: `${ADDITIONAL_META_TYPE}+${number}`;
Expand Down Expand Up @@ -92,13 +100,13 @@ export type CreateChannelOptions = {
};

export type NotificationSetting = {
type: number,
default: number,
description: string,
type: number;
default: number;
description: string;
data?: {
upper: number;
lower: number;
}
}
};
};

export type NotificationSettings = NotificationSetting[]
export type NotificationSettings = NotificationSetting[];
9 changes: 6 additions & 3 deletions packages/restapi/src/lib/pushNotification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class Notification extends PushNotificationBaseClass {
options?: SubscribeUnsubscribeOptions
) => {
try {
const { onSuccess, onError } = options || {};
const { onSuccess, onError, settings } = options || {};
// Vaidatiions
// validates if signer object is present
this.checkSignerObjectExists();
Expand All @@ -142,11 +142,14 @@ export class Notification extends PushNotificationBaseClass {
this.account!,
parseInt(caipDetail?.networkId as string)
);
return await PUSH_CHANNEL.subscribe({
// convert the setting to minimal version
const minimalSetting = this.getMinimalUserSetting(settings!)
return await PUSH_CHANNEL.subscribeV2({
signer: this.signer!,
channelAddress: channel,
userAddress: userAddressInCaip,
env: this.env,
settings: settings? '' : minimalSetting,
onSuccess: onSuccess,
onError: onError,
});
Expand Down Expand Up @@ -188,7 +191,7 @@ export class Notification extends PushNotificationBaseClass {
this.account!,
parseInt(caipDetail?.networkId as string)
);
return await PUSH_CHANNEL.unsubscribe({
return await PUSH_CHANNEL.unsubscribeV2({
signer: this.signer!,
channelAddress: channel,
userAddress: userAddressInCaip,
Expand Down
44 changes: 44 additions & 0 deletions packages/restapi/src/lib/pushNotification/pushNotificationBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
NotificationOptions,
CreateChannelOptions,
NotificationSettings,
UserSetting,
} from './PushNotificationTypes';
import CONFIG, * as config from '../config';
import { getAccountAddress } from '../chat/helpers';
Expand All @@ -27,6 +28,8 @@ const LENGTH_UPPER_LIMIT = 125;
const LENGTH_LOWER_LIMTI = 1;
const SETTING_DELIMITER = '-';
const SETTING_SEPARATOR = '+';
const SLIDER_TYPE = 2;
const BOOLEAN_TYPE = 1;

export const FEED_MAP = {
INBOX: false,
Expand Down Expand Up @@ -142,6 +145,20 @@ export class PushNotificationBaseClass {
}
const notificationType = this.getNotificationType(recipients, channel);
const identityType = IDENTITY_TYPE.DIRECT_PAYLOAD;
// fetch the minimal version based on conifg that was passed
let index;
if (options.payload?.index) {
if (options.payload?.index.value) {
index =
options.payload.index.index +
SETTING_DELIMITER +
SLIDER_TYPE +
SETTING_DELIMITER +
options.payload.index.value;
} else {
index = options.payload.index.index + SETTING_DELIMITER + BOOLEAN_TYPE;
}
}
const notificationPayload: ISendNotificationInputOptions = {
signer: signer,
channel: channel,
Expand All @@ -157,6 +174,7 @@ export class PushNotificationBaseClass {
etime: options.config?.expiry,
silent: options.config?.silent,
additionalMeta: options.payload?.meta,
index: options.payload?.index ? index : '',
},
recipients: notificationType.recipient,
graph: options.advanced?.graph,
Expand Down Expand Up @@ -681,4 +699,30 @@ export class PushNotificationBaseClass {
description: notificationSettingDescription.replace(/^\+/, ''),
};
}

protected getMinimalUserSetting(setting: UserSetting[]) {
let userSetting = '';
let numberOfSettings = 0;
for (let i = 0; i < setting.length; i++) {
const ele = setting[i];
const enabled = ele.enabled ? 1 : 0;
if (ele.enabled) numberOfSettings++;
// slider type
if (Object.keys(ele).includes('value')) {
userSetting =
userSetting +
SLIDER_TYPE +
SETTING_DELIMITER +
enabled +
SETTING_DELIMITER +
ele.value;
} else {
// boolean type
userSetting = userSetting + BOOLEAN_TYPE + SETTING_DELIMITER + enabled;
}
if (i != setting.length - 1)
userSetting = userSetting + SETTING_SEPARATOR;
}
return numberOfSettings + SETTING_SEPARATOR + userSetting;
}
}
128 changes: 128 additions & 0 deletions packages/restapi/tests/lib/pushNotification/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// import * as path from 'path';
// import * as dotenv from 'dotenv';
// dotenv.config({ path: path.resolve(__dirname, '../../../.env') });
// import { expect } from 'chai';
// import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
// import { PushNotificationBaseClass } from '../../../src/lib/pushNotification/pushNotificationBase';
// import * as config from '../../../src/lib/config';
// import {
// createWalletClient,
// http,
// getContract,
// createPublicClient,
// } from 'viem';
// import { abi } from './tokenABI';
// import { goerli, polygonMumbai } from 'viem/chains';
// import { BigNumber, ethers } from 'ethers';

// enum ENV {
// PROD = 'prod',
// STAGING = 'staging',
// DEV = 'dev',
// /**
// * **This is for local development only**
// */
// LOCAL = 'local',
// }
// describe.only('test', () => {
// const signer = createWalletClient({
// account: privateKeyToAccount(`0x${process.env['WALLET_PRIVATE_KEY']}`),
// chain: goerli,
// transport: http('https://goerli.blockpi.network/v1/rpc/public'),
// });

// const signer3 = createWalletClient({
// account: privateKeyToAccount(`0x${process.env['WALLET_PRIVATE_KEY']}`),
// chain: polygonMumbai,
// transport: http(),
// });

// const provider = new ethers.providers.JsonRpcProvider(
// 'https://goerli.blockpi.network/v1/rpc/public'
// );
// const signer2 = new ethers.Wallet(
// `0x${process.env['WALLET_PRIVATE_KEY']}`,
// provider
// );

// it.only('Test minimal conversion', async () => {
// const account2 = await signer2.getAddress();
// const viemUser = new PushNotificationBaseClass(
// signer,
// ENV.STAGING,
// account2
// );
// viemUser.getMinimalUserSetting([
// { enabled: true },
// { enabled: false, value: 10 },
// { enabled: false },
// { enabled: true, value: 10 },
// ]);
// });
// it('testing with viem', async () => {
// const account2 = await signer2.getAddress();
// const viemUser = new PushNotificationBaseClass(signer, ENV.STAGING, account2)
// const contract = viemUser.createContractInstance("0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C", config.ABIS.CORE, goerli)
// const res = await viemUser.fetchUpdateCounter(contract, account2);
// console.log(res)
// const viemContract = await userViem.createContractInstance(
// '0x2b9bE9259a4F5Ba6344c1b1c07911539642a2D33',
// abi,
// goerli
// );
// const balance = await userViem.fetchBalance(
// viemContract,
// '0xD8634C39BBFd4033c0d3289C4515275102423681'
// );
// console.log(balance);
// const allowance = await userViem.fetchAllownace(
// viemContract,
// '0xD8634C39BBFd4033c0d3289C4515275102423681',
// '0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C'
// );
// console.log(allowance);
// const approveAmount = ethers.BigNumber.from(10000);
// const approveRes = await userViem.approveToken(
// viemContract,
// '0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C',
// approveAmount
// );
// console.log(approveRes);

// const addDelegate = await userViem.delegate.add(
// 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681'
// );
// console.log(addDelegate);
// });

// it.only('test with ethers', async () => {
// const account2 = await signer2.getAddress();
// const userEthers = new PushNotificationBaseClass(signer2, ENV.STAGING, account2,);
// const contract = userEthers.createContractInstance("0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C", config.ABIS.CORE, goerli)
// const res = await userEthers.fetchUpdateCounter(contract, account2);
// console.log(res)
// const ethersContract = await userEthers.createContractInstance(
// '0x2b9bE9259a4F5Ba6344c1b1c07911539642a2D33',
// abi,
// goerli
// );
// const balance2 = await userEthers.fetchBalance(
// ethersContract,
// '0xD8634C39BBFd4033c0d3289C4515275102423681'
// );
// console.log(balance2);
// const allowance2 = await userEthers.fetchAllownace(
// ethersContract,
// '0xD8634C39BBFd4033c0d3289C4515275102423681',
// '0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C'
// );
// console.log(allowance2);
// const approveAmount2 = ethers.BigNumber.from(10000);
// const approveRes2 = await userEthers.approveToken(
// ethersContract,
// '0xd4E3ceC407cD36d9e3767cD189ccCaFBF549202C',
// approveAmount2
// );
// console.log(approveRes2);
// });
// });
Loading
Loading