Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(notifications): optimistically update updated_at when notifications come in (#1535)

teo8ac57b2f52f9
diff --git a/js/app/packages/queries/notification/tests/user-notifications.test.tsx b/js/app/packages/queries/notification/tests/user-notifications.test.tsx
index 3a0ac6fbf0..d823cef11b 100644
--- a/js/app/packages/queries/notification/tests/user-notifications.test.tsx
+++ b/js/app/packages/queries/notification/tests/user-notifications.test.tsx
@@ -28,7 +28,12 @@ vi.mock('@service-notification/client', () => ({
documentMentionMetadata: {},
}));
+vi.mock('@queries/soup/normalized-cache', () => ({
+ optimisticUpdateSoupItemUpdatedAt: vi.fn(),
+}));
+
import { notificationServiceClient } from '@service-notification/client';
+import { optimisticUpdateSoupItemUpdatedAt } from '@queries/soup/normalized-cache';
const mockBulkMarkNotificationAsSeen = vi.mocked(
notificationServiceClient.bulkMarkNotificationAsSeen
@@ -36,6 +41,9 @@ const mockBulkMarkNotificationAsSeen = vi.mocked(
const mockBulkMarkNotificationAsDone = vi.mocked(
notificationServiceClient.bulkMarkNotificationAsDone
);
+const mockOptimisticUpdateSoupItemUpdatedAt = vi.mocked(
+ optimisticUpdateSoupItemUpdatedAt
+);
let testQueryClient: QueryClient;
@@ -353,6 +361,11 @@ describe('optimisticInsertNotification', () => {
expect(notifications[0].id).toBe('new-notification');
expect(notifications[1].id).toBe('n1');
expect(notifications[2].id).toBe('n2');
+ expect(mockOptimisticUpdateSoupItemUpdatedAt).toHaveBeenCalledWith(
+ newNotification.entity_id,
+ 'document',
+ newNotification.created_at
+ );
});
it('should not insert duplicate notifications', () => {
@@ -368,4 +381,47 @@ describe('optimisticInsertNotification', () => {
expect(notifications[0].id).toBe('n1');
expect(notifications[1].id).toBe('n2');
});
+
+ it('should map email notifications to emailThread soup tag', () => {
+ seedQueryCache([createMockNotificationPage([])]);
+
+ const emailNotification = createMockNotification({
+ entity_type: 'email_thread',
+ entity_id: 'thread-1',
+ created_at: '2024-01-01T00:00:00.000Z',
+ });
+
+ optimisticInsertNotification(emailNotification);
+
+ expect(mockOptimisticUpdateSoupItemUpdatedAt).toHaveBeenCalledWith(
+ 'thread-1',
+ 'emailThread',
+ '2024-01-01T00:00:00.000Z'
+ );
+ });
+
+ it('should skip soup update when notification has no created_at', () => {
+ seedQueryCache([createMockNotificationPage([])]);
+
+ const notificationWithoutCreatedAt = createMockNotification({
+ created_at: null,
+ });
+
+ optimisticInsertNotification(notificationWithoutCreatedAt);
+
+ expect(mockOptimisticUpdateSoupItemUpdatedAt).not.toHaveBeenCalled();
+ });
+
+ it('should skip soup update for unsupported entity types', () => {
+ seedQueryCache([createMockNotificationPage([])]);
+
+ const userNotification = createMockNotification({
+ entity_type: 'user',
+ created_at: '2024-01-01T00:00:00.000Z',
+ });
+
+ optimisticInsertNotification(userNotification);
+
+ expect(mockOptimisticUpdateSoupItemUpdatedAt).not.toHaveBeenCalled();
+ });
});
diff --git a/js/app/packages/queries/notification/user-notifications.ts b/js/app/packages/queries/notification/user-notifications.ts
index 9322b03d88..1b54f97e3d 100644
--- a/js/app/packages/queries/notification/user-notifications.ts
+++ b/js/app/packages/queries/notification/user-notifications.ts
@@ -10,9 +10,14 @@ import {
useInfiniteQuery,
useMutation,
} from '@tanstack/solid-query';
+import { P, match } from 'ts-pattern';
import { queryClient } from '../client';
import { notificationKeys } from './keys';
import type { UnifiedNotification } from '@notifications/types';
+import {
+ type SoupEntityTag,
+ optimisticUpdateSoupItemUpdatedAt,
+} from '@queries/soup/normalized-cache';
function stripOwnerId({
owner_id: _,
@@ -387,10 +392,24 @@ export async function getNotificationById(
return stripOwnerId(res as NotificationItem);
}
+function notificationEntityTypeToSoupTag(
+ entityType: UnifiedNotification['entity_type']
+): SoupEntityTag | null {
+ return match(entityType)
+ .with('document', () => 'document' as const)
+ .with('chat', () => 'chat' as const)
+ .with('channel', () => 'channel' as const)
+ .with('project', () => 'project' as const)
+ .with('email_thread', () => 'emailThread' as const)
+ .with(P.union('user', 'team'), () => null)
+ .exhaustive();
+}
+
export function optimisticInsertNotification(
notification: UnifiedNotification
) {
const item = notification as NotificationItem;
+ const soupTag = notificationEntityTypeToSoupTag(notification.entity_type);
queryClient.setQueriesData<NotificationData<UserNotificationsPageParam>>(
{ queryKey: notificationKeys.user._def },
@@ -411,5 +430,13 @@ export function optimisticInsertNotification(
}
);
+ if (soupTag && notification.created_at) {
+ optimisticUpdateSoupItemUpdatedAt(
+ notification.entity_id,
+ soupTag,
+ notification.created_at
+ );
+ }
+
invalidateUserNotifications();
}
diff --git a/js/app/packages/queries/soup/normalized-cache/index.ts b/js/app/packages/queries/soup/normalized-cache/index.ts
index ad6eb8f663..65a9347ff2 100644
--- a/js/app/packages/queries/soup/normalized-cache/index.ts
+++ b/js/app/packages/queries/soup/normalized-cache/index.ts
@@ -17,4 +17,5 @@ export {
removeSearchEntities,
refetchSoupEntity,
optimisticUpdateSoupItemViewedAt,
+ optimisticUpdateSoupItemUpdatedAt,
} from './operations';
diff --git a/js/app/packages/queries/soup/normalized-cache/operations.test.ts b/js/app/packages/queries/soup/normalized-cache/operations.test.ts
index a4b50846d8..35da190d95 100644
--- a/js/app/packages/queries/soup/normalized-cache/operations.test.ts
+++ b/js/app/packages/queries/soup/normalized-cache/operations.test.ts
@@ -45,6 +45,7 @@ import {
insertSoupEntity,
removeSoupEntities,
removeSearchEntities,
+ optimisticUpdateSoupItemUpdatedAt,
optimisticUpdateSoupEntity,
} from './operations';
import { soupKeys } from '../keys';
@@ -59,6 +60,17 @@ function mockDocumentItem(id: string): SoupApiItem {
} as unknown as SoupApiItem;
}
+function mockDocumentItemWithUpdatedAt(
+ id: string,
+ updatedAt: string
+): SoupApiItem {
+ return {
+ tag: 'document',
+ data: { id, title: 'doc', updatedAt },
+ frecency_score: 1,
+ } as unknown as SoupApiItem;
+}
+
function mockChannelItem(id: string): SoupApiItem {
return {
tag: 'channel',
@@ -67,6 +79,17 @@ function mockChannelItem(id: string): SoupApiItem {
} as unknown as SoupApiItem;
}
+function mockChannelItemWithUpdatedAt(
+ id: string,
+ updatedAt: string
+): SoupApiItem {
+ return {
+ tag: 'channel',
+ data: { channel: { id, name: 'ch', updated_at: updatedAt } },
+ frecency_score: 1,
+ } as unknown as SoupApiItem;
+}
+
function mockChatItem(id: string): SoupApiItem {
return {
tag: 'chat',
@@ -340,3 +363,92 @@ describe('optimisticUpdateSoupEntity', () => {
expect(restored).toEqual(originalData);
});
});
+
+describe('optimisticUpdateSoupItemUpdatedAt', () => {
+ it('updates updatedAt for non-channel entities', () => {
+ mockNormalizer.getObjectById.mockReturnValueOnce(mockDocumentItem('doc-1'));
+
+ optimisticUpdateSoupItemUpdatedAt(
+ 'doc-1',
+ 'document',
+ '2024-01-01T00:00:00.000Z'
+ );
+
+ expect(mockNormalizer.setNormalizedData).toHaveBeenCalledWith({
+ tag: 'document',
+ data: { id: 'doc-1', updatedAt: '2024-01-01T00:00:00.000Z' },
+ frecency_score: 1,
+ });
+ });
+
+ it('updates updated_at for channel entities', () => {
+ mockNormalizer.getObjectById.mockReturnValueOnce(mockChannelItem('ch-1'));
+
+ optimisticUpdateSoupItemUpdatedAt(
+ 'ch-1',
+ 'channel',
+ '2024-01-01T00:00:00.000Z'
+ );
+
+ expect(mockNormalizer.setNormalizedData).toHaveBeenCalledWith({
+ tag: 'channel',
+ data: {
+ channel: { id: 'ch-1', updated_at: '2024-01-01T00:00:00.000Z' },
+ },
+ frecency_score: 1,
+ });
+ });
+
+ it('does not update when incoming updatedAt is older or equal (non-channel)', () => {
+ mockNormalizer.getObjectById.mockReturnValueOnce(
+ mockDocumentItemWithUpdatedAt('doc-1', '2024-01-02T00:00:00.000Z')
+ );
+ optimisticUpdateSoupItemUpdatedAt(
+ 'doc-1',
+ 'document',
+ '2024-01-01T00:00:00.000Z'
+ );
+
+ mockNormalizer.getObjectById.mockReturnValueOnce(
+ mockDocumentItemWithUpdatedAt('doc-1', '2024-01-02T00:00:00.000Z')
+ );
+ optimisticUpdateSoupItemUpdatedAt(
+ 'doc-1',
+ 'document',
+ '2024-01-02T00:00:00.000Z'
+ );
+
+ expect(mockNormalizer.setNormalizedData).not.toHaveBeenCalled();
+ });
+
+ it('does not update when incoming updated_at is older (channel)', () => {
+ mockNormalizer.getObjectById.mockReturnValueOnce(
+ mockChannelItemWithUpdatedAt('ch-1', '2024-01-02T00:00:00.000Z')
+ );
+
+ optimisticUpdateSoupItemUpdatedAt(
+ 'ch-1',
+ 'channel',
+ '2024-01-01T00:00:00.000Z'
+ );
+
+ expect(mockNormalizer.setNormalizedData).not.toHaveBeenCalled();
+ });
+
+ it('does nothing when cache entity is missing or tag mismatches', () => {
+ optimisticUpdateSoupItemUpdatedAt(
+ 'doc-1',
+ 'document',
+ '2024-01-01T00:00:00.000Z'
+ );
+
+ mockNormalizer.getObjectById.mockReturnValueOnce(mockDocumentItem('doc-1'));
+ optimisticUpdateSoupItemUpdatedAt(
+ 'doc-1',
+ 'chat',
+ '2024-01-01T00:00:00.000Z'
+ );
+
+ expect(mockNormalizer.setNormalizedData).not.toHaveBeenCalled();
+ });
+});
diff --git a/js/app/packages/queries/soup/normalized-cache/operations.ts b/js/app/packages/queries/soup/normalized-cache/operations.ts
index 1d92ba5086..de78709897 100644
--- a/js/app/packages/queries/soup/normalized-cache/operations.ts
+++ b/js/app/packages/queries/soup/normalized-cache/operations.ts
@@ -20,6 +20,7 @@ import type {
SoupEntityPartial,
} from './types';
import type { SoupApiItemFilter } from '../items';
+import { isAfter } from 'date-fns';
/**
* Optimistically update a single soup entity across all queries that reference it.
@@ -305,6 +306,54 @@ export function optimisticUpdateSoupItemViewedAt(itemId: string) {
}
}
+/**
+ * Optimistically update the updatedAt/updated_at timestamp for a soup item.
+ * Updates the item across all soup queries if it exists and matches the expected tag.
+ */
+export function optimisticUpdateSoupItemUpdatedAt(
+ itemId: string,
+ tag: SoupEntityTag,
+ updatedAt: string
+) {
+ const current = getSoupEntityById(itemId);
+ if (!current || current.tag !== tag) return;
+
+ if (current.tag === 'channel') {
+ if (
+ !shouldUpdateOptimisticTimestamp(
+ current.data.channel.updated_at,
+ updatedAt
+ )
+ )
+ return;
+
+ optimisticUpdateSoupEntity({
+ tag: 'channel',
+ data: { channel: { id: itemId, updated_at: updatedAt } },
+ frecency_score: current.frecency_score,
+ });
+ } else {
+ if (!shouldUpdateOptimisticTimestamp(current.data.updatedAt, updatedAt))
+ return;
+
+ optimisticUpdateSoupEntity({
+ tag: current.tag,
+ data: { id: itemId, updatedAt },
+ frecency_score: current.frecency_score,
+ });
+ }
+}
+
+/** @private */
+function shouldUpdateOptimisticTimestamp(
+ currentUpdatedAt: string | undefined,
+ incomingUpdatedAt: string
+): boolean {
+ return currentUpdatedAt
+ ? isAfter(Date.parse(incomingUpdatedAt), Date.parse(currentUpdatedAt))
+ : true;
+}
+
/** @private */
function getSearchResultId(result: UnifiedSearchResponseItem): string {
return match(result)