Skip to content

Commit

Permalink
chore: emit event to backend only if csr is absent in csrs list
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkabuki committed Nov 15, 2023
1 parent 032585e commit 19fd1af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
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)
if (!identity?.userCsr) {
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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 19fd1af

Please sign in to comment.