Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better management of realtime with contacts #334

Merged
merged 10 commits into from
Nov 18, 2024
1 change: 1 addition & 0 deletions apps/browser/src/background/main.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ export default class MainBackground {
this.accountService,
this.i18nService,
this.tokenService,
this.logService,
);
// Cozy customization end

Expand Down
14 changes: 12 additions & 2 deletions apps/browser/src/cozy/realtime/RealtimeNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CouchDBDocument, IOCozyContact } from "cozy-client/types/types";

import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";

Expand All @@ -15,13 +16,15 @@ import {
} from "../../../../../libs/cozy/note.helper";
import { convertPaperToCipherData } from "../../../../../libs/cozy/paper.helper";
import { fetchPaper } from "../../../../../libs/cozy/queries";
import { shouldDisplayContact } from "../../../../../libs/cozy/sync";

export class RealTimeNotifications {
constructor(
private messagingService: MessagingService,
private cipherService: CipherService,
private i18nService: I18nService,
private accountService: AccountService,
private logService: LogService,
private client: CozyClient,
) {}

Expand Down Expand Up @@ -59,9 +62,16 @@ export class RealTimeNotifications {
}

async dispatchCreateContact(data: IOCozyContact) {
await this.upsertContactData(data);
const contactMustBeDisplayed = await shouldDisplayContact(this.client, data);

if (contactMustBeDisplayed) {
this.logService.info(`Contact ${data.displayName} (${data._id}) added from realtime`)
await this.upsertContactData(data);

await dispatchCreate(this.client, "io.cozy.contacts", data as CouchDBDocument);
await dispatchCreate(this.client, "io.cozy.contacts", data as CouchDBDocument);
} else {
this.logService.info(`Contact ${data.displayName} (${data._id}) not added from realtime`)
}
}

async dispatchUpdateContact(data: IOCozyContact) {
Expand Down
3 changes: 3 additions & 0 deletions apps/browser/src/popup/services/cozyClient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"
import { UriMatchStrategy } from "@bitwarden/common/models/domain/domain-service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { SecureNoteType } from "@bitwarden/common/vault/enums";
Expand Down Expand Up @@ -61,6 +62,7 @@ export class CozyClientService {
protected accountService: AccountService,
private i18nService: I18nService,
private tokenService: TokenService,
private logService: LogService,
) {
this.flagChangedPointer = this.flagChanged.bind(this);
}
Expand Down Expand Up @@ -189,6 +191,7 @@ export class CozyClientService {
this.cipherService,
this.i18nService,
this.accountService,
this.logService,
this.instance,
);
this.realTimeNotifications.init();
Expand Down
20 changes: 20 additions & 0 deletions libs/cozy/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ export const fetchPaper = async (client: CozyClient, _id: string) => {

// Contacts

export const buildMyselfQuery = () => {
return {
definition: Q(CONTACTS_DOCTYPE).where({ me: true }),
options: {
as: `${CONTACTS_DOCTYPE}/myself`
},
};
};

export const buildContactsQuery = () => ({
definition: Q(CONTACTS_DOCTYPE)
.where({
Expand Down Expand Up @@ -122,6 +131,17 @@ export const buildContactsQuery = () => ({
},
});

export const fetchMyself = async (client: CozyClient): Promise<IOCozyContact[]> => {
const myselfQuery = buildMyselfQuery();

const { data }: { data: IOCozyContact[] } = await client.query(
myselfQuery.definition,
myselfQuery.options,
);

return data;
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export const fetchContacts = async (client: CozyClient): Promise<IOCozyContact[]> => {
const contactsQuery = buildContactsQuery();

Expand Down
35 changes: 34 additions & 1 deletion libs/cozy/sync.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CozyClient from "cozy-client";
import { IOCozyContact, IOCozyFile } from "cozy-client/types/types";

import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
Expand All @@ -10,8 +11,40 @@ import { CozyClientService } from "../../apps/browser/src/popup/services/cozyCli

import { convertAllContactsAsCiphers } from "./contactCipher";
import { convertAllPapersAsCiphers } from "./paperCipher";
import { fetchContactsAndPapers } from "./queries";
import { fetchContactsAndPapers, fetchPapers, fetchMyself } from "./queries";

export const shouldDisplayContact = async (client: CozyClient, contact: IOCozyContact) => {
if (contact.me) {
return true;
}

if (contact.cozyMetadata.favorite) {
return true;
}

const me = await fetchMyself(client);

const contactRelatedToMe =
// @ts-expect-error related added manually with an hydration
me?.[0]?.relationships?.related?.data.find((relatedContact) => relatedContact._id === contact._id);
zatteo marked this conversation as resolved.
Show resolved Hide resolved

if (contactRelatedToMe) {
return true;
}

const papers = await fetchPapers(client);

const contactIsInPaper = papers.find((paper) => {
// @ts-expect-error contacts added manually with an hydration
return paper.contacts.data.find((paperContact) => paperContact._id === contact._id);
});

if (contactIsInPaper) {
return true;
}

return false;
};
export const selectContactsAndPapers = (
contacts: IOCozyContact[],
papers: IOCozyFile[],
Expand Down