Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable notifications hook to include basic messages #1415

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading