diff options
Diffstat (limited to 'src/state/queries/notifications/util.ts')
-rw-r--r-- | src/state/queries/notifications/util.ts | 83 |
1 files changed, 65 insertions, 18 deletions
diff --git a/src/state/queries/notifications/util.ts b/src/state/queries/notifications/util.ts index 8ed1c0390..ade98b317 100644 --- a/src/state/queries/notifications/util.ts +++ b/src/state/queries/notifications/util.ts @@ -3,6 +3,8 @@ import { AppBskyFeedLike, AppBskyFeedPost, AppBskyFeedRepost, + AppBskyGraphDefs, + AppBskyGraphStarterpack, AppBskyNotificationListNotifications, BskyAgent, moderateNotification, @@ -40,6 +42,7 @@ export async function fetchPage({ limit, cursor, }) + const indexedAt = res.data.notifications[0]?.indexedAt // filter out notifs by mod rules @@ -56,9 +59,18 @@ export async function fetchPage({ const subjects = await fetchSubjects(agent, notifsGrouped) for (const notif of notifsGrouped) { if (notif.subjectUri) { - notif.subject = subjects.get(notif.subjectUri) - if (notif.subject) { - precacheProfile(queryClient, notif.subject.author) + if ( + notif.type === 'starterpack-joined' && + notif.notification.reasonSubject + ) { + notif.subject = subjects.starterPacks.get( + notif.notification.reasonSubject, + ) + } else { + notif.subject = subjects.posts.get(notif.subjectUri) + if (notif.subject) { + precacheProfile(queryClient, notif.subject.author) + } } } } @@ -120,12 +132,21 @@ export function groupNotifications( } if (!grouped) { const type = toKnownType(notif) - groupedNotifs.push({ - _reactKey: `notif-${notif.uri}`, - type, - notification: notif, - subjectUri: getSubjectUri(type, notif), - }) + if (type !== 'starterpack-joined') { + groupedNotifs.push({ + _reactKey: `notif-${notif.uri}`, + type, + notification: notif, + subjectUri: getSubjectUri(type, notif), + }) + } else { + groupedNotifs.push({ + _reactKey: `notif-${notif.uri}`, + type: 'starterpack-joined', + notification: notif, + subjectUri: notif.uri, + }) + } } } return groupedNotifs @@ -134,29 +155,54 @@ export function groupNotifications( async function fetchSubjects( agent: BskyAgent, groupedNotifs: FeedNotification[], -): Promise<Map<string, AppBskyFeedDefs.PostView>> { - const uris = new Set<string>() +): Promise<{ + posts: Map<string, AppBskyFeedDefs.PostView> + starterPacks: Map<string, AppBskyGraphDefs.StarterPackViewBasic> +}> { + const postUris = new Set<string>() + const packUris = new Set<string>() for (const notif of groupedNotifs) { if (notif.subjectUri?.includes('app.bsky.feed.post')) { - uris.add(notif.subjectUri) + postUris.add(notif.subjectUri) + } else if ( + notif.notification.reasonSubject?.includes('app.bsky.graph.starterpack') + ) { + packUris.add(notif.notification.reasonSubject) } } - const uriChunks = chunk(Array.from(uris), 25) + const postUriChunks = chunk(Array.from(postUris), 25) + const packUriChunks = chunk(Array.from(packUris), 25) const postsChunks = await Promise.all( - uriChunks.map(uris => + postUriChunks.map(uris => agent.app.bsky.feed.getPosts({uris}).then(res => res.data.posts), ), ) - const map = new Map<string, AppBskyFeedDefs.PostView>() + const packsChunks = await Promise.all( + packUriChunks.map(uris => + agent.app.bsky.graph + .getStarterPacks({uris}) + .then(res => res.data.starterPacks), + ), + ) + const postsMap = new Map<string, AppBskyFeedDefs.PostView>() + const packsMap = new Map<string, AppBskyGraphDefs.StarterPackView>() for (const post of postsChunks.flat()) { if ( AppBskyFeedPost.isRecord(post.record) && AppBskyFeedPost.validateRecord(post.record).success ) { - map.set(post.uri, post) + postsMap.set(post.uri, post) + } + } + for (const pack of packsChunks.flat()) { + if (AppBskyGraphStarterpack.isRecord(pack.record)) { + packsMap.set(pack.uri, pack) } } - return map + return { + posts: postsMap, + starterPacks: packsMap, + } } function toKnownType( @@ -173,7 +219,8 @@ function toKnownType( notif.reason === 'mention' || notif.reason === 'reply' || notif.reason === 'quote' || - notif.reason === 'follow' + notif.reason === 'follow' || + notif.reason === 'starterpack-joined' ) { return notif.reason as NotificationType } |