From 16ded2ba865173b337d343432310dce50e147be8 Mon Sep 17 00:00:00 2001 From: AlexNi245 Date: Thu, 8 Aug 2024 14:45:23 +0200 Subject: [PATCH 1/5] add createOrUpdateConversation db method --- .../storage/postgres/addConversation.ts | 4 +- .../utils/createOrUpdateConversation.ts | 42 +++++++++++++++++ .../postgres/utils/getOrCreateConversation.ts | 2 - packages/backend/src/storage.test.ts | 46 +++++++++++++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 packages/backend/src/persistence/storage/postgres/utils/createOrUpdateConversation.ts 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..3fbc024cb 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 add 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'; From 3444888171d6402b6cdb89387a0981a97e92ac96 Mon Sep 17 00:00:00 2001 From: AlexNi245 Date: Thu, 8 Aug 2024 15:17:56 +0200 Subject: [PATCH 2/5] add TLD in handkeMEssagesFromWebsocket --- .../hooks/messages/sources/handleMessagesFromWebSocket.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts b/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts index b5f1e9f44..c8361b26e 100644 --- a/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts +++ b/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts @@ -49,9 +49,11 @@ export const handleMessagesFromWebSocket = async ( await resolveTLDtoAlias(decryptedEnvelop.message.metadata.from), ); - decryptedEnvelop.message.metadata.from = contact; + console.log(contact); + console.log(decryptedEnvelop.message.metadata.from); + //TODO use TLD name - await addConversation(contact); + await addConversation(decryptedEnvelop.message.metadata.from); const messageState = selectedContact?.contactDetails.account.ensName === contact From 64090221aef717332c784001e77f72381ebb5bf9 Mon Sep 17 00:00:00 2001 From: AlexNi245 Date: Thu, 8 Aug 2024 15:19:03 +0200 Subject: [PATCH 3/5] rmeove console.log statements --- .../src/hooks/messages/sources/handleMessagesFromWebSocket.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts b/packages/messenger-widget/src/hooks/messages/sources/handleMessagesFromWebSocket.ts index c8361b26e..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,6 @@ export const handleMessagesFromWebSocket = async ( await resolveTLDtoAlias(decryptedEnvelop.message.metadata.from), ); - console.log(contact); - console.log(decryptedEnvelop.message.metadata.from); - //TODO use TLD name await addConversation(decryptedEnvelop.message.metadata.from); From 5a7e9aaf691db6eb33225ca839b86a6e5f72dc9e Mon Sep 17 00:00:00 2001 From: AlexNi245 Date: Thu, 8 Aug 2024 15:48:03 +0200 Subject: [PATCH 4/5] add version --- packages/messenger-widget/src/version.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'; From 5fbe691a7d5560293d278341fa5df10dabd1d3ba Mon Sep 17 00:00:00 2001 From: AlexNi245 Date: Thu, 8 Aug 2024 15:48:27 +0200 Subject: [PATCH 5/5] fix typo in test file --- packages/backend/src/storage.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/src/storage.test.ts b/packages/backend/src/storage.test.ts index 3fbc024cb..35f0d5495 100644 --- a/packages/backend/src/storage.test.ts +++ b/packages/backend/src/storage.test.ts @@ -176,7 +176,7 @@ describe('Storage', () => { expect(body[0].encryptedProfileLocation).toEqual('123'); expect(body.length).toBe(1); }); - it('can add update existing conversation with encryptedProfileLocation', async () => { + it('can update existing conversation with encryptedProfileLocation', async () => { const aliceId = 'alice.eth'; const { status } = await request(app)