Skip to content

Commit

Permalink
adress pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Nov 22, 2024
1 parent 7d14017 commit 0620b51
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 83 deletions.
77 changes: 6 additions & 71 deletions packages/js-sdk/src/Dm3Sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ describe('Dm3Sdk', () => {
ethers.Wallet.createRandom(),
'http://localhost:3000',
);
});

beforeAll(() => {
axiosMock = new MockAdapter(axios);

//Mock BackendConnector HttpRequests
Expand Down Expand Up @@ -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 () =>
Expand Down Expand Up @@ -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');
});
});
});
8 changes: 4 additions & 4 deletions packages/js-sdk/src/message/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Messages {
this._messages = [];
}

public list() {
public get list() {
return renderMessage(this._messages);
}
public async sendMessage(msg: string) {
Expand All @@ -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(),
},
};

Expand Down Expand Up @@ -241,10 +241,10 @@ export class Messages {
return regex.test(input);
};

private async checkIfEnvelopAreInSizeLimit(
private checkIfEnvelopAreInSizeLimit(
encryptedEnvelops: EncryptionEnvelop[],
receiversMessageSizeLimit: number,
): Promise<boolean> {
): boolean {
try {
const atLeastOneEnvelopIsToLarge = !!encryptedEnvelops
//get the size of each envelop
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
6 changes: 0 additions & 6 deletions packages/js-sdk/src/message/renderer/renderMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0620b51

Please sign in to comment.