Skip to content

Commit

Permalink
filter out duplicate messages before adding them via batch
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Sep 20, 2024
1 parent 9e860c8 commit d4afd02
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ export const addMessageBatch =
account.id,
encryptedContactName,
);

const uniqueMessageBatch = messageBatch.filter(
(message, index, self) =>
index ===
self.findIndex((m) => m.messageId === message.messageId),
);

//store each message in the db
const createMessagePromises = messageBatch.map(
const createMessagePromises = uniqueMessageBatch.map(
({
messageId,
createdAt,
Expand Down
69 changes: 69 additions & 0 deletions packages/backend/src/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,75 @@ describe('Storage', () => {
expect(body[0].contact).toEqual(sha256(receiver.account.ensName));
expect(body.length).toBe(1);

const { status: getMessagesStatus, body: messages } = await request(
app,
)
.get(
`/new/bob.eth/getMessages/${sha256(
receiver.account.ensName,
)}`,
)
.set({
authorization: 'Bearer ' + token,
})
.send();

expect(getMessagesStatus).toBe(200);
expect(messages.length).toBe(2);
expect(
JSON.parse(JSON.parse(messages[0]).encryptedEnvelopContainer),
).toStrictEqual(envelop);
});
it('if batch contains duplicates it only creates the message once', async () => {
const messageFactory = MockMessageFactory(
sender,
receiver,
deliveryService,
);
const envelop = await messageFactory.createEncryptedEnvelop(
'Hello1',
);
const { status } = await request(app)
.post(`/new/bob.eth/addMessageBatch`)
.set({
authorization: 'Bearer ' + token,
})
.send({
encryptedContactName: sha256(receiver.account.ensName),
messageBatch: [
{
encryptedEnvelopContainer: JSON.stringify(envelop),
messageId: '123',
createdAt: 1,
isHalted: false,
},
{
encryptedEnvelopContainer: JSON.stringify(envelop),
messageId: '123',
createdAt: 1,
isHalted: false,
},
{
encryptedEnvelopContainer: JSON.stringify(envelop),
messageId: '456',
createdAt: 2,
isHalted: false,
},
],
});
expect(status).toBe(200);

const { body } = await request(app)
.get(`/new/bob.eth/getConversations`)
.set({
authorization: 'Bearer ' + token,
})
.send();

expect(status).toBe(200);
expect(body[0].contact).toEqual(sha256(receiver.account.ensName));
expect(body.length).toBe(1);

const { status: getMessagesStatus, body: messages } = await request(
app,
)
Expand Down

0 comments on commit d4afd02

Please sign in to comment.