Skip to content

Commit

Permalink
fix ownerData selector
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkabuki committed Nov 30, 2023
1 parent 8946870 commit a1fe675
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getOldestParsedCerificate } from '../users/users.selectors'
// Workaround for "The inferred type of 'communitiesSelectors' cannot be named without a reference to
// 'packages/identity/node_modules/pkijs/build'. This is likely not portable. A type annotation is necessary."
// https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1270716220
import type {} from 'pkijs'
import type { } from 'pkijs'

const communitiesSlice: CreatedSelectors[StoreKeys.Communities] = (state: StoreState) => state[StoreKeys.Communities]

Expand Down Expand Up @@ -83,7 +83,7 @@ export const ownerNickname = createSelector(
const parsedCert = parseCertificate(certificate)
nickname = getCertFieldValue(parsedCert, CertFieldsTypes.nickName)
} else {
nickname = getCertFieldValue(oldestParsedCerificate, CertFieldsTypes.nickName)
nickname = getCertFieldValue(oldestParsedCerificate[1], CertFieldsTypes.nickName)
}

if (!nickname) {
Expand Down
26 changes: 12 additions & 14 deletions packages/state-manager/src/sagas/users/users.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,26 +124,24 @@ export const getUserByPubKey = (pubKey: string) => createSelector(allUsers, user

export const getOldestParsedCerificate = createSelector(certificates, certs => {
const getTimestamp = (cert: Certificate) => new Date(cert.notBefore.value).getTime()
let certificates: Certificate[] = []
Object.keys(certs).map(pubKey => {
certificates = [...certificates, certs[pubKey]]
})
certificates.sort((a, b) => {
const aTimestamp = getTimestamp(a)
const bTimestamp = getTimestamp(b)
return aTimestamp - bTimestamp
})

const certificates: [string, Certificate][] =
Array.from(Object.entries(certs))
.sort((a, b) => {
const aTimestamp = getTimestamp(a[1])
const bTimestamp = getTimestamp(b[1])
return aTimestamp - bTimestamp
})
return certificates[0]
})

export const ownerData = createSelector(getOldestParsedCerificate, ownerCert => {
if (!ownerCert) return null
const username = getCertFieldValue(ownerCert, CertFieldsTypes.nickName)
const onionAddress = getCertFieldValue(ownerCert, CertFieldsTypes.commonName)
const peerId = getCertFieldValue(ownerCert, CertFieldsTypes.peerId)
const dmPublicKey = getCertFieldValue(ownerCert, CertFieldsTypes.dmPublicKey)
const pubKey = keyFromCertificate(ownerCert)
const username = getCertFieldValue(ownerCert[1], CertFieldsTypes.nickName)
const onionAddress = getCertFieldValue(ownerCert[1], CertFieldsTypes.commonName)
const peerId = getCertFieldValue(ownerCert[1], CertFieldsTypes.peerId)
const dmPublicKey = getCertFieldValue(ownerCert[1], CertFieldsTypes.dmPublicKey)
const pubKey = ownerCert[0]

return {
username,
Expand Down

0 comments on commit a1fe675

Please sign in to comment.