Skip to content

Commit

Permalink
change to notifications v2
Browse files Browse the repository at this point in the history
  • Loading branch information
backmeupplz committed Oct 27, 2024
1 parent 82612fa commit e125117
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/helpers/fetchNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { NeynarAPIClient } from '@neynar/nodejs-sdk'

export default function (fid: number, cursor: string, apiKey: string) {
const client = new NeynarAPIClient(apiKey)
return client.fetchMentionAndReplyNotifications(fid, {
return client.fetchAllNotifications(fid, {
cursor,
isPriority: false,
priorityMode: false,
})
}
19 changes: 9 additions & 10 deletions src/helpers/startPolling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CastWithInteractions } from '@neynar/nodejs-sdk/build/neynar-api/v1'
import { Notification } from '@neynar/nodejs-sdk/build/neynar-api/v2'
import {
getCurrentNotificationId,
setCurrentNotificationId,
Expand All @@ -9,7 +9,7 @@ let polling = false
async function pollNotifications(
fid: number,
apiKey: string,
handler: (notification: CastWithInteractions) => void,
handler: (notification: Notification) => void,
debug = false
) {
if (polling) return
Expand All @@ -19,11 +19,10 @@ async function pollNotifications(
const currentNotificationId = await getCurrentNotificationId()
let currentNotificationIdInSet = true
let cursor = ''
const notifications = [] as CastWithInteractions[]
const notifications = [] as Notification[]
do {
const {
result: { notifications: newNotifications, next },
} = await fetchNotifications(fid, cursor, apiKey)
const { notifications: newNotifications, next } =
await fetchNotifications(fid, cursor, apiKey)
if (debug) {
console.log('New notifications:', newNotifications)
console.log('Next:', next)
Expand All @@ -32,12 +31,12 @@ async function pollNotifications(
if (currentNotificationId) {
cursor = next?.cursor || ''
currentNotificationIdInSet = notifications.some(
(n) => n.hash === currentNotificationId
(n) => n.cast?.hash && n.cast.hash === currentNotificationId
)
}
} while (!!currentNotificationId && !currentNotificationIdInSet && !!cursor)
if (notifications[0]?.hash) {
await setCurrentNotificationId(notifications[0].hash)
if (notifications[0]?.cast?.hash) {
await setCurrentNotificationId(notifications[0].cast.hash)
}
for (const notification of notifications) {
await handler(notification)
Expand All @@ -53,7 +52,7 @@ async function pollNotifications(
export function startPolling(
fid: number,
apiKey: string,
handler: (notification: CastWithInteractions) => void,
handler: (notification: Notification) => void,
debug = false
) {
void pollNotifications(fid, apiKey, handler, debug)
Expand Down

0 comments on commit e125117

Please sign in to comment.