From 932b659c2b0b24ea043cf1b93be0c22f2b57ae1a Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Fri, 24 Jan 2025 14:54:44 +0100 Subject: [PATCH] Update sendMessage --- src/pages/chat/chat.ts | 113 +++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 55 deletions(-) diff --git a/src/pages/chat/chat.ts b/src/pages/chat/chat.ts index 103c3a2..49fd6f0 100755 --- a/src/pages/chat/chat.ts +++ b/src/pages/chat/chat.ts @@ -262,12 +262,12 @@ class ChatElement extends HTMLElement { console.error('❌ Missing message input or selected member'); return; } - - if (!this.selectedRole) { - console.error('❌ No role selected'); + + if (!this.processId) { + console.error('no process id set'); return; } - + const messageText = messageInput.value.trim(); if (messageText === '') { console.error('❌ Empty message'); @@ -280,60 +280,63 @@ class ChatElement extends HTMLElement { if (!myAddresses) throw new Error('No paired member found'); const timestamp = Date.now(); - // Ouvrir IndexedDB - const dbRequest = indexedDB.open('4nk'); - - dbRequest.onerror = (event) => { - console.error("Database error:", dbRequest.error); - }; - - dbRequest.onsuccess = async (event) => { - const db = dbRequest.result; - const transaction = db.transaction(['diffs'], 'readwrite'); - const store = transaction.objectStore('diffs'); - - try { - const message = { - state: this.messageState, - type: 'text', - content: messageText, - metadata: { - createdAt: timestamp, - lastModified: timestamp, - sender: myAddresses, - recipient: this.selectedMember, - } - }; - - await new Promise((resolve, reject) => { - const request = store.add(message); - request.onsuccess = () => { - console.log('✅ Message saved'); - messageInput.value = ''; - resolve(); - }; - request.onerror = () => reject(request.error); - }); - - // Attendre la fin de la transaction - await new Promise((resolve, reject) => { - transaction.oncomplete = () => { - console.log('✅ Transaction completed'); - resolve(); - }; - transaction.onerror = () => reject(transaction.error); - }); - - // Recharger les messages - if (this.selectedMember) { - await this.loadMemberChat(this.selectedMember); - } - - } catch (error) { - console.error('❌ Transaction error:', error); + const message = { + state: this.messageState, + type: 'text', + content: messageText, + metadata: { + createdAt: timestamp, + lastModified: timestamp, + sender: myAddresses, + recipient: this.selectedMember, } }; + const process = await service.getProcess(this.processId); + + if (!process) { + console.error('Failed to retrieve process from DB'); + return; + } else { + console.log(process); + } + + const processTip = process.states[process.states.length - 1].commited_in; + console.log(processTip); + + // We take the last commited state and copy all values + const lastCommitedState = process.states.findLast( + state => state.commited_in !== processTip + ); + + if (!lastCommitedState) { + console.error('Failed to find last commited state'); + return; + } else { + console.log(lastCommitedState); + } + + let newState = {}; + // fetch all the values from diff + for (const [attribute, hash] of Object.entries(lastCommitedState.pcd_commitment)) { + const value = await service.getDiffByValue(hash); + newState[attribute] = value.new_value; + } + + newState.message = message; + + // Now we create a new state for the dm process + const apiReturn = service.updateProcess(this.processId, newState); + const updatedProcess = apiReturn.updated_process.current_process; + const newStateId = updatedProcess.states[updatedProcess.states.length - 2 ].state_id; // We take the last concurrent state, just before the tip + await service.handleApiReturn(apiReturn); + + const createPrdReturn = service.createPrdUpdate(this.processId, newStateId); + await service.handleApiReturn(createPrdReturn); + + // Now we validate the new state + const approveChangeReturn = service.approveChange(this.processId, newStateId); + await service.handleApiReturn(approveChangeReturn); } catch (error) { console.error('❌ Error in sendMessage:', error); }