Skip to content

Commit

Permalink
fix: revocation notification navigation + log in history (#175)
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Christophe Drouin <jean-christophe.drouin@mcn.gouv.qc.ca>
  • Loading branch information
jcdrouin21 authored Dec 10, 2024
1 parent 30cf44b commit b6f3f3c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 60 deletions.
2 changes: 1 addition & 1 deletion app/src/components/NotificationListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ const NotificationListItem: React.FC<NotificationListItemProps> = ({
case NotificationTypeEnum.Revocation:
navigation.getParent()?.navigate(Stacks.NotificationStack, {
screen: Screens.CredentialDetails,
params: { credential: notification },
params: { credential: notification.id },
})
break
case NotificationTypeEnum.Custom:
Expand Down
60 changes: 2 additions & 58 deletions app/src/hooks/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,16 @@ import {
W3cCredentialRecord,
} from '@credo-ts/core'
import { useCredentialByState, useProofByState, useBasicMessages, useAgent } from '@credo-ts/react-hooks'
import { BifoldAgent, TOKENS, useServices, useStore } from '@hyperledger/aries-bifold-core'
import {
CustomRecord,
HistoryCardType,
HistoryRecord,
RecordType,
} from '@hyperledger/aries-bifold-core/App/modules/history/types'
import { BifoldAgent, useStore } from '@hyperledger/aries-bifold-core'
import {
BasicMessageMetadata,
CredentialMetadata,
basicMessageCustomMetadata,
credentialCustomMetadata,
} from '@hyperledger/aries-bifold-core/App/types/metadata'
import { CustomNotificationRecord } from '@hyperledger/aries-bifold-core/App/types/notification'
import { parseCredDefFromId } from '@hyperledger/aries-bifold-core/App/utils/cred-def'
import { getCredentialIdentifiers } from '@hyperledger/aries-bifold-core/App/utils/credential'
import { ProofCustomMetadata, ProofMetadata } from '@hyperledger/aries-bifold-verifier'
import { useCallback, useEffect, useState } from 'react'
import { useEffect, useState } from 'react'

import { attestationCredDefIds } from '../constants'
import { showPersonCredentialSelector } from '../helpers/BCIDHelper'
Expand Down Expand Up @@ -79,53 +71,6 @@ export const useNotifications = ({ isHome = true }: NotificationsInputProps): No
const { agent } = useAgent()
const [store] = useStore<BCState>()
const [nonAttestationProofs, setNonAttestationProofs] = useState<ProofExchangeRecord[]>([])
const [logger, historyManagerCurried, historyEnabled] = useServices([
TOKENS.UTIL_LOGGER,
TOKENS.FN_LOAD_HISTORY,
TOKENS.HISTORY_ENABLED,
])

const logHistoryRecord = useCallback(
async (credential: CredentialRecord) => {
const connection = await agent?.connections.findById(credential?.connectionId ?? '')
const correspondenceName = connection?.alias || connection?.theirLabel || credential.connectionId
try {
if (!(agent && historyEnabled)) {
logger.trace(
`[${useNotifications.name}]:[logHistoryRecord] Skipping history log, either history function disabled or agent undefined!`
)
return
}
const historyManager = historyManagerCurried(agent)

const type = HistoryCardType.CardRevoked

const events = await historyManager.getHistoryItems({ type: RecordType.HistoryRecord })
if (
events.some(
(event: CustomRecord) => event.content.type === type && event.content.correspondenceId === credential.id
)
) {
return
}
const ids = getCredentialIdentifiers(credential)
const name = parseCredDefFromId(ids.credentialDefinitionId, ids.schemaId)

/** Save history record for card accepted */
const recordData: HistoryRecord = {
type: type,
message: name,
createdAt: new Date(),
correspondenceId: credential.id,
correspondenceName: correspondenceName,
}
await historyManager.saveHistory(recordData)
} catch (err: unknown) {
logger.error(`[${useNotifications.name}]:[logHistoryRecord] Error saving history: ${err}`)
}
},
[agent, historyEnabled, logger, historyManagerCurried]
)

useEffect(() => {
// get all unseen messages
Expand Down Expand Up @@ -158,7 +103,6 @@ export const useNotifications = ({ isHome = true }: NotificationsInputProps): No
metadata?.revoked_seen == undefined &&
(metadata?.seenOnHome == undefined || !isHome)
) {
logHistoryRecord(cred)
return cred
}
})
Expand Down
68 changes: 67 additions & 1 deletion app/src/navigators/TabStack.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CredentialExchangeRecord as CredentialRecord } from '@credo-ts/core'
import { useAgent } from '@credo-ts/react-hooks'
import {
AttachTourStep,
Expand All @@ -13,7 +14,15 @@ import {
useStore,
useTheme,
} from '@hyperledger/aries-bifold-core'
import {
CustomRecord,
HistoryCardType,
HistoryRecord,
RecordType,
} from '@hyperledger/aries-bifold-core/App/modules/history/types'
import { TourID } from '@hyperledger/aries-bifold-core/App/types/tour'
import { parseCredDefFromId } from '@hyperledger/aries-bifold-core/App/utils/cred-def'
import { getCredentialIdentifiers } from '@hyperledger/aries-bifold-core/App/utils/credential'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import { useNavigation } from '@react-navigation/native'
import { StackNavigationProp } from '@react-navigation/stack'
Expand Down Expand Up @@ -41,10 +50,18 @@ const TabStack: React.FC = () => {
const [store, dispatch] = useStore<BCState>()
const navigation = useNavigation<StackNavigationProp<TabStackParams>>()

const [{ useNotifications }, { enableImplicitInvitations, enableReuseConnections }, logger] = useServices([
const [
{ useNotifications },
{ enableImplicitInvitations, enableReuseConnections },
logger,
historyManagerCurried,
historyEnabled,
] = useServices([
TOKENS.NOTIFICATIONS,
TOKENS.CONFIG,
TOKENS.UTIL_LOGGER,
TOKENS.FN_LOAD_HISTORY,
TOKENS.HISTORY_ENABLED,
])

const notifications = useNotifications({ isHome: false } as NotificationsInputProps)
Expand All @@ -58,6 +75,48 @@ const TabStack: React.FC = () => {
},
})

const logHistoryRecord = useCallback(
async (credential: CredentialRecord) => {
const connection = await agent?.connections.findById(credential?.connectionId ?? '')
const correspondenceName = connection?.alias || connection?.theirLabel || credential.connectionId
try {
if (!(agent && historyEnabled)) {
logger.trace(
`[${TabStack.name}]:[logHistoryRecord] Skipping history log, either history function disabled or agent undefined!`
)
return
}
const historyManager = historyManagerCurried(agent)

const type = HistoryCardType.CardRevoked

const events = await historyManager.getHistoryItems({ type: RecordType.HistoryRecord })
if (
events.some(
(event: CustomRecord) => event.content.type === type && event.content.correspondenceId === credential.id
)
) {
return
}
const ids = getCredentialIdentifiers(credential)
const name = parseCredDefFromId(ids.credentialDefinitionId, ids.schemaId)

/** Save history record for card accepted */
const recordData: HistoryRecord = {
type: type,
message: name,
createdAt: new Date(),
correspondenceId: credential.id,
correspondenceName: correspondenceName,
}
await historyManager.saveHistory(recordData)
} catch (err: unknown) {
logger.error(`[${TabStack.name}]:[logHistoryRecord] Error saving history: ${err}`)
}
},
[agent, historyEnabled, logger, historyManagerCurried]
)

const handleDeepLink = useCallback(
async (deepLink: string) => {
logger.info(`Handling deeplink: ${deepLink}`)
Expand Down Expand Up @@ -158,6 +217,13 @@ const TabStack: React.FC = () => {
isTempDeleted: false,
}
}
if (
(n as CredentialRecord).type === 'CredentialRecord' &&
(n as CredentialRecord).state === 'done' &&
(n as CredentialRecord).revocationNotification
) {
logHistoryRecord(n as CredentialRecord)
}
}
if (Object.keys(notificationsToAdd).length > 0) {
dispatch({
Expand Down

0 comments on commit b6f3f3c

Please sign in to comment.