From 2469e49b4071853ae4cbb5d8db2ff7bb8425e8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Poizat?= Date: Fri, 15 Nov 2024 17:35:14 +0100 Subject: [PATCH] feat: Remove special case for "me" update I had to start with a special case for "me" update, because in Cozy Contacts, relations were stored only in the contact who created the relation. So when "me" was updated, a relation of me could have been deleted, and it was impossible to know which one easily, so it was impossible to know if the contact should be removed from the extension. With https://github.com/cozy/cozy-contacts/pull/1020, relations are propagated to the target contact. So we will get a realtime event and we will be able to check if the contact must be displayed or not without any special case. And I can also check that a contact is related to me directly by checking its relationships now. --- .../src/cozy/realtime/RealtimeNotifications.ts | 10 ---------- libs/cozy/sync.ts | 11 ++++++----- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/apps/browser/src/cozy/realtime/RealtimeNotifications.ts b/apps/browser/src/cozy/realtime/RealtimeNotifications.ts index 26ad9d4cb0c..566b7e5c415 100644 --- a/apps/browser/src/cozy/realtime/RealtimeNotifications.ts +++ b/apps/browser/src/cozy/realtime/RealtimeNotifications.ts @@ -75,16 +75,6 @@ export class RealTimeNotifications { } async dispatchUpdateContact(data: IOCozyContact) { - if (data.me) { - // We need to do a fullSync here because we have no other way to know - // if a contact related to me was removed of the me relation - this.logService.info(`Starting full sync from realtime because me`); - - this.messagingService.send("fullSync"); - - return; - } - const contactMustBeDisplayed = await shouldDisplayContact(this.client, data); if (contactMustBeDisplayed) { diff --git a/libs/cozy/sync.ts b/libs/cozy/sync.ts index dc919dec03f..3e085a923a6 100644 --- a/libs/cozy/sync.ts +++ b/libs/cozy/sync.ts @@ -9,6 +9,7 @@ import { CipherData } from "@bitwarden/common/vault/models/data/cipher.data"; import { CozyClientService } from "../../apps/browser/src/popup/services/cozyClient.service"; +import { CONTACTS_DOCTYPE } from "./constants"; import { convertAllContactsAsCiphers } from "./contactCipher"; import { convertAllPapersAsCiphers } from "./paperCipher"; import { fetchContactsAndPapers, fetchPapers, fetchMyself } from "./queries"; @@ -24,11 +25,11 @@ export const shouldDisplayContact = async (client: CozyClient, contact: IOCozyCo const me = await fetchMyself(client); - const contactRelatedToMe = // @ts-expect-error related added manually with an hydration - me[0].relationships?.related?.data?.find( - // @ts-expect-error related added manually with an hydration - (relatedContact) => relatedContact._id === contact._id, - ); + // @ts-expect-error related added manually with an hydration + const contactRelatedToMe = contact.relationships?.related?.data?.find( + // @ts-expect-error related added manually with an hydration + (relation) => relation._id === me[0]._id && relation._type === CONTACTS_DOCTYPE, + ); if (contactRelatedToMe) { return true;