Skip to content

Commit 59fea01

Browse files
committed
storage lib - fix duplicate conversations bug
1 parent 860b2e6 commit 59fea01

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

packages/lib/storage/src/new/storage.test.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-console */
21
import { stringify } from '@dm3-org/dm3-lib-shared';
32
import { createStorage } from './index';
43
import { makeEnvelop } from './testHelper';
@@ -18,15 +17,13 @@ describe('createStorage Integration Tests', () => {
1817
const s = new Map<string, string>();
1918
return {
2019
read: async (key: string): Promise<Chunk | undefined> => {
21-
console.log('read', key);
2220
const res = await s.get(key);
2321
if (!res) {
2422
return undefined;
2523
}
2624
return JSON.parse(res) as Chunk;
2725
},
2826
write: async (key: string, value: Chunk): Promise<void> => {
29-
console.log('write', key, value);
3027
await s.set(key, stringify(value));
3128
},
3229
s,
@@ -64,6 +61,22 @@ describe('createStorage Integration Tests', () => {
6461
expect(list.length).toBe(1);
6562
expect(list[0]).toBe('bob.eth');
6663
});
64+
it('addConversation - conversationList should not contain duplicates', async () => {
65+
await storageApi.addConversation('bob.eth');
66+
await storageApi.addConversation('max.eth');
67+
await storageApi.addConversation('bob.eth');
68+
//We acreate an newStorageApi to verify that the data is stored in remote storage and not just locally
69+
storageApi = await createStorage('alice.eth', mockSign, {
70+
readStrategy: ReadStrategy.RemoteFirst,
71+
keyValueStoreRemote: remoteKeyValueStore,
72+
});
73+
const list = await storageApi.getConversationList(0);
74+
75+
expect(list.length).toBe(2);
76+
expect(list[0]).toBe('bob.eth');
77+
expect(list[1]).toBe('max.eth');
78+
expect(list[2]).toBe(undefined);
79+
});
6780
it('add new message - stores new message', async () => {
6881
await storageApi.addConversation('bob.eth');
6982
//We acreate an newStorageApi to verify that the data is stored in remote storage and not just locally

packages/lib/storage/src/new/write.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-len */
12
import { Envelop } from '@dm3-org/dm3-lib-messaging';
23
import {
34
MAX_CONVERATION_ENTRIES_PER_CHUNK,
@@ -52,17 +53,29 @@ export async function addConversation(
5253
// with the new conversation added
5354
const key = await getConversationListKey(db, targetChunkIndex);
5455

56+
const conversationIsAlreadyInList =
57+
conversationListChunk &&
58+
conversationListChunk.conversationList.includes(contactEnsName);
59+
60+
const conversationList = !conversationListChunk
61+
? //If the conversation list chunk does not exist, we have to create it and add the conversation as the first element
62+
[contactEnsName]
63+
: //If the conversation list chunk exists, we have to check if the conversation has been already added. If so we can just return the conversation list chunk as it is.
64+
conversationIsAlreadyInList
65+
? [...conversationListChunk.conversationList]
66+
: //If the conversation has not been added yet, we have to add it to the conversation list chunk
67+
[...conversationListChunk.conversationList, contactEnsName];
68+
5569
return {
5670
conversationList: {
5771
key,
58-
conversationList: conversationListChunk
59-
? [...conversationListChunk.conversationList, contactEnsName]
60-
: [contactEnsName],
72+
conversationList,
6173
},
6274
accountManifest: {
6375
...accountMainfest,
64-
conversationListCounter:
65-
accountMainfest.conversationListCounter + 1,
76+
conversationListCounter: conversationListChunk
77+
? accountMainfest.conversationListCounter
78+
: accountMainfest.conversationListCounter + 1,
6679
},
6780
};
6881
}

0 commit comments

Comments
 (0)