From 877a03824bbd8721d1e62a47c5ef5127f4a86fe2 Mon Sep 17 00:00:00 2001 From: siepra Date: Thu, 16 Nov 2023 14:03:39 +0100 Subject: [PATCH] fix: lint --- .../certificates/certificates.store.ts | 239 +++++++++--------- .../storage/certificatesRequestsStore.spec.ts | 2 +- .../nest/storage/certificatesRequestsStore.ts | 32 +-- .../src/nest/storage/storage.service.spec.ts | 9 +- .../src/nest/storage/storage.service.ts | 6 +- 5 files changed, 148 insertions(+), 140 deletions(-) diff --git a/packages/backend/src/nest/storage/certificates/certificates.store.ts b/packages/backend/src/nest/storage/certificates/certificates.store.ts index 3002e5df11..108dbe28a5 100644 --- a/packages/backend/src/nest/storage/certificates/certificates.store.ts +++ b/packages/backend/src/nest/storage/certificates/certificates.store.ts @@ -4,7 +4,13 @@ import { StorageEvents } from '../storage.types' import EventStore from 'orbit-db-eventstore' import OrbitDB from 'orbit-db' -import { keyFromCertificate, CertFieldsTypes, parseCertificate, getReqFieldValue, getCertFieldValue } from '@quiet/identity' +import { + keyFromCertificate, + CertFieldsTypes, + parseCertificate, + getReqFieldValue, + getCertFieldValue, +} from '@quiet/identity' import { ConnectionProcessInfo, SocketActionTypes, UserData } from '@quiet/types' @@ -13,128 +19,125 @@ import createLogger from '../../common/logger' const logger = createLogger('CertificatesStore') export class CertificatesStore { - public orbitDb: OrbitDB - public store: EventStore - - private filteredCertificatesMapping: Map> - private usernameMapping: Map - - constructor(orbitDb: OrbitDB) { - this.orbitDb = orbitDb - this.filteredCertificatesMapping = new Map() - this.usernameMapping = new Map() + public orbitDb: OrbitDB + public store: EventStore + + private filteredCertificatesMapping: Map> + private usernameMapping: Map + + constructor(orbitDb: OrbitDB) { + this.orbitDb = orbitDb + this.filteredCertificatesMapping = new Map() + this.usernameMapping = new Map() + } + + public async init(emitter: EventEmitter) { + logger('Initializing certificates log store') + + this.store = await this.orbitDb.log('certificates', { + replicate: false, + accessController: { + write: ['*'], + }, + }) + + this.store.events.on('ready', async () => { + logger('Loaded certificates to memory') + emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED) + }) + + this.store.events.on('write', async () => { + logger('Saved certificate locally') + await loadedCertificates() + }) + + this.store.events.on('replicated', async () => { + logger('REPLICATED: Certificates') + emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED) + await loadedCertificates() + }) + + const loadedCertificates = async () => { + console.log('dupa') + emitter.emit(StorageEvents.LOADED_CERTIFICATES, { + certificates: await this.getCertificates(), + }) } - public async init(emitter: EventEmitter) { - logger('Initializing certificates log store') - - this.store = await this.orbitDb.log('certificates', { - replicate: false, - accessController: { - write: ['*'], - }, - }) - - this.store.events.on('ready', async () => { - logger('Loaded certificates to memory') - emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED) - }) - - this.store.events.on('write', async () => { - logger('Saved certificate locally') - await loadedCertificates() - }) - - this.store.events.on('replicated', async () => { - logger('REPLICATED: Certificates') - emitter.emit(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED) - await loadedCertificates() - }) - - 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() { + await this.store?.close() + } + + public getAddress() { + return this.store?.address + } + + public async addCertificate(certificate: string) { + logger('Adding user certificate') + await this.store.add(certificate) + return true + } + + public async loadAllCertificates() { + return this.getCertificates() + } + + /* + * In order to split up the work scope, we mock validating function, + * until we move the certificates section into it's own store + */ + private async validateCertificate(_certificate: string): Promise { + logger('Validating certificate') + return true + } + + /* + * Method returning store entries, filtered by validation result + * as specified in the comment section of + * https://github.com/TryQuiet/quiet/issues/1899 + */ + protected async getCertificates() { + const allCertificates = this.store + .iterator({ limit: -1 }) + .collect() + .map(e => e.payload.value) + + const validCertificates = allCertificates.map(async certificate => { + if (this.filteredCertificatesMapping.has(certificate)) { + return certificate // Only validate certificates + } + + const validation = await this.validateCertificate(certificate) + if (validation) { + const parsedCertificate = parseCertificate(certificate) + const pubkey = keyFromCertificate(parsedCertificate) + + const username = getCertFieldValue(parsedCertificate, CertFieldsTypes.nickName) + + // @ts-expect-error + this.usernameMapping.set(pubkey, username) + + const data: Partial = { + // @ts-expect-error + username: username, } - // @ts-expect-error - OrbitDB's type declaration of `load` lacks 'options' - await this.store.load({ fetchEntryTimeout: 15000 }) - - logger('Initialized') - } - - public async close() { - await this.store?.close() - } - - public getAddress() { - return this.store?.address - } + this.filteredCertificatesMapping.set(certificate, data) - public async addCertificate(certificate: string) { - logger('Adding user certificate') - await this.store.add(certificate) - return true - } + return certificate + } + }) - public async loadAllCertificates() { - return this.getCertificates() - } - - /* - * In order to split up the work scope, we mock validating function, - * until we move the certificates section into it's own store - */ - private async validateCertificate(_certificate: string): Promise { - logger('Validating certificate') - return true - } - - /* - * Method returning store entries, filtered by validation result - * as specified in the comment section of - * https://github.com/TryQuiet/quiet/issues/1899 - */ - protected async getCertificates() { - - const allCertificates = this.store - .iterator({ limit: -1 }) - .collect() - .map(e => e.payload.value) - - const validCertificates = allCertificates.map(async (certificate) => { - if (this.filteredCertificatesMapping.has(certificate)) { - return certificate // Only validate certificates - } - - const validation = await this.validateCertificate(certificate) - if (validation) { - - const parsedCertificate = parseCertificate(certificate) - const pubkey = keyFromCertificate(parsedCertificate) - - const username = getCertFieldValue(parsedCertificate, CertFieldsTypes.nickName) - - // @ts-expect-error - this.usernameMapping.set(pubkey, username) - - const data: Partial = { - // @ts-expect-error - username: username - } - - this.filteredCertificatesMapping.set(certificate, data) - - return certificate - } - }) - - return validCertificates.filter(i => Boolean(i)) // Filter out undefineds - } - - public getCertificateUsername(pubkey: string) { - return this.usernameMapping.get(pubkey) - } + return validCertificates.filter(i => Boolean(i)) // Filter out undefineds + } + public getCertificateUsername(pubkey: string) { + return this.usernameMapping.get(pubkey) + } } diff --git a/packages/backend/src/nest/storage/certificatesRequestsStore.spec.ts b/packages/backend/src/nest/storage/certificatesRequestsStore.spec.ts index bbc65149f7..596c60d34e 100644 --- a/packages/backend/src/nest/storage/certificatesRequestsStore.spec.ts +++ b/packages/backend/src/nest/storage/certificatesRequestsStore.spec.ts @@ -90,4 +90,4 @@ describe('CertificatesRequestsStore csr validation methods', () => { // expect(spyOnUpdatePeersList).toBeCalledTimes(3) // }) -// }) \ No newline at end of file +// }) diff --git a/packages/backend/src/nest/storage/certificatesRequestsStore.ts b/packages/backend/src/nest/storage/certificatesRequestsStore.ts index ae28ec9b48..9eedadcfbe 100644 --- a/packages/backend/src/nest/storage/certificatesRequestsStore.ts +++ b/packages/backend/src/nest/storage/certificatesRequestsStore.ts @@ -149,21 +149,23 @@ export class CertificatesRequestsStore { .collect() .map(e => e.payload.value) await Promise.all( - allCsrs.filter(async csr => { - // const parsedCsr = await loadCSR(csr) - const validation = await CertificatesRequestsStore.validateUserCsr(csr) - if (validation) return true - return false - }).map(async csr => { - const parsedCsr = await loadCSR(csr) - const pubKey = keyFromCertificate(parsedCsr) - - if (filteredCsrsMap.has(pubKey)) { - filteredCsrsMap.delete(pubKey) - } - filteredCsrsMap.set(pubKey, csr) - }) + allCsrs + .filter(async csr => { + // const parsedCsr = await loadCSR(csr) + const validation = await CertificatesRequestsStore.validateUserCsr(csr) + if (validation) return true + return false + }) + .map(async csr => { + const parsedCsr = await loadCSR(csr) + const pubKey = keyFromCertificate(parsedCsr) + + if (filteredCsrsMap.has(pubKey)) { + filteredCsrsMap.delete(pubKey) + } + filteredCsrsMap.set(pubKey, csr) + }) ) return [...filteredCsrsMap.values()] } -} \ No newline at end of file +} diff --git a/packages/backend/src/nest/storage/storage.service.spec.ts b/packages/backend/src/nest/storage/storage.service.spec.ts index 27fabc7b85..8086d52e45 100644 --- a/packages/backend/src/nest/storage/storage.service.spec.ts +++ b/packages/backend/src/nest/storage/storage.service.spec.ts @@ -343,8 +343,11 @@ describe('StorageService', () => { storageService.certificatesStore.store.events.emit('replicated') - expect(eventSpy).toBeCalledWith(SocketActionTypes.CONNECTION_PROCESS_INFO, ConnectionProcessInfo.CERTIFICATES_REPLICATED) - + expect(eventSpy).toBeCalledWith( + SocketActionTypes.CONNECTION_PROCESS_INFO, + ConnectionProcessInfo.CERTIFICATES_REPLICATED + ) + await waitForExpect(() => { expect(eventSpy).toBeCalledWith(StorageEvents.REPLICATED_CERTIFICATES, { certificates: [] }) expect(spyOnUpdatePeersList).toBeCalled() @@ -681,4 +684,4 @@ describe('StorageService', () => { expect(spyOnUpdatePeersList).toBeCalledTimes(3) }) }) -}) \ No newline at end of file +}) diff --git a/packages/backend/src/nest/storage/storage.service.ts b/packages/backend/src/nest/storage/storage.service.ts index 146358c360..8e2747ae4a 100644 --- a/packages/backend/src/nest/storage/storage.service.ts +++ b/packages/backend/src/nest/storage/storage.service.ts @@ -339,14 +339,14 @@ export class StorageService extends EventEmitter { } public async attachCertificatesStoreListeners() { - this.on(StorageEvents.LOADED_CERTIFICATES, async (payload) => { + this.on(StorageEvents.LOADED_CERTIFICATES, async payload => { this.emit(StorageEvents.REPLICATED_CERTIFICATES, payload) await this.updatePeersList() }) } public async attachCsrsStoreListeners() { - this.on(StorageEvents.LOADED_USER_CSRS, async (payload) => { + this.on(StorageEvents.LOADED_USER_CSRS, async payload => { const allCertificates = this.getAllEventLogEntries(this.certificatesStore.store) this.emit(StorageEvents.REPLICATED_CSR, { csrs: payload.csrs, certificates: allCertificates, id: payload.id }) // TODO @@ -834,4 +834,4 @@ export class StorageService extends EventEmitter { this.certificatesRequestsStore.resetCsrReplicatedMapAndId() } } -} \ No newline at end of file +}