chore: vault tests refactors and lint (#2374)
This commit is contained in:
@@ -25,6 +25,7 @@ import {
|
||||
KeySystemRootKeyContentSpecialized,
|
||||
TrustedContactInterface,
|
||||
} from '@standardnotes/models'
|
||||
import { Result } from '@standardnotes/domain-core'
|
||||
|
||||
describe('AsymmetricMessageService', () => {
|
||||
let sync: jest.Mocked<SyncServiceInterface>
|
||||
@@ -61,6 +62,7 @@ describe('AsymmetricMessageService', () => {
|
||||
encryption,
|
||||
mutator,
|
||||
sessions,
|
||||
sync,
|
||||
messageServer,
|
||||
createOrEditContact,
|
||||
findContact,
|
||||
@@ -115,6 +117,45 @@ describe('AsymmetricMessageService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('handleTrustedMessageResult', () => {
|
||||
it('should not double handle the same message', async () => {
|
||||
/**
|
||||
* Because message retrieval is based on a syncToken, and the server aligns syncTokens to items sent back
|
||||
* rather than messages, we may receive the same message twice. We want to keep track of processed messages
|
||||
* and avoid double processing.
|
||||
*/
|
||||
|
||||
const message: AsymmetricMessageServerHash = {
|
||||
uuid: 'message',
|
||||
recipient_uuid: '1',
|
||||
sender_uuid: '2',
|
||||
encrypted_message: 'encrypted_message',
|
||||
created_at_timestamp: 2,
|
||||
updated_at_timestamp: 2,
|
||||
}
|
||||
|
||||
const decryptedMessagePayload: AsymmetricMessageTrustedContactShare = {
|
||||
type: AsymmetricMessagePayloadType.ContactShare,
|
||||
data: {
|
||||
recipientUuid: '1',
|
||||
trustedContact: {} as TrustedContactInterface,
|
||||
},
|
||||
}
|
||||
|
||||
service.getTrustedMessagePayload = service.getUntrustedMessagePayload = jest
|
||||
.fn()
|
||||
.mockReturnValue(Result.ok(decryptedMessagePayload))
|
||||
|
||||
service.handleTrustedContactShareMessage = jest.fn()
|
||||
await service.handleTrustedMessageResult(message, decryptedMessagePayload)
|
||||
expect(service.handleTrustedContactShareMessage).toHaveBeenCalledTimes(1)
|
||||
|
||||
service.handleTrustedContactShareMessage = jest.fn()
|
||||
await service.handleTrustedMessageResult(message, decryptedMessagePayload)
|
||||
expect(service.handleTrustedContactShareMessage).toHaveBeenCalledTimes(0)
|
||||
})
|
||||
})
|
||||
|
||||
it('should process incoming messages oldest first', async () => {
|
||||
const messages: AsymmetricMessageServerHash[] = [
|
||||
{
|
||||
@@ -139,7 +180,7 @@ describe('AsymmetricMessageService', () => {
|
||||
|
||||
service.getTrustedMessagePayload = service.getUntrustedMessagePayload = jest
|
||||
.fn()
|
||||
.mockReturnValue(trustedPayloadMock)
|
||||
.mockReturnValue(Result.ok(trustedPayloadMock))
|
||||
|
||||
const handleTrustedContactShareMessageMock = jest.fn()
|
||||
service.handleTrustedContactShareMessage = handleTrustedContactShareMessageMock
|
||||
@@ -171,7 +212,7 @@ describe('AsymmetricMessageService', () => {
|
||||
service.handleTrustedContactShareMessage = jest.fn()
|
||||
service.getTrustedMessagePayload = service.getUntrustedMessagePayload = jest
|
||||
.fn()
|
||||
.mockReturnValue(decryptedMessagePayload)
|
||||
.mockReturnValue(Result.ok(decryptedMessagePayload))
|
||||
|
||||
await service.handleRemoteReceivedAsymmetricMessages([message])
|
||||
|
||||
@@ -200,7 +241,7 @@ describe('AsymmetricMessageService', () => {
|
||||
service.handleTrustedSenderKeypairChangedMessage = jest.fn()
|
||||
service.getTrustedMessagePayload = service.getUntrustedMessagePayload = jest
|
||||
.fn()
|
||||
.mockReturnValue(decryptedMessagePayload)
|
||||
.mockReturnValue(Result.ok(decryptedMessagePayload))
|
||||
|
||||
await service.handleRemoteReceivedAsymmetricMessages([message])
|
||||
|
||||
@@ -228,7 +269,7 @@ describe('AsymmetricMessageService', () => {
|
||||
service.handleTrustedSharedVaultRootKeyChangedMessage = jest.fn()
|
||||
service.getTrustedMessagePayload = service.getUntrustedMessagePayload = jest
|
||||
.fn()
|
||||
.mockReturnValue(decryptedMessagePayload)
|
||||
.mockReturnValue(Result.ok(decryptedMessagePayload))
|
||||
|
||||
await service.handleRemoteReceivedAsymmetricMessages([message])
|
||||
|
||||
@@ -258,7 +299,7 @@ describe('AsymmetricMessageService', () => {
|
||||
service.handleTrustedVaultMetadataChangedMessage = jest.fn()
|
||||
service.getTrustedMessagePayload = service.getUntrustedMessagePayload = jest
|
||||
.fn()
|
||||
.mockReturnValue(decryptedMessagePayload)
|
||||
.mockReturnValue(Result.ok(decryptedMessagePayload))
|
||||
|
||||
await service.handleRemoteReceivedAsymmetricMessages([message])
|
||||
|
||||
@@ -284,7 +325,7 @@ describe('AsymmetricMessageService', () => {
|
||||
|
||||
service.getTrustedMessagePayload = service.getUntrustedMessagePayload = jest
|
||||
.fn()
|
||||
.mockReturnValue(decryptedMessagePayload)
|
||||
.mockReturnValue(Result.ok(decryptedMessagePayload))
|
||||
|
||||
await expect(service.handleRemoteReceivedAsymmetricMessages([message])).rejects.toThrow(
|
||||
'Shared vault invites payloads are not handled as part of asymmetric messages',
|
||||
@@ -313,7 +354,7 @@ describe('AsymmetricMessageService', () => {
|
||||
service.handleTrustedContactShareMessage = jest.fn()
|
||||
service.getTrustedMessagePayload = service.getUntrustedMessagePayload = jest
|
||||
.fn()
|
||||
.mockReturnValue(decryptedMessagePayload)
|
||||
.mockReturnValue(Result.ok(decryptedMessagePayload))
|
||||
|
||||
await service.handleRemoteReceivedAsymmetricMessages([message])
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { SyncServiceInterface } from './../Sync/SyncServiceInterface'
|
||||
import { SessionsClientInterface } from './../Session/SessionsClientInterface'
|
||||
import { MutatorClientInterface } from './../Mutator/MutatorClientInterface'
|
||||
import { AsymmetricMessageServerHash, ClientDisplayableError, isClientDisplayableError } from '@standardnotes/responses'
|
||||
import { AsymmetricMessageServerHash } from '@standardnotes/responses'
|
||||
import { SyncEvent, SyncEventReceivedAsymmetricMessagesData } from '../Event/SyncEvent'
|
||||
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||
import { InternalEventHandlerInterface } from '../Internal/InternalEventHandlerInterface'
|
||||
@@ -20,7 +21,6 @@ import {
|
||||
VaultListingInterface,
|
||||
} from '@standardnotes/models'
|
||||
import { HandleRootKeyChangedMessage } from './UseCase/HandleRootKeyChangedMessage'
|
||||
import { SessionEvent } from '../Session/SessionEvent'
|
||||
import { AsymmetricMessageServer } from '@standardnotes/api'
|
||||
import { GetOutboundMessages } from './UseCase/GetOutboundMessages'
|
||||
import { GetInboundMessages } from './UseCase/GetInboundMessages'
|
||||
@@ -31,15 +31,19 @@ import { FindContact } from '../Contacts/UseCase/FindContact'
|
||||
import { CreateOrEditContact } from '../Contacts/UseCase/CreateOrEditContact'
|
||||
import { ReplaceContactData } from '../Contacts/UseCase/ReplaceContactData'
|
||||
import { EncryptionProviderInterface } from '../Encryption/EncryptionProviderInterface'
|
||||
import { Result } from '@standardnotes/domain-core'
|
||||
|
||||
export class AsymmetricMessageService
|
||||
extends AbstractService
|
||||
implements AsymmetricMessageServiceInterface, InternalEventHandlerInterface
|
||||
{
|
||||
private handledMessages = new Set<string>()
|
||||
|
||||
constructor(
|
||||
private encryption: EncryptionProviderInterface,
|
||||
private mutator: MutatorClientInterface,
|
||||
private sessions: SessionsClientInterface,
|
||||
private sync: SyncServiceInterface,
|
||||
private messageServer: AsymmetricMessageServer,
|
||||
private _createOrEditContact: CreateOrEditContact,
|
||||
private _findContact: FindContact,
|
||||
@@ -73,30 +77,27 @@ export class AsymmetricMessageService
|
||||
|
||||
async handleEvent(event: InternalEventInterface): Promise<void> {
|
||||
switch (event.type) {
|
||||
case SessionEvent.UserKeyPairChanged:
|
||||
void this.messageServer.deleteAllInboundMessages()
|
||||
break
|
||||
case SyncEvent.ReceivedAsymmetricMessages:
|
||||
void this.handleRemoteReceivedAsymmetricMessages(event.payload as SyncEventReceivedAsymmetricMessagesData)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
public async getOutboundMessages(): Promise<AsymmetricMessageServerHash[] | ClientDisplayableError> {
|
||||
public async getOutboundMessages(): Promise<Result<AsymmetricMessageServerHash[]>> {
|
||||
return this._getOutboundMessagesUseCase.execute()
|
||||
}
|
||||
|
||||
public async getInboundMessages(): Promise<AsymmetricMessageServerHash[] | ClientDisplayableError> {
|
||||
public async getInboundMessages(): Promise<Result<AsymmetricMessageServerHash[]>> {
|
||||
return this._getInboundMessagesUseCase.execute()
|
||||
}
|
||||
|
||||
public async downloadAndProcessInboundMessages(): Promise<void> {
|
||||
const messages = await this.getInboundMessages()
|
||||
if (isClientDisplayableError(messages)) {
|
||||
if (messages.isFailed()) {
|
||||
return
|
||||
}
|
||||
|
||||
await this.handleRemoteReceivedAsymmetricMessages(messages)
|
||||
await this.handleRemoteReceivedAsymmetricMessages(messages.getValue())
|
||||
}
|
||||
|
||||
sortServerMessages(messages: AsymmetricMessageServerHash[]): AsymmetricMessageServerHash[] {
|
||||
@@ -143,11 +144,11 @@ export class AsymmetricMessageService
|
||||
getServerMessageType(message: AsymmetricMessageServerHash): AsymmetricMessagePayloadType | undefined {
|
||||
const result = this.getUntrustedMessagePayload(message)
|
||||
|
||||
if (!result) {
|
||||
if (result.isFailed()) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return result.type
|
||||
return result.getValue().type
|
||||
}
|
||||
|
||||
async handleRemoteReceivedAsymmetricMessages(messages: AsymmetricMessageServerHash[]): Promise<void> {
|
||||
@@ -159,18 +160,26 @@ export class AsymmetricMessageService
|
||||
|
||||
for (const message of sortedMessages) {
|
||||
const trustedPayload = this.getTrustedMessagePayload(message)
|
||||
if (!trustedPayload) {
|
||||
if (trustedPayload.isFailed()) {
|
||||
continue
|
||||
}
|
||||
|
||||
await this.handleTrustedMessageResult(message, trustedPayload)
|
||||
await this.handleTrustedMessageResult(message, trustedPayload.getValue())
|
||||
}
|
||||
|
||||
void this.sync.sync()
|
||||
}
|
||||
|
||||
private async handleTrustedMessageResult(
|
||||
async handleTrustedMessageResult(
|
||||
message: AsymmetricMessageServerHash,
|
||||
payload: AsymmetricMessagePayload,
|
||||
): Promise<void> {
|
||||
if (this.handledMessages.has(message.uuid)) {
|
||||
return
|
||||
}
|
||||
|
||||
this.handledMessages.add(message.uuid)
|
||||
|
||||
if (payload.type === AsymmetricMessagePayloadType.ContactShare) {
|
||||
await this.handleTrustedContactShareMessage(message, payload)
|
||||
} else if (payload.type === AsymmetricMessagePayloadType.SenderKeypairChanged) {
|
||||
@@ -186,23 +195,23 @@ export class AsymmetricMessageService
|
||||
await this.deleteMessageAfterProcessing(message)
|
||||
}
|
||||
|
||||
getUntrustedMessagePayload(message: AsymmetricMessageServerHash): AsymmetricMessagePayload | undefined {
|
||||
getUntrustedMessagePayload(message: AsymmetricMessageServerHash): Result<AsymmetricMessagePayload> {
|
||||
const result = this._getUntrustedPayload.execute({
|
||||
privateKey: this.encryption.getKeyPair().privateKey,
|
||||
message,
|
||||
})
|
||||
|
||||
if (result.isFailed()) {
|
||||
return undefined
|
||||
return Result.fail(result.getError())
|
||||
}
|
||||
|
||||
return result.getValue()
|
||||
return result
|
||||
}
|
||||
|
||||
getTrustedMessagePayload(message: AsymmetricMessageServerHash): AsymmetricMessagePayload | undefined {
|
||||
getTrustedMessagePayload(message: AsymmetricMessageServerHash): Result<AsymmetricMessagePayload> {
|
||||
const contact = this._findContact.execute({ userUuid: message.sender_uuid })
|
||||
if (contact.isFailed()) {
|
||||
return undefined
|
||||
return Result.fail(contact.getError())
|
||||
}
|
||||
|
||||
const result = this._getTrustedPayload.execute({
|
||||
@@ -213,10 +222,10 @@ export class AsymmetricMessageService
|
||||
})
|
||||
|
||||
if (result.isFailed()) {
|
||||
return undefined
|
||||
return Result.fail(result.getError())
|
||||
}
|
||||
|
||||
return result.getValue()
|
||||
return result
|
||||
}
|
||||
|
||||
async deleteMessageAfterProcessing(message: AsymmetricMessageServerHash): Promise<void> {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { AsymmetricMessageServerHash, ClientDisplayableError } from '@standardnotes/responses'
|
||||
import { Result } from '@standardnotes/domain-core'
|
||||
import { AsymmetricMessageServerHash } from '@standardnotes/responses'
|
||||
|
||||
export interface AsymmetricMessageServiceInterface {
|
||||
getOutboundMessages(): Promise<AsymmetricMessageServerHash[] | ClientDisplayableError>
|
||||
getInboundMessages(): Promise<AsymmetricMessageServerHash[] | ClientDisplayableError>
|
||||
getOutboundMessages(): Promise<Result<AsymmetricMessageServerHash[]>>
|
||||
getInboundMessages(): Promise<Result<AsymmetricMessageServerHash[]>>
|
||||
downloadAndProcessInboundMessages(): Promise<void>
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import { ClientDisplayableError, isErrorResponse, AsymmetricMessageServerHash } from '@standardnotes/responses'
|
||||
import { isErrorResponse, AsymmetricMessageServerHash, getErrorFromErrorResponse } from '@standardnotes/responses'
|
||||
import { AsymmetricMessageServerInterface } from '@standardnotes/api'
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
|
||||
export class GetInboundMessages {
|
||||
export class GetInboundMessages implements UseCaseInterface<AsymmetricMessageServerHash[]> {
|
||||
constructor(private messageServer: AsymmetricMessageServerInterface) {}
|
||||
|
||||
async execute(): Promise<AsymmetricMessageServerHash[] | ClientDisplayableError> {
|
||||
async execute(): Promise<Result<AsymmetricMessageServerHash[]>> {
|
||||
const response = await this.messageServer.getMessages()
|
||||
|
||||
if (isErrorResponse(response)) {
|
||||
return ClientDisplayableError.FromNetworkError(response)
|
||||
return Result.fail(getErrorFromErrorResponse(response).message)
|
||||
}
|
||||
|
||||
return response.data.messages
|
||||
return Result.ok(response.data.messages)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import { ClientDisplayableError, isErrorResponse, AsymmetricMessageServerHash } from '@standardnotes/responses'
|
||||
import { isErrorResponse, AsymmetricMessageServerHash, getErrorFromErrorResponse } from '@standardnotes/responses'
|
||||
import { AsymmetricMessageServerInterface } from '@standardnotes/api'
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
|
||||
export class GetOutboundMessages {
|
||||
export class GetOutboundMessages implements UseCaseInterface<AsymmetricMessageServerHash[]> {
|
||||
constructor(private messageServer: AsymmetricMessageServerInterface) {}
|
||||
|
||||
async execute(): Promise<AsymmetricMessageServerHash[] | ClientDisplayableError> {
|
||||
async execute(): Promise<Result<AsymmetricMessageServerHash[]>> {
|
||||
const response = await this.messageServer.getOutboundUserMessages()
|
||||
|
||||
if (isErrorResponse(response)) {
|
||||
return ClientDisplayableError.FromNetworkError(response)
|
||||
return Result.fail(getErrorFromErrorResponse(response).message)
|
||||
}
|
||||
|
||||
return response.data.messages
|
||||
return Result.ok(response.data.messages)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import { ResendAllMessages } from './ResendAllMessages'
|
||||
import { Result } from '@standardnotes/domain-core'
|
||||
import { PkcKeyPair } from '@standardnotes/sncrypto-common'
|
||||
import { AsymmetricMessagePayloadType } from '@standardnotes/models'
|
||||
|
||||
describe('ResendAllMessages', () => {
|
||||
let mockDecryptOwnMessage: any
|
||||
let mockMessageServer: any
|
||||
let mockResendMessage: any
|
||||
let mockFindContact: any
|
||||
|
||||
let useCase: ResendAllMessages
|
||||
let params: {
|
||||
keys: { encryption: PkcKeyPair; signing: PkcKeyPair }
|
||||
previousKeys?: { encryption: PkcKeyPair; signing: PkcKeyPair }
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
|
||||
mockDecryptOwnMessage = {
|
||||
execute: jest.fn(),
|
||||
}
|
||||
|
||||
mockMessageServer = {
|
||||
getOutboundUserMessages: jest.fn(),
|
||||
deleteMessage: jest.fn(),
|
||||
}
|
||||
|
||||
mockResendMessage = {
|
||||
execute: jest.fn(),
|
||||
}
|
||||
|
||||
mockFindContact = {
|
||||
execute: jest.fn(),
|
||||
}
|
||||
|
||||
useCase = new ResendAllMessages(mockResendMessage, mockDecryptOwnMessage, mockMessageServer, mockFindContact)
|
||||
params = {
|
||||
keys: {
|
||||
encryption: { publicKey: 'new_public_key', privateKey: 'new_private_key' },
|
||||
signing: { publicKey: 'new_public_key', privateKey: 'new_private_key' },
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
it('should successfully resend all messages', async () => {
|
||||
const messages = {
|
||||
data: { messages: [{ recipient_uuid: 'uuid', uuid: 'uuid', encrypted_message: 'encrypted_message' }] },
|
||||
}
|
||||
const recipient = { publicKeySet: { encryption: 'public_key' } }
|
||||
const decryptedMessage = { type: AsymmetricMessagePayloadType.ContactShare }
|
||||
|
||||
mockMessageServer.getOutboundUserMessages.mockReturnValue(messages)
|
||||
mockFindContact.execute.mockReturnValue(Result.ok(recipient))
|
||||
mockDecryptOwnMessage.execute.mockReturnValue(Result.ok(decryptedMessage))
|
||||
|
||||
const result = await useCase.execute(params)
|
||||
|
||||
expect(result).toEqual(Result.ok())
|
||||
expect(mockMessageServer.getOutboundUserMessages).toHaveBeenCalled()
|
||||
expect(mockFindContact.execute).toHaveBeenCalled()
|
||||
expect(mockDecryptOwnMessage.execute).toHaveBeenCalled()
|
||||
expect(mockResendMessage.execute).toHaveBeenCalled()
|
||||
expect(mockMessageServer.deleteMessage).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should handle errors while getting outbound user messages', async () => {
|
||||
mockMessageServer.getOutboundUserMessages.mockReturnValue({ data: { error: 'Error' } })
|
||||
|
||||
const result = await useCase.execute(params)
|
||||
|
||||
expect(result.isFailed()).toBeTruthy()
|
||||
expect(result.getError()).toBe('Failed to get outbound user messages')
|
||||
})
|
||||
|
||||
it('should handle errors while finding contact', async () => {
|
||||
const messages = {
|
||||
data: { messages: [{ recipient_uuid: 'uuid', uuid: 'uuid', encrypted_message: 'encrypted_message' }] },
|
||||
}
|
||||
|
||||
mockMessageServer.getOutboundUserMessages.mockReturnValue(messages)
|
||||
mockFindContact.execute.mockReturnValue(Result.fail('Contact not found'))
|
||||
|
||||
const result = await useCase.execute(params)
|
||||
|
||||
expect(result.isFailed()).toBeTruthy()
|
||||
expect(result.getError()).toContain('Contact not found')
|
||||
})
|
||||
|
||||
it('should skip messages of excluded types', async () => {
|
||||
const messages = {
|
||||
data: {
|
||||
messages: [
|
||||
{ recipient_uuid: 'uuid', uuid: 'uuid', encrypted_message: 'encrypted_message' },
|
||||
{ recipient_uuid: 'uuid2', uuid: 'uuid2', encrypted_message: 'encrypted_message2' },
|
||||
],
|
||||
},
|
||||
}
|
||||
const recipient = { publicKeySet: { encryption: 'public_key' } }
|
||||
const decryptedMessage1 = { type: AsymmetricMessagePayloadType.SenderKeypairChanged }
|
||||
const decryptedMessage2 = { type: AsymmetricMessagePayloadType.ContactShare }
|
||||
|
||||
mockMessageServer.getOutboundUserMessages.mockReturnValue(messages)
|
||||
mockFindContact.execute.mockReturnValue(Result.ok(recipient))
|
||||
|
||||
mockDecryptOwnMessage.execute
|
||||
.mockReturnValueOnce(Result.ok(decryptedMessage1))
|
||||
.mockReturnValueOnce(Result.ok(decryptedMessage2))
|
||||
|
||||
const result = await useCase.execute(params)
|
||||
|
||||
expect(result).toEqual(Result.ok())
|
||||
expect(mockMessageServer.getOutboundUserMessages).toHaveBeenCalled()
|
||||
expect(mockFindContact.execute).toHaveBeenCalledTimes(2)
|
||||
expect(mockDecryptOwnMessage.execute).toHaveBeenCalledTimes(2)
|
||||
expect(mockResendMessage.execute).toHaveBeenCalledTimes(1)
|
||||
expect(mockMessageServer.deleteMessage).toHaveBeenCalledTimes(1)
|
||||
expect(mockResendMessage.execute).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ rawMessage: messages.data.messages[1] }),
|
||||
)
|
||||
expect(mockMessageServer.deleteMessage).toHaveBeenCalledWith({ messageUuid: messages.data.messages[1].uuid })
|
||||
})
|
||||
})
|
||||
@@ -1,17 +1,28 @@
|
||||
import { DecryptOwnMessage } from './../../Encryption/UseCase/Asymmetric/DecryptOwnMessage'
|
||||
import { AsymmetricMessageServerHash, isErrorResponse } from '@standardnotes/responses'
|
||||
import { PkcKeyPair } from '@standardnotes/sncrypto-common'
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
import { AsymmetricMessageServerInterface } from '@standardnotes/api'
|
||||
import { ResendMessage } from './ResendMessage'
|
||||
import { FindContact } from '../../Contacts/UseCase/FindContact'
|
||||
import { AsymmetricMessagePayload, AsymmetricMessagePayloadType } from '@standardnotes/models'
|
||||
|
||||
export class ResendAllMessages implements UseCaseInterface<void> {
|
||||
constructor(
|
||||
private resendMessage: ResendMessage,
|
||||
private decryptOwnMessage: DecryptOwnMessage<AsymmetricMessagePayload>,
|
||||
private messageServer: AsymmetricMessageServerInterface,
|
||||
private findContact: FindContact,
|
||||
) {}
|
||||
|
||||
messagesToExcludeFromResending(): AsymmetricMessagePayloadType[] {
|
||||
/**
|
||||
* Sender key pair changed messages should never be re-encrypted with new keys as they must use the
|
||||
* previous keys used by the sender before their keypair changed.
|
||||
*/
|
||||
return [AsymmetricMessagePayloadType.SenderKeypairChanged]
|
||||
}
|
||||
|
||||
async execute(params: {
|
||||
keys: {
|
||||
encryption: PkcKeyPair
|
||||
@@ -37,10 +48,27 @@ export class ResendAllMessages implements UseCaseInterface<void> {
|
||||
continue
|
||||
}
|
||||
|
||||
const decryptionResult = this.decryptOwnMessage.execute({
|
||||
message: message.encrypted_message,
|
||||
privateKey: params.previousKeys?.encryption.privateKey ?? params.keys.encryption.privateKey,
|
||||
recipientPublicKey: recipient.getValue().publicKeySet.encryption,
|
||||
})
|
||||
|
||||
if (decryptionResult.isFailed()) {
|
||||
errors.push(`Failed to decrypt message ${message.uuid}`)
|
||||
continue
|
||||
}
|
||||
|
||||
const decryptedMessage = decryptionResult.getValue()
|
||||
if (this.messagesToExcludeFromResending().includes(decryptedMessage.type)) {
|
||||
continue
|
||||
}
|
||||
|
||||
await this.resendMessage.execute({
|
||||
keys: params.keys,
|
||||
previousKeys: params.previousKeys,
|
||||
message: message,
|
||||
decryptedMessage: decryptedMessage,
|
||||
rawMessage: message,
|
||||
recipient: recipient.getValue(),
|
||||
})
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { DecryptOwnMessage } from '../../Encryption/UseCase/Asymmetric/DecryptOwnMessage'
|
||||
import { AsymmetricMessagePayload, TrustedContactInterface } from '@standardnotes/models'
|
||||
import { AsymmetricMessageServerHash } from '@standardnotes/responses'
|
||||
import { PkcKeyPair } from '@standardnotes/sncrypto-common'
|
||||
@@ -8,7 +7,6 @@ import { SendMessage } from './SendMessage'
|
||||
|
||||
export class ResendMessage implements UseCaseInterface<void> {
|
||||
constructor(
|
||||
private decryptOwnMessage: DecryptOwnMessage<AsymmetricMessagePayload>,
|
||||
private sendMessage: SendMessage,
|
||||
private encryptMessage: EncryptMessage,
|
||||
) {}
|
||||
@@ -23,22 +21,11 @@ export class ResendMessage implements UseCaseInterface<void> {
|
||||
signing: PkcKeyPair
|
||||
}
|
||||
recipient: TrustedContactInterface
|
||||
message: AsymmetricMessageServerHash
|
||||
rawMessage: AsymmetricMessageServerHash
|
||||
decryptedMessage: AsymmetricMessagePayload
|
||||
}): Promise<Result<AsymmetricMessageServerHash>> {
|
||||
const decryptionResult = this.decryptOwnMessage.execute({
|
||||
message: params.message.encrypted_message,
|
||||
privateKey: params.previousKeys?.encryption.privateKey ?? params.keys.encryption.privateKey,
|
||||
recipientPublicKey: params.recipient.publicKeySet.encryption,
|
||||
})
|
||||
|
||||
if (decryptionResult.isFailed()) {
|
||||
return Result.fail(decryptionResult.getError())
|
||||
}
|
||||
|
||||
const decryptedMessage = decryptionResult.getValue()
|
||||
|
||||
const encryptedMessage = this.encryptMessage.execute({
|
||||
message: decryptedMessage,
|
||||
message: params.decryptedMessage,
|
||||
keys: params.keys,
|
||||
recipientPublicKey: params.recipient.publicKeySet.encryption,
|
||||
})
|
||||
@@ -50,7 +37,7 @@ export class ResendMessage implements UseCaseInterface<void> {
|
||||
const sendMessageResult = await this.sendMessage.execute({
|
||||
recipientUuid: params.recipient.contactUuid,
|
||||
encryptedMessage: encryptedMessage.getValue(),
|
||||
replaceabilityIdentifier: params.message.replaceabilityIdentifier,
|
||||
replaceabilityIdentifier: params.rawMessage.replaceabilityIdentifier,
|
||||
})
|
||||
|
||||
return sendMessageResult
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
import { SendOwnContactChangeMessage } from './UseCase/SendOwnContactChangeMessage'
|
||||
import { DeleteContact } from './UseCase/DeleteContact'
|
||||
import { MutatorClientInterface } from './../Mutator/MutatorClientInterface'
|
||||
import { UserKeyPairChangedEventData } from './../Session/UserKeyPairChangedEventData'
|
||||
import { SessionEvent } from './../Session/SessionEvent'
|
||||
import { InternalEventInterface } from './../Internal/InternalEventInterface'
|
||||
import { InternalEventHandlerInterface } from './../Internal/InternalEventHandlerInterface'
|
||||
import { PureCryptoInterface } from '@standardnotes/sncrypto-common'
|
||||
import { SharedVaultInviteServerHash, SharedVaultUserServerHash } from '@standardnotes/responses'
|
||||
import { TrustedContactInterface, TrustedContactMutator, DecryptedItemInterface } from '@standardnotes/models'
|
||||
@@ -25,10 +20,7 @@ import { GetAllContacts } from './UseCase/GetAllContacts'
|
||||
import { EncryptionProviderInterface } from '../Encryption/EncryptionProviderInterface'
|
||||
import { Result } from '@standardnotes/domain-core'
|
||||
|
||||
export class ContactService
|
||||
extends AbstractService<ContactServiceEvent>
|
||||
implements ContactServiceInterface, InternalEventHandlerInterface
|
||||
{
|
||||
export class ContactService extends AbstractService<ContactServiceEvent> implements ContactServiceInterface {
|
||||
constructor(
|
||||
private sync: SyncServiceInterface,
|
||||
private mutator: MutatorClientInterface,
|
||||
@@ -43,48 +35,25 @@ export class ContactService
|
||||
private _createOrEditContact: CreateOrEditContact,
|
||||
private _editContact: EditContact,
|
||||
private _validateItemSigner: ValidateItemSigner,
|
||||
private _sendOwnContactChangedMessage: SendOwnContactChangeMessage,
|
||||
eventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(eventBus)
|
||||
|
||||
eventBus.addEventHandler(this, SessionEvent.UserKeyPairChanged)
|
||||
}
|
||||
|
||||
async handleEvent(event: InternalEventInterface): Promise<void> {
|
||||
if (event.type === SessionEvent.UserKeyPairChanged) {
|
||||
const data = event.payload as UserKeyPairChangedEventData
|
||||
await this.selfContactManager.updateWithNewPublicKeySet({
|
||||
encryption: data.current.encryption.publicKey,
|
||||
signing: data.current.signing.publicKey,
|
||||
})
|
||||
void this.sendOwnContactChangeEventToAllContacts(event.payload as UserKeyPairChangedEventData)
|
||||
}
|
||||
}
|
||||
|
||||
private async sendOwnContactChangeEventToAllContacts(data: UserKeyPairChangedEventData): Promise<void> {
|
||||
if (!data.previous) {
|
||||
return
|
||||
}
|
||||
|
||||
const contacts = this._getAllContacts.execute()
|
||||
if (contacts.isFailed()) {
|
||||
return
|
||||
}
|
||||
|
||||
for (const contact of contacts.getValue()) {
|
||||
if (contact.isMe) {
|
||||
continue
|
||||
}
|
||||
|
||||
await this._sendOwnContactChangedMessage.execute({
|
||||
senderOldKeyPair: data.previous.encryption,
|
||||
senderOldSigningKeyPair: data.previous.signing,
|
||||
senderNewKeyPair: data.current.encryption,
|
||||
senderNewSigningKeyPair: data.current.signing,
|
||||
contact,
|
||||
})
|
||||
}
|
||||
override deinit(): void {
|
||||
super.deinit()
|
||||
;(this.sync as unknown) = undefined
|
||||
;(this.mutator as unknown) = undefined
|
||||
;(this.session as unknown) = undefined
|
||||
;(this.crypto as unknown) = undefined
|
||||
;(this.user as unknown) = undefined
|
||||
;(this.selfContactManager as unknown) = undefined
|
||||
;(this.encryption as unknown) = undefined
|
||||
;(this._findContact as unknown) = undefined
|
||||
;(this._getAllContacts as unknown) = undefined
|
||||
;(this._createOrEditContact as unknown) = undefined
|
||||
;(this._editContact as unknown) = undefined
|
||||
;(this._validateItemSigner as unknown) = undefined
|
||||
}
|
||||
|
||||
getSelfContact(): TrustedContactInterface | undefined {
|
||||
@@ -183,6 +152,8 @@ export class ContactService
|
||||
): Promise<TrustedContactInterface> {
|
||||
const updatedContact = await this._editContact.execute(contact, params)
|
||||
|
||||
void this.sync.sync()
|
||||
|
||||
return updatedContact
|
||||
}
|
||||
|
||||
@@ -194,6 +165,9 @@ export class ContactService
|
||||
isMe?: boolean
|
||||
}): Promise<TrustedContactInterface | undefined> {
|
||||
const contact = await this._createOrEditContact.execute(params)
|
||||
|
||||
void this.sync.sync()
|
||||
|
||||
return contact
|
||||
}
|
||||
|
||||
@@ -233,20 +207,4 @@ export class ContactService
|
||||
getItemSignatureStatus(item: DecryptedItemInterface): ItemSignatureValidationResult {
|
||||
return this._validateItemSigner.execute(item)
|
||||
}
|
||||
|
||||
override deinit(): void {
|
||||
super.deinit()
|
||||
;(this.sync as unknown) = undefined
|
||||
;(this.mutator as unknown) = undefined
|
||||
;(this.session as unknown) = undefined
|
||||
;(this.crypto as unknown) = undefined
|
||||
;(this.user as unknown) = undefined
|
||||
;(this.selfContactManager as unknown) = undefined
|
||||
;(this.encryption as unknown) = undefined
|
||||
;(this._findContact as unknown) = undefined
|
||||
;(this._getAllContacts as unknown) = undefined
|
||||
;(this._createOrEditContact as unknown) = undefined
|
||||
;(this._editContact as unknown) = undefined
|
||||
;(this._validateItemSigner as unknown) = undefined
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,7 @@ import {
|
||||
TrustedContactContent,
|
||||
TrustedContactContentSpecialized,
|
||||
TrustedContactInterface,
|
||||
PortablePublicKeySet,
|
||||
} from '@standardnotes/models'
|
||||
import { CreateOrEditContact } from './UseCase/CreateOrEditContact'
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
|
||||
const SelfContactName = 'Me'
|
||||
@@ -35,7 +33,6 @@ export class SelfContactManager implements InternalEventHandlerInterface {
|
||||
items: ItemManagerInterface,
|
||||
private session: SessionsClientInterface,
|
||||
private singletons: SingletonManagerInterface,
|
||||
private createOrEditContact: CreateOrEditContact,
|
||||
) {
|
||||
this.eventDisposers.push(
|
||||
sync.addEventObserver((event) => {
|
||||
@@ -82,23 +79,6 @@ export class SelfContactManager implements InternalEventHandlerInterface {
|
||||
)
|
||||
}
|
||||
|
||||
public async updateWithNewPublicKeySet(publicKeySet: PortablePublicKeySet) {
|
||||
if (!InternalFeatureService.get().isFeatureEnabled(InternalFeature.Vaults)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!this.selfContact) {
|
||||
return
|
||||
}
|
||||
|
||||
await this.createOrEditContact.execute({
|
||||
name: SelfContactName,
|
||||
contactUuid: this.selfContact.contactUuid,
|
||||
publicKey: publicKeySet.encryption,
|
||||
signingPublicKey: publicKeySet.signing,
|
||||
})
|
||||
}
|
||||
|
||||
private async reloadSelfContactAndCreateIfNecessary() {
|
||||
if (!InternalFeatureService.get().isFeatureEnabled(InternalFeature.Vaults)) {
|
||||
return
|
||||
@@ -146,6 +126,5 @@ export class SelfContactManager implements InternalEventHandlerInterface {
|
||||
this.eventDisposers.forEach((disposer) => disposer())
|
||||
;(this.session as unknown) = undefined
|
||||
;(this.singletons as unknown) = undefined
|
||||
;(this.createOrEditContact as unknown) = undefined
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { SyncServiceInterface } from '../../Sync/SyncServiceInterface'
|
||||
import { MutatorClientInterface } from '../../Mutator/MutatorClientInterface'
|
||||
import {
|
||||
ContactPublicKeySet,
|
||||
@@ -15,7 +14,6 @@ import { ContentType } from '@standardnotes/domain-core'
|
||||
export class CreateOrEditContact {
|
||||
constructor(
|
||||
private mutator: MutatorClientInterface,
|
||||
private sync: SyncServiceInterface,
|
||||
private findContact: FindContact,
|
||||
private editContact: EditContact,
|
||||
) {}
|
||||
@@ -54,8 +52,6 @@ export class CreateOrEditContact {
|
||||
true,
|
||||
)
|
||||
|
||||
await this.sync.sync()
|
||||
|
||||
return contact
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import { SyncServiceInterface } from '../../Sync/SyncServiceInterface'
|
||||
import { MutatorClientInterface } from '../../Mutator/MutatorClientInterface'
|
||||
import { TrustedContactInterface, TrustedContactMutator } from '@standardnotes/models'
|
||||
|
||||
export class EditContact {
|
||||
constructor(
|
||||
private mutator: MutatorClientInterface,
|
||||
private sync: SyncServiceInterface,
|
||||
) {}
|
||||
constructor(private mutator: MutatorClientInterface) {}
|
||||
|
||||
async execute(
|
||||
contact: TrustedContactInterface,
|
||||
@@ -28,8 +24,6 @@ export class EditContact {
|
||||
},
|
||||
)
|
||||
|
||||
await this.sync.sync()
|
||||
|
||||
return updatedContact
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,16 +8,20 @@ export class FindContact implements SyncUseCaseInterface<TrustedContactInterface
|
||||
|
||||
execute(query: FindContactQuery): Result<TrustedContactInterface> {
|
||||
if ('userUuid' in query && query.userUuid) {
|
||||
const contact = this.items.itemsMatchingPredicate<TrustedContactInterface>(
|
||||
const contacts = this.items.itemsMatchingPredicate<TrustedContactInterface>(
|
||||
ContentType.TYPES.TrustedContact,
|
||||
new Predicate<TrustedContactInterface>('contactUuid', '=', query.userUuid),
|
||||
)[0]
|
||||
)
|
||||
|
||||
if (contact) {
|
||||
return Result.ok(contact)
|
||||
} else {
|
||||
return Result.fail('Contact not found')
|
||||
if (contacts.length === 0) {
|
||||
return Result.fail(`Contact not found for user ${query.userUuid}`)
|
||||
}
|
||||
|
||||
if (contacts.length > 1) {
|
||||
return Result.fail(`Multiple contacts found for user ${query.userUuid}`)
|
||||
}
|
||||
|
||||
return Result.ok(contacts[0])
|
||||
}
|
||||
|
||||
if ('signingPublicKey' in query && query.signingPublicKey) {
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
import { HandleKeyPairChange } from './HandleKeyPairChange'
|
||||
import { Result } from '@standardnotes/domain-core'
|
||||
import { PkcKeyPair } from '@standardnotes/sncrypto-common'
|
||||
import { LoggerInterface } from '@standardnotes/utils'
|
||||
|
||||
describe('HandleKeyPairChange', () => {
|
||||
let useCase: HandleKeyPairChange
|
||||
let mockSelfContactManager: any
|
||||
let mockInvitesServer: any
|
||||
let mockMessageServer: any
|
||||
let mockReuploadAllInvites: any
|
||||
let mockResendAllMessages: any
|
||||
let mockGetAllContacts: any
|
||||
let mockCreateOrEditContact: any
|
||||
let mockSendOwnContactChangedMessage: any
|
||||
let logger: LoggerInterface
|
||||
|
||||
const dto = {
|
||||
newKeys: {
|
||||
encryption: <PkcKeyPair>{
|
||||
publicKey: 'new-encryption-public-key',
|
||||
privateKey: 'new-encryption-private-key',
|
||||
},
|
||||
signing: <PkcKeyPair>{
|
||||
publicKey: 'new-signing-public-key',
|
||||
privateKey: 'new-signing-private-key',
|
||||
},
|
||||
},
|
||||
previousKeys: {
|
||||
encryption: <PkcKeyPair>{
|
||||
publicKey: 'previous-encryption-public-key',
|
||||
privateKey: 'previous-encryption-private-key',
|
||||
},
|
||||
signing: <PkcKeyPair>{
|
||||
publicKey: 'previous-signing-public-key',
|
||||
privateKey: 'previous-signing-private-key',
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
mockSelfContactManager = {
|
||||
updateWithNewPublicKeySet: jest.fn().mockReturnValue({}),
|
||||
}
|
||||
|
||||
mockInvitesServer = {
|
||||
deleteAllInboundInvites: jest.fn().mockReturnValue({}),
|
||||
}
|
||||
|
||||
mockMessageServer = {
|
||||
deleteAllInboundMessages: jest.fn().mockReturnValue({}),
|
||||
}
|
||||
|
||||
mockReuploadAllInvites = {
|
||||
execute: jest.fn().mockReturnValue(Result.ok()),
|
||||
}
|
||||
|
||||
mockResendAllMessages = {
|
||||
execute: jest.fn().mockReturnValue(Result.ok()),
|
||||
}
|
||||
|
||||
mockGetAllContacts = {
|
||||
execute: jest.fn().mockReturnValue(Result.ok()),
|
||||
}
|
||||
|
||||
mockSendOwnContactChangedMessage = {
|
||||
execute: jest.fn().mockReturnValue(Result.ok()),
|
||||
}
|
||||
|
||||
mockCreateOrEditContact = {
|
||||
execute: jest.fn().mockReturnValue(Result.ok()),
|
||||
}
|
||||
|
||||
logger = {} as jest.Mocked<LoggerInterface>
|
||||
logger.error = jest.fn()
|
||||
|
||||
useCase = new HandleKeyPairChange(
|
||||
mockSelfContactManager,
|
||||
mockInvitesServer,
|
||||
mockMessageServer,
|
||||
mockReuploadAllInvites,
|
||||
mockResendAllMessages,
|
||||
mockGetAllContacts,
|
||||
mockSendOwnContactChangedMessage,
|
||||
mockCreateOrEditContact,
|
||||
logger,
|
||||
)
|
||||
})
|
||||
|
||||
it('should handle key pair change correctly', async () => {
|
||||
mockGetAllContacts.execute.mockReturnValue(Result.ok([]))
|
||||
|
||||
const result = await useCase.execute(dto)
|
||||
|
||||
expect(mockReuploadAllInvites.execute).toBeCalledWith({ keys: dto.newKeys, previousKeys: dto.previousKeys })
|
||||
expect(mockResendAllMessages.execute).toBeCalledWith({ keys: dto.newKeys, previousKeys: dto.previousKeys })
|
||||
expect(mockSendOwnContactChangedMessage.execute).not.toBeCalled()
|
||||
expect(mockMessageServer.deleteAllInboundMessages).toBeCalled()
|
||||
expect(mockInvitesServer.deleteAllInboundInvites).toBeCalled()
|
||||
|
||||
expect(result.isFailed()).toBe(false)
|
||||
})
|
||||
|
||||
it('should handle sending contact change event to all contacts', async () => {
|
||||
const contact = { isMe: false }
|
||||
mockGetAllContacts.execute.mockReturnValue(Result.ok([contact]))
|
||||
|
||||
await useCase.execute(dto)
|
||||
|
||||
expect(mockSendOwnContactChangedMessage.execute).toBeCalledWith({
|
||||
senderOldKeyPair: dto.previousKeys.encryption,
|
||||
senderOldSigningKeyPair: dto.previousKeys.signing,
|
||||
senderNewKeyPair: dto.newKeys.encryption,
|
||||
senderNewSigningKeyPair: dto.newKeys.signing,
|
||||
contact,
|
||||
})
|
||||
})
|
||||
|
||||
it('should not send contact change event if previous keys are missing', async () => {
|
||||
const contact = { isMe: false }
|
||||
mockGetAllContacts.execute.mockReturnValue(Result.ok([contact]))
|
||||
|
||||
await useCase.execute({ newKeys: dto.newKeys })
|
||||
|
||||
expect(mockSendOwnContactChangedMessage.execute).not.toBeCalled()
|
||||
})
|
||||
|
||||
it('should not send contact change event if getAllContacts fails', async () => {
|
||||
mockGetAllContacts.execute.mockReturnValue(Result.fail('Some error'))
|
||||
|
||||
await useCase.execute(dto)
|
||||
|
||||
expect(mockSendOwnContactChangedMessage.execute).not.toBeCalled()
|
||||
})
|
||||
|
||||
it('should not send contact change event for self contact', async () => {
|
||||
const contact = { isMe: true }
|
||||
mockGetAllContacts.execute.mockReturnValue(Result.ok([contact]))
|
||||
|
||||
await useCase.execute(dto)
|
||||
|
||||
expect(mockSendOwnContactChangedMessage.execute).not.toBeCalled()
|
||||
})
|
||||
|
||||
it('should reupload invites and resend messages before sending contact change message', async () => {
|
||||
const contact = { isMe: false }
|
||||
mockGetAllContacts.execute.mockReturnValue(Result.ok([contact]))
|
||||
|
||||
await useCase.execute(dto)
|
||||
|
||||
const callOrder = [
|
||||
mockReuploadAllInvites.execute,
|
||||
mockResendAllMessages.execute,
|
||||
mockSendOwnContactChangedMessage.execute,
|
||||
].map((fn) => fn.mock.invocationCallOrder[0])
|
||||
|
||||
for (let i = 0; i < callOrder.length - 1; i++) {
|
||||
expect(callOrder[i]).toBeLessThan(callOrder[i + 1])
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -1,34 +1,121 @@
|
||||
import { InternalFeatureService } from './../../InternalFeatures/InternalFeatureService'
|
||||
import { Result, UseCaseInterface } from '@standardnotes/domain-core'
|
||||
import { PkcKeyPair } from '@standardnotes/sncrypto-common'
|
||||
import { ReuploadAllInvites } from '../../VaultInvite/UseCase/ReuploadAllInvites'
|
||||
import { ResendAllMessages } from '../../AsymmetricMessage/UseCase/ResendAllMessages'
|
||||
import { SelfContactManager } from '../SelfContactManager'
|
||||
import { GetAllContacts } from './GetAllContacts'
|
||||
import { SendOwnContactChangeMessage } from './SendOwnContactChangeMessage'
|
||||
import { AsymmetricMessageServer, SharedVaultInvitesServer } from '@standardnotes/api'
|
||||
import { PortablePublicKeySet } from '@standardnotes/models'
|
||||
import { InternalFeature } from '../../InternalFeatures/InternalFeature'
|
||||
import { CreateOrEditContact } from './CreateOrEditContact'
|
||||
import { isErrorResponse } from '@standardnotes/responses'
|
||||
import { LoggerInterface } from '@standardnotes/utils'
|
||||
|
||||
type Dto = {
|
||||
newKeys: {
|
||||
encryption: PkcKeyPair
|
||||
signing: PkcKeyPair
|
||||
}
|
||||
previousKeys?: {
|
||||
encryption: PkcKeyPair
|
||||
signing: PkcKeyPair
|
||||
}
|
||||
}
|
||||
|
||||
export class HandleKeyPairChange implements UseCaseInterface<void> {
|
||||
constructor(
|
||||
private reuploadAllInvites: ReuploadAllInvites,
|
||||
private resendAllMessages: ResendAllMessages,
|
||||
private selfContactManager: SelfContactManager,
|
||||
private invitesServer: SharedVaultInvitesServer,
|
||||
private messageServer: AsymmetricMessageServer,
|
||||
private _reuploadAllInvites: ReuploadAllInvites,
|
||||
private _resendAllMessages: ResendAllMessages,
|
||||
private _getAllContacts: GetAllContacts,
|
||||
private _sendOwnContactChangedMessage: SendOwnContactChangeMessage,
|
||||
private _createOrEditContact: CreateOrEditContact,
|
||||
private logger: LoggerInterface,
|
||||
) {}
|
||||
|
||||
async execute(dto: {
|
||||
newKeys: {
|
||||
encryption: PkcKeyPair
|
||||
signing: PkcKeyPair
|
||||
}
|
||||
previousKeys?: {
|
||||
encryption: PkcKeyPair
|
||||
signing: PkcKeyPair
|
||||
}
|
||||
}): Promise<Result<void>> {
|
||||
await this.reuploadAllInvites.execute({
|
||||
keys: dto.newKeys,
|
||||
previousKeys: dto.previousKeys,
|
||||
async execute(dto: Dto): Promise<Result<void>> {
|
||||
await this.updateSelfContact({
|
||||
encryption: dto.newKeys.encryption.publicKey,
|
||||
signing: dto.newKeys.signing.publicKey,
|
||||
})
|
||||
|
||||
await this.resendAllMessages.execute({
|
||||
keys: dto.newKeys,
|
||||
previousKeys: dto.previousKeys,
|
||||
})
|
||||
const results = await Promise.all([
|
||||
this._reuploadAllInvites.execute({
|
||||
keys: dto.newKeys,
|
||||
previousKeys: dto.previousKeys,
|
||||
}),
|
||||
|
||||
this._resendAllMessages.execute({
|
||||
keys: dto.newKeys,
|
||||
previousKeys: dto.previousKeys,
|
||||
}),
|
||||
])
|
||||
|
||||
for (const result of results) {
|
||||
if (result.isFailed()) {
|
||||
this.logger.error(result.getError())
|
||||
}
|
||||
}
|
||||
|
||||
await this.sendOwnContactChangeEventToAllContacts(dto)
|
||||
|
||||
const deleteResponses = await Promise.all([
|
||||
this.messageServer.deleteAllInboundMessages(),
|
||||
this.invitesServer.deleteAllInboundInvites(),
|
||||
])
|
||||
|
||||
for (const response of deleteResponses) {
|
||||
if (isErrorResponse(response)) {
|
||||
this.logger.error(JSON.stringify(response))
|
||||
}
|
||||
}
|
||||
|
||||
return Result.ok()
|
||||
}
|
||||
|
||||
private async updateSelfContact(publicKeySet: PortablePublicKeySet) {
|
||||
if (!InternalFeatureService.get().isFeatureEnabled(InternalFeature.Vaults)) {
|
||||
return
|
||||
}
|
||||
|
||||
const selfContact = this.selfContactManager.selfContact
|
||||
if (!selfContact) {
|
||||
return
|
||||
}
|
||||
|
||||
await this._createOrEditContact.execute({
|
||||
contactUuid: selfContact.contactUuid,
|
||||
publicKey: publicKeySet.encryption,
|
||||
signingPublicKey: publicKeySet.signing,
|
||||
})
|
||||
}
|
||||
|
||||
private async sendOwnContactChangeEventToAllContacts(data: Dto): Promise<void> {
|
||||
if (!data.previousKeys) {
|
||||
return
|
||||
}
|
||||
|
||||
const contacts = this._getAllContacts.execute()
|
||||
if (contacts.isFailed()) {
|
||||
return
|
||||
}
|
||||
|
||||
for (const contact of contacts.getValue()) {
|
||||
if (contact.isMe) {
|
||||
continue
|
||||
}
|
||||
|
||||
await this._sendOwnContactChangedMessage.execute({
|
||||
senderOldKeyPair: data.previousKeys.encryption,
|
||||
senderOldSigningKeyPair: data.previousKeys.signing,
|
||||
senderNewKeyPair: data.newKeys.encryption,
|
||||
senderNewSigningKeyPair: data.newKeys.signing,
|
||||
contact,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,12 +36,15 @@ import {
|
||||
RootKeyParamsInterface,
|
||||
} from '@standardnotes/models'
|
||||
import { ClientDisplayableError } from '@standardnotes/responses'
|
||||
import { extendArray } from '@standardnotes/utils'
|
||||
import { extendArray, LoggerInterface } from '@standardnotes/utils'
|
||||
import { EncryptionService } from '../EncryptionService'
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
|
||||
export class DecryptBackupFile {
|
||||
constructor(private encryption: EncryptionService) {}
|
||||
constructor(
|
||||
private encryption: EncryptionService,
|
||||
private logger: LoggerInterface,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
file: BackupFile,
|
||||
@@ -273,7 +276,7 @@ export class DecryptBackupFile {
|
||||
errorDecrypting: true,
|
||||
}),
|
||||
)
|
||||
console.error('Error decrypting payload', encryptedPayload, e)
|
||||
this.logger.error('Error decrypting payload', encryptedPayload, e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import {
|
||||
AsymmetricMessageServerHash,
|
||||
SharedVaultInviteServerHash,
|
||||
SharedVaultServerHash,
|
||||
UserEventServerHash,
|
||||
NotificationServerHash,
|
||||
} from '@standardnotes/responses'
|
||||
|
||||
/* istanbul ignore file */
|
||||
@@ -31,11 +31,11 @@ export enum SyncEvent {
|
||||
SyncRequestsIntegrityCheck = 'sync:requests-integrity-check',
|
||||
ReceivedRemoteSharedVaults = 'received-shared-vaults',
|
||||
ReceivedSharedVaultInvites = 'received-shared-vault-invites',
|
||||
ReceivedUserEvents = 'received-user-events',
|
||||
ReceivedNotifications = 'received-user-events',
|
||||
ReceivedAsymmetricMessages = 'received-asymmetric-messages',
|
||||
}
|
||||
|
||||
export type SyncEventReceivedRemoteSharedVaultsData = SharedVaultServerHash[]
|
||||
export type SyncEventReceivedSharedVaultInvitesData = SharedVaultInviteServerHash[]
|
||||
export type SyncEventReceivedAsymmetricMessagesData = AsymmetricMessageServerHash[]
|
||||
export type SyncEventReceivedUserEventsData = UserEventServerHash[]
|
||||
export type SyncEventReceivedNotificationsData = NotificationServerHash[]
|
||||
|
||||
@@ -11,6 +11,7 @@ import { SyncServiceInterface } from '../Sync/SyncServiceInterface'
|
||||
import { FileService } from './FileService'
|
||||
import { BackupServiceInterface } from '@standardnotes/files'
|
||||
import { HttpServiceInterface } from '@standardnotes/api'
|
||||
import { LoggerInterface } from '@standardnotes/utils'
|
||||
|
||||
describe('fileService', () => {
|
||||
let apiService: LegacyApiServiceInterface
|
||||
@@ -26,6 +27,8 @@ describe('fileService', () => {
|
||||
let backupService: BackupServiceInterface
|
||||
let http: HttpServiceInterface
|
||||
|
||||
let logger: LoggerInterface
|
||||
|
||||
beforeEach(() => {
|
||||
apiService = {} as jest.Mocked<LegacyApiServiceInterface>
|
||||
apiService.addEventObserver = jest.fn()
|
||||
@@ -82,6 +85,9 @@ describe('fileService', () => {
|
||||
backupService.readEncryptedFileFromBackup = jest.fn()
|
||||
backupService.getFileBackupInfo = jest.fn()
|
||||
|
||||
logger = {} as jest.Mocked<LoggerInterface>
|
||||
logger.info = jest.fn()
|
||||
|
||||
http = {} as jest.Mocked<HttpServiceInterface>
|
||||
|
||||
fileService = new FileService(
|
||||
@@ -94,6 +100,7 @@ describe('fileService', () => {
|
||||
alertService,
|
||||
crypto,
|
||||
internalEventBus,
|
||||
logger,
|
||||
backupService,
|
||||
)
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
SharedVaultListingInterface,
|
||||
} from '@standardnotes/models'
|
||||
import { PureCryptoInterface } from '@standardnotes/sncrypto-common'
|
||||
import { spaceSeparatedStrings, UuidGenerator } from '@standardnotes/utils'
|
||||
import { LoggerInterface, spaceSeparatedStrings, UuidGenerator } from '@standardnotes/utils'
|
||||
import { SNItemsKey } from '@standardnotes/encryption'
|
||||
import {
|
||||
DownloadAndDecryptFileOperation,
|
||||
@@ -47,7 +47,6 @@ import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
import { SyncServiceInterface } from '../Sync/SyncServiceInterface'
|
||||
import { DecryptItemsKeyWithUserFallback } from '../Encryption/Functions'
|
||||
import { log, LoggingDomain } from '../Logging'
|
||||
import { SharedVaultServer, SharedVaultServerInterface, HttpServiceInterface } from '@standardnotes/api'
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
import { EncryptionProviderInterface } from '../Encryption/EncryptionProviderInterface'
|
||||
@@ -68,6 +67,7 @@ export class FileService extends AbstractService implements FilesClientInterface
|
||||
private alertService: AlertService,
|
||||
private crypto: PureCryptoInterface,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
private logger: LoggerInterface,
|
||||
private backupsService?: BackupServiceInterface,
|
||||
) {
|
||||
super(internalEventBus)
|
||||
@@ -317,19 +317,19 @@ export class FileService extends AbstractService implements FilesClientInterface
|
||||
const fileBackup = await this.backupsService?.getFileBackupInfo(file)
|
||||
|
||||
if (this.backupsService && fileBackup) {
|
||||
log(LoggingDomain.FilesService, 'Downloading file from backup', fileBackup)
|
||||
this.logger.info('Downloading file from backup', fileBackup)
|
||||
|
||||
await readAndDecryptBackupFileUsingBackupService(file, this.backupsService, this.crypto, async (chunk) => {
|
||||
log(LoggingDomain.FilesService, 'Got local file chunk', chunk.progress)
|
||||
this.logger.info('Got local file chunk', chunk.progress)
|
||||
|
||||
return onDecryptedBytes(chunk.data, chunk.progress)
|
||||
})
|
||||
|
||||
log(LoggingDomain.FilesService, 'Finished downloading file from backup')
|
||||
this.logger.info('Finished downloading file from backup')
|
||||
|
||||
return undefined
|
||||
} else {
|
||||
log(LoggingDomain.FilesService, 'Downloading file from network')
|
||||
this.logger.info('Downloading file from network')
|
||||
|
||||
const addToCache = file.encryptedSize < this.encryptedCache.maxSize
|
||||
|
||||
|
||||
@@ -8,14 +8,16 @@ import { IntegrityApiInterface } from './IntegrityApiInterface'
|
||||
import { IntegrityService } from './IntegrityService'
|
||||
import { PayloadManagerInterface } from '../Payloads/PayloadManagerInterface'
|
||||
import { IntegrityPayload } from '@standardnotes/responses'
|
||||
import { LoggerInterface } from '@standardnotes/utils'
|
||||
|
||||
describe('IntegrityService', () => {
|
||||
let integrityApi: IntegrityApiInterface
|
||||
let itemApi: ItemsServerInterface
|
||||
let payloadManager: PayloadManagerInterface
|
||||
let logger: LoggerInterface
|
||||
let internalEventBus: InternalEventBusInterface
|
||||
|
||||
const createService = () => new IntegrityService(integrityApi, itemApi, payloadManager, internalEventBus)
|
||||
const createService = () => new IntegrityService(integrityApi, itemApi, payloadManager, logger, internalEventBus)
|
||||
|
||||
beforeEach(() => {
|
||||
integrityApi = {} as jest.Mocked<IntegrityApiInterface>
|
||||
@@ -29,6 +31,10 @@ describe('IntegrityService', () => {
|
||||
|
||||
internalEventBus = {} as jest.Mocked<InternalEventBusInterface>
|
||||
internalEventBus.publishSync = jest.fn()
|
||||
|
||||
logger = {} as jest.Mocked<LoggerInterface>
|
||||
logger.info = jest.fn()
|
||||
logger.error = jest.fn()
|
||||
})
|
||||
|
||||
it('should check integrity of payloads and publish mismatches', async () => {
|
||||
@@ -63,7 +69,7 @@ describe('IntegrityService', () => {
|
||||
uuid: '1-2-3',
|
||||
},
|
||||
],
|
||||
source: "AfterDownloadFirst",
|
||||
source: 'AfterDownloadFirst',
|
||||
},
|
||||
type: 'IntegrityCheckCompleted',
|
||||
},
|
||||
@@ -90,7 +96,7 @@ describe('IntegrityService', () => {
|
||||
{
|
||||
payload: {
|
||||
rawPayloads: [],
|
||||
source: "AfterDownloadFirst",
|
||||
source: 'AfterDownloadFirst',
|
||||
},
|
||||
type: 'IntegrityCheckCompleted',
|
||||
},
|
||||
@@ -140,7 +146,7 @@ describe('IntegrityService', () => {
|
||||
{
|
||||
payload: {
|
||||
rawPayloads: [],
|
||||
source: "AfterDownloadFirst",
|
||||
source: 'AfterDownloadFirst',
|
||||
},
|
||||
type: 'IntegrityCheckCompleted',
|
||||
},
|
||||
|
||||
@@ -10,6 +10,7 @@ import { SyncEvent } from '../Event/SyncEvent'
|
||||
import { IntegrityEventPayload } from './IntegrityEventPayload'
|
||||
import { SyncSource } from '../Sync/SyncSource'
|
||||
import { PayloadManagerInterface } from '../Payloads/PayloadManagerInterface'
|
||||
import { LoggerInterface } from '@standardnotes/utils'
|
||||
|
||||
export class IntegrityService
|
||||
extends AbstractService<IntegrityEvent, IntegrityEventPayload>
|
||||
@@ -19,6 +20,7 @@ export class IntegrityService
|
||||
private integrityApi: IntegrityApiInterface,
|
||||
private itemApi: ItemsServerInterface,
|
||||
private payloadManager: PayloadManagerInterface,
|
||||
private logger: LoggerInterface,
|
||||
protected override internalEventBus: InternalEventBusInterface,
|
||||
) {
|
||||
super(internalEventBus)
|
||||
@@ -31,7 +33,7 @@ export class IntegrityService
|
||||
|
||||
const integrityCheckResponse = await this.integrityApi.checkIntegrity(this.payloadManager.integrityPayloads)
|
||||
if (isErrorResponse(integrityCheckResponse)) {
|
||||
this.log(`Could not obtain integrity check: ${integrityCheckResponse.data.error?.message}`)
|
||||
this.logger.error(`Could not obtain integrity check: ${integrityCheckResponse.data.error?.message}`)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -50,7 +52,7 @@ export class IntegrityService
|
||||
isErrorResponse(serverItemResponse) ||
|
||||
!('item' in serverItemResponse.data)
|
||||
) {
|
||||
this.log(
|
||||
this.logger.error(
|
||||
`Could not obtain item for integrity adjustments: ${
|
||||
isErrorResponse(serverItemResponse) ? serverItemResponse.data.error?.message : ''
|
||||
}`,
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
import { logWithColor } from '@standardnotes/utils'
|
||||
|
||||
declare const process: {
|
||||
env: {
|
||||
NODE_ENV: string | null | undefined
|
||||
}
|
||||
}
|
||||
|
||||
export const isDev = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'
|
||||
|
||||
export enum LoggingDomain {
|
||||
FilesService,
|
||||
FilesBackups,
|
||||
}
|
||||
|
||||
const LoggingStatus: Record<LoggingDomain, boolean> = {
|
||||
[LoggingDomain.FilesService]: false,
|
||||
[LoggingDomain.FilesBackups]: false,
|
||||
}
|
||||
|
||||
const LoggingColor: Record<LoggingDomain, string> = {
|
||||
[LoggingDomain.FilesService]: 'blue',
|
||||
[LoggingDomain.FilesBackups]: 'yellow',
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function log(domain: LoggingDomain, ...args: any[]): void {
|
||||
if (!isDev || !LoggingStatus[domain]) {
|
||||
return
|
||||
}
|
||||
|
||||
logWithColor(LoggingDomain[domain], LoggingColor[domain], ...args)
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/* istanbul ignore file */
|
||||
|
||||
import { log, removeFromArray } from '@standardnotes/utils'
|
||||
import { removeFromArray } from '@standardnotes/utils'
|
||||
import { EventObserver } from '../Event/EventObserver'
|
||||
import { ApplicationServiceInterface } from './ApplicationServiceInterface'
|
||||
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||
@@ -99,11 +99,4 @@ export abstract class AbstractService<EventName = string, EventData = unknown>
|
||||
isApplicationService(): true {
|
||||
return true
|
||||
}
|
||||
|
||||
log(..._args: unknown[]): void {
|
||||
if (this.loggingEnabled) {
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
log(this.getServiceName(), ...arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,5 +6,4 @@ export interface ApplicationServiceInterface<E, D> extends ServiceDiagnostics {
|
||||
addEventObserver(observer: EventObserver<E, D>): () => void
|
||||
blockDeinit(): Promise<void>
|
||||
deinit(): void
|
||||
log(message: string, ...args: unknown[]): void
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface
|
||||
import { SyncEvent } from '../Event/SyncEvent'
|
||||
import { SessionEvent } from '../Session/SessionEvent'
|
||||
import { InternalEventInterface } from '../Internal/InternalEventInterface'
|
||||
import { UserEventServiceEvent, UserEventServiceEventPayload } from '../UserEvent/UserEventServiceEvent'
|
||||
import { NotificationServiceEvent, NotificationServiceEventPayload } from '../UserEvent/NotificationServiceEvent'
|
||||
import { DeleteThirdPartyVault } from './UseCase/DeleteExternalSharedVault'
|
||||
import { DeleteSharedVault } from './UseCase/DeleteSharedVault'
|
||||
import { VaultServiceEvent, VaultServiceEventPayload } from '../Vault/VaultServiceEvent'
|
||||
@@ -106,8 +106,8 @@ export class SharedVaultService
|
||||
})
|
||||
break
|
||||
}
|
||||
case UserEventServiceEvent.UserEventReceived:
|
||||
await this.handleUserEvent(event.payload as UserEventServiceEventPayload)
|
||||
case NotificationServiceEvent.NotificationReceived:
|
||||
await this.handleUserEvent(event.payload as NotificationServiceEventPayload)
|
||||
break
|
||||
case VaultServiceEvent.VaultRootKeyRotated: {
|
||||
const payload = event.payload as VaultServiceEventPayload[VaultServiceEvent.VaultRootKeyRotated]
|
||||
@@ -120,7 +120,7 @@ export class SharedVaultService
|
||||
}
|
||||
}
|
||||
|
||||
private async handleUserEvent(event: UserEventServiceEventPayload): Promise<void> {
|
||||
private async handleUserEvent(event: NotificationServiceEventPayload): Promise<void> {
|
||||
switch (event.eventPayload.props.type.value) {
|
||||
case NotificationType.TYPES.RemovedFromSharedVault: {
|
||||
const vault = this._getVault.execute<SharedVaultListingInterface>({
|
||||
|
||||
@@ -20,7 +20,7 @@ export class ConvertToSharedVault {
|
||||
|
||||
const serverResult = await this.sharedVaultServer.createSharedVault()
|
||||
if (isErrorResponse(serverResult)) {
|
||||
return ClientDisplayableError.FromString(`Failed to create shared vault ${JSON.stringify(serverResult)}`)
|
||||
return ClientDisplayableError.FromString(`Failed to convert to shared vault ${JSON.stringify(serverResult)}`)
|
||||
}
|
||||
|
||||
const serverVaultHash = serverResult.data.sharedVault
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import { NotificationServerHash } from '@standardnotes/responses'
|
||||
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'
|
||||
|
||||
export class NotificationService
|
||||
extends AbstractService<NotificationServiceEvent, NotificationServiceEventPayload>
|
||||
implements InternalEventHandlerInterface
|
||||
{
|
||||
private handledNotifications = new Set<string>()
|
||||
|
||||
constructor(internalEventBus: InternalEventBusInterface) {
|
||||
super(internalEventBus)
|
||||
|
||||
internalEventBus.addEventHandler(this, SyncEvent.ReceivedNotifications)
|
||||
}
|
||||
|
||||
async handleEvent(event: InternalEventInterface): Promise<void> {
|
||||
if (event.type === SyncEvent.ReceivedNotifications) {
|
||||
return this.handleReceivedNotifications(event.payload as SyncEventReceivedNotificationsData)
|
||||
}
|
||||
}
|
||||
|
||||
private async handleReceivedNotifications(notifications: NotificationServerHash[]): Promise<void> {
|
||||
if (notifications.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
for (const notification of notifications) {
|
||||
if (this.handledNotifications.has(notification.uuid)) {
|
||||
continue
|
||||
}
|
||||
|
||||
this.handledNotifications.add(notification.uuid)
|
||||
|
||||
const eventPayloadOrError = NotificationPayload.createFromString(notification.payload)
|
||||
if (eventPayloadOrError.isFailed()) {
|
||||
continue
|
||||
}
|
||||
|
||||
const payload: NotificationPayload = eventPayloadOrError.getValue()
|
||||
|
||||
const serviceEvent: NotificationServiceEventPayload = { eventPayload: payload }
|
||||
|
||||
await this.notifyEventSync(NotificationServiceEvent.NotificationReceived, serviceEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { NotificationPayload } from '@standardnotes/domain-core'
|
||||
|
||||
export enum NotificationServiceEvent {
|
||||
NotificationReceived = 'NotificationReceived',
|
||||
}
|
||||
|
||||
export type NotificationServiceEventPayload = {
|
||||
eventPayload: NotificationPayload
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import { UserEventServerHash } from '@standardnotes/responses'
|
||||
import { SyncEvent, SyncEventReceivedUserEventsData } from '../Event/SyncEvent'
|
||||
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||
import { InternalEventHandlerInterface } from '../Internal/InternalEventHandlerInterface'
|
||||
import { InternalEventInterface } from '../Internal/InternalEventInterface'
|
||||
import { AbstractService } from '../Service/AbstractService'
|
||||
import { UserEventServiceEventPayload, UserEventServiceEvent } from './UserEventServiceEvent'
|
||||
import { NotificationPayload } from '@standardnotes/domain-core'
|
||||
|
||||
export class UserEventService
|
||||
extends AbstractService<UserEventServiceEvent, UserEventServiceEventPayload>
|
||||
implements InternalEventHandlerInterface
|
||||
{
|
||||
constructor(internalEventBus: InternalEventBusInterface) {
|
||||
super(internalEventBus)
|
||||
|
||||
internalEventBus.addEventHandler(this, SyncEvent.ReceivedUserEvents)
|
||||
}
|
||||
|
||||
async handleEvent(event: InternalEventInterface): Promise<void> {
|
||||
if (event.type === SyncEvent.ReceivedUserEvents) {
|
||||
return this.handleReceivedUserEvents(event.payload as SyncEventReceivedUserEventsData)
|
||||
}
|
||||
}
|
||||
|
||||
private async handleReceivedUserEvents(userEvents: UserEventServerHash[]): Promise<void> {
|
||||
if (userEvents.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
for (const serverEvent of userEvents) {
|
||||
const eventPayloadOrError = NotificationPayload.createFromString(serverEvent.payload)
|
||||
if (eventPayloadOrError.isFailed()) {
|
||||
continue
|
||||
}
|
||||
const eventPayload = eventPayloadOrError.getValue()
|
||||
|
||||
const serviceEvent: UserEventServiceEventPayload = { eventPayload }
|
||||
|
||||
await this.notifyEventSync(UserEventServiceEvent.UserEventReceived, serviceEvent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import { NotificationPayload } from '@standardnotes/domain-core'
|
||||
|
||||
export enum UserEventServiceEvent {
|
||||
UserEventReceived = 'UserEventReceived',
|
||||
}
|
||||
|
||||
export type UserEventServiceEventPayload = {
|
||||
eventPayload: NotificationPayload
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { AcceptVaultInvite } from './UseCase/AcceptVaultInvite'
|
||||
import { SyncEvent, SyncEventReceivedSharedVaultInvitesData } from './../Event/SyncEvent'
|
||||
import { SessionEvent } from './../Session/SessionEvent'
|
||||
import { InternalEventInterface } from './../Internal/InternalEventInterface'
|
||||
import { InternalEventHandlerInterface } from './../Internal/InternalEventHandlerInterface'
|
||||
import { ItemManagerInterface } from './../Item/ItemManagerInterface'
|
||||
@@ -92,9 +91,6 @@ export class VaultInviteService
|
||||
|
||||
async handleEvent(event: InternalEventInterface): Promise<void> {
|
||||
switch (event.type) {
|
||||
case SessionEvent.UserKeyPairChanged:
|
||||
void this.invitesServer.deleteAllInboundInvites()
|
||||
break
|
||||
case SyncEvent.ReceivedSharedVaultInvites:
|
||||
await this.processInboundInvites(event.payload as SyncEventReceivedSharedVaultInvitesData)
|
||||
break
|
||||
@@ -238,6 +234,8 @@ export class VaultInviteService
|
||||
}
|
||||
|
||||
for (const invite of invites) {
|
||||
delete this.pendingInvites[invite.uuid]
|
||||
|
||||
const sender = this._findContact.execute({ userUuid: invite.sender_uuid })
|
||||
if (!sender.isFailed()) {
|
||||
const trustedMessage = this._getTrustedPayload.execute<AsymmetricMessageSharedVaultInvite>({
|
||||
|
||||
@@ -177,8 +177,8 @@ export * from './User/SignedOutEventPayload'
|
||||
export * from './User/UserClientInterface'
|
||||
export * from './User/UserClientInterface'
|
||||
export * from './User/UserService'
|
||||
export * from './UserEvent/UserEventService'
|
||||
export * from './UserEvent/UserEventServiceEvent'
|
||||
export * from './UserEvent/NotificationService'
|
||||
export * from './UserEvent/NotificationServiceEvent'
|
||||
export * from './VaultInvite/InviteRecord'
|
||||
export * from './VaultInvite/UseCase/AcceptVaultInvite'
|
||||
export * from './VaultInvite/UseCase/InviteToVault'
|
||||
|
||||
Reference in New Issue
Block a user