Skip to content

Commit

Permalink
feat: Decline problem report (#99)
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Lanser <[email protected]>
  • Loading branch information
Tommylans authored Apr 12, 2024
1 parent 572538b commit 791415e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/agent/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './useCredentialsForDisplay'
export * from './useCredentialForDisplayById'
export * from './useAcceptDidCommCredential'
export * from './useAcceptDidCommPresentation'
export * from './useDidCommCredentialActions'
export * from './useDidCommPresentationActions'
export * from './useInboxNotifications'
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ function useOfferAttributes(credentialExchangeId: string) {
}
}

export function useAcceptDidCommCredential(credentialExchangeId: string) {
export function useDidCommCredentialActions(credentialExchangeId: string) {
const { agent } = useAgent()

const credentialExchange = useCredentialById(credentialExchangeId)
const { data } = useOfferAttributes(credentialExchangeId)

const { mutateAsync, status } = useMutation({
const { mutateAsync: acceptCredentialMutation, status: acceptStatus } = useMutation({
mutationKey: ['acceptDidCommCredential', credentialExchangeId],
mutationFn: async () => {
const credentialDone$ = agent.events
Expand All @@ -68,13 +68,41 @@ export function useAcceptDidCommCredential(credentialExchangeId: string) {
},
})

const { mutateAsync: declineCredentialMutation, status: declineStatus } = useMutation({
mutationKey: ['declineDidCommCredential', credentialExchangeId],
mutationFn: async () => {
const credentialDone$ = agent.events
.observable<CredentialStateChangedEvent>(CredentialEventTypes.CredentialStateChanged)
.pipe(
// Correct record with id and state
filter(
(event) =>
event.payload.credentialRecord.id === credentialExchangeId &&
event.payload.credentialRecord.state === CredentialState.Declined
),
// 10 seconds to complete exchange
timeout(10000),
first()
)

const credentialDonePromise = firstValueFrom(credentialDone$)

await agent.credentials.declineOffer(credentialExchangeId, {
sendProblemReport: true,
})
await credentialDonePromise
},
})

const didcommDisplayMetadata = credentialExchange
? getDidCommCredentialExchangeDisplayMetadata(credentialExchange)
: undefined

return {
acceptCredential: mutateAsync,
status,
acceptCredential: acceptCredentialMutation,
declineCredential: declineCredentialMutation,
acceptStatus,
declineStatus,
credentialExchange,
display: {
issuer: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { filter, first, timeout } from 'rxjs/operators'
import { useAgent } from '../agent'
import { getDidCommCredentialExchangeDisplayMetadata } from '../didcomm/metadata'

export function useAcceptDidCommPresentation(proofExchangeId: string) {
export function useDidCommPresentationActions(proofExchangeId: string) {
const { agent } = useAgent()

const proofExchange = useProofById(proofExchangeId)
Expand Down Expand Up @@ -131,7 +131,7 @@ export function useAcceptDidCommPresentation(proofExchangeId: string) {
},
})

const { mutateAsync, status } = useMutation({
const { mutateAsync: acceptMutateAsync, status: acceptStatus } = useMutation({
mutationKey: ['acceptDidCommPresentation', proofExchangeId],
mutationFn: async () => {
const presentationDone$ = agent.events
Expand All @@ -157,9 +157,34 @@ export function useAcceptDidCommPresentation(proofExchangeId: string) {
},
})

const { mutateAsync: declineMutateAsync, status: declineStatus } = useMutation({
mutationKey: ['declineDidCommPresentation', proofExchangeId],
mutationFn: async () => {
const presentationDeclined$ = agent.events
.observable<ProofStateChangedEvent>(ProofEventTypes.ProofStateChanged)
.pipe(
// Correct record with id and state
filter(
(event) =>
event.payload.proofRecord.id === proofExchangeId &&
[ProofState.Declined].includes(event.payload.proofRecord.state)
),
// 10 seconds to complete exchange
timeout(10000),
first()
)

const presentationDeclinePromise = firstValueFrom(presentationDeclined$)
await agent.proofs.declineRequest({ proofRecordId: proofExchangeId, sendProblemReport: true })
await presentationDeclinePromise
},
})

return {
acceptPresentation: mutateAsync,
status,
acceptPresentation: acceptMutateAsync,
declinePresentation: declineMutateAsync,
acceptStatus,
declineStatus,
proofExchange,
submission: data,
verifierName: connection?.theirLabel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAcceptDidCommCredential, useAgent } from '@internal/agent'
import { useDidCommCredentialActions, useAgent } from '@internal/agent'
import { useToastController } from '@internal/ui'
import React from 'react'
import { useRouter } from 'solito/router'
Expand All @@ -18,8 +18,14 @@ export function DidCommCredentialNotificationScreen({
const router = useRouter()
const toast = useToastController()

const { acceptCredential, status, credentialExchange, attributes, display } =
useAcceptDidCommCredential(credentialExchangeId)
const {
acceptCredential,
acceptStatus,
declineCredential,
credentialExchange,
attributes,
display,
} = useDidCommCredentialActions(credentialExchangeId)

const pushToWallet = () => {
router.back()
Expand All @@ -44,7 +50,9 @@ export function DidCommCredentialNotificationScreen({
}

const onCredentialDecline = () => {
void agent.credentials.deleteById(credentialExchange.id)
declineCredential().finally(() => {
void agent.credentials.deleteById(credentialExchange.id)
})

toast.show('Credential has been declined.')
pushToWallet()
Expand All @@ -59,7 +67,7 @@ export function DidCommCredentialNotificationScreen({
}}
onDecline={onCredentialDecline}
// If state is not idle, it means we have pressed accept
isAccepting={status !== 'idle'}
isAccepting={acceptStatus !== 'idle'}
/>
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAcceptDidCommPresentation, useAgent } from '@internal/agent'
import { useDidCommPresentationActions, useAgent } from '@internal/agent'
import { useToastController } from '@internal/ui'
import React from 'react'
import { useRouter } from 'solito/router'
Expand All @@ -18,8 +18,14 @@ export function DidCommPresentationNotificationScreen({
const router = useRouter()
const toast = useToastController()

const { acceptPresentation, proofExchange, status, submission, verifierName } =
useAcceptDidCommPresentation(proofExchangeId)
const {
acceptPresentation,
declinePresentation,
proofExchange,
acceptStatus,
submission,
verifierName,
} = useDidCommPresentationActions(proofExchangeId)

const pushToWallet = () => {
router.back()
Expand All @@ -44,7 +50,9 @@ export function DidCommPresentationNotificationScreen({
}

const onProofDecline = () => {
void agent.proofs.deleteById(proofExchange.id)
declinePresentation().finally(() => {
void agent.proofs.deleteById(proofExchange.id)
})

toast.show('Information request has been declined.')
pushToWallet()
Expand All @@ -56,7 +64,7 @@ export function DidCommPresentationNotificationScreen({
onDecline={onProofDecline}
submission={submission}
// If state is not idle, it means we have pressed accept
isAccepting={status !== 'idle'}
isAccepting={acceptStatus !== 'idle'}
verifierName={verifierName}
/>
)
Expand Down

0 comments on commit 791415e

Please sign in to comment.