diff --git a/src/pages/chat/chat.ts b/src/pages/chat/chat.ts index 22ea154..6ec3203 100755 --- a/src/pages/chat/chat.ts +++ b/src/pages/chat/chat.ts @@ -270,68 +270,68 @@ class ChatElement extends HTMLElement { processId: string, roleName: string ): Promise { - console.log('🚀 Début createMessagingProcess'); - console.log('📝 Parent Process ID:', this.processId); - console.log('👥 Other Member:', otherMembers[0]); - console.log('👑 Role Name:', roleName); - - if (!this.isPaired()) { - throw new Error('Device not paired'); - } - - const myAddresses = await this.getMemberFromDevice(); - console.log('🔑 Mes adresses:', myAddresses); - - if (!myAddresses) { - throw new Error('No paired member found'); - } - - const currentProcessId = this.getAttribute('process-id'); - console.log('🔍 Current Process ID:', currentProcessId); - - if (!currentProcessId) { - throw new Error('No process ID found'); - } - - // Vérifier si le processus existe déjà - const existingProcess = await this.getExistingMessageProcess( - currentProcessId, - otherMembers[0].sp_addresses - ); - - if (existingProcess) { - console.log('✅ Process message déjà existant, réutilisation:', existingProcess); - return existingProcess; - } - - // Si non existant, créer un nouveau processus - console.log('🆕 Création d\'un nouveau processus message'); - - const messagingTemplate = { - parent_id: currentProcessId, - description: 'messaging', - roles: { - [roleName]: { - members: [ - { sp_addresses: myAddresses }, - { sp_addresses: otherMembers[0].sp_addresses } - ], - validation_rules: [ - { - quorum: 1.0, - fields: ['messages'], - min_sig_member: 1.0, - }, - ], - storages: [storageUrl] - } - } - }; - - console.log('📋 Template final:', JSON.stringify(messagingTemplate, null, 2)); - console.log('👥 Création du process message comme enfant de:', currentProcessId); - try { + console.log('🚀 Début createMessagingProcess'); + console.log('📝 Parent Process ID:', this.processId); + console.log('👥 Other Member:', otherMembers[0]); + console.log('👑 Role Name:', roleName); + + if (!this.isPaired()) { + throw new Error('Device not paired'); + } + + const myAddresses = await this.getMemberFromDevice(); + console.log('🔑 Mes adresses:', myAddresses); + + if (!myAddresses) { + throw new Error('No paired member found'); + } + + const currentProcessId = this.getAttribute('process-id'); + console.log('🔍 Current Process ID:', currentProcessId); + + if (!currentProcessId) { + throw new Error('No process ID found'); + } + + // Vérifier si le processus existe déjà + const existingProcess = await this.getExistingMessageProcess( + currentProcessId, + otherMembers[0].sp_addresses + ); + + if (existingProcess) { + console.log('✅ Process message déjà existant, réutilisation:', existingProcess); + return existingProcess; + } + + // Si non existant, créer un nouveau processus + console.log('🆕 Création d\'un nouveau processus message'); + + const messagingTemplate = { + parent_id: currentProcessId, + description: 'messaging', + roles: { + [roleName]: { + members: [ + { sp_addresses: myAddresses }, + { sp_addresses: otherMembers[0].sp_addresses } + ], + validation_rules: [ + { + quorum: 1.0, + fields: ['messages'], + min_sig_member: 1.0, + }, + ], + storages: [storageUrl] + } + } + }; + + console.log('📋 Template final:', JSON.stringify(messagingTemplate, null, 2)); + console.log('👥 Création du process message comme enfant de:', currentProcessId); + const result = await this.sdkClient.create_new_process( JSON.stringify(messagingTemplate), currentProcessId, @@ -340,15 +340,25 @@ class ChatElement extends HTMLElement { ); console.log('✅ Process message créé:', result); - // Vérification dans IndexedDB + const db = await Database.getInstance(); - const processStore = 'processes'; - const storedProcess = await db.getObject(processStore, (result as any).process_id); - console.log('💾 Process message stocké dans IndexedDB:', { - store: processStore, - processId: (result as any).process_id, - data: storedProcess + await db.addObject({ + storeName: 'diffs', + key: (result as any).process_id, + object: { + value_commitment: { + process_id: (result as any).process_id, + parent_id: processId, + description: 'messaging', + roles: messagingTemplate.roles, + states: [{ + encrypted_pcd: messagingTemplate + }] + }, + timestamp: Date.now() + } }); + console.log('💾 Process message sauvegardé dans diffs'); return result; } catch (e) { @@ -388,7 +398,6 @@ class ChatElement extends HTMLElement { } } -// Send a messsage private async sendMessage() { const messageInput = this.shadowRoot?.querySelector('#message-input') as HTMLInputElement; if (!messageInput || !this.selectedMemberId) return; @@ -416,14 +425,12 @@ class ChatElement extends HTMLElement { member.sp_addresses.includes(this.selectedMemberId) ) )?.[0] || 'default'; - const messageTemplate = { - messaging_id: messageProcess.process_id, - description: 'message_content', + messaging_id: this.getAttribute('process-id'), + description: 'message_content', state: 'sent', content: { text: messageText, - timestamp: Date.now(), sender: myAddresses[0], recipient: this.selectedMemberId },