Remove wasm init and load service instead
This commit is contained in:
parent
3acc8b7035
commit
28883062db
@ -12,6 +12,7 @@ import { getCorrectDOM } from '../../utils/document.utils';
|
|||||||
import chatStyle from '../../../public/style/chat.css?inline';
|
import chatStyle from '../../../public/style/chat.css?inline';
|
||||||
import { addressToEmoji } from '../../utils/sp-address.utils';
|
import { addressToEmoji } from '../../utils/sp-address.utils';
|
||||||
import Database from '../../services/database.service';
|
import Database from '../../services/database.service';
|
||||||
|
import Services from '../../services/service';
|
||||||
|
|
||||||
const storageUrl = `/storage`;
|
const storageUrl = `/storage`;
|
||||||
|
|
||||||
@ -34,7 +35,6 @@ class ChatElement extends HTMLElement {
|
|||||||
return ['process-id'];
|
return ['process-id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
private sdkClient: any;
|
|
||||||
private processId: string | null = null;
|
private processId: string | null = null;
|
||||||
private selectedMemberId: string | null = null;
|
private selectedMemberId: string | null = null;
|
||||||
private notifications: LocalNotification[] = [];
|
private notifications: LocalNotification[] = [];
|
||||||
@ -55,8 +55,6 @@ class ChatElement extends HTMLElement {
|
|||||||
this.attachShadow({ mode: 'open' });
|
this.attachShadow({ mode: 'open' });
|
||||||
this.processId = this.getAttribute('process-id');
|
this.processId = this.getAttribute('process-id');
|
||||||
|
|
||||||
this.initSDKClient();
|
|
||||||
|
|
||||||
console.log('🔍 Constructor - Process ID from element:', this.processId);
|
console.log('🔍 Constructor - Process ID from element:', this.processId);
|
||||||
|
|
||||||
this.shadowRoot!.innerHTML = `
|
this.shadowRoot!.innerHTML = `
|
||||||
@ -242,129 +240,6 @@ class ChatElement extends HTMLElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public isPaired(): boolean {
|
|
||||||
try {
|
|
||||||
return this.sdkClient.is_paired();
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(`isPaired ~ Error: ${e}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async createMessagingProcess(
|
|
||||||
otherMembers: Member[],
|
|
||||||
relayAddress: string,
|
|
||||||
feeRate: number,
|
|
||||||
processId: string,
|
|
||||||
roleName: string
|
|
||||||
): Promise<ApiReturn> {
|
|
||||||
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: 0.01,
|
|
||||||
fields: ['messages'],
|
|
||||||
min_sig_member: 0.01,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
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,
|
|
||||||
relayAddress,
|
|
||||||
feeRate
|
|
||||||
);
|
|
||||||
console.log('✅ Process message créé:', result);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
} catch (e) {
|
|
||||||
console.error('❌ Erreur:', e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getMemberFromDevice(): Promise<string[] | null> {
|
|
||||||
try {
|
|
||||||
const device = await this.getDeviceFromDatabase();
|
|
||||||
if (device) {
|
|
||||||
const parsed: Device = JSON.parse(device);
|
|
||||||
const pairedMember = parsed['paired_member'];
|
|
||||||
return pairedMember.sp_addresses;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(`Failed to retrieve paired_member from device: ${e}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getDeviceFromDatabase(): Promise<string | null> {
|
|
||||||
const db = await Database.getInstance();
|
|
||||||
const walletStore = 'wallet';
|
|
||||||
try {
|
|
||||||
const dbRes = await db.getObject(walletStore, '1');
|
|
||||||
if (dbRes) {
|
|
||||||
const wallet = dbRes['device'];
|
|
||||||
return wallet;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(`Failed to retrieve device from db: ${e}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async sendMessage() {
|
private async sendMessage() {
|
||||||
const messageInput = this.shadowRoot?.querySelector('#message-input') as HTMLInputElement;
|
const messageInput = this.shadowRoot?.querySelector('#message-input') as HTMLInputElement;
|
||||||
if (!messageInput || !this.selectedMemberId) {
|
if (!messageInput || !this.selectedMemberId) {
|
||||||
@ -384,7 +259,8 @@ class ChatElement extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const myAddresses = await this.getMemberFromDevice();
|
const service = Services.getInstance();
|
||||||
|
const myAddresses = await service.getMemberFromDevice();
|
||||||
if (!myAddresses) throw new Error('No paired member found');
|
if (!myAddresses) throw new Error('No paired member found');
|
||||||
|
|
||||||
const timestamp = Date.now();
|
const timestamp = Date.now();
|
||||||
@ -484,12 +360,15 @@ class ChatElement extends HTMLElement {
|
|||||||
private scrollToBottom(container: Element) {
|
private scrollToBottom(container: Element) {
|
||||||
(container as HTMLElement).scrollTop = (container as HTMLElement).scrollHeight;
|
(container as HTMLElement).scrollTop = (container as HTMLElement).scrollHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async loadMemberChat(member: string[]) {
|
||||||
// Load the list of members
|
if (member.length === 0) {
|
||||||
private async loadMemberChat(memberId: string) {
|
console.error('Empty member');
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const myAddresses = await this.getMemberFromDevice();
|
const service = await Services.getInstance();
|
||||||
|
const myAddresses = await service.getMemberFromDevice();
|
||||||
if (!myAddresses) {
|
if (!myAddresses) {
|
||||||
console.error('No paired member found');
|
console.error('No paired member found');
|
||||||
return;
|
return;
|
||||||
@ -654,7 +533,7 @@ class ChatElement extends HTMLElement {
|
|||||||
|
|
||||||
memberContainer.appendChild(emojiSpan);
|
memberContainer.appendChild(emojiSpan);
|
||||||
memberItem.appendChild(memberContainer);
|
memberItem.appendChild(memberContainer);
|
||||||
|
|
||||||
memberItem.onclick = async (event) => {
|
memberItem.onclick = async (event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
// Stocker le rôle sélectionné
|
// Stocker le rôle sélectionné
|
||||||
@ -662,13 +541,13 @@ class ChatElement extends HTMLElement {
|
|||||||
console.log('🎭 Selected role:', this.selectedRole);
|
console.log('🎭 Selected role:', this.selectedRole);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('🔍 Début création process message');
|
// console.log('🔍 Début création process message');
|
||||||
const parentProcessId = this.getAttribute('process-id');
|
const parentProcessId = this.getAttribute('process-id');
|
||||||
if (!parentProcessId) {
|
if (!parentProcessId) {
|
||||||
throw new Error('No parent process ID found');
|
throw new Error('No parent process ID found');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.loadMemberChat(member.sp_addresses[0]);
|
await this.loadMemberChat(member.sp_addresses);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error handling member click:', error);
|
console.error('❌ Error handling member click:', error);
|
||||||
}
|
}
|
||||||
@ -766,7 +645,7 @@ class ChatElement extends HTMLElement {
|
|||||||
...roleData,
|
...roleData,
|
||||||
members: Array.from(uniqueMembers.values())
|
members: Array.from(uniqueMembers.values())
|
||||||
};
|
};
|
||||||
|
|
||||||
roleContainer.addEventListener('click', (event) => {
|
roleContainer.addEventListener('click', (event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
this.toggleMembers(filteredRoleData, roleItem);
|
this.toggleMembers(filteredRoleData, roleItem);
|
||||||
@ -810,7 +689,8 @@ class ChatElement extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const myAddresses = await this.getMemberFromDevice();
|
const service = Services.getInstance();
|
||||||
|
const myAddresses = await service.getMemberFromDevice();
|
||||||
if (!myAddresses) throw new Error('No paired member found');
|
if (!myAddresses) throw new Error('No paired member found');
|
||||||
|
|
||||||
let fileData: string;
|
let fileData: string;
|
||||||
@ -1038,32 +918,6 @@ class ChatElement extends HTMLElement {
|
|||||||
console.log('🎯 New messaging process added successfully');
|
console.log('🎯 New messaging process added successfully');
|
||||||
}
|
}
|
||||||
|
|
||||||
private async initSDKClient() {
|
|
||||||
try {
|
|
||||||
this.sdkClient = (window as any).sdk || await this.createSDKClient();
|
|
||||||
if (!this.sdkClient) {
|
|
||||||
throw new Error('Failed to initialize SDK client');
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error initializing SDK client:', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async createSDKClient() {
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
resolve({
|
|
||||||
is_paired: () => true,
|
|
||||||
create_new_process: async (template: string, parentId: string | null, relayAddress: string, feeRate: number) => {
|
|
||||||
return { success: true };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private async getExistingMessageProcess(parentId: string, spAddresses: string[]): Promise<any> {
|
private async getExistingMessageProcess(parentId: string, spAddresses: string[]): Promise<any> {
|
||||||
try {
|
try {
|
||||||
console.log('🔍 Recherche du processus message');
|
console.log('🔍 Recherche du processus message');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user