Fix getting of the child process in loadMemberChat

This commit is contained in:
NicolasCantu 2025-01-16 10:01:36 +01:00
parent 511ea710fa
commit 769952a5de
2 changed files with 91 additions and 105 deletions

View File

@ -388,13 +388,12 @@ class ChatElement extends HTMLElement {
messagesContainer.innerHTML = ''; messagesContainer.innerHTML = '';
// Filter processes for the children of current process // Filter processes for the children of current process
const children = await service.getChildrenOfProcess(this.processId); const children: string[] = await service.getChildrenOfProcess(this.processId);
let messagesProcess: Process | null = null; let messagesProcess: Process | null = null;
console.log(this.processRoles);
const processRoles = this.processRoles; const processRoles = this.processRoles;
for (const child of children) { for (const child of children) {
const roles = await this.getRoles(child); const roles = await this.getRoles(JSON.parse(child));
// Check that we and the other members are in the role // Check that we and the other members are in the role
if (!service.isChildRole(processRoles, roles)) { if (!service.isChildRole(processRoles, roles)) {
console.error('Child process roles are not a subset of parent') console.error('Child process roles are not a subset of parent')
@ -408,7 +407,7 @@ class ChatElement extends HTMLElement {
console.error('We\'re not part of child process'); console.error('We\'re not part of child process');
continue; continue;
} }
messagesProcess = child; messagesProcess = JSON.parse(child);
} }
if (messagesProcess === null) { if (messagesProcess === null) {
@ -428,98 +427,91 @@ class ChatElement extends HTMLElement {
console.log('Found child process', messagesProcess); console.log('Found child process', messagesProcess);
} }
const dbRequest = indexedDB.open('4nk'); // Get the diffs from the database
for (const state of messagesProcess.states) {
dbRequest.onerror = () => { const pcd_commitment = state.pcd_commitment;
console.error("Database error:", dbRequest.error); if (pcd_commitment) {
}; const message_hash = pcd_commitment.message;
if (message_hash) {
dbRequest.onsuccess = async (event) => { const diff = await service.getDiffByValue(message_hash);
const db = dbRequest.result; const message = diff.new_value;
const transaction = db.transaction(['diffs'], 'readonly'); console.log(message);
const store = transaction.objectStore('diffs'); }
const messages: any[] = [];
const cursorRequest = store.openCursor();
cursorRequest.onsuccess = async (e) => {
const cursor = (e.target as IDBRequest).result;
if (cursor) {
const record = cursor.value;
if (record.description === 'message_content' &&
record.metadata &&
record.metadata.roleName === this.selectedRole &&
((record.metadata.sender === myAddresses[0] && record.metadata.recipient === memberId) ||
(record.metadata.sender === memberId && record.metadata.recipient === myAddresses[0]))) {
messages.push(record);
} }
cursor.continue();
} else {
messages.sort((a, b) => a.metadata.timestamp - b.metadata.timestamp);
for (const message of messages) {
const messageElement = document.createElement('div');
messageElement.className = 'message-container';
const isCurrentUser = message.metadata.sender === myAddresses[0];
messageElement.style.justifyContent = isCurrentUser ? 'flex-end' : 'flex-start';
const messageContent = document.createElement('div');
messageContent.className = `message ${isCurrentUser ? 'user' : ''}`;
const senderEmoji = await addressToEmoji(message.metadata.sender);
if (message.metadata.type === 'file') {
let fileContent = '';
if (message.metadata.fileType.startsWith('image/')) {
fileContent = `
<div class="file-preview">
<img src="${message.metadata.fileData}" alt="${message.metadata.fileName}" style="max-width: 200px; max-height: 200px;"/>
</div>
`;
} else {
const blob = this.base64ToBlob(message.metadata.fileData, message.metadata.fileType);
const url = URL.createObjectURL(blob);
fileContent = `
<div class="file-download">
<a href="${url}" download="${message.metadata.fileName}">
📎 ${message.metadata.fileName}
</a>
</div>
`;
} }
messageContent.innerHTML = ` // if (record.description === 'message_content' &&
<div class="message-content"> // record.metadata &&
<strong>${senderEmoji}</strong>: ${fileContent} // record.metadata.roleName === this.selectedRole &&
</div> // ((record.metadata.sender === myAddresses[0] && record.metadata.recipient === memberId) ||
<div class="message-time"> // (record.metadata.sender === memberId && record.metadata.recipient === myAddresses[0]))) {
${new Date(message.metadata.timestamp).toLocaleString('fr-FR')}
</div>
`;
} else {
// Message texte normal
messageContent.innerHTML = `
<div class="message-content">
<strong>${senderEmoji}</strong>: ${message.metadata.text}
</div>
<div class="message-time">
${new Date(message.metadata.timestamp).toLocaleString('fr-FR')}
</div>
`;
}
messageElement.appendChild(messageContent); // messages.push(record);
messagesContainer.appendChild(messageElement); // }
} // cursor.continue();
// } else {
// messages.sort((a, b) => a.metadata.timestamp - b.metadata.timestamp);
this.scrollToBottom(messagesContainer); // for (const message of messages) {
} // const messageElement = document.createElement('div');
}; // messageElement.className = 'message-container';
};
// const isCurrentUser = message.metadata.sender === myAddresses[0];
// messageElement.style.justifyContent = isCurrentUser ? 'flex-end' : 'flex-start';
// const messageContent = document.createElement('div');
// messageContent.className = `message ${isCurrentUser ? 'user' : ''}`;
// const senderEmoji = await addressToEmoji(message.metadata.sender);
// if (message.metadata.type === 'file') {
// let fileContent = '';
// if (message.metadata.fileType.startsWith('image/')) {
// fileContent = `
// <div class="file-preview">
// <img src="${message.metadata.fileData}" alt="${message.metadata.fileName}" style="max-width: 200px; max-height: 200px;"/>
// </div>
// `;
// } else {
// const blob = this.base64ToBlob(message.metadata.fileData, message.metadata.fileType);
// const url = URL.createObjectURL(blob);
// fileContent = `
// <div class="file-download">
// <a href="${url}" download="${message.metadata.fileName}">
// 📎 ${message.metadata.fileName}
// </a>
// </div>
// `;
// }
// messageContent.innerHTML = `
// <div class="message-content">
// <strong>${senderEmoji}</strong>: ${fileContent}
// </div>
// <div class="message-time">
// ${new Date(message.metadata.timestamp).toLocaleString('fr-FR')}
// </div>
// `;
// } else {
// // Message texte normal
// messageContent.innerHTML = `
// <div class="message-content">
// <strong>${senderEmoji}</strong>: ${message.metadata.text}
// </div>
// <div class="message-time">
// ${new Date(message.metadata.timestamp).toLocaleString('fr-FR')}
// </div>
// `;
// }
// messageElement.appendChild(messageContent);
// messagesContainer.appendChild(messageElement);
// }
// this.scrollToBottom(messagesContainer);
// }
// };
// };
} catch (error) { } catch (error) {
console.error('Error loading chat:', error); console.error('Error loading chat:', error);
@ -583,12 +575,6 @@ 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');
const parentProcessId = this.getAttribute('process-id');
if (!parentProcessId) {
throw new Error('No parent process ID found');
}
await this.loadMemberChat(member.sp_addresses); await this.loadMemberChat(member.sp_addresses);
} catch (error) { } catch (error) {
console.error('❌ Error handling member click:', error); console.error('❌ Error handling member click:', error);

View File

@ -715,7 +715,7 @@ export default class Services {
return true; return true;
} }
rolesContainsUs(role: any): boolean { rolesContainsUs(roles: any): boolean {
try { try {
this.sdkClient.roles_contains_us(JSON.stringify(roles)); this.sdkClient.roles_contains_us(JSON.stringify(roles));
} catch (e) { } catch (e) {
@ -726,7 +726,7 @@ export default class Services {
return true; return true;
} }
rolesContainsMember(role: any, member: string[]): boolean { rolesContainsMember(roles: any, member: string[]): boolean {
try { try {
this.sdkClient.roles_contains_member(JSON.stringify(roles), member); this.sdkClient.roles_contains_member(JSON.stringify(roles), member);
} catch (e) { } catch (e) {
@ -838,7 +838,7 @@ export default class Services {
return processes; return processes;
} }
public async getChildrenOfProcess(processId: string): Promise<Process[]> { public async getChildrenOfProcess(processId: string): Promise<string[]> {
const processes = await this.getProcesses(); const processes = await this.getProcesses();
const res = []; const res = [];
@ -849,7 +849,7 @@ export default class Services {
const parentIdHash = pcdCommitment['parent_id']; const parentIdHash = pcdCommitment['parent_id'];
const diff = await this.getDiffByValue(parentIdHash); const diff = await this.getDiffByValue(parentIdHash);
if (diff && diff['new_value'] === processId) { if (diff && diff['new_value'] === processId) {
res.push(process); res.push(JSON.stringify(process));
} }
} catch (e) { } catch (e) {
continue; continue;