Skip to content

Commit

Permalink
feat: enable notifications hook to include basic messages (#1415)
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce McMath <bryce.j.mcmath@gmail.com>
  • Loading branch information
bryce-mcmath authored Aug 30, 2023
1 parent c35dac5 commit 051810e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
32 changes: 27 additions & 5 deletions app/src/hooks/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { AnonCredsCredentialMetadataKey } from '@aries-framework/anoncreds/build/utils/metadata'
import {
BasicMessageRecord,
CredentialExchangeRecord as CredentialRecord,
CredentialState,
ProofExchangeRecord,
ProofState,
} from '@aries-framework/core'
import { useCredentialByState, useProofByState } from '@aries-framework/react-hooks'
import { useCredentialByState, useProofByState, useBasicMessages } from '@aries-framework/react-hooks'
import { useStore } from 'aries-bifold'
import { CredentialMetadata, customMetadata } from 'aries-bifold/App/types/metadata'
import {
BasicMessageMetadata,
CredentialMetadata,
basicMessageCustomMetadata,
credentialCustomMetadata,
} from 'aries-bifold/App/types/metadata'
import { ProofCustomMetadata, ProofMetadata } from 'aries-bifold/verifier'

import { getInvitationCredentialDate, showBCIDSelector } from '../helpers/BCIDHelper'
Expand All @@ -20,13 +26,28 @@ interface CustomNotification {

interface Notifications {
total: number
notifications: Array<CredentialRecord | ProofExchangeRecord | CustomNotification>
notifications: Array<BasicMessageRecord | CredentialRecord | ProofExchangeRecord | CustomNotification>
}

export const useNotifications = (): Notifications => {
const [store] = useStore<BCState>()
const offers = useCredentialByState(CredentialState.OfferReceived)
const proofsRequested = useProofByState(ProofState.RequestReceived)
const { records: basicMessages } = useBasicMessages()
// get all unseen messages
const unseenMessages: BasicMessageRecord[] = basicMessages.filter((msg) => {
const meta = msg.metadata.get(BasicMessageMetadata.customMetadata) as basicMessageCustomMetadata
return !meta?.seen
})
// add one unseen message per contact to notifications
const contactsWithUnseenMessages: string[] = []
const messagesToShow: BasicMessageRecord[] = []
unseenMessages.forEach((msg) => {
if (!contactsWithUnseenMessages.includes(msg.connectionId)) {
contactsWithUnseenMessages.push(msg.connectionId)
messagesToShow.push(msg)
}
})
const proofsDone = useProofByState([ProofState.Done, ProofState.PresentationReceived]).filter(
(proof: ProofExchangeRecord) => {
if (proof.isVerified === undefined) return false
Expand All @@ -37,7 +58,7 @@ export const useNotifications = (): Notifications => {
)
const revoked = useCredentialByState(CredentialState.Done).filter((cred: CredentialRecord) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const metadata = cred!.metadata.get(CredentialMetadata.customMetadata) as customMetadata
const metadata = cred!.metadata.get(CredentialMetadata.customMetadata) as credentialCustomMetadata
if (cred?.revocationNotification && metadata?.revoked_seen == undefined) {
return cred
}
Expand All @@ -58,7 +79,8 @@ export const useNotifications = (): Notifications => {
? [{ type: 'CustomNotification', createdAt: invitationDate, id: 'custom' }]
: []

let notifications: (CredentialRecord | ProofExchangeRecord | CustomNotification)[] = [
let notifications: (BasicMessageRecord | CredentialRecord | ProofExchangeRecord | CustomNotification)[] = [
...messagesToShow,
...offers,
...proofsRequested,
...proofsDone,
Expand Down
2 changes: 1 addition & 1 deletion bifold

0 comments on commit 051810e

Please sign in to comment.