Skip to content

Commit

Permalink
[Session] Drill getAgent into notifications/utils (#3703)
Browse files Browse the repository at this point in the history
* Drill into notifications/util

(cherry picked from commit 84b535ed54f4fe93debcd198809bb184519c3507)

* Thread getAgent instead

---------

Co-authored-by: Dan Abramov <[email protected]>
  • Loading branch information
estrattonbailey and gaearon authored Apr 25, 2024
1 parent a69e567 commit 35005da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/state/queries/notifications/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '@tanstack/react-query'

import {useMutedThreads} from '#/state/muted-threads'
import {getAgent} from '#/state/session'
import {STALE} from '..'
import {useModerationOpts} from '../preferences'
import {embedViewRecordToPostView, getEmbeddedPost} from '../util'
Expand Down Expand Up @@ -71,6 +72,7 @@ export function useNotificationFeedQuery(opts?: {enabled?: boolean}) {
if (!page) {
page = (
await fetchPage({
getAgent,
limit: PAGE_SIZE,
cursor: pageParam,
queryClient,
Expand Down
1 change: 1 addition & 0 deletions src/state/queries/notifications/unread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {

// count
const {page, indexedAt: lastIndexed} = await fetchPage({
getAgent,
cursor: undefined,
limit: 40,
queryClient,
Expand Down
22 changes: 13 additions & 9 deletions src/state/queries/notifications/util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {
AppBskyNotificationListNotifications,
ModerationOpts,
moderateNotification,
AppBskyEmbedRecord,
AppBskyFeedDefs,
AppBskyFeedLike,
AppBskyFeedPost,
AppBskyFeedRepost,
AppBskyFeedLike,
AppBskyEmbedRecord,
AppBskyNotificationListNotifications,
BskyAgent,
moderateNotification,
ModerationOpts,
} from '@atproto/api'
import chunk from 'lodash.chunk'
import {QueryClient} from '@tanstack/react-query'
import {getAgent} from '../../session'
import chunk from 'lodash.chunk'

import {precacheProfile} from '../profile'
import {NotificationType, FeedNotification, FeedPage} from './types'
import {FeedNotification, FeedPage, NotificationType} from './types'

const GROUPABLE_REASONS = ['like', 'repost', 'follow']
const MS_1HR = 1e3 * 60 * 60
Expand All @@ -22,13 +23,15 @@ const MS_2DAY = MS_1HR * 48
// =

export async function fetchPage({
getAgent,
cursor,
limit,
queryClient,
moderationOpts,
threadMutes,
fetchAdditionalData,
}: {
getAgent: () => BskyAgent
cursor: string | undefined
limit: number
queryClient: QueryClient
Expand All @@ -53,7 +56,7 @@ export async function fetchPage({
// we fetch subjects of notifications (usually posts) now instead of lazily
// in the UI to avoid relayouts
if (fetchAdditionalData) {
const subjects = await fetchSubjects(notifsGrouped)
const subjects = await fetchSubjects(getAgent, notifsGrouped)
for (const notif of notifsGrouped) {
if (notif.subjectUri) {
notif.subject = subjects.get(notif.subjectUri)
Expand Down Expand Up @@ -137,6 +140,7 @@ export function groupNotifications(
}

async function fetchSubjects(
getAgent: () => BskyAgent,
groupedNotifs: FeedNotification[],
): Promise<Map<string, AppBskyFeedDefs.PostView>> {
const uris = new Set<string>()
Expand Down

0 comments on commit 35005da

Please sign in to comment.