diff --git a/packages/js-sdk/src/Dm3Sdk.test.ts b/packages/js-sdk/src/Dm3Sdk.test.ts index eb2b7097e..14bf478da 100644 --- a/packages/js-sdk/src/Dm3Sdk.test.ts +++ b/packages/js-sdk/src/Dm3Sdk.test.ts @@ -42,9 +42,6 @@ describe('Dm3Sdk', () => { ethers.Wallet.createRandom(), 'http://localhost:3000', ); - }); - - beforeAll(() => { axiosMock = new MockAdapter(axios); //Mock BackendConnector HttpRequests @@ -197,58 +194,6 @@ describe('Dm3Sdk', () => { }); describe('Messages', () => { - it('can send a message', async () => { - const mockTldResolver = { - resolveTLDtoAlias: async () => - `${normalizeEnsName(bob.address)}.addr.test`, - resolveAliasToTLD: async () => 'bob.eth', - } as unknown as ITLDResolver; - - const mockConfig: Dm3SdkConfig = { - mainnetProvider: {} as ethers.providers.JsonRpcProvider, - storageApi: { - addConversation: async () => {}, - addMessage: async () => {}, - } as unknown as StorageAPI, - nonce: '1', - defaultDeliveryService: 'test.io', - addressEnsSubdomain: '.addr.test', - userEnsSubdomain: '.user.test', - resolverBackendUrl: 'resolver.io', - backendUrl: 'http://localhost:4060', - _tld: mockTldResolver, - }; - - const dm3 = await new Dm3Sdk(mockConfig).login({ - profileKeys: alice.profileKeys, - profile: alice.signedUserProfile, - accountAddress: alice.address, - }); - - const msgFactory = MockMessageFactory(alice, bob, deliveryService); - - const msg1 = await msgFactory.createMessage('Hi'); - - expect( - ( - await dm3.conversations.addConversation('bob.eth') - )?.messages.list().length, - ).toBe(0); - await ( - await dm3.conversations.addConversation('bob.eth') - )?.messages.addMessage('bob.eth', msg1); - - expect( - ( - await dm3.conversations.addConversation('bob.eth') - )?.messages.list().length, - ).toBe(1); - expect( - ( - await dm3.conversations.addConversation('bob.eth') - )?.messages.list()[0].envelop.message.message, - ).toBe('Hi'); - }); it('can send a message', async () => { const mockTldResolver = { resolveTLDtoAlias: async () => @@ -278,25 +223,15 @@ describe('Dm3Sdk', () => { }); expect( - ( - await dm3.conversations.addConversation('bob.eth') - )?.messages.list().length, + (await dm3.conversations.addConversation('bob.eth'))?.messages + .list.length, ).toBe(0); - await ( - await dm3.conversations.addConversation('bob.eth') - )?.messages.sendMessage('Hi'); + const c = await dm3.conversations.addConversation('bob.eth'); - expect( - ( - await dm3.conversations.addConversation('bob.eth') - )?.messages.list().length, - ).toBe(1); - expect( - ( - await dm3.conversations.addConversation('bob.eth') - )?.messages.list()[0].envelop.message.message, - ).toBe('Hi'); + await c?.messages.sendMessage('Hi'); + expect(c?.messages.list.length).toBe(1); + expect(c?.messages.list[0].envelop.message.message).toBe('Hi'); }); }); }); diff --git a/packages/js-sdk/src/message/Messages.ts b/packages/js-sdk/src/message/Messages.ts index 3c739cb4e..a59534c8c 100644 --- a/packages/js-sdk/src/message/Messages.ts +++ b/packages/js-sdk/src/message/Messages.ts @@ -44,7 +44,7 @@ export class Messages { this._messages = []; } - public list() { + public get list() { return renderMessage(this._messages); } public async sendMessage(msg: string) { @@ -56,7 +56,7 @@ export class Messages { type: 'NEW', to: this.receiver.account.ensName, from: this.senderAccount.ensName, - timestamp: new Date().getTime(), + timestamp: Date.now(), }, }; @@ -241,10 +241,10 @@ export class Messages { return regex.test(input); }; - private async checkIfEnvelopAreInSizeLimit( + private checkIfEnvelopAreInSizeLimit( encryptedEnvelops: EncryptionEnvelop[], receiversMessageSizeLimit: number, - ): Promise { + ): boolean { try { const atLeastOneEnvelopIsToLarge = !!encryptedEnvelops //get the size of each envelop diff --git a/packages/js-sdk/src/message/renderer/messageTypes/renderReactions.ts b/packages/js-sdk/src/message/renderer/messageTypes/renderReactions.ts index c727cb15f..6dea30543 100644 --- a/packages/js-sdk/src/message/renderer/messageTypes/renderReactions.ts +++ b/packages/js-sdk/src/message/renderer/messageTypes/renderReactions.ts @@ -1,10 +1,12 @@ -import { MessageModel } from '../../types'; +import { MessageActionType, MessageModel } from '../../types'; export const renderReactions = (messages: MessageModel[]) => { //We filter out all messages that are reactions const reactions = messages .filter( - (message) => message.envelop.message.metadata.type === 'REACTION', + (message) => + message.envelop.message.metadata.type === + MessageActionType.REACT, ) .map((reaction) => reaction.envelop); diff --git a/packages/js-sdk/src/message/renderer/renderMessage.ts b/packages/js-sdk/src/message/renderer/renderMessage.ts index 01701c1de..1d23a8222 100644 --- a/packages/js-sdk/src/message/renderer/renderMessage.ts +++ b/packages/js-sdk/src/message/renderer/renderMessage.ts @@ -21,12 +21,6 @@ export const renderMessage = (messages: MessageModel[]) => { const withReply = renderReply(withReactions); const withoutEdited = renderEdit(withReply); - //Sort the messages by timestamp DESC to show them in the right order - // withoutEdited.sort( - // (a, b) => - // b.envelop.message.metadata.timestamp - - // a.envelop.message.metadata.timestamp, - // ); //There a several ways a message can added to the client. I.e via Websocket, multiple DS or from the storage. //This leads occasionally to duplicates we don't want to display.