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

chore: handle notifications from websockets #2472

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions packages/services/src/Domain/Api/WebSocketsEventData.ts
@@ -0,0 +1,7 @@
import { Either } from '@standardnotes/common'
import { UserRolesChangedEventPayload, NotificationAddedForUserEventPayload } from '@standardnotes/domain-events'

export interface WebSocketsEventData {
type: string
payload: Either<UserRolesChangedEventPayload, NotificationAddedForUserEventPayload>
}
@@ -1,3 +1,4 @@
export enum WebSocketsServiceEvent {
UserRoleMessageReceived = 'WebSocketMessageReceived',
NotificationAddedForUser = 'NotificationAddedForUser',
}
@@ -1,13 +1,14 @@
import { InternalEventBusInterface } from '@standardnotes/services'
import { WebSocketApiServiceInterface } from '@standardnotes/api'

import { StorageKey, DiskStorageService } from '@Lib/index'
import { WebSocketsService } from './WebsocketsService'
import { StorageServiceInterface } from '../Storage/StorageServiceInterface'
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
import { StorageKey } from '../Storage/StorageKeys'

describe('webSocketsService', () => {
const webSocketUrl = ''

let storageService: DiskStorageService
let storageService: StorageServiceInterface
let webSocketApiService: WebSocketApiServiceInterface
let internalEventBus: InternalEventBusInterface

Expand All @@ -16,7 +17,7 @@ describe('webSocketsService', () => {
}

beforeEach(() => {
storageService = {} as jest.Mocked<DiskStorageService>
storageService = {} as jest.Mocked<StorageServiceInterface>
storageService.setValue = jest.fn()

internalEventBus = {} as jest.Mocked<InternalEventBusInterface>
Expand All @@ -27,9 +28,9 @@ describe('webSocketsService', () => {
})

describe('setWebSocketUrl()', () => {
it('saves url in local storage', async () => {
it('saves url in local storage', () => {
const webSocketUrl = 'wss://test-websocket'
await createService().setWebSocketUrl(webSocketUrl)
createService().setWebSocketUrl(webSocketUrl)
expect(storageService.setValue).toHaveBeenCalledWith(StorageKey.WebSocketUrl, webSocketUrl)
})
})
Expand Down
@@ -1,15 +1,18 @@
import { isErrorResponse } from '@standardnotes/responses'
import { UserRolesChangedEvent } from '@standardnotes/domain-events'
import {
AbstractService,
InternalEventBusInterface,
StorageKey,
StorageServiceInterface,
} from '@standardnotes/services'
DomainEventInterface,
UserRolesChangedEvent,
NotificationAddedForUserEvent,
} from '@standardnotes/domain-events'
import { WebSocketApiServiceInterface } from '@standardnotes/api'
import { WebSocketsServiceEvent } from './WebSocketsServiceEvent'
import { StorageServiceInterface } from '../Storage/StorageServiceInterface'
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
import { AbstractService } from '../Service/AbstractService'
import { StorageKey } from '../Storage/StorageKeys'
import { WebSocketsEventData } from './WebSocketsEventData'

export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, UserRolesChangedEvent> {
export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, WebSocketsEventData> {
private webSocket?: WebSocket

constructor(
Expand Down Expand Up @@ -61,9 +64,21 @@ export class WebSocketsService extends AbstractService<WebSocketsServiceEvent, U
this.webSocket?.close()
}

private onWebSocketMessage(event: MessageEvent) {
const eventData: UserRolesChangedEvent = JSON.parse(event.data)
void this.notifyEvent(WebSocketsServiceEvent.UserRoleMessageReceived, eventData)
private onWebSocketMessage(messageEvent: MessageEvent) {
const eventData: DomainEventInterface = JSON.parse(messageEvent.data)
switch (eventData.type) {
case 'USER_ROLES_CHANGED':
void this.notifyEvent(WebSocketsServiceEvent.UserRoleMessageReceived, eventData as UserRolesChangedEvent)
break
case 'NOTIFICATION_ADDED_FOR_USER':
void this.notifyEvent(
WebSocketsServiceEvent.NotificationAddedForUser,
eventData as NotificationAddedForUserEvent,
)
break
default:
break
}
}

private onWebSocketClose() {
Expand Down
11 changes: 9 additions & 2 deletions packages/services/src/Domain/UserEvent/NotificationService.ts
@@ -1,11 +1,13 @@
import { NotificationServerHash } from '@standardnotes/responses'
import { NotificationAddedForUserEvent } from '@standardnotes/domain-events'
import { SyncEvent, SyncEventReceivedNotificationsData } from '../Event/SyncEvent'
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
import { InternalEventHandlerInterface } from '../Internal/InternalEventHandlerInterface'
import { InternalEventInterface } from '../Internal/InternalEventInterface'
import { AbstractService } from '../Service/AbstractService'
import { NotificationServiceEventPayload, NotificationServiceEvent } from './NotificationServiceEvent'
import { NotificationPayload } from '@standardnotes/domain-core'
import { WebSocketsServiceEvent } from '../Api/WebSocketsServiceEvent'

export class NotificationService
extends AbstractService<NotificationServiceEvent, NotificationServiceEventPayload>
Expand All @@ -18,8 +20,13 @@ export class NotificationService
}

async handleEvent(event: InternalEventInterface): Promise<void> {
if (event.type === SyncEvent.ReceivedNotifications) {
return this.handleReceivedNotifications(event.payload as SyncEventReceivedNotificationsData)
switch (event.type) {
case SyncEvent.ReceivedNotifications:
return this.handleReceivedNotifications(event.payload as SyncEventReceivedNotificationsData)
case WebSocketsServiceEvent.NotificationAddedForUser:
return this.handleReceivedNotifications([(event as NotificationAddedForUserEvent).payload.notification])
default:
break
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/services/src/Domain/index.ts
Expand Up @@ -4,6 +4,9 @@ export * from './Api/ApiServiceEventData'
export * from './Api/LegacyApiServiceInterface'
export * from './Api/MetaReceivedData'
export * from './Api/SessionRefreshedData'
export * from './Api/WebSocketsEventData'
export * from './Api/WebsocketsService'
export * from './Api/WebSocketsServiceEvent'
export * from './Application/AppGroupManagedApplication'
export * from './Application/ApplicationInterface'
export * from './Application/ApplicationStage'
Expand Down
2 changes: 1 addition & 1 deletion packages/snjs/lib/Application/Application.ts
@@ -1,6 +1,5 @@
import { MfaService } from './../Services/Mfa/MfaService'
import { KeyRecoveryService } from './../Services/KeyRecovery/KeyRecoveryService'
import { WebSocketsService } from './../Services/Api/WebsocketsService'
import { MigrationService } from './../Services/Migration/MigrationService'
import { LegacyApiService } from './../Services/Api/ApiService'
import { FeaturesService } from '@Lib/Services/Features/FeaturesService'
Expand Down Expand Up @@ -83,6 +82,7 @@ import {
CreateEncryptedBackupFile,
GetTransitionStatus,
StartTransition,
WebSocketsService,
} from '@standardnotes/services'
import {
SNNote,
Expand Down
2 changes: 1 addition & 1 deletion packages/snjs/lib/Application/Dependencies/Dependencies.ts
Expand Up @@ -22,7 +22,6 @@ import { ProtectionService } from '../../Services/Protection/ProtectionService'
import { SyncService } from '../../Services/Sync/SyncService'
import { HistoryManager } from '../../Services/History/HistoryManager'
import { SessionManager } from '../../Services/Session/SessionManager'
import { WebSocketsService } from '../../Services/Api/WebsocketsService'
import { LegacyApiService } from '../../Services/Api/ApiService'
import { SnjsVersion } from '../../Version'
import { DeprecatedHttpService } from '../../Services/Api/DeprecatedHttpService'
Expand Down Expand Up @@ -143,6 +142,7 @@ import {
SyncLocalVaultsWithRemoteSharedVaults,
GetTransitionStatus,
StartTransition,
WebSocketsService,
} from '@standardnotes/services'
import { ItemManager } from '../../Services/Items/ItemManager'
import { PayloadManager } from '../../Services/Payloads/PayloadManager'
Expand Down
1 change: 0 additions & 1 deletion packages/snjs/lib/Services/Api/index.ts
Expand Up @@ -2,4 +2,3 @@ export * from './ApiService'
export * from './DeprecatedHttpService'
export * from './Paths'
export * from '../Session/SessionManager'
export * from './WebsocketsService'
Expand Up @@ -4,7 +4,6 @@ import { SettingName } from '@standardnotes/settings'
import { FeaturesService } from '@Lib/Services/Features'
import { RoleName, ContentType, Uuid, Result } from '@standardnotes/domain-core'
import { NativeFeatureIdentifier, GetFeatures } from '@standardnotes/features'
import { WebSocketsService } from '../Api/WebsocketsService'
import { SettingsService } from '../Settings'
import { PureCryptoInterface } from '@standardnotes/sncrypto-common'
import {
Expand All @@ -23,6 +22,7 @@ import {
UserServiceInterface,
UserService,
IsApplicationUsingThirdPartyHost,
WebSocketsService,
} from '@standardnotes/services'
import { LegacyApiService, SessionManager } from '../Api'
import { ItemManager } from '../Items'
Expand Down
4 changes: 2 additions & 2 deletions packages/snjs/lib/Services/Features/FeaturesService.ts
Expand Up @@ -3,8 +3,6 @@ import { arraysEqual, removeFromArray, lastElement, LoggerInterface } from '@sta
import { ClientDisplayableError } from '@standardnotes/responses'
import { RoleName, ContentType, Uuid } from '@standardnotes/domain-core'
import { PureCryptoInterface } from '@standardnotes/sncrypto-common'
import { WebSocketsService } from '../Api/WebsocketsService'
import { WebSocketsServiceEvent } from '../Api/WebSocketsServiceEvent'
import { UserRolesChangedEvent } from '@standardnotes/domain-events'
import { ExperimentalFeatures, FindNativeFeature, NativeFeatureIdentifier } from '@standardnotes/features'
import {
Expand Down Expand Up @@ -47,6 +45,8 @@ import {
ApplicationEvent,
ApplicationStageChangedEventPayload,
IsApplicationUsingThirdPartyHost,
WebSocketsServiceEvent,
WebSocketsService,
} from '@standardnotes/services'

import { DownloadRemoteThirdPartyFeatureUseCase } from './UseCase/DownloadRemoteThirdPartyFeature'
Expand Down
2 changes: 1 addition & 1 deletion packages/snjs/lib/Services/Session/SessionManager.ts
Expand Up @@ -34,6 +34,7 @@ import {
ApplicationStage,
GetKeyPairs,
IsApplicationUsingThirdPartyHost,
WebSocketsService,
} from '@standardnotes/services'
import { Base64String, PureCryptoInterface } from '@standardnotes/sncrypto-common'
import {
Expand All @@ -59,7 +60,6 @@ import { RawStorageValue } from './Sessions/Types'
import { ShareToken } from './ShareToken'
import { LegacyApiService } from '../Api/ApiService'
import { DiskStorageService } from '../Storage/DiskStorageService'
import { WebSocketsService } from '../Api/WebsocketsService'
import { Strings } from '@Lib/Strings'
import { UuidString } from '@Lib/Types/UuidString'
import { ChallengeResponse, ChallengeService } from '../Challenge'
Expand Down
2 changes: 1 addition & 1 deletion packages/snjs/package.json
Expand Up @@ -38,7 +38,7 @@
"@standardnotes/api": "workspace:*",
"@standardnotes/common": "^1.50.0",
"@standardnotes/domain-core": "^1.25.0",
"@standardnotes/domain-events": "^2.108.1",
"@standardnotes/domain-events": "^2.120.0",
"@standardnotes/encryption": "workspace:*",
"@standardnotes/features": "workspace:*",
"@standardnotes/files": "workspace:*",
Expand Down
24 changes: 17 additions & 7 deletions yarn.lock
Expand Up @@ -4349,13 +4349,13 @@ __metadata:
languageName: node
linkType: hard

"@standardnotes/domain-events@npm:^2.108.1":
version: 2.113.1
resolution: "@standardnotes/domain-events@npm:2.113.1"
"@standardnotes/domain-events@npm:^2.120.0":
version: 2.120.0
resolution: "@standardnotes/domain-events@npm:2.120.0"
dependencies:
"@standardnotes/predicates": 1.6.9
"@standardnotes/security": 1.8.1
checksum: de7a64b5882c2126fb6c7c7485c330e7065f38cfad83d6bfe3b4025bd42b28e6de6b7fb137086661b8ab16e8ff63b9b9a91e1b52b23ba413f108605d02f8e61a
"@standardnotes/security": 1.12.0
checksum: bcd6caf10bc050199db4d0fb13605d43773418c1420efea583381ae6cd33a1a365451a61f3d8d6e08d19a52005e5d67ad2b4c8f3b8f951269b1a2a4bc0ac33cf
languageName: node
linkType: hard

Expand Down Expand Up @@ -4703,7 +4703,17 @@ __metadata:
languageName: node
linkType: hard

"@standardnotes/security@npm:1.8.1, @standardnotes/security@npm:^1.2.0":
"@standardnotes/security@npm:1.12.0":
version: 1.12.0
resolution: "@standardnotes/security@npm:1.12.0"
dependencies:
jsonwebtoken: ^9.0.0
reflect-metadata: ^0.1.13
checksum: 96d42255e79fc2cf4c52f3d370c04ae00296673993e1c0cea2356ca4a6c934d22d6100ba95f9ad602ffb72ee686367ef4a263dc6a6c51795d9b61dc835e635cd
languageName: node
linkType: hard

"@standardnotes/security@npm:^1.2.0":
version: 1.8.1
resolution: "@standardnotes/security@npm:1.8.1"
dependencies:
Expand Down Expand Up @@ -4819,7 +4829,7 @@ __metadata:
"@standardnotes/api": "workspace:*"
"@standardnotes/common": ^1.50.0
"@standardnotes/domain-core": ^1.25.0
"@standardnotes/domain-events": ^2.108.1
"@standardnotes/domain-events": ^2.120.0
"@standardnotes/encryption": "workspace:*"
"@standardnotes/features": "workspace:*"
"@standardnotes/files": "workspace:*"
Expand Down