Skip to content

Commit b820c01

Browse files
committed
remove circular structure
1 parent 4d8661e commit b820c01

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

packages/js-sdk/src/message/Messages.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ import { sha256, stringify } from '@dm3-org/dm3-lib-shared';
1515
import { StorageAPI } from '@dm3-org/dm3-lib-storage';
1616
import { submitEnvelopsToReceiversDs } from '../api/ds/submitEnvelopsToReceiversDs';
1717
import { Conversations } from '../conversation/Conversations';
18-
import { Contact } from '../conversation/types';
18+
import { Contact, Conversation } from '../conversation/types';
1919
import { renderMessage } from './renderer/renderMessage';
2020
import { MessageModel, MessageSource } from './types';
2121

2222
export 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

Comments
 (0)