-
Notifications
You must be signed in to change notification settings - Fork 1.5k
INB-348: Keep inbox unread count updated in real time #3470
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
Open
gentlemandev
wants to merge
9
commits into
main
Choose a base branch
from
elie/inb-348-unread-message-count-in-mail-inbox-not-updated-in-real-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
996fedc
fix: keep inbox unread count current
elie222 9a10a4e
fix: retain folder IDs in thread lists
elie222 9897eef
fix: stabilize unread count updates
elie222 19f405e
fix: handle unread count edge cases
elie222 6899b46
fix: retain pending unread deltas
elie222 c4ee7f6
fix: scope unread deltas by account
elie222 62980eb
fix: ignore stale account count updates
elie222 92fabcd
fix: reset unread state after commit
elie222 a96e947
Merge main into INB-348 unread count branch
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
apps/web/app/(app)/[emailAccountId]/mail/inbox-unread-count.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import type { ListThread } from "./types"; | ||
| import { getInboxUnreadDelta } from "./inbox-unread-count"; | ||
|
|
||
| describe("getInboxUnreadDelta", () => { | ||
| it("decrements only unread inbox conversations marked as read", () => { | ||
| const threads = [ | ||
| createThread("unread-inbox", ["INBOX", "UNREAD"]), | ||
| createThread("read-inbox", ["INBOX"]), | ||
| createThread("unread-archive", ["UNREAD"]), | ||
| ]; | ||
|
|
||
| expect( | ||
| getInboxUnreadDelta({ | ||
| read: true, | ||
| threadKeys: threads.map((thread) => thread.id), | ||
| threads, | ||
| }), | ||
| ).toBe(-1); | ||
| }); | ||
|
|
||
| it("increments only read inbox conversations marked as unread", () => { | ||
| const threads = [ | ||
| createThread("read-inbox", ["INBOX"]), | ||
| createThread("unread-inbox", ["INBOX", "UNREAD"]), | ||
| ]; | ||
|
|
||
| expect( | ||
| getInboxUnreadDelta({ | ||
| read: false, | ||
| threadKeys: threads.map((thread) => thread.id), | ||
| threads, | ||
| }), | ||
| ).toBe(1); | ||
| }); | ||
|
|
||
| it("counts each unread Outlook inbox message", () => { | ||
| const thread = createThread("outlook", ["UNREAD"], "outlook-inbox", 2); | ||
|
|
||
| expect( | ||
| getInboxUnreadDelta({ | ||
| countByMessage: true, | ||
| inboxFolderId: "outlook-inbox", | ||
| read: true, | ||
| threadKeys: [thread.id], | ||
| threads: [thread], | ||
| }), | ||
| ).toBe(-2); | ||
| }); | ||
| }); | ||
|
|
||
| function createThread( | ||
| id: string, | ||
| labelIds: string[], | ||
| parentFolderId?: string, | ||
| messageCount = 1, | ||
| ): ListThread { | ||
| return { | ||
| id, | ||
| snippet: "snippet", | ||
| plan: undefined, | ||
| plans: [], | ||
| messages: Array.from({ length: messageCount }, (_, index) => ({ | ||
| id: `message-${id}-${index}`, | ||
| threadId: id, | ||
| snippet: "snippet", | ||
| subject: "Subject", | ||
| date: "0", | ||
| internalDate: "0", | ||
| labelIds, | ||
| parentFolderId, | ||
| headers: { subject: "Subject" }, | ||
| })), | ||
| }; | ||
| } |
46 changes: 46 additions & 0 deletions
46
apps/web/app/(app)/[emailAccountId]/mail/inbox-unread-count.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { GmailLabel } from "@/utils/gmail/label"; | ||
| import { isThreadUnread } from "./read-state"; | ||
| import { getListThreadKey, type ListThread } from "./types"; | ||
|
|
||
| export function getInboxUnreadDelta({ | ||
| countByMessage, | ||
| inboxFolderId, | ||
| read, | ||
| threadKeys, | ||
| threads, | ||
| }: { | ||
| countByMessage?: boolean; | ||
| inboxFolderId?: string; | ||
| read: boolean; | ||
| threadKeys: string[]; | ||
| threads: ListThread[]; | ||
| }) { | ||
| const targets = new Set(threadKeys); | ||
| let delta = 0; | ||
|
|
||
| for (const thread of threads) { | ||
| if (!targets.has(getListThreadKey(thread))) continue; | ||
| if (countByMessage) { | ||
| const affectedMessages = thread.messages.filter((message) => { | ||
| const isInInbox = | ||
| message.labelIds?.includes(GmailLabel.INBOX) || | ||
| (inboxFolderId && message.parentFolderId === inboxFolderId); | ||
| const isUnread = message.labelIds?.includes(GmailLabel.UNREAD) ?? false; | ||
| return isInInbox && isUnread !== !read; | ||
| }); | ||
| delta += affectedMessages.length * (read ? -1 : 1); | ||
| continue; | ||
| } | ||
|
|
||
| const isInInbox = thread.messages.some((message) => | ||
| message.labelIds?.includes(GmailLabel.INBOX), | ||
| ); | ||
| if (!isInInbox) continue; | ||
|
|
||
| const isUnread = isThreadUnread(thread.messages); | ||
| if (isUnread === !read) continue; | ||
| delta += read ? -1 : 1; | ||
| } | ||
|
|
||
| return delta; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: When an Outlook conversation has multiple unread messages, this optimistic delta changes the Inbox badge by one instead of by each unread folder item. Make the delta provider-specific or count unread inbox messages so it matches Outlook's folder count.
Prompt for AI agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 19f405e: Outlook now adjusts the badge per affected inbox message, matching unreadItemCount, while Gmail continues to count unread conversations. The focused test covers multiple unread Outlook messages.