WIP
This commit is contained in:
parent
886778805d
commit
d3e3e790d1
@ -36,7 +36,7 @@ class ChatElement extends HTMLElement {
|
|||||||
|
|
||||||
private processId: string | null = null;
|
private processId: string | null = null;
|
||||||
private processRoles: any | null = null;
|
private processRoles: any | null = null;
|
||||||
private selectedMember: string[] | null = [];
|
private selectedMember: string | null = null;
|
||||||
private notifications: LocalNotification[] = [];
|
private notifications: LocalNotification[] = [];
|
||||||
private notificationBadge = document.querySelector('.notification-badge');
|
private notificationBadge = document.querySelector('.notification-badge');
|
||||||
private notificationBoard = document.getElementById('notification-board');
|
private notificationBoard = document.getElementById('notification-board');
|
||||||
@ -292,6 +292,7 @@ class ChatElement extends HTMLElement {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log("----this.processId",this.processId );
|
||||||
const process = await service.getProcess(this.processId);
|
const process = await service.getProcess(this.processId);
|
||||||
|
|
||||||
if (!process) {
|
if (!process) {
|
||||||
@ -419,7 +420,7 @@ class ChatElement extends HTMLElement {
|
|||||||
|
|
||||||
memberItem.addEventListener('click', async () => {
|
memberItem.addEventListener('click', async () => {
|
||||||
try {
|
try {
|
||||||
await this.loadMemberChat(member.sp_addresses);
|
await this.loadMemberChat(processId);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error loading member chat:', error);
|
console.error('Error loading member chat:', error);
|
||||||
}
|
}
|
||||||
@ -434,7 +435,7 @@ class ChatElement extends HTMLElement {
|
|||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
const processes = await service.getProcesses();
|
const processes = await service.getProcesses();
|
||||||
|
|
||||||
const selectedMember = this.selectedMember;
|
const recipientAddresses = service.getAddressesForMemberId(this.selectedMember);
|
||||||
for (const [processId, process] of Object.entries(processes)) {
|
for (const [processId, process] of Object.entries(processes)) {
|
||||||
const description = await service.getDescription(processId, process);
|
const description = await service.getDescription(processId, process);
|
||||||
console.log('Process description:'description);
|
console.log('Process description:'description);
|
||||||
@ -442,7 +443,7 @@ class ChatElement extends HTMLElement {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const roles = await this.getRoles(process);
|
const roles = await this.getRoles(process);
|
||||||
if (!service.rolesContainsMember(roles, selectedMember)) {
|
if (!service.rolesContainsMember(roles, recipientAddresses)) {
|
||||||
console.error('Member is not part of the process');
|
console.error('Member is not part of the process');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -456,11 +457,7 @@ class ChatElement extends HTMLElement {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async loadMemberChat(member: string[]) {
|
private async loadMemberChat(pairingProcess: string) {
|
||||||
if (member.length === 0) {
|
|
||||||
console.error('Empty member');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
const myAddresses = await service.getMemberFromDevice();
|
const myAddresses = await service.getMemberFromDevice();
|
||||||
@ -470,29 +467,26 @@ class ChatElement extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set the selected member
|
// Set the selected member
|
||||||
this.selectedMember = member;
|
this.selectedMember = pairingProcess;
|
||||||
|
|
||||||
const chatHeader = this.shadowRoot?.querySelector('#chat-header');
|
const chatHeader = this.shadowRoot?.querySelector('#chat-header');
|
||||||
const messagesContainer = this.shadowRoot?.querySelector('#messages');
|
const messagesContainer = this.shadowRoot?.querySelector('#messages');
|
||||||
|
|
||||||
if (!chatHeader || !messagesContainer) return;
|
if (!chatHeader || !messagesContainer) return;
|
||||||
|
|
||||||
const pairingProcess = await this.getPairingProcess(this.selectedMember[0]);
|
emojis = await addressToEmoji(pairingProcess);
|
||||||
const emojis = await addressToEmoji(pairingProcess);
|
|
||||||
chatHeader.textContent = `Chat with ${emojis}`;
|
chatHeader.textContent = `Chat with ${emojis}`;
|
||||||
messagesContainer.innerHTML = '';
|
messagesContainer.innerHTML = '';
|
||||||
|
|
||||||
let messagesProcess = await this.lookForChildren();
|
let dmProcessId = await this.lookForDmProcess();
|
||||||
|
|
||||||
if (messagesProcess === null) {
|
if (dmProcessId === null) {
|
||||||
console.log('Create a new child process');
|
console.log('Create a new dm process');
|
||||||
// We need to create a new process
|
// We need to create a new process
|
||||||
try {
|
try {
|
||||||
if (!this.processId) {
|
// await service.checkConnections(otherMembers);
|
||||||
console.error('No process id');
|
const res = await service.createDmProcess(this.selectedMember);
|
||||||
return;
|
|
||||||
}
|
|
||||||
const res = await service.createDmProcess(member, this.processId);
|
|
||||||
// We catch the new process here
|
// We catch the new process here
|
||||||
const updatedProcess = res.updated_process?.current_process;
|
const updatedProcess = res.updated_process?.current_process;
|
||||||
const processId = updatedProcess?.states[0]?.commited_in;
|
const processId = updatedProcess?.states[0]?.commited_in;
|
||||||
@ -500,10 +494,13 @@ class ChatElement extends HTMLElement {
|
|||||||
await service.handleApiReturn(res);
|
await service.handleApiReturn(res);
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
// Now create a first commitment
|
// Now create a first commitment
|
||||||
console.log('Created child process', processId);
|
console.log('Created process', processId);
|
||||||
|
this.processId = ProcessId;
|
||||||
const createPrdReturn = await service.createPrdUpdate(processId, stateId);
|
const createPrdReturn = await service.createPrdUpdate(processId, stateId);
|
||||||
|
console.log(createPrdReturn);
|
||||||
await service.handleApiReturn(createPrdReturn);
|
await service.handleApiReturn(createPrdReturn);
|
||||||
const approveChangeReturn = service.approveChange(processId, stateId);
|
const approveChangeReturn = service.approveChange(processId, stateId);
|
||||||
|
console.log(approveChangeReturn);
|
||||||
await service.handleApiReturn(approveChangeReturn);
|
await service.handleApiReturn(approveChangeReturn);
|
||||||
}, 500);
|
}, 500);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -511,12 +508,13 @@ class ChatElement extends HTMLElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (messagesProcess === null) {
|
while (dmProcessId === null) {
|
||||||
messagesProcess = await this.lookForChildren();
|
dmProcessId = await this.lookForDmProcess();
|
||||||
await new Promise(r => setTimeout(r, 500));
|
await new Promise(r => setTimeout(r, 1000));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log('Found child process', messagesProcess);
|
console.log('Found DM process', dmProcessId);
|
||||||
|
this.processId = dmProcessId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO
|
/* TODO
|
||||||
@ -534,16 +532,21 @@ class ChatElement extends HTMLElement {
|
|||||||
// Récupérer les messages depuis les états du processus
|
// Récupérer les messages depuis les états du processus
|
||||||
const allMessages: any[] = [];
|
const allMessages: any[] = [];
|
||||||
|
|
||||||
console.log(messagesProcess);
|
const dmProcess = await service.getProcess(dmProcessId);
|
||||||
const childProcess = JSON.parse(messagesProcess);
|
|
||||||
if (childProcess?.states) {
|
console.log(dmProcess);
|
||||||
for (const state of childProcess.states) {
|
|
||||||
|
if (dmProcess?.states) {
|
||||||
|
for (const state of dmProcess.states) {
|
||||||
const pcd_commitment = state.pcd_commitment;
|
const pcd_commitment = state.pcd_commitment;
|
||||||
if (pcd_commitment) {
|
if (pcd_commitment) {
|
||||||
const message_hash = pcd_commitment.message;
|
const message_hash = pcd_commitment.message;
|
||||||
if (message_hash) {
|
if (message_hash) {
|
||||||
const diff = await service.getDiffByValue(message_hash);
|
const diff = await service.getDiffByValue(message_hash);
|
||||||
const message = diff?.new_value;
|
const message = diff?.new_value;
|
||||||
|
if (message === "" || message === undefined) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
console.log('message', message);
|
console.log('message', message);
|
||||||
allMessages.push(message);
|
allMessages.push(message);
|
||||||
}
|
}
|
||||||
@ -552,10 +555,7 @@ class ChatElement extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
allMessages.sort((a, b) => a.metadata.createdAt - b.metadata.createdAt);
|
allMessages.sort((a, b) => a.metadata.createdAt - b.metadata.createdAt);
|
||||||
if (allMessages.length === 0) {
|
if (allMessages.length > 0) {
|
||||||
console.log('No messages found');
|
|
||||||
return;
|
|
||||||
} else if (allMessages.length > 1 && allMessages[0].metadata.sender === myAddresses[0] && allMessages[0].metadata.recipient === this.selectedMember[0]) {
|
|
||||||
console.log('Messages found:', allMessages);
|
console.log('Messages found:', allMessages);
|
||||||
for (const message of allMessages) {
|
for (const message of allMessages) {
|
||||||
const messageElement = document.createElement('div');
|
const messageElement = document.createElement('div');
|
||||||
@ -566,8 +566,10 @@ class ChatElement extends HTMLElement {
|
|||||||
|
|
||||||
const messageContent = document.createElement('div');
|
const messageContent = document.createElement('div');
|
||||||
messageContent.className = `message ${isCurrentUser ? 'user' : ''}`;
|
messageContent.className = `message ${isCurrentUser ? 'user' : ''}`;
|
||||||
|
|
||||||
const senderEmoji = await addressToEmoji(message.metadata.sender);
|
console.log("SENDER: ", message.metadata.sender);
|
||||||
|
const pairingProcess = this.addressMap[]
|
||||||
|
const senderEmoji = await addressToEmoji(pairingProcess);
|
||||||
|
|
||||||
if (message.type === 'file') {
|
if (message.type === 'file') {
|
||||||
let fileContent = '';
|
let fileContent = '';
|
||||||
@ -661,19 +663,21 @@ class ChatElement extends HTMLElement {
|
|||||||
return this.addressMap;
|
return this.addressMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
//To get a pairing process from an sp adsress
|
async findProcessIdFromAddresses(addresses: string[]): string | null {
|
||||||
async getPairingProcess(spAddress: string){
|
const sortedAddresses = [...addresses].sort();
|
||||||
|
const service = await Services.getInstance();
|
||||||
|
const allMembers = service.getAllMembers();
|
||||||
|
|
||||||
const addressMap = await this.getAddressMap();
|
for (const [key, value] of Object.entries(allMembers)) {
|
||||||
console.log("ADDRESS MAP: ", addressMap);
|
if (value.length === sortedTarget.length) {
|
||||||
|
const sortedValue = [...value].sort();
|
||||||
for (const [key, value] of Object.entries(this.addressMap)){
|
if (sortedValue.every((val, index) => val === sortedTarget[index])) {
|
||||||
if (key === spAddress) {
|
return key; // Found a match
|
||||||
return value;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null; // No match found
|
||||||
}
|
}
|
||||||
|
|
||||||
private async toggleMembers(roleData: any, roleElement: HTMLElement) {
|
private async toggleMembers(roleData: any, roleElement: HTMLElement) {
|
||||||
@ -700,11 +704,15 @@ class ChatElement extends HTMLElement {
|
|||||||
const emojiSpan = document.createElement('span');
|
const emojiSpan = document.createElement('span');
|
||||||
emojiSpan.className = 'member-emoji';
|
emojiSpan.className = 'member-emoji';
|
||||||
|
|
||||||
const pairingProcess = await this.getPairingProcess(member.sp_addresses[0]);
|
const pairingProcess = await this.findProcessIdFromAddresses(member.sp_addresses);
|
||||||
console.log("SP ADDRESS: ",member.sp_addresses[0]);
|
if (pairingProcess) {
|
||||||
console.log("PAIRING PROCESS:", pairingProcess);
|
console.log("PAIRING PROCESS:", pairingProcess);
|
||||||
const emojis = await addressToEmoji(pairingProcess);
|
let emojis = await addressToEmoji(pairingProcess);
|
||||||
emojiSpan.textContent = emojis;
|
} else {
|
||||||
|
console.error('Failed to find the pairing id');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
emojiSpan.textContent = emojis;
|
||||||
|
|
||||||
memberContainer.appendChild(emojiSpan);
|
memberContainer.appendChild(emojiSpan);
|
||||||
memberItem.appendChild(memberContainer);
|
memberItem.appendChild(memberContainer);
|
||||||
@ -712,7 +720,7 @@ class ChatElement extends HTMLElement {
|
|||||||
memberItem.onclick = async (event) => {
|
memberItem.onclick = async (event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
try {
|
try {
|
||||||
await this.loadMemberChat(member.sp_addresses);
|
await this.loadMemberChat(pairingProcess);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error handling member click:', error);
|
console.error('❌ Error handling member click:', error);
|
||||||
}
|
}
|
||||||
@ -927,45 +935,45 @@ class ChatElement extends HTMLElement {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Créer un nouveau roleData avec les membres uniques
|
// Créer un nouveau roleData avec les membres uniques
|
||||||
const filteredRoleData = {
|
const filteredRoleData = {
|
||||||
...roleData,
|
...roleData,
|
||||||
members: Array.from(uniqueMembers.values())
|
members: Array.from(uniqueMembers.values())
|
||||||
};
|
};
|
||||||
|
|
||||||
roleContainer.addEventListener('click', async (event) => {
|
roleContainer.addEventListener('click', async (event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
await this.toggleMembers(filteredRoleData, roleItem);
|
await this.toggleMembers(filteredRoleData, roleItem);
|
||||||
});
|
});
|
||||||
|
|
||||||
roleContainer.appendChild(roleNameSpan);
|
roleContainer.appendChild(roleNameSpan);
|
||||||
roleItem.appendChild(roleContainer);
|
roleItem.appendChild(roleContainer);
|
||||||
roleList.appendChild(roleItem);
|
roleList.appendChild(roleItem);
|
||||||
});
|
});
|
||||||
|
|
||||||
li.appendChild(roleList);
|
li.appendChild(roleList);
|
||||||
groupList.appendChild(li);
|
groupList.appendChild(li);
|
||||||
|
|
||||||
|
|
||||||
// Ajouter un écouteur d'événements pour gérer le clic sur le container
|
// Ajouter un écouteur d'événements pour gérer le clic sur le container
|
||||||
container.addEventListener('click', (event) => {
|
container.addEventListener('click', (event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
container.classList.toggle('expanded');
|
container.classList.toggle('expanded');
|
||||||
const roleList = container.parentElement?.querySelector('.role-list');
|
const roleList = container.parentElement?.querySelector('.role-list');
|
||||||
const dm = container.parentElement?.querySelector('.dm');
|
const dm = container.parentElement?.querySelector('.dm');
|
||||||
|
|
||||||
if (roleList) {
|
if (roleList) {
|
||||||
// Si le container est expanded, on montre la liste des rôles
|
// Si le container est expanded, on montre la liste des rôles
|
||||||
if (container.classList.contains('expanded')) {
|
if (container.classList.contains('expanded')) {
|
||||||
(roleList as HTMLElement).style.display = 'block';
|
(roleList as HTMLElement).style.display = 'block';
|
||||||
if (dm) (dm as HTMLElement).style.display = 'block';
|
if (dm) (dm as HTMLElement).style.display = 'block';
|
||||||
} else {
|
} else {
|
||||||
// Sinon on cache la liste des rôles
|
// Sinon on cache la liste des rôles
|
||||||
(roleList as HTMLElement).style.display = 'none';
|
(roleList as HTMLElement).style.display = 'none';
|
||||||
if (dm) (dm as HTMLElement).style.display = 'none';
|
if (dm) (dm as HTMLElement).style.display = 'none';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -984,8 +992,7 @@ class ChatElement extends HTMLElement {
|
|||||||
//Load tous les processus où le sp_adress est impliqué et renvoie un tableau d'adresses de processus
|
//Load tous les processus où le sp_adress est impliqué et renvoie un tableau d'adresses de processus
|
||||||
private async getMyProcessId() {
|
private async getMyProcessId() {
|
||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
return service.getPairingProcessId();
|
return service.getPairingProcessId();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//fonction qui renvoie les processus où le sp_adress est impliqué
|
//fonction qui renvoie les processus où le sp_adress est impliqué
|
||||||
@ -998,7 +1005,7 @@ class ChatElement extends HTMLElement {
|
|||||||
return this.userProcessSet;
|
return this.userProcessSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
const pairingProcess = await this.getPairingProcess(currentMember[0]);
|
const pairingProcess = await getMyProcessId();
|
||||||
const memberEmoji = await addressToEmoji(pairingProcess);
|
const memberEmoji = await addressToEmoji(pairingProcess);
|
||||||
console.log("Mon adresse:", currentMember[0], memberEmoji);
|
console.log("Mon adresse:", currentMember[0], memberEmoji);
|
||||||
|
|
||||||
|
@ -1128,7 +1128,7 @@ export default class Services {
|
|||||||
return this.membersList;
|
return this.membersList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getAddressesForMemberId(memberId: string): string[] | null {
|
||||||
|
return this.memberList[memberId];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user