1+ import { EventEmitter } from 'events' ;
12import {
23 normalizeEnsName ,
34 ProfileKeys ,
@@ -12,7 +13,7 @@ import { BackendConnector } from './api/BackendConnector';
1213import { StorageAPI } from '@dm3-org/dm3-lib-storage' ;
1314import { ethers } from 'ethers' ;
1415import { Tld } from './tld/Tld' ;
15- import { Dm3 } from './Dm3' ;
16+ // import { Dm3 } from './Dm3';
1617import { ITLDResolver } from './tld/nameService/ITLDResolver' ;
1718
1819/**
@@ -40,7 +41,7 @@ export interface Dm3SdkConfig {
4041 _tld ?: ITLDResolver ;
4142}
4243
43- export class Dm3Sdk {
44+ export class Dm3Sdk extends EventEmitter {
4445 private readonly mainnetProvider : ethers . providers . JsonRpcProvider ;
4546 private readonly lukso ?: ethers . providers . ExternalProvider ;
4647
@@ -70,12 +71,38 @@ export class Dm3Sdk {
7071 */
7172 public conversations : Conversations ;
7273
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+
7399 /**
74100 * DM3 TLD
75101 */
76102 private _tld ?: ITLDResolver ;
77103
78104 constructor ( config : Dm3SdkConfig ) {
105+ super ( ) ;
79106 //TODO keep ethers v5 for know but extract into common interface later
80107 this . mainnetProvider = config . mainnetProvider ;
81108 this . nonce = config . nonce ;
@@ -154,10 +181,15 @@ export class Dm3Sdk {
154181 account ,
155182 profileKeys ,
156183 this . addressEnsSubdomain ,
184+ ( event : string , eventData : any ) => {
185+ this . emit ( 'dm3_event' , { event, eventData } ) ;
186+ } ,
157187 ) ;
158188 await conversations . _init ( ) ;
159189
160- return new Dm3 ( conversations , tld ) ;
190+ this . conversations = conversations ;
191+
192+ return this ;
161193 }
162194
163195 /**
0 commit comments