diff --git a/packages/backend/src/persistence/storage/postgres/addConversation.ts b/packages/backend/src/persistence/storage/postgres/addConversation.ts index b1ea94ae7..0e65bd3d5 100644 --- a/packages/backend/src/persistence/storage/postgres/addConversation.ts +++ b/packages/backend/src/persistence/storage/postgres/addConversation.ts @@ -1,6 +1,6 @@ import { PrismaClient } from '@prisma/client'; +import { createOrUpdateConversation } from './utils/createOrUpdateConversation'; import { getOrCreateAccount } from './utils/getOrCreateAccount'; -import { getOrCreateConversation } from './utils/getOrCreateConversation'; export const addConversation = (db: PrismaClient) => async ( @@ -10,7 +10,7 @@ export const addConversation = ) => { try { const account = await getOrCreateAccount(db, ensName); - await getOrCreateConversation( + await createOrUpdateConversation( db, account.id, contactName, diff --git a/packages/backend/src/persistence/storage/postgres/utils/createOrUpdateConversation.ts b/packages/backend/src/persistence/storage/postgres/utils/createOrUpdateConversation.ts new file mode 100644 index 000000000..56aaaec3a --- /dev/null +++ b/packages/backend/src/persistence/storage/postgres/utils/createOrUpdateConversation.ts @@ -0,0 +1,42 @@ +import { PrismaClient } from '@prisma/client'; + +export const createOrUpdateConversation = async ( + db: PrismaClient, + accountId: string, + encryptedContactName: string, + encryptedProfileLocation: string, +) => { + //Check if conversation already exists + const conversation = await db.conversation.findFirst({ + where: { + accountId, + encryptedContactName, + }, + }); + if (conversation) { + //If a conversation already exist. Update the encryptedProfileLocation. + //At the moemnt this is the only updatable field + await db.conversation.update({ + where: { + id: conversation.id, + }, + data: { + encryptedProfileLocation, + }, + }); + + //If conversation exists, return it + return conversation; + } + //If conversation does not exist, create it + return await db.conversation.create({ + data: { + accountId, + encryptedProfileLocation, + encryptedContactName, + //Internal field to order conversations properly + //Will set whenever a conversation is created or a message is added + updatedAt: new Date(), + }, + }); +}; diff --git a/packages/backend/src/persistence/storage/postgres/utils/getOrCreateConversation.ts b/packages/backend/src/persistence/storage/postgres/utils/getOrCreateConversation.ts index 0e198cd9f..2c73acc7c 100644 --- a/packages/backend/src/persistence/storage/postgres/utils/getOrCreateConversation.ts +++ b/packages/backend/src/persistence/storage/postgres/utils/getOrCreateConversation.ts @@ -4,7 +4,6 @@ export const getOrCreateConversation = async ( db: PrismaClient, accountId: string, encryptedContactName: string, - encryptedProfileLocation: string = '', ) => { //Check if conversation already exists const conversation = await db.conversation.findFirst({ @@ -21,7 +20,6 @@ export const getOrCreateConversation = async ( return await db.conversation.create({ data: { accountId, - encryptedProfileLocation, encryptedContactName, //Internal field to order conversations properly //Will set whenever a conversation is created or a message is added diff --git a/packages/backend/src/storage.test.ts b/packages/backend/src/storage.test.ts index cab9a2c9d..35f0d5495 100644 --- a/packages/backend/src/storage.test.ts +++ b/packages/backend/src/storage.test.ts @@ -176,6 +176,52 @@ describe('Storage', () => { expect(body[0].encryptedProfileLocation).toEqual('123'); expect(body.length).toBe(1); }); + it('can update existing conversation with encryptedProfileLocation', async () => { + const aliceId = 'alice.eth'; + + const { status } = await request(app) + .post(`/new/bob.eth/addConversation`) + .set({ + authorization: 'Bearer ' + token, + }) + .send({ + encryptedContactName: aliceId, + encryptedProfileLocation: '123', + }); + 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(aliceId); + expect(body[0].encryptedProfileLocation).toEqual('123'); + expect(body.length).toBe(1); + + const { status: updateStatus } = await request(app) + .post(`/new/bob.eth/addConversation`) + .set({ + authorization: 'Bearer ' + token, + }) + .send({ + encryptedContactName: aliceId, + encryptedProfileLocation: '123456', + }); + expect(updateStatus).toBe(200); + + const { body: updatedBody } = await request(app) + .get(`/new/bob.eth/getConversations`) + .set({ + authorization: 'Bearer ' + token, + }); + + expect(updatedBody[0].contact).toEqual(aliceId); + expect(updatedBody[0].encryptedProfileLocation).toEqual('123456'); + }); it('handle duplicates add conversation', async () => { const aliceId = 'alice.eth'; const ronId = 'ron.eth'; diff --git a/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts b/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts index b5f1e9f44..0ebf326bc 100644 --- a/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts +++ b/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts @@ -49,9 +49,8 @@ export const handleMessagesFromWebSocket = async ( await resolveTLDtoAlias(decryptedEnvelop.message.metadata.from), ); - decryptedEnvelop.message.metadata.from = contact; //TODO use TLD name - await addConversation(contact); + await addConversation(decryptedEnvelop.message.metadata.from); const messageState = selectedContact?.contactDetails.account.ensName === contact diff --git a/packages/messenger-widget/src/version.ts b/packages/messenger-widget/src/version.ts index 924281959..906d0451a 100644 --- a/packages/messenger-widget/src/version.ts +++ b/packages/messenger-widget/src/version.ts @@ -1 +1 @@ -export const version = '1.5.0'; +export const version = '1.6.0';