-
Notifications
You must be signed in to change notification settings - Fork 45
/
chat.ts
312 lines (293 loc) · 12.4 KB
/
chat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { CONSTANTS, PushAPI } from '@pushprotocol/restapi';
import {
adjectives,
animals,
colors,
uniqueNamesGenerator,
} from 'unique-names-generator';
import { config } from '../config';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { createWalletClient, http } from 'viem';
import { sepolia } from 'viem/chains';
import { STREAM } from '@pushprotocol/restapi/src/lib/pushstream/pushStreamTypes';
import { PushStream } from '@pushprotocol/restapi/src/lib/pushstream/PushStream';
import { ethers } from 'ethers';
// CONFIGS
const { env, showAPIResponse } = config;
/***************** SAMPLE SIGNER GENERATION *********************/
// Uing VIEM
// Random Wallet Signers
// const signer = createWalletClient({
// account: privateKeyToAccount(generatePrivateKey()),
// chain: sepolia,
// transport: http(),
// });
// const signerAddress = signer.account.address;
// const secondSigner = createWalletClient({
// account: privateKeyToAccount(generatePrivateKey()),
// chain: sepolia,
// transport: http(),
// });
// const secondSignerAddress = secondSigner.account.address;
// const thirdSigner = createWalletClient({
// account: privateKeyToAccount(generatePrivateKey()),
// chain: sepolia,
// transport: http(),
// });
// const thirdSignerAddress = thirdSigner.account.address;
// // Dummy Wallet Addresses
// const randomWallet1 = privateKeyToAccount(generatePrivateKey()).address;
// const randomWallet2 = privateKeyToAccount(generatePrivateKey()).address;
// const randomWallet3 = privateKeyToAccount(generatePrivateKey()).address;
/****************************************************************/
/***************** SAMPLE SIGNER GENERATION *********************/
// Uing ETHERS
// Random Wallet Signers
const signer = ethers.Wallet.createRandom();
const signerAddress = signer.address;
const secondSigner = ethers.Wallet.createRandom();
const secondSignerAddress = secondSigner.address;
const thirdSigner = ethers.Wallet.createRandom();
const thirdSignerAddress = thirdSigner.address;
// Dummy Wallet Addresses
const randomWallet1 = ethers.Wallet.createRandom().address;
const randomWallet2 = ethers.Wallet.createRandom().address;
const randomWallet3 = ethers.Wallet.createRandom().address;
/****************************************************************/
/***************** SAMPLE GROUP DATA ****************************/
const groupName = uniqueNamesGenerator({
dictionaries: [adjectives, colors, animals],
});
const groupDescription = uniqueNamesGenerator({
dictionaries: [adjectives, colors, animals],
});
const groupImage =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAvklEQVR4AcXBsW2FMBiF0Y8r3GQb6jeBxRauYRpo4yGQkMd4A7kg7Z/GUfSKe8703fKDkTATZsJsrr0RlZSJ9r4RLayMvLmJjnQS1d6IhJkwE2bT13U/DBzp5BN73xgRZsJMmM1HOolqb/yWiWpvjJSUiRZWopIykTATZsJs5g+1N6KSMiO1N/5DmAkzYTa9Lh6MhJkwE2ZzSZlo7xvRwson3txERzqJhJkwE2bT6+JhoKTMJ2pvjAgzYSbMfgDlXixqjH6gRgAAAABJRU5ErkJggg==';
/***************** SAMPLE GROUP DATA ****************************/
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const eventlistener = async (
stream: PushStream,
eventName: string
): Promise<void> => {
stream.on(eventName, (data: any) => {
if (showAPIResponse) {
console.log('Stream Event Received');
console.log(data);
console.log('\n');
}
});
};
export const runChatClassUseCases = async (): Promise<void> => {
const userAlice = await PushAPI.initialize(signer, { env });
const stream = await userAlice.initStream(
[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: true, // 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
}
);
stream.on(CONSTANTS.STREAM.CONNECT, (a) => {
console.log('Stream Connected');
});
await stream.connect();
stream.on(CONSTANTS.STREAM.DISCONNECT, () => {
console.log('Stream Disconnected');
});
const userBob = await PushAPI.initialize(secondSigner, { env });
const userKate = await PushAPI.initialize(thirdSigner, { env });
// Listen stream events to receive websocket events
console.log(`Listening ${CONSTANTS.STREAM.CHAT} Events`);
eventlistener(stream, CONSTANTS.STREAM.CHAT);
console.log(`Listening ${CONSTANTS.STREAM.CHAT_OPS} Events`);
eventlistener(stream, CONSTANTS.STREAM.CHAT_OPS);
console.log('\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.list');
const aliceChats = await userAlice.chat.list(CONSTANTS.CHAT.LIST_TYPE.CHATS);
const aliceRequests = await userAlice.chat.list(
CONSTANTS.CHAT.LIST_TYPE.REQUESTS
);
if (showAPIResponse) {
console.log(aliceChats);
console.log(aliceRequests);
}
console.log('PushAPI.chat.list | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.latest');
const aliceLatestChatWithBob = await userAlice.chat.latest(
secondSignerAddress
);
if (showAPIResponse) {
console.log(aliceLatestChatWithBob);
}
console.log('PushAPI.chat.latest | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.history');
const aliceChatHistoryWithBob = await userAlice.chat.history(
secondSignerAddress
);
if (showAPIResponse) {
console.log(aliceChatHistoryWithBob);
}
console.log('PushAPI.chat.history | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.send');
const aliceMessagesBob = await userAlice.chat.send(secondSignerAddress, {
content: 'Hello Bob!',
type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT,
});
if (showAPIResponse) {
console.log(aliceMessagesBob);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.chat.send | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.accept');
const bobAcceptsRequest = await userBob.chat.accept(signerAddress);
if (showAPIResponse) {
console.log(bobAcceptsRequest);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.chat.accept | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.reject');
await userKate.chat.send(signerAddress, {
content: 'Sending malicious message',
type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT,
});
const AliceRejectsRequest = await userAlice.chat.reject(thirdSignerAddress);
if (showAPIResponse) {
console.log(AliceRejectsRequest);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.chat.reject | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.block');
const AliceBlocksBob = await userAlice.chat.block([secondSignerAddress]);
if (showAPIResponse) {
console.log(AliceBlocksBob);
}
console.log('PushAPI.chat.block | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.unblock');
const AliceUnblocksBob = await userAlice.chat.unblock([secondSignerAddress]);
if (showAPIResponse) {
console.log(AliceUnblocksBob);
}
console.log('PushAPI.chat.unblock | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.create');
const createdGroup = await userAlice.chat.group.create(groupName, {
description: groupDescription,
image: groupImage,
members: [randomWallet1, randomWallet2],
admins: [],
private: false,
});
const groupChatId = createdGroup.chatId; // to be used in other examples
if (showAPIResponse) {
console.log(createdGroup);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.group.create | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.permissions');
const grouppermissions = await userAlice.chat.group.permissions(groupChatId);
if (showAPIResponse) {
console.log(grouppermissions);
}
console.log('PushAPI.group.permissions | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.info');
const groupInfo = await userAlice.chat.group.info(groupChatId);
if (showAPIResponse) {
console.log(groupInfo);
}
console.log('PushAPI.group.info | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.update');
const updatedGroup = await userAlice.chat.group.update(groupChatId, {
description: 'Updated Description',
});
if (showAPIResponse) {
console.log(updatedGroup);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.group.update | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.add');
const addMember = await userAlice.chat.group.add(groupChatId, {
role: 'MEMBER',
accounts: [randomWallet3],
});
if (showAPIResponse) {
console.log(addMember);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.group.add | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.remove');
const removeMember = await userAlice.chat.group.remove(groupChatId, {
role: 'MEMBER',
accounts: [randomWallet3],
});
if (showAPIResponse) {
console.log(removeMember);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.group.remove | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.join');
const joinGrp = await userBob.chat.group.join(groupChatId);
if (showAPIResponse) {
console.log(joinGrp);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.group.join | Response - 200 OK\n\n');
//-------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.leave');
const leaveGrp = await userBob.chat.group.leave(groupChatId);
if (showAPIResponse) {
console.log(leaveGrp);
}
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.group.leave | Response - 200 OK\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.group.reject');
const sampleGrp = await userAlice.chat.group.create('Sample Grp', {
description: groupDescription,
image: groupImage,
members: [secondSignerAddress], // invite bob
admins: [],
private: true,
});
await userBob.chat.group.reject(sampleGrp.chatId);
await delay(2000); // Delay added to log the events in order
console.log('PushAPI.group.reject | Response - 200 OK\n\n');
};