Skip to content

Commit

Permalink
fix: merged main
Browse files Browse the repository at this point in the history
  • Loading branch information
akp111 committed Oct 26, 2023
2 parents 87f469d + 4c342a1 commit f7bc7af
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 146 deletions.
13 changes: 13 additions & 0 deletions packages/examples/token-gated-chat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# About Token Gated Chat
Token gated chat shows you how to create group chats using Push Chat that can gated in various ways including token or NFT gating on entry of the group or when a participant of that group wants to send a message

## What's the use case
You can use this example to see the functionality of token gating. Some use cases are:

- Creating your token gated community and hosting that particular chat on your frontend
- Enabling more token use cases and ensuring that conversations that happen in your group are coming from token holders or from participants that have certain on-chain traits (for example: POAPs, ENS domain names, UD domain names, tec)

## Install instructions
1. Navigate to this directory from the terminal
2. do `npm install` or `yarn install`
3. do `yarn start`
59 changes: 59 additions & 0 deletions packages/examples/token-gated-chat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { PushAPI } from '@pushprotocol/restapi';
import { ethers } from 'ethers';

// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();

console.log(`Signer address: ${signer.address} | Signer private key: ${signer.privateKey}`);

// Initialize wallet user, pass 'prod' instead of 'staging' for mainnet apps
const userAlice = await PushAPI.initialize(signer, { env: 'staging' });

// Creating your token gated community
const createTokenGatedGroup = await userAlice.chat.group.create('Push Community', {
description: 'Token gated web3 native chat example', // provide short description of group
image: 'data:image/png;base64,iVBORw0K...', // provide base64 encoded image
members: [], // not needed, rules define this, can omit
admins: [], // not needed as per problem statement, can omit
private: true,
rules: {
"entry": { // entry is based on conditions
"conditions": {
"any": [ // any of the decider should allow entry
{ // decider 1 - If admin or owner invites someone
"any": [
{ // criteria 1
"type": "PUSH",
"category": "INVITE",
"subcategory": "DEFAULT",
"data": {
"inviterRoles": [
"ADMIN",
"OWNER"
]
}
}
]
},
{ // decicder 2 - If wallet holds 1 NFT on polygon testnet
"any": [
{ // criteria 1
type: "PUSH", // define type that rules engine should go for
category: "ERC721", // define it's ERC20 token that you want to check, supports ERC721 as well
subcategory: "holder", // define if you are checking 'holder'
data: {
"contract": "eip155:80001:0x9105D95577575116948F5afcF479254f49F27939",
"comparison": ">=", // what comparison needs to pass
"amount": 1, // amount that needs to passed
"decimals": 18,
}
}
]
}
]
}
}
}
});

console.log("Chat created successfully!", createTokenGatedGroup);
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "automated-chat",
"name": "token-gated-chat",
"version": "1.0.0",
"description": "Example of Push Chat from backend, auto-responding to messages sent",
"main": "src/index.js",
"description": "Example of Push Chat token gated group in action",
"main": "index.js",
"type": "module",
"scripts": {
"build": "rimraf ./build && tsc",
"start": "nodemon",
"inspect": "nodemon --inspect src/index.js",
"dev": "node-dev --respawn --transpile-only src/index.js",
"inspect": "nodemon --inspect index.js",
"dev": "node-dev --respawn --transpile-only index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@pushprotocol/restapi": "1.4.19",
"@pushprotocol/restapi": "latest",
"@pushprotocol/socket": "0.5.2",
"ethers": "5.7.2"
},
Expand Down
Empty file.
42 changes: 0 additions & 42 deletions packages/examples/use-cases/automated-chat/src/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export type IPayload = {
body?: string;
cta?: string;
embed?: string;
index?: {
index: number;
value?: number;
};
category?: number;
meta?: {
domain?: string;
type: `${ADDITIONAL_META_TYPE}+${number}`;
Expand Down
6 changes: 6 additions & 0 deletions packages/restapi/src/lib/pushNotification/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ export class Channel extends PushNotificationBaseClass {
send = async (recipients: string[], options: NotificationOptions) => {
try {
this.checkSignerObjectExists();
const info = await this.info(options.channel?? this.account);
let settings = null;
if(info && info.channel_settings){
settings = JSON.parse(info.channel_settings);
}
const lowLevelPayload = this.generateNotificationLowLevelPayload({
signer: this.signer!,
env: this.env!,
recipients: recipients,
options: options,
channel: options.channel ?? this.account,
settings: settings,
});
return await PUSH_PAYLOAD.sendNotification(lowLevelPayload);
} catch (error) {
Expand Down
36 changes: 21 additions & 15 deletions packages/restapi/src/lib/pushNotification/pushNotificationBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,33 @@ export class PushNotificationBaseClass {
recipients,
options,
channel,
settings,
}: {
signer: SignerType;
env: ENV;
recipients: string[];
options: NotificationOptions;
channel?: string;
settings: any | null;
}): ISendNotificationInputOptions {
if (!channel) {
channel = `${this.account}`;
}
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) {
let index = '';
if (options.payload?.category && settings) {
if (settings[options.payload.category - 1].type == 2) {
index =
options.payload.index.index +
options.payload.category +
SETTING_DELIMITER +
SLIDER_TYPE +
SETTING_DELIMITER +
options.payload.index.value;
} else {
index = options.payload.index.index + SETTING_DELIMITER + BOOLEAN_TYPE;
settings[options.payload.category - 1].default;
}
if (settings[options.payload.category - 1].type == 1) {
index = options.payload.category + SETTING_DELIMITER + BOOLEAN_TYPE;
}
}
const notificationPayload: ISendNotificationInputOptions = {
Expand All @@ -176,7 +179,7 @@ export class PushNotificationBaseClass {
etime: options.config?.expiry,
silent: options.config?.silent,
additionalMeta: options.payload?.meta,
index: options.payload?.index ? index : '',
index: options.payload?.category ? index : '',
},
recipients: notificationType.recipient,
graph: options.advanced?.graph,
Expand Down Expand Up @@ -678,22 +681,25 @@ export class PushNotificationBaseClass {
}
if (ele.type == SLIDER_TYPE) {
if (ele.data) {
const enabled = (ele.data && ele.data.enabled != undefined) ? Number(ele.data.enabled).toString() : DEFAULT_ENABLE_VALUE
const ticker = ele.data.ticker ?? DEFAULT_TICKER_VALUE
const enabled =
ele.data && ele.data.enabled != undefined
? Number(ele.data.enabled).toString()
: DEFAULT_ENABLE_VALUE;
const ticker = ele.data.ticker ?? DEFAULT_TICKER_VALUE;
notificationSetting =
notificationSetting +
SETTING_SEPARATOR +
SLIDER_TYPE +
SETTING_DELIMITER+
enabled+
SETTING_DELIMITER +
enabled +
SETTING_DELIMITER +
ele.default +
SETTING_DELIMITER +
ele.data.lower +
SETTING_DELIMITER +
ele.data.upper +
SETTING_DELIMITER+
ticker
SETTING_DELIMITER +
ticker;

notificationSettingDescription =
notificationSettingDescription +
Expand All @@ -709,7 +715,7 @@ export class PushNotificationBaseClass {
}

protected getMinimalUserSetting(setting: UserSetting[]) {
if(!setting){
if (!setting) {
return null;
}
let userSetting = '';
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/user/getFeedsPerChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const getFeedsPerChannel = async (options: FeedsPerChannelOptionsType) =>
throw new Error('channels cannot be empty');
}
const _channel = await getCAIPAddress(env, channels[0], 'Channel');
const apiEndpoint = `${API_BASE_URL}/v1/users/${_channel}/channels/${_user}/feeds`;
const apiEndpoint = `${API_BASE_URL}/v1/users/${_user}/channels/${_channel}/feeds`;
const queryObj = {
page,
limit: getLimit(limit),
Expand Down
4 changes: 4 additions & 0 deletions packages/restapi/tests/lib/pushNotification/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,14 @@ describe('PushAPI.channel functionality', () => {
body: 'testing with random body',
cta: 'https://google.com/',
embed: 'https://avatars.githubusercontent.com/u/64157541?s=200&v=4',
<<<<<<< HEAD
index: {
index: 1,
value: 10 // for slider type and omit it for boolean type
}
=======
category: 2
>>>>>>> 4c342a19abe3a192cd5555749e224f80188032a4
},
}
);
Expand Down
2 changes: 1 addition & 1 deletion packages/uiweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"date-fns": "^2.28.0",
"emoji-picker-react": "^4.4.9",
"ethers": "^5.6.8",
"font-awesome": "^4.7.0",

"gif-picker-react": "^1.1.0",
"html-react-parser": "^1.4.13",
"livekit-client": "^1.13.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { ChatAndNotificationMainContextProvider, ChatAndNotificationPropsContext, NotificationMainStateContextProvider } from '../../context';
import 'font-awesome/css/font-awesome.min.css';
// import 'font-awesome/css/font-awesome.min.css';
import { Constants } from '../../config';
import { ChatMainStateContextProvider } from '../../context';

Expand Down
Loading

0 comments on commit f7bc7af

Please sign in to comment.