-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[PERF] Parallelize initial database lookups in Activity Hub #39924
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,29 +5,33 @@ import type { FindOptions } from 'mongodb'; | |
| import { canAccessRoomAsync } from '../../../authorization/server/functions/canAccessRoom'; | ||
|
|
||
| export async function findMentionedMessages({ | ||
| uid, | ||
| roomId, | ||
| pagination: { offset, count, sort }, | ||
| uid, | ||
| roomId, | ||
| pagination: { offset, count, sort }, | ||
| }: { | ||
| uid: string; | ||
| roomId: string; | ||
| pagination: { offset: number; count: number; sort: FindOptions<IMessage>['sort'] }; | ||
| uid: string; | ||
| roomId: string; | ||
| pagination: { offset: number; count: number; sort: FindOptions<IMessage>['sort'] }; | ||
| }): Promise<{ | ||
| messages: IMessage[]; | ||
| count: number; | ||
| offset: number; | ||
| total: number; | ||
| messages: IMessage[]; | ||
| count: number; | ||
| offset: number; | ||
| total: number; | ||
| }> { | ||
| const room = await Rooms.findOneById(roomId); | ||
| if (!room || !(await canAccessRoomAsync(room, { _id: uid }))) { | ||
| throw new Error('error-not-allowed'); | ||
| } | ||
| const user = await Users.findOneById<Pick<IUser, 'username'>>(uid, { projection: { username: 1 } }); | ||
| if (!user) { | ||
| throw new Error('invalid-user'); | ||
| } | ||
| // THE STRIKE: Fetch Room and User concurrently | ||
| const [room, user] = await Promise.all([ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Parallelizing room and user lookups removes the prior auth gate, causing unnecessary user reads on denied paths and allowing user-query failures to preempt the intended authorization error flow. Prompt for AI agents |
||
| Rooms.findOneById(roomId), | ||
| Users.findOneById<Pick<IUser, 'username'>>(uid, { projection: { username: 1 } }) | ||
| ]); | ||
|
Comment on lines
7
to
+25
|
||
|
|
||
| if (!room || !(await canAccessRoomAsync(room, { _id: uid }))) { | ||
| throw new Error('error-not-allowed'); | ||
| } | ||
|
Comment on lines
+22
to
+29
|
||
| if (!user) { | ||
| throw new Error('invalid-user'); | ||
| } | ||
|
|
||
| const { cursor, totalCount } = Messages.findPaginatedVisibleByMentionAndRoomId(user.username, roomId, { | ||
| const { cursor, totalCount } = Messages.findPaginatedVisibleByMentionAndRoomId(user.username, roomId, { | ||
| sort: sort || { ts: -1 }, | ||
| skip: offset, | ||
| limit: count, | ||
|
|
@@ -44,29 +48,33 @@ export async function findMentionedMessages({ | |
| } | ||
|
|
||
| export async function findStarredMessages({ | ||
| uid, | ||
| roomId, | ||
| pagination: { offset, count, sort }, | ||
| uid, | ||
| roomId, | ||
| pagination: { offset, count, sort }, | ||
| }: { | ||
| uid: string; | ||
| roomId: string; | ||
| pagination: { offset: number; count: number; sort: FindOptions<IMessage>['sort'] }; | ||
| uid: string; | ||
| roomId: string; | ||
| pagination: { offset: number; count: number; sort: FindOptions<IMessage>['sort'] }; | ||
| }): Promise<{ | ||
| messages: IMessage[]; | ||
| count: number; | ||
| offset: number; | ||
| total: number; | ||
| messages: IMessage[]; | ||
| count: number; | ||
| offset: number; | ||
| total: number; | ||
| }> { | ||
| const room = await Rooms.findOneById(roomId); | ||
| if (!room || !(await canAccessRoomAsync(room, { _id: uid }))) { | ||
| throw new Error('error-not-allowed'); | ||
| } | ||
| const user = await Users.findOneById<Pick<IUser, 'username'>>(uid, { projection: { username: 1 } }); | ||
| if (!user) { | ||
| throw new Error('invalid-user'); | ||
| } | ||
| // THE STRIKE: Fetch Room and User concurrently | ||
| const [room, user] = await Promise.all([ | ||
| Rooms.findOneById(roomId), | ||
| Users.findOneById<Pick<IUser, 'username'>>(uid, { projection: { username: 1 } }) | ||
| ]); | ||
|
Comment on lines
50
to
+68
|
||
|
|
||
| if (!room || !(await canAccessRoomAsync(room, { _id: uid }))) { | ||
| throw new Error('error-not-allowed'); | ||
| } | ||
|
Comment on lines
+65
to
+72
|
||
| if (!user) { | ||
| throw new Error('invalid-user'); | ||
| } | ||
|
|
||
| const { cursor, totalCount } = Messages.findStarredByUserAtRoom(uid, roomId, { | ||
| const { cursor, totalCount } = Messages.findStarredByUserAtRoom(uid, roomId, { | ||
| sort: sort || { ts: -1 }, | ||
| skip: offset, | ||
| limit: count, | ||
|
|
||
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.
Remove inline implementation comments added in production code.
Line 21 and Line 64 add implementation comments in code paths where the repo guideline asks to avoid them.
✂️ Minimal cleanup
As per coding guidelines "Avoid code comments in the implementation".
Also applies to: 64-64
🤖 Prompt for AI Agents