1
+ import { EventEmitter } from 'events' ;
1
2
import {
2
3
normalizeEnsName ,
3
4
ProfileKeys ,
@@ -12,7 +13,7 @@ import { BackendConnector } from './api/BackendConnector';
12
13
import { StorageAPI } from '@dm3-org/dm3-lib-storage' ;
13
14
import { ethers } from 'ethers' ;
14
15
import { Tld } from './tld/Tld' ;
15
- import { Dm3 } from './Dm3' ;
16
+ // import { Dm3 } from './Dm3';
16
17
import { ITLDResolver } from './tld/nameService/ITLDResolver' ;
17
18
18
19
/**
@@ -40,7 +41,7 @@ export interface Dm3SdkConfig {
40
41
_tld ?: ITLDResolver ;
41
42
}
42
43
43
- export class Dm3Sdk {
44
+ export class Dm3Sdk extends EventEmitter {
44
45
private readonly mainnetProvider : ethers . providers . JsonRpcProvider ;
45
46
private readonly lukso ?: ethers . providers . ExternalProvider ;
46
47
@@ -70,12 +71,38 @@ export class Dm3Sdk {
70
71
*/
71
72
public conversations : Conversations ;
72
73
74
+ private _selectedConversationId : string ;
75
+
76
+ getConversations ( ) {
77
+ return this . conversations ;
78
+ }
79
+
80
+ getMessagesByConversation ( ensName ?: string ) {
81
+ if ( ensName ) {
82
+ this . _selectedConversationId = ensName ;
83
+ }
84
+
85
+ if ( ! this . _selectedConversationId ) {
86
+ throw new Error ( 'No conversation selected' ) ;
87
+ }
88
+
89
+ // TODO: ens name might not be the best option to identify the conversation, we should introduce some id
90
+ const selectedConversation = this . conversations . list . find ( c => c . contact . account . ensName === this . _selectedConversationId ) ;
91
+
92
+ if ( ! selectedConversation ) {
93
+ throw new Error ( 'Selected conversation not found' ) ;
94
+ }
95
+
96
+ return selectedConversation . messages ;
97
+ }
98
+
73
99
/**
74
100
* DM3 TLD
75
101
*/
76
102
private _tld ?: ITLDResolver ;
77
103
78
104
constructor ( config : Dm3SdkConfig ) {
105
+ super ( ) ;
79
106
//TODO keep ethers v5 for know but extract into common interface later
80
107
this . mainnetProvider = config . mainnetProvider ;
81
108
this . nonce = config . nonce ;
@@ -154,10 +181,15 @@ export class Dm3Sdk {
154
181
account ,
155
182
profileKeys ,
156
183
this . addressEnsSubdomain ,
184
+ ( event : string , eventData : any ) => {
185
+ this . emit ( 'dm3_event' , { event, eventData } ) ;
186
+ } ,
157
187
) ;
158
188
await conversations . _init ( ) ;
159
189
160
- return new Dm3 ( conversations , tld ) ;
190
+ this . conversations = conversations ;
191
+
192
+ return this ;
161
193
}
162
194
163
195
/**
0 commit comments