chore: handle notifications from websockets (#2472)
This commit is contained in:
Binary file not shown.
BIN
.yarn/cache/@standardnotes-security-npm-1.12.0-b20ed17598-96d42255e7.zip
vendored
Normal file
BIN
.yarn/cache/@standardnotes-security-npm-1.12.0-b20ed17598-96d42255e7.zip
vendored
Normal file
Binary file not shown.
7
packages/services/src/Domain/Api/WebSocketsEventData.ts
Normal file
7
packages/services/src/Domain/Api/WebSocketsEventData.ts
Normal file
@@ -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
|
||||
|
||||
@@ -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>
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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(
|
||||
@@ -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() {
|
||||
@@ -1,4 +1,5 @@
|
||||
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'
|
||||
@@ -6,6 +7,7 @@ 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>
|
||||
@@ -18,8 +20,13 @@ export class NotificationService
|
||||
}
|
||||
|
||||
async handleEvent(event: InternalEventInterface): Promise<void> {
|
||||
if (event.type === SyncEvent.ReceivedNotifications) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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'
|
||||
@@ -83,6 +82,7 @@ import {
|
||||
CreateEncryptedBackupFile,
|
||||
GetTransitionStatus,
|
||||
StartTransition,
|
||||
WebSocketsService,
|
||||
} from '@standardnotes/services'
|
||||
import {
|
||||
SNNote,
|
||||
|
||||
@@ -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'
|
||||
@@ -143,6 +142,7 @@ import {
|
||||
SyncLocalVaultsWithRemoteSharedVaults,
|
||||
GetTransitionStatus,
|
||||
StartTransition,
|
||||
WebSocketsService,
|
||||
} from '@standardnotes/services'
|
||||
import { ItemManager } from '../../Services/Items/ItemManager'
|
||||
import { PayloadManager } from '../../Services/Payloads/PayloadManager'
|
||||
|
||||
@@ -2,4 +2,3 @@ export * from './ApiService'
|
||||
export * from './DeprecatedHttpService'
|
||||
export * from './Paths'
|
||||
export * from '../Session/SessionManager'
|
||||
export * from './WebsocketsService'
|
||||
|
||||
@@ -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 {
|
||||
@@ -23,6 +22,7 @@ import {
|
||||
UserServiceInterface,
|
||||
UserService,
|
||||
IsApplicationUsingThirdPartyHost,
|
||||
WebSocketsService,
|
||||
} from '@standardnotes/services'
|
||||
import { LegacyApiService, SessionManager } from '../Api'
|
||||
import { ItemManager } from '../Items'
|
||||
|
||||
@@ -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 {
|
||||
@@ -47,6 +45,8 @@ import {
|
||||
ApplicationEvent,
|
||||
ApplicationStageChangedEventPayload,
|
||||
IsApplicationUsingThirdPartyHost,
|
||||
WebSocketsServiceEvent,
|
||||
WebSocketsService,
|
||||
} from '@standardnotes/services'
|
||||
|
||||
import { DownloadRemoteThirdPartyFeatureUseCase } from './UseCase/DownloadRemoteThirdPartyFeature'
|
||||
|
||||
@@ -34,6 +34,7 @@ import {
|
||||
ApplicationStage,
|
||||
GetKeyPairs,
|
||||
IsApplicationUsingThirdPartyHost,
|
||||
WebSocketsService,
|
||||
} from '@standardnotes/services'
|
||||
import { Base64String, PureCryptoInterface } from '@standardnotes/sncrypto-common'
|
||||
import {
|
||||
@@ -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'
|
||||
|
||||
@@ -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:*",
|
||||
|
||||
24
yarn.lock
24
yarn.lock
@@ -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
|
||||
|
||||
@@ -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:
|
||||
@@ -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:*"
|
||||
|
||||
Reference in New Issue
Block a user