Skip to content

Commit

Permalink
feat: Manage correctly contacts when a paper is updated from realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
zatteo committed Nov 14, 2024
1 parent b051084 commit dd2aab3
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions apps/browser/src/cozy/realtime/RealtimeNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,40 @@ export class RealTimeNotifications {
if (!data.metadata?.qualification?.label) {
return;
}
if (data.dir_id === "io.cozy.files.trash-dir") {
// We don't want to display trashed papers in the extension's bin so we remove them from the vault
return this.dispatchDeletePaper(data);
}

const isCreate = !(await this.cipherService.get(data._id));

if (isCreate) {
await dispatchCreate(this.client, "io.cozy.files", data);
} else {
await dispatchUpdate(this.client, "io.cozy.files", data);
}
/*
A paper may be updated because a contact was added or removed from the association.
If it has been removed, we can not know if we need to remove the contact from the vault
without checking all the other papers and the contact which is almost equivalent
to a fullSync.
So, since now our fullSync is efficient, it's easier to do a fullSync than checking
every case.
*/

const hydratedPaper = await fetchHydratedPaper(this.client, data._id);
this.logService.info(`Starting full sync from realtime because paper updated`)

await this.upsertPaperData(hydratedPaper);
this.messagingService.send("fullSync");
}

async dispatchDeletePaper(data: any) {
await this.cipherService.delete(data._id);
this.messagingService.send("syncedDeletedCipher", { cipherId: data._id });
this.messagingService.send("syncCompleted", { successfully: true });
if (data.type !== "file") {
return;
}
if (!data.metadata?.qualification?.label) {
return;
}

/*
When a paper is deleted, we can not know if we need to remove the contact from the vault
without checking all the other papers and the contact which is almost equivalent
to a fullSync.
So, since now our fullSync is efficient, it's easier to do a fullSync than checking
every case.
*/

this.logService.info(`Starting full sync from realtime because paper deleted`)

await dispatchDelete(this.client, "io.cozy.files", data);
this.messagingService.send("fullSync");
}

async upsertPaperData(paper: IOCozyFile) {
Expand Down

0 comments on commit dd2aab3

Please sign in to comment.