From 19fd1af24f7d8f49a08efb5f1acbf56ae35f5b8d Mon Sep 17 00:00:00 2001 From: Vin Kabuki Date: Wed, 15 Nov 2023 21:24:58 +0100 Subject: [PATCH] chore: emit event to backend only if csr is absent in csrs list --- .../sagas/identity/saveUserCsr/saveUserCsr.saga.ts | 13 +++++++++++-- .../socket/startConnection/startConnection.saga.ts | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.ts b/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.ts index 55ed536eb1..5615366511 100644 --- a/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.ts +++ b/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.ts @@ -1,7 +1,9 @@ import { SaveCSRPayload, SocketActionTypes } from '@quiet/types' import { applyEmitParams, type Socket } from '../../../types' -import { apply, select } from 'typed-redux-saga' +import { apply, call, select } from 'typed-redux-saga' import { identitySelectors } from '../identity.selectors' +import { usersSelectors } from '../../users/users.selectors' +import { keyFromCertificate, parseCertificationRequest } from '@quiet/identity' export function* saveUserCsrSaga(socket: Socket): Generator { const identity = yield* select(identitySelectors.currentIdentity) @@ -9,9 +11,16 @@ export function* saveUserCsrSaga(socket: Socket): Generator { console.error('Cannot save user csr to backend, no userCsr') return } + + // Because we run this saga everytime we launch community (to make sure that our csr is saved to db) we need below logic to avoid duplicates of csrs in the csr database. + const parsedCsr = parseCertificationRequest(identity.userCsr.userCsr) + const pubKey = yield* call(keyFromCertificate, parsedCsr) + const csrs = yield* select(usersSelectors.csrsMapping) + if (Object.keys(csrs).includes(pubKey)) return + const payload: SaveCSRPayload = { csr: identity.userCsr?.userCsr, } console.log(`Send ${SocketActionTypes.SAVE_USER_CSR}`) yield* apply(socket, socket.emit, applyEmitParams(SocketActionTypes.SAVE_USER_CSR, payload)) -} +} \ No newline at end of file diff --git a/packages/state-manager/src/sagas/socket/startConnection/startConnection.saga.ts b/packages/state-manager/src/sagas/socket/startConnection/startConnection.saga.ts index 4fcf7250cc..4bf198da45 100644 --- a/packages/state-manager/src/sagas/socket/startConnection/startConnection.saga.ts +++ b/packages/state-manager/src/sagas/socket/startConnection/startConnection.saga.ts @@ -185,6 +185,7 @@ export function subscribe(socket: Socket) { socket.on(SocketActionTypes.COMMUNITY, (payload: ResponseLaunchCommunityPayload) => { console.log('Hunting for heisenbug: Community event received in state-manager') emit(communitiesActions.launchRegistrar(payload.id)) + // Not sure about this saveUserCsr. It seems that we've added it to secure case when user closes the app unexpectedly before csr is saved to db, so we'll do that on restart. emit(identityActions.saveUserCsr()) emit(filesActions.checkForMissingFiles(payload.id)) emit(networkActions.addInitializedCommunity(payload.id))