From b2ead8fc843c5a46a45824c866bd1e2d07142b33 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Tue, 26 Sep 2023 15:53:18 +0530 Subject: [PATCH] chore: handle notification on guest when host cancels invite (#2537) --- .../src/Domain/VaultInvite/VaultInviteService.ts | 16 +++++++++++++++- .../Application/Dependencies/DependencyEvents.ts | 1 + .../Preferences/Panes/Vaults/Vaults.tsx | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/services/src/Domain/VaultInvite/VaultInviteService.ts b/packages/services/src/Domain/VaultInvite/VaultInviteService.ts index 3c9fa5df3..a4ac26cf6 100644 --- a/packages/services/src/Domain/VaultInvite/VaultInviteService.ts +++ b/packages/services/src/Domain/VaultInvite/VaultInviteService.ts @@ -6,7 +6,7 @@ import { isClientDisplayableError, isErrorResponse, } from '@standardnotes/responses' -import { ContentType, Result } from '@standardnotes/domain-core' +import { ContentType, NotificationType, Result } from '@standardnotes/domain-core' import { SharedVaultInvitesServer } from '@standardnotes/api' import { AsymmetricMessageSharedVaultInvite, @@ -40,6 +40,7 @@ import { DecryptErroredPayloads } from '../Encryption/UseCase/DecryptErroredPayl import { StatusServiceInterface } from '../Status/StatusServiceInterface' import { ApplicationEvent } from '../Event/ApplicationEvent' import { WebSocketsServiceEvent } from '../Api/WebSocketsServiceEvent' +import { NotificationServiceEvent, NotificationServiceEventPayload } from '../UserEvent/NotificationServiceEvent' export class VaultInviteService extends AbstractService @@ -124,12 +125,25 @@ export class VaultInviteService } void this.downloadInboundInvites() break + case NotificationServiceEvent.NotificationReceived: + await this.handleNotification(event.payload as NotificationServiceEventPayload) + break case WebSocketsServiceEvent.UserInvitedToSharedVault: await this.processInboundInvites([(event as UserInvitedToSharedVaultEvent).payload.invite]) break } } + private async handleNotification(event: NotificationServiceEventPayload): Promise { + switch (event.eventPayload.props.type.value) { + case NotificationType.TYPES.SharedVaultInviteCanceled: { + this.removePendingInvite(event.eventPayload.props.primaryIdentifier.value) + void this.notifyEvent(VaultInviteServiceEvent.InvitesReloaded) + break + } + } + } + public getCachedPendingInviteRecords(): InviteRecord[] { return Object.values(this.pendingInvites) } diff --git a/packages/snjs/lib/Application/Dependencies/DependencyEvents.ts b/packages/snjs/lib/Application/Dependencies/DependencyEvents.ts index 6d7781f80..5a3896bc9 100644 --- a/packages/snjs/lib/Application/Dependencies/DependencyEvents.ts +++ b/packages/snjs/lib/Application/Dependencies/DependencyEvents.ts @@ -31,6 +31,7 @@ export function RegisterApplicationServicesEvents(container: Dependencies, event events.addEventHandler(container.get(TYPES.SessionManager), ApiServiceEvent.SessionRefreshed) events.addEventHandler(container.get(TYPES.SessionManager), ApplicationEvent.ApplicationStageChanged) events.addEventHandler(container.get(TYPES.SharedVaultService), NotificationServiceEvent.NotificationReceived) + events.addEventHandler(container.get(TYPES.VaultInviteService), NotificationServiceEvent.NotificationReceived) events.addEventHandler(container.get(TYPES.SharedVaultService), SessionEvent.UserKeyPairChanged) events.addEventHandler(container.get(TYPES.SharedVaultService), SyncEvent.ReceivedRemoteSharedVaults) events.addEventHandler(container.get(TYPES.SubscriptionManager), ApplicationEvent.ApplicationStageChanged) diff --git a/packages/web/src/javascripts/Components/Preferences/Panes/Vaults/Vaults.tsx b/packages/web/src/javascripts/Components/Preferences/Panes/Vaults/Vaults.tsx index 6bbb2d6b1..8d29353a5 100644 --- a/packages/web/src/javascripts/Components/Preferences/Panes/Vaults/Vaults.tsx +++ b/packages/web/src/javascripts/Components/Preferences/Panes/Vaults/Vaults.tsx @@ -17,6 +17,7 @@ import { RoleName, ProtocolVersion, compareVersions, + VaultInviteServiceEvent, } from '@standardnotes/snjs' import VaultItem from './Vaults/VaultItem' import Button from '@/Components/Button/Button' @@ -70,6 +71,13 @@ const Vaults = observer(() => { const updateInvites = useCallback(async () => { setInvites(application.vaultInvites.getCachedPendingInviteRecords()) }, [application.vaultInvites]) + useEffect(() => { + return application.vaultInvites.addEventObserver((event) => { + if (event === VaultInviteServiceEvent.InvitesReloaded) { + void updateInvites() + } + }) + }, [application.vaultInvites, updateInvites]) const updateContacts = useCallback(async () => { setContacts(contactService.getAllContacts())