@@ -15,19 +15,18 @@ import { sha256, stringify } from '@dm3-org/dm3-lib-shared';
1515import { StorageAPI } from '@dm3-org/dm3-lib-storage' ;
1616import { submitEnvelopsToReceiversDs } from '../api/ds/submitEnvelopsToReceiversDs' ;
1717import { Conversations } from '../conversation/Conversations' ;
18- import { Contact } from '../conversation/types' ;
18+ import { Contact , Conversation } from '../conversation/types' ;
1919import { renderMessage } from './renderer/renderMessage' ;
2020import { MessageModel , MessageSource } from './types' ;
2121
2222export class Messages {
2323 private readonly storageApi : StorageAPI ;
24- private readonly conversations : Conversations ;
25-
24+ private readonly contacts : Contact [ ] ;
2625 private readonly _messages : MessageModel [ ] ;
27-
2826 private readonly senderAccount : Account ;
2927 private readonly senderProfileKeys : ProfileKeys ;
3028 private readonly receiver : Contact ;
29+ private hydrateFn : ( contact : Contact ) => Promise < Conversation > ;
3130
3231 constructor (
3332 storageApi : StorageAPI ,
@@ -37,11 +36,13 @@ export class Messages {
3736 receiver : Contact ,
3837 ) {
3938 this . storageApi = storageApi ;
40- this . conversations = conversations ;
39+ this . contacts = conversations . list . map ( ( c ) => c . contact ) ;
4140 this . senderAccount = senderAccount ;
4241 this . senderProfileKeys = senderProfileKeys ;
4342 this . receiver = receiver ;
4443 this . _messages = [ ] ;
44+ // TODO: can we make this a pure/static to reduce complexity?
45+ this . hydrateFn = conversations . hydrateExistingContactAsync ;
4546 }
4647
4748 get meta ( ) {
@@ -90,39 +91,39 @@ export class Messages {
9091 }
9192
9293 //Find the recipient of the message in the contact list
93- const recipient = this . conversations . list . find (
94- ( c ) => c . contact . account . ensName === contact ,
94+ const recipient = this . contacts . find (
95+ // #needed
96+ ( c ) => c . account . ensName === contact ,
9597 ) ;
9698 /**
9799 * Check if the recipient has a PublicEncrptionKey
98100 * if not only keep the msg at the senders storage
99101 */
100102 const recipientIsDm3User =
101- ! ! recipient ?. contact . account . profile ?. publicEncryptionKey ;
103+ ! ! recipient ?. account . profile ?. publicEncryptionKey ;
102104
103105 //If the recipient is a dm3 user we can send the message to the delivery service
104106 if ( recipientIsDm3User ) {
105107 return await this . _dispatchMessage (
106108 contact ,
107- recipient . contact ,
109+ recipient ,
108110 message ,
109111 ) ;
110112 }
111113
112114 //There are cases were a messages is already to be send even though the contract hydration is not finished yet.
113115 //This happens if a message has been picked up from the delivery service and the clients sends READ_RECEIVE or READ_OPENED acknowledgements
114116 //In that case we've to check again to the if the user is a DM3 user, before we decide to keep the message
115- const potentialReceiver = this . conversations . list . find (
116- ( c ) => c . contact . account . ensName === contact ,
117+ const potentialReceiver = this . contacts . find (
118+ // #needed
119+ ( c ) => c . account . ensName === contact ,
117120 ) ;
118121
119122 //This should normally not happen, since the contact should be already in the contact list
120123 if ( ! potentialReceiver ) {
121124 return await this . _haltMessage ( contact , message ) ;
122125 }
123- const hydratedC = await this . conversations . hydrateExistingContactAsync (
124- potentialReceiver . contact ,
125- ) ;
126+ const hydratedC = await this . hydrateFn ( potentialReceiver ) ;
126127
127128 //If the user is a DM3 user we can send the message to the delivery service
128129 if ( hydratedC . contact . account . profile ?. publicEncryptionKey ) {
0 commit comments