From c37af73529ab92d8e58a6a30dc81add8a26cac0c Mon Sep 17 00:00:00 2001 From: Aleksei Lunin <alexey.lunin@vereign.com> Date: Tue, 5 Dec 2023 02:08:45 +0200 Subject: [PATCH] Remove constant notification pulling logic and fix receiving oob messages --- src/hooks/notifications.ts | 45 +++++++++++++++++++++++++------------- src/utils/agentUtils.ts | 43 +----------------------------------- todo | 22 ------------------- 3 files changed, 31 insertions(+), 79 deletions(-) delete mode 100644 todo diff --git a/src/hooks/notifications.ts b/src/hooks/notifications.ts index c9a7fab..32d498d 100644 --- a/src/hooks/notifications.ts +++ b/src/hooks/notifications.ts @@ -1,22 +1,37 @@ -import { useEffect, useState } from 'react'; -import {getNotifications, AgentNotification} from "src/utils/agentUtils"; -import rootStore from "src/store/rootStore"; +import {useMemo} from 'react'; +import {AgentNotification} from "src/utils/agentUtils"; import {Results, useQuery} from "@realm/react"; +import {useConnections, useCredentialByState, useProofByState} from "@aries-framework/react-hooks"; import Email from "../db-models/Email"; +import {CredentialState, ProofState} from "@aries-framework/core"; +import {ConnectionRecord} from "@aries-framework/core/build/modules/connections/repository/ConnectionRecord"; export const useAgentNotifications = (): AgentNotification[] => { - const [notifications, setNotifications] = useState<AgentNotification[]>([]); - - const fetchNotifications = async () => { - const data = await getNotifications(rootStore.agentStore.agent); - setNotifications(data); - } - - useEffect(() => { - const interval = setInterval(fetchNotifications, 2000); - fetchNotifications(); - return () => clearInterval(interval); - }, []); + const allConnections = useConnections(); + const newOffers = useCredentialByState(CredentialState.OfferReceived); + const newProofRequests = useProofByState(ProofState.RequestReceived); + + const notifications = useMemo(() => { + const result: AgentNotification[] = []; + + for (const offer of newOffers) { + let connection: ConnectionRecord | null = null; + if (offer.connectionId) { + connection = allConnections.records.find(p => p.id === offer.connectionId) || null; + } + result.push({ key: offer.id, credentialOffer: offer, connection: connection }); + } + + for (const request of newProofRequests) { + let connection: ConnectionRecord | null = null; + if (request.connectionId) { + connection = allConnections.records.find(p => p.id === request.connectionId) || null; + } + result.push({ key: request.id, proofRequest: request, connection: connection }); + } + + return result; + }, [newOffers, newProofRequests, allConnections]); return notifications; }; diff --git a/src/utils/agentUtils.ts b/src/utils/agentUtils.ts index 4eb10f4..642ea60 100644 --- a/src/utils/agentUtils.ts +++ b/src/utils/agentUtils.ts @@ -1,4 +1,4 @@ -import {Agent, CredentialExchangeRecord, CredentialState, ProofExchangeRecord, ProofState} from "@aries-framework/core"; +import {CredentialExchangeRecord, ProofExchangeRecord} from "@aries-framework/core"; export interface AgentNotification { @@ -7,44 +7,3 @@ export interface AgentNotification { credentialOffer?: CredentialExchangeRecord; proofRequest?: ProofExchangeRecord; } - -export const getNotifications = async (agent: Agent): Promise<AgentNotification[]> => { - const newOffers = await agent.credentials.findAllByQuery({ state: CredentialState.OfferReceived }); - const newProofRequests = await agent.proofs.findAllByQuery({ state: ProofState.RequestReceived }); - - const rejectedIds: string[] = []; - // Declining the credential if connection is deleted - for await (const offer of newOffers) { - if (!offer.connectionId) { - await agent.credentials.declineOffer(offer.id); - rejectedIds.push(offer.id); - } - } - // Declining the proof if connection is deleted and message doesn't have ~service decorator - for await (const proof of newProofRequests) { - if (!proof.connectionId) { - await agent.proofs.declineRequest(proof.id); - rejectedIds.push(proof.id); - } - } - - const notifications: AgentNotification[] = []; - - const filteredOffers = newOffers.filter(p => !rejectedIds.some(rejId => rejId === p.id)); - for await (const offer of filteredOffers) { - const connection = await agent.connections.getById(offer.connectionId as string); - notifications.push({ key: offer.id, credentialOffer: offer, connection: connection }); - } - - const filteredProofRequests = newProofRequests.filter(p => !rejectedIds.some(rejId => rejId === p.id)) - for await (const request of filteredProofRequests) { - const connection = await agent.connections.getById(request.connectionId as string); - notifications.push({ key: request.id, proofRequest: request, connection: connection }); - } - - return notifications.sort((a, b) => { - const aCreatedAt = a.credentialOffer?.createdAt || a.proofRequest?.createdAt as Date; - const bCreatedAt = b.credentialOffer?.createdAt || b.proofRequest?.createdAt as Date; - return new Date(bCreatedAt).getTime() - new Date(aCreatedAt).getTime(); - }); -}; diff --git a/todo b/todo deleted file mode 100644 index 02adac6..0000000 --- a/todo +++ /dev/null @@ -1,22 +0,0 @@ -ConnectionInvitation -CredentialDetails -CredentialOffer - -ListCredentials -Notifications -ProofRequest -Scan - - - - - - -Import Wallet Select Wallet does not work (For some reason android does not want to show file selector) -Export Wallet - Does not have access to write files -SealDetailsInfo - issue on chain component. First two steps displays as broken -App is crashed after pin change -Wallet does not work at all after pin change - - -SCAN issue. it should display accept connection screen, but actually it do it automatically -- GitLab