Skip to content

Commit

Permalink
feat(core): expose proposal status of ipex in notification and refact…
Browse files Browse the repository at this point in the history
…oring
  • Loading branch information
iFergal committed Dec 13, 2024
1 parent 5d6cdc1 commit b15bd4d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 30 deletions.
73 changes: 65 additions & 8 deletions src/core/agent/services/keriaNotificationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,29 +1009,29 @@ describe("Signify notification service of agent", () => {
const mockNotifications = [
{
id: "0AC0W27tnnd2WyHWUh-368EI",
createdAt: new Date("2024-04-29T11:01:04.903Z"),
createdAt: DATETIME,
a: { r: NotificationRoute.ExnIpexGrant },
multisigId: "multisig1",
read: false,
connectionId: "ED_3K5-VPI8N3iRrV7o75fIMOnJfoSmEJy679HTkWsFQ",
linkedGroupRequest: {},
linkedGroupRequest: { accepted: false },
},
{
id: "0AC0W34tnnd2WyUCOy-790AY",
createdAt: new Date("2024-04-29T11:01:04.903Z"),
createdAt: DATETIME,
a: { r: NotificationRoute.ExnIpexOffer },
multisigId: "multisig2",
read: false,
connectionId: "ED_5C2-UOA8N3iRrV7o75fIMOnJfoSmYAe829YCiSaVB",
linkedGroupRequest: {},
linkedGroupRequest: { accepted: false },
},
];

notificationStorage.findAllByQuery = jest
.fn()
.mockResolvedValue(mockNotifications);

const result = await keriaNotificationService.getAllNotifications();
const result = await keriaNotificationService.getNotifications();

expect(notificationStorage.findAllByQuery).toHaveBeenCalledWith({
$not: {
Expand All @@ -1041,26 +1041,83 @@ describe("Signify notification service of agent", () => {
expect(result).toEqual([
{
id: "0AC0W27tnnd2WyHWUh-368EI",
createdAt: "2024-04-29T11:01:04.903Z",
createdAt: DATETIME.toISOString(),
a: { r: NotificationRoute.ExnIpexGrant },
multisigId: "multisig1",
read: false,
connectionId: "ED_3K5-VPI8N3iRrV7o75fIMOnJfoSmEJy679HTkWsFQ",
groupReplied: false,
},
{
id: "0AC0W34tnnd2WyUCOy-790AY",
createdAt: "2024-04-29T11:01:04.903Z",
createdAt: DATETIME.toISOString(),
a: { r: NotificationRoute.ExnIpexOffer },
multisigId: "multisig2",
read: false,
connectionId: "ED_5C2-UOA8N3iRrV7o75fIMOnJfoSmYAe829YCiSaVB",
groupReplied: false,
},
]);
});

test("Notifications should indiciate if there in a current response or proposal", async () => {
const mockNotifications = [
{
id: "0AC0W27tnnd2WyHWUh-368EI",
createdAt: DATETIME,
a: { r: NotificationRoute.ExnIpexGrant },
multisigId: "multisig1",
read: false,
connectionId: "ED_3K5-VPI8N3iRrV7o75fIMOnJfoSmEJy679HTkWsFQ",
linkedGroupRequest: { accepted: false },
},
{
id: "0AC0W34tnnd2WyUCOy-790AY",
createdAt: DATETIME,
a: { r: NotificationRoute.ExnIpexGrant },
multisigId: "multisig2",
read: false,
connectionId: "ED_5C2-UOA8N3iRrV7o75fIMOnJfoSmYAe829YCiSaVB",
linkedGroupRequest: { accepted: false, current: "current-admit-said" },
},
];

notificationStorage.findAllByQuery = jest
.fn()
.mockResolvedValue(mockNotifications);

const result = await keriaNotificationService.getNotifications();

expect(notificationStorage.findAllByQuery).toHaveBeenCalledWith({
$not: {
route: NotificationRoute.ExnIpexAgree,
},
});
expect(result).toEqual([
{
id: "0AC0W27tnnd2WyHWUh-368EI",
createdAt: DATETIME.toISOString(),
a: { r: NotificationRoute.ExnIpexGrant },
multisigId: "multisig1",
read: false,
connectionId: "ED_3K5-VPI8N3iRrV7o75fIMOnJfoSmEJy679HTkWsFQ",
groupReplied: false,
},
{
id: "0AC0W34tnnd2WyUCOy-790AY",
createdAt: DATETIME.toISOString(),
a: { r: NotificationRoute.ExnIpexGrant },
multisigId: "multisig2",
read: false,
connectionId: "ED_5C2-UOA8N3iRrV7o75fIMOnJfoSmYAe829YCiSaVB",
groupReplied: true,
},
]);
});

test("Should return an empty list notification if no notifications are found", async () => {
notificationStorage.findAllByQuery.mockResolvedValue([]);
const result = await keriaNotificationService.getAllNotifications();
const result = await keriaNotificationService.getNotifications();

expect(result).toEqual([]);
});
Expand Down
25 changes: 11 additions & 14 deletions src/core/agent/services/keriaNotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
} from "../records";
import { OperationPendingRecordType } from "../records/operationPendingRecord.type";
import { OperationPendingRecord } from "../records/operationPendingRecord";
import { IonicStorage } from "../../storage/ionicStorage";
import { MultiSigService } from "./multiSigService";
import { IpexCommunicationService } from "./ipexCommunicationService";
import {
Expand Down Expand Up @@ -279,8 +278,9 @@ class KeriaNotificationService extends AgentService {
return;
}

const exn = await this.getExternalExnMessage(notif);
if (!exn) {
const exn = await this.props.signifyClient.exchanges().get(notif.a.d);
if (await this.outboundExchange(exn)) {
await this.markNotification(notif.i);
return;
}

Expand All @@ -302,9 +302,11 @@ class KeriaNotificationService extends AgentService {
exn
);
}

if (!shouldCreateRecord) {
return;
}

try {
const keriaNotif = await this.createNotificationRecord(notif);
if (notif.a.r !== NotificationRoute.ExnIpexAgree) {
Expand Down Expand Up @@ -389,11 +391,9 @@ class KeriaNotificationService extends AgentService {
}
}

private async getExternalExnMessage(
notif: Notification
): Promise<ExnMessage | undefined> {
const exchange = await this.props.signifyClient.exchanges().get(notif.a.d);

private async outboundExchange(
exchange: ExnMessage
): Promise<boolean> {
const ourIdentifier = await this.identifierStorage
.getIdentifierMetadata(exchange.exn.i)
.catch((error) => {
Expand All @@ -407,11 +407,7 @@ class KeriaNotificationService extends AgentService {
}
});

if (ourIdentifier) {
await this.markNotification(notif.i);
return undefined;
}
return exchange;
return ourIdentifier !== undefined;
}

private async processExnIpexApplyNotification(
Expand Down Expand Up @@ -795,7 +791,7 @@ class KeriaNotificationService extends AgentService {
await this.notificationStorage.update(notificationRecord);
}

async getAllNotifications(): Promise<KeriaNotification[]> {
async getNotifications(): Promise<KeriaNotification[]> {
const notifications = await this.notificationStorage.findAllByQuery({
$not: {
route: NotificationRoute.ExnIpexAgree,
Expand All @@ -809,6 +805,7 @@ class KeriaNotificationService extends AgentService {
multisigId: notification.multisigId,
connectionId: notification.connectionId,
read: notification.read,
groupReplied: notification.linkedGroupRequest.current !== undefined
};
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jest.mock("../core/agent/agent", () => ({
keriaNotifications: {
pollNotifications: jest.fn(),
pollLongOperations: jest.fn(),
getAllNotifications: jest.fn(),
getNotifications: jest.fn(),
stopNotification: jest.fn(),
startNotification: jest.fn(),
onNewNotification: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/AppWrapper/AppWrapper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jest.mock("../../../core/agent/agent", () => ({
keriaNotifications: {
pollNotifications: jest.fn(),
pollLongOperations: jest.fn(),
getAllNotifications: jest.fn(),
getNotifications: jest.fn(),
onNewNotification: jest.fn(),
onLongOperationComplete: jest.fn(),
onRemoveNotification: jest.fn(),
Expand Down
8 changes: 4 additions & 4 deletions src/ui/components/AppWrapper/AppWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import { CardListViewType } from "../SwitchCardView";
import "./AppWrapper.scss";
import { useActivityTimer } from "./hooks/useActivityTimer";
import {
notificatiStateChanged,
notificationStateChanged,
signifyOperationStateChangeHandler,
} from "./coreEventListeners";
import {
Expand Down Expand Up @@ -274,7 +274,7 @@ const AppWrapper = (props: { children: ReactNode }) => {
const storedPeerConnections =
await Agent.agent.peerConnectionMetadataStorage.getAllPeerConnectionMetadata();
const notifications =
await Agent.agent.keriaNotifications.getAllNotifications();
await Agent.agent.keriaNotifications.getNotifications();

dispatch(setIdentifiersCache(storedIdentifiers));
dispatch(setCredsCache(credsCache));
Expand Down Expand Up @@ -479,15 +479,15 @@ const AppWrapper = (props: { children: ReactNode }) => {
}
);
Agent.agent.keriaNotifications.onNewNotification((event) => {
notificatiStateChanged(event, dispatch);
notificationStateChanged(event, dispatch);
});

Agent.agent.keriaNotifications.onLongOperationComplete((event) => {
signifyOperationStateChangeHandler(event.payload, dispatch);
});

Agent.agent.keriaNotifications.onRemoveNotification((event) => {
notificatiStateChanged(event, dispatch);
notificationStateChanged(event, dispatch);
});
};

Expand Down
4 changes: 2 additions & 2 deletions src/ui/components/AppWrapper/coreEventListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { setToastMsg } from "../../../store/reducers/stateCache";
import { ToastMsgType } from "../../globals/types";

const notificatiStateChanged = (
const notificationStateChanged = (
event: NotificationRemovedEvent | NotificationAddedEvent,
dispatch: ReturnType<typeof useAppDispatch>
) => {
Expand Down Expand Up @@ -41,4 +41,4 @@ const signifyOperationStateChangeHandler = async (
break;
}
};
export { notificatiStateChanged, signifyOperationStateChangeHandler };
export { notificationStateChanged, signifyOperationStateChangeHandler };

0 comments on commit b15bd4d

Please sign in to comment.