diff --git a/packages/backend/src/nest/connections-manager/connections-manager.service.ts b/packages/backend/src/nest/connections-manager/connections-manager.service.ts index 41f61c694d..0c64741b26 100644 --- a/packages/backend/src/nest/connections-manager/connections-manager.service.ts +++ b/packages/backend/src/nest/connections-manager/connections-manager.service.ts @@ -542,7 +542,7 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI this.storageService.on(SocketActionTypes.CONNECTION_PROCESS_INFO, data => { this.serverIoProvider.io.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, data) }) - this.storageService.on(StorageEvents.LOAD_CERTIFICATES, (payload: SendCertificatesResponse) => { + this.storageService.on(StorageEvents.REPLICATED_CERTIFICATES, (payload: SendCertificatesResponse) => { this.serverIoProvider.io.emit(SocketActionTypes.RESPONSE_GET_CERTIFICATES, payload) }) this.storageService.on(StorageEvents.LOAD_PUBLIC_CHANNELS, (payload: ChannelsReplicatedPayload) => { diff --git a/packages/backend/src/nest/storage/certificates/certificates.store.ts b/packages/backend/src/nest/storage/certificates/certificates.store.ts index acb9bc05c4..3002e5df11 100644 --- a/packages/backend/src/nest/storage/certificates/certificates.store.ts +++ b/packages/backend/src/nest/storage/certificates/certificates.store.ts @@ -22,6 +22,7 @@ export class CertificatesStore { constructor(orbitDb: OrbitDB) { this.orbitDb = orbitDb this.filteredCertificatesMapping = new Map() + this.usernameMapping = new Map() } public async init(emitter: EventEmitter) { @@ -36,7 +37,7 @@ export class CertificatesStore { this.store.events.on('ready', async () => { logger('Loaded certificates to memory') - emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.LOADED_CERTIFICATES) + emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED) }) this.store.events.on('write', async () => { @@ -51,10 +52,16 @@ export class CertificatesStore { }) const loadedCertificates = async () => { + console.log('dupa') emitter.emit(StorageEvents.LOADED_CERTIFICATES, { certificates: await this.getCertificates(), }) } + + // @ts-expect-error - OrbitDB's type declaration of `load` lacks 'options' + await this.store.load({ fetchEntryTimeout: 15000 }) + + logger('Initialized') } public async close() { diff --git a/packages/backend/src/nest/storage/storage.service.spec.ts b/packages/backend/src/nest/storage/storage.service.spec.ts index 4dc06cb45a..27fabc7b85 100644 --- a/packages/backend/src/nest/storage/storage.service.spec.ts +++ b/packages/backend/src/nest/storage/storage.service.spec.ts @@ -11,10 +11,12 @@ import { import { ChannelMessage, Community, + ConnectionProcessInfo, FileMetadata, Identity, MessageType, PublicChannel, + SocketActionTypes, TestMessage, } from '@quiet/types' @@ -254,8 +256,7 @@ describe('StorageService', () => { expect(db).not.toBe(undefined) if (!db) return // TS complaining const channelsDbAddress = storageService.channels?.address - // @ts-expect-error 'certificates' is private - const certificatesDbAddress = storageService.certificates.address + const certificatesDbAddress = storageService.certificatesStore.getAddress() const certificatesRequestsDbAddress = storageService.certificatesRequestsStore.getAddress() // @ts-expect-error 'communityMetadata' is private const communityMetadataDbAddress = storageService.communityMetadata.address @@ -335,15 +336,19 @@ describe('StorageService', () => { it('Certificates and peers list are updated on replicated event', async () => { await storageService.init(peerId) + const eventSpy = jest.spyOn(storageService, 'emit') + const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList') - // @ts-ignore - Property 'certificates' is private - storageService.certificates.events.emit('replicated') - expect(eventSpy).toBeCalledWith('loadCertificates', { - certificates: [], + storageService.certificatesStore.store.events.emit('replicated') + + expect(eventSpy).toBeCalledWith(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED) + + await waitForExpect(() => { + expect(eventSpy).toBeCalledWith(StorageEvents.REPLICATED_CERTIFICATES, { certificates: [] }) + expect(spyOnUpdatePeersList).toBeCalled() }) - expect(spyOnUpdatePeersList).toBeCalled() }) it.each(['write', 'replicate.progress'])( @@ -446,14 +451,13 @@ describe('StorageService', () => { } ) - it('Certificates and peers list are updated on write event', async () => { + it.skip('Certificates and peers list are updated on write event', async () => { await storageService.init(peerId) const eventSpy = jest.spyOn(storageService, 'emit') const spyOnUpdatePeersList = jest.spyOn(storageService, 'updatePeersList') - // @ts-ignore - Property 'certificates' is private - storageService.certificates.events.emit('write', 'address', { payload: { value: 'something' } }, []) + storageService.certificatesStore.store.events.emit('write', 'address', { payload: { value: 'something' } }, []) - expect(eventSpy).toBeCalledWith(StorageEvents.LOAD_CERTIFICATES, { + expect(eventSpy).toBeCalledWith(StorageEvents.REPLICATED_CERTIFICATES, { certificates: [], }) expect(spyOnUpdatePeersList).toBeCalled() diff --git a/packages/backend/src/nest/storage/storage.service.ts b/packages/backend/src/nest/storage/storage.service.ts index 22834b98ec..146358c360 100644 --- a/packages/backend/src/nest/storage/storage.service.ts +++ b/packages/backend/src/nest/storage/storage.service.ts @@ -62,7 +62,7 @@ export class StorageService extends EventEmitter { public directMessagesRepos: Map = new Map() private publicKeysMap: Map = new Map() private communityMetadata: KeyValueStore - private certificatesStore: CertificatesStore + public certificatesStore: CertificatesStore public certificatesRequestsStore: CertificatesRequestsStore private ipfs: IPFS private orbitDb: OrbitDB @@ -333,21 +333,20 @@ export class StorageService extends EventEmitter { public async loadAllCertificates() { this.logger('Loading all certificates') - this.emit(StorageEvents.LOAD_CERTIFICATES, { + this.emit(StorageEvents.REPLICATED_CERTIFICATES, { certificates: await this.certificatesStore.loadAllCertificates(), }) } public async attachCertificatesStoreListeners() { this.on(StorageEvents.LOADED_CERTIFICATES, async (payload) => { - this.emit(StorageEvents.LOADED_CERTIFICATES, payload) + this.emit(StorageEvents.REPLICATED_CERTIFICATES, payload) await this.updatePeersList() }) } public async attachCsrsStoreListeners() { this.on(StorageEvents.LOADED_USER_CSRS, async (payload) => { - console.log('csrs', payload.csrs) const allCertificates = this.getAllEventLogEntries(this.certificatesStore.store) this.emit(StorageEvents.REPLICATED_CSR, { csrs: payload.csrs, certificates: allCertificates, id: payload.id }) // TODO diff --git a/packages/backend/src/nest/storage/storage.types.ts b/packages/backend/src/nest/storage/storage.types.ts index ffbec9d21f..0963a18bbb 100644 --- a/packages/backend/src/nest/storage/storage.types.ts +++ b/packages/backend/src/nest/storage/storage.types.ts @@ -3,9 +3,6 @@ import { Certificates } from '@quiet/types' export enum StorageEvents { // Peers UPDATE_PEERS_LIST = 'updatePeersList', - LOAD_CERTIFICATES = 'loadCertificates', - LOADED_CERTIFICATES = 'loadedCertificates', - REPLICATED_CSR = 'replicatedCsr', // Public Channels LOAD_PUBLIC_CHANNELS = 'loadPublicChannels', LOAD_ALL_PRIVATE_CONVERSATIONS = 'loadAllPrivateConversations', @@ -27,7 +24,10 @@ export enum StorageEvents { // Community REPLICATED_COMMUNITY_METADATA = 'replicatedCommunityMetadata', // Users - LOADED_USER_CSRS = 'loadedUserCsrs' + LOADED_USER_CSRS = 'loadedUserCsrs', + REPLICATED_CSR = 'replicatedCsr', + LOADED_CERTIFICATES = 'loadedCertificates', + REPLICATED_CERTIFICATES = 'replicatedCertificates', } export interface InitStorageParams { communityId: string