-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathConversations.ts
468 lines (441 loc) · 16.9 KB
/
Conversations.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import { keystore } from '@xmtp/proto'
import { Client, InboxId } from './Client'
import { ConversationVersion } from './Conversation'
import { DecodedMessage } from './DecodedMessage'
import { DisappearingMessageSettings } from './DisappearingMessageSettings'
import { Dm, DmParams } from './Dm'
import { Group, GroupParams } from './Group'
import { ConversationOptions } from './types/ConversationOptions'
import { CreateGroupOptions } from './types/CreateGroupOptions'
import { DecodedMessageUnion } from './types/DecodedMessageUnion'
import { DefaultContentTypes } from './types/DefaultContentType'
import { EventTypes } from './types/EventTypes'
import { PermissionPolicySet } from './types/PermissionPolicySet'
import * as XMTPModule from '../index'
import {
Address,
ConsentState,
Conversation,
ConversationId,
ConversationTopic,
ConversationType,
MessageId,
} from '../index'
import { getAddress } from '../utils/address'
export default class Conversations<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
> {
client: Client<ContentTypes>
private subscriptions: { [key: string]: { remove: () => void } } = {}
constructor(client: Client<ContentTypes>) {
this.client = client
}
/**
* This method returns a group by the group id if that group exists in the local database.
* To get the latest list of groups from the network, call sync() first.
*
* @returns {Promise<Group>} A Promise that resolves to a Group or undefined if not found.
*/
async findGroup(
groupId: ConversationId
): Promise<Group<ContentTypes> | undefined> {
return await XMTPModule.findGroup(this.client, groupId)
}
/**
* This method returns a Dm by the inboxId if that dm exists in the local database.
* To get the latest list of dms from the network, call sync() first.
*
* @returns {Promise<Dm>} A Promise that resolves to a Dm or undefined if not found.
*/
async findDmByInboxId(
inboxId: InboxId
): Promise<Dm<ContentTypes> | undefined> {
return await XMTPModule.findDmByInboxId(this.client, inboxId)
}
/**
* This method returns a Dm by the address if that dm exists in the local database.
* To get the latest list of dms from the network, call sync() first.
*
* @returns {Promise<Dm>} A Promise that resolves to a Dm or undefined if not found.
*/
async findDmByAddress(
address: Address
): Promise<Dm<ContentTypes> | undefined> {
return await XMTPModule.findDmByAddress(this.client, address)
}
/**
* This method returns a conversation by the topic if that conversation exists in the local database.
* To get the latest list of conversations from the network, call sync() first.
*
* @returns {Promise<Conversation>} A Promise that resolves to a Conversation or undefined if not found.
*/
async findConversationByTopic(
topic: ConversationTopic
): Promise<Conversation<ContentTypes> | undefined> {
return await XMTPModule.findConversationByTopic(this.client, topic)
}
/**
* This method returns a conversation by the conversation id if that conversation exists in the local database.
* To get the latest list of conversations from the network, call sync() first.
*
* @returns {Promise<Conversation>} A Promise that resolves to a Conversation or undefined if not found.
*/
async findConversation(
conversationId: ConversationId
): Promise<Conversation<ContentTypes> | undefined> {
return await XMTPModule.findConversation(this.client, conversationId)
}
/**
* This method returns a message by the message id if that message exists in the local database.
* To get the latest list of messages from the network, call sync() first.
*
* @returns {Promise<DecodedMessage>} A Promise that resolves to a DecodedMessage or undefined if not found.
*/
async findMessage(
messageId: MessageId
): Promise<DecodedMessageUnion<ContentTypes> | undefined> {
return await XMTPModule.findMessage(this.client.installationId, messageId)
}
async fromWelcome(
encryptedMessage: string
): Promise<Conversation<ContentTypes>> {
try {
return await XMTPModule.processWelcomeMessage(
this.client,
encryptedMessage
)
} catch (e) {
console.info('ERROR in processWelcomeMessage()', e)
throw e
}
}
/**
* Creates a new conversation.
*
* This method creates a new conversation with the specified peer address and context.
*
* @param {Address} peerAddress - The address of the peer to create a conversation with.
* @param {DisappearingMessageSettings} disappearingMessageSettings - The disappearing message settings for this dm or undefined.
* @returns {Promise<Conversation>} A Promise that resolves to a Conversation object.
*/
async newConversation(
peerAddress: Address,
disappearingMessageSettings?: DisappearingMessageSettings | undefined
): Promise<Conversation<ContentTypes>> {
const checksumAddress = getAddress(peerAddress)
return await XMTPModule.findOrCreateDm(
this.client,
checksumAddress,
disappearingMessageSettings?.disappearStartingAtNs,
disappearingMessageSettings?.retentionDurationInNs
)
}
/**
* Creates a new conversation.
*
* This method creates a new conversation with the specified peer address.
*
* @param {Address} peerAddress - The address of the peer to create a conversation with.
* @param {DisappearingMessageSettings} disappearingMessageSettings - The disappearing message settings for this dm or undefined.
* @returns {Promise<Dm>} A Promise that resolves to a Dm object.
*/
async findOrCreateDm(
peerAddress: Address,
disappearingMessageSettings?: DisappearingMessageSettings | undefined
): Promise<Dm<ContentTypes>> {
return await XMTPModule.findOrCreateDm(
this.client,
peerAddress,
disappearingMessageSettings?.disappearStartingAtNs,
disappearingMessageSettings?.retentionDurationInNs
)
}
/**
* Creates a new conversation.
*
* This method creates a new conversation with the specified peer inboxId.
*
* @param {InboxId} peerInboxId - The inboxId of the peer to create a conversation with.
* @param {DisappearingMessageSettings} disappearingMessageSettings - The disappearing message settings for this dm or undefined.
* @returns {Promise<Dm>} A Promise that resolves to a Dm object.
*/
async findOrCreateDmWithInboxId(
peerInboxId: InboxId,
disappearingMessageSettings?: DisappearingMessageSettings | undefined
): Promise<Dm<ContentTypes>> {
return await XMTPModule.findOrCreateDmWithInboxId(
this.client,
peerInboxId,
disappearingMessageSettings?.disappearStartingAtNs,
disappearingMessageSettings?.retentionDurationInNs
)
}
/**
* Creates a new group.
*
* This method creates a new group with the specified peer addresses and options.
*
* @param {Address[]} peerAddresses - The addresses of the peers to create a group with.
* @param {CreateGroupOptions} opts - The options to use for the group.
* @returns {Promise<Group<ContentTypes>>} A Promise that resolves to a Group object.
*/
async newGroup(
peerAddresses: Address[],
opts?: CreateGroupOptions | undefined
): Promise<Group<ContentTypes>> {
return await XMTPModule.createGroup(
this.client,
peerAddresses,
opts?.permissionLevel,
opts?.name,
opts?.imageUrlSquare,
opts?.description,
opts?.disappearingMessageSettings?.disappearStartingAtNs,
opts?.disappearingMessageSettings?.retentionDurationInNs
)
}
/**
* Creates a new group with custom permissions.
*
* This method creates a new group with the specified peer addresses and options.
*
* @param {Address[]} peerAddresses - The addresses of the peers to create a group with.
* @param {PermissionPolicySet} permissionPolicySet - The permission policy set to use for the group.
* @param {CreateGroupOptions} opts - The options to use for the group.
* @returns {Promise<Group<ContentTypes>>} A Promise that resolves to a Group object.
*/
async newGroupCustomPermissions(
peerAddresses: Address[],
permissionPolicySet: PermissionPolicySet,
opts?: CreateGroupOptions | undefined
): Promise<Group<ContentTypes>> {
return await XMTPModule.createGroupCustomPermissions(
this.client,
peerAddresses,
permissionPolicySet,
opts?.name,
opts?.imageUrlSquare,
opts?.description,
opts?.disappearingMessageSettings?.disappearStartingAtNs,
opts?.disappearingMessageSettings?.retentionDurationInNs
)
}
/**
* Creates a new group.
*
* This method creates a new group with the specified peer inboxIds and options.
*
* @param {InboxId[]} peerInboxIds - The inboxIds of the peers to create a group with.
* @param {CreateGroupOptions} opts - The options to use for the group.
* @returns {Promise<Group<ContentTypes>>} A Promise that resolves to a Group object.
*/
async newGroupWithInboxIds(
peerInboxIds: InboxId[],
opts?: CreateGroupOptions | undefined
): Promise<Group<ContentTypes>> {
return await XMTPModule.createGroupWithInboxIds(
this.client,
peerInboxIds,
opts?.permissionLevel,
opts?.name,
opts?.imageUrlSquare,
opts?.description,
opts?.disappearingMessageSettings?.disappearStartingAtNs,
opts?.disappearingMessageSettings?.retentionDurationInNs
)
}
/**
* Creates a new group with custom permissions.
*
* This method creates a new group with the specified peer inboxIds and options.
*
* @param {InboxId[]} peerInboxIds - The inboxIds of the peers to create a group with.
* @param {PermissionPolicySet} permissionPolicySet - The permission policy set to use for the group.
* @param {CreateGroupOptions} opts - The options to use for the group.
* @returns {Promise<Group<ContentTypes>>} A Promise that resolves to a Group object.
*/
async newGroupCustomPermissionsWithInboxIds(
peerInboxIds: InboxId[],
permissionPolicySet: PermissionPolicySet,
opts?: CreateGroupOptions | undefined
): Promise<Group<ContentTypes>> {
return await XMTPModule.createGroupCustomPermissionsWithInboxIds(
this.client,
peerInboxIds,
permissionPolicySet,
opts?.name,
opts?.imageUrlSquare,
opts?.description,
opts?.disappearingMessageSettings?.disappearStartingAtNs,
opts?.disappearingMessageSettings?.retentionDurationInNs
)
}
/**
* This method returns a list of all groups that the client is a member of.
* To get the latest list of groups from the network, call syncGroups() first.
* @param {ConversationOptions} opts - The options to specify what fields you want returned for the groups in the list.
* @param {number} limit - Limit the number of groups returned in the list.
* @param {consentStates} ConsentState[] - Filter the groups by a list of consent states.
*
* @returns {Promise<Group[]>} A Promise that resolves to an array of Group objects.
*/
async listGroups(
opts?: ConversationOptions | undefined,
limit?: number | undefined,
consentStates?: ConsentState[] | undefined
): Promise<Group<ContentTypes>[]> {
return await XMTPModule.listGroups(this.client, opts, limit, consentStates)
}
/**
* This method returns a list of all dms that the client is a member of.
* To get the latest list of dms from the network, call sync() first.
* @param {ConversationOptions} opts - The options to specify what fields you want returned for the dms in the list.
* @param {number} limit - Limit the number of dms returned in the list.
* @param {consentStates} ConsentState[] - Filter the dms by a list of consent states.
*
* @returns {Promise<Dm[]>} A Promise that resolves to an array of Dms objects.
*/
async listDms(
opts?: ConversationOptions | undefined,
limit?: number | undefined,
consentStates?: ConsentState[] | undefined
): Promise<Dm<ContentTypes>[]> {
return await XMTPModule.listDms(this.client, opts, limit, consentStates)
}
/**
* This method returns a list of all conversations that the client is a member of.
* @param {ConversationOptions} opts - The options to specify what fields you want returned for the conversations in the list.
* @param {number} limit - Limit the number of conversations returned in the list.
* @param {consentStates} ConsentState[] - Filter the conversations by a list of consent states.
*
* @returns {Promise<Conversation[]>} A Promise that resolves to an array of Conversation objects.
*/
async list(
opts?: ConversationOptions | undefined,
limit?: number | undefined,
consentStates?: ConsentState[] | undefined
): Promise<Conversation<ContentTypes>[]> {
return await XMTPModule.listConversations(
this.client,
opts,
limit,
consentStates
)
}
/**
* This method returns a list of hmac keys for the conversation to help filter self push notifications
*/
async getHmacKeys(): Promise<keystore.GetConversationHmacKeysResponse> {
return await XMTPModule.getHmacKeys(this.client.installationId)
}
/**
* Executes a network request to fetch the latest list of conversations associated with the client
* and save them to the local state.
*/
async sync() {
await XMTPModule.syncConversations(this.client.installationId)
}
/**
* Executes a network request to sync all active conversations associated with the client
* @param {consentStates} ConsentState[] - Filter the conversations to sync by a list of consent states.
*
* @returns {Promise<number>} A Promise that resolves to the number of conversations synced.
*/
async syncAllConversations(
consentStates: ConsentState[] | undefined = undefined
): Promise<number> {
return await XMTPModule.syncAllConversations(
this.client.installationId,
consentStates
)
}
/**
* This method streams conversations that the client is a member of.
* @param {type} ConversationType - Whether to stream groups, dms, or both
* @returns {Promise<Conversation[]>} A Promise that resolves to an array of Conversation objects.
*/
async stream(
callback: (conversation: Conversation<ContentTypes>) => Promise<void>,
type: ConversationType = 'all'
): Promise<void> {
XMTPModule.subscribeToConversations(this.client.installationId, type)
const subscription = XMTPModule.emitter.addListener(
EventTypes.Conversation,
async ({
installationId,
conversation,
}: {
installationId: string
conversation: Conversation<ContentTypes>
}) => {
if (installationId !== this.client.installationId) {
return
}
if (conversation.version === ConversationVersion.GROUP) {
return await callback(
new Group(this.client, conversation as unknown as GroupParams)
)
} else if (conversation.version === ConversationVersion.DM) {
return await callback(
new Dm(this.client, conversation as unknown as DmParams)
)
}
}
)
this.subscriptions[EventTypes.Conversation] = subscription
}
/**
* Listen for new messages in all conversations.
*
* This method subscribes to all conversations in real-time and listens for incoming and outgoing messages.
* @param {type} ConversationType - Whether to stream messages from groups, dms, or both
* @param {Function} callback - A callback function that will be invoked when a message is sent or received.
* @returns {Promise<void>} A Promise that resolves when the stream is set up.
*/
async streamAllMessages(
callback: (message: DecodedMessageUnion<ContentTypes>) => Promise<void>,
type: ConversationType = 'all'
): Promise<void> {
XMTPModule.subscribeToAllMessages(this.client.installationId, type)
const subscription = XMTPModule.emitter.addListener(
EventTypes.Message,
async ({
installationId,
message,
}: {
installationId: string
message: DecodedMessage
}) => {
if (installationId !== this.client.installationId) {
return
}
await callback(
DecodedMessage.fromObject(
message
) as DecodedMessageUnion<ContentTypes>
)
}
)
this.subscriptions[EventTypes.Message] = subscription
}
/**
* Cancels the stream for new conversations.
*/
cancelStream() {
if (this.subscriptions[EventTypes.Conversation]) {
this.subscriptions[EventTypes.Conversation].remove()
delete this.subscriptions[EventTypes.Conversation]
}
XMTPModule.unsubscribeFromConversations(this.client.installationId)
}
/**
* Cancels the stream for new messages in all conversations.
*/
cancelStreamAllMessages() {
if (this.subscriptions[EventTypes.Message]) {
this.subscriptions[EventTypes.Message].remove()
delete this.subscriptions[EventTypes.Message]
}
XMTPModule.unsubscribeFromAllMessages(this.client.installationId)
}
}