prevent redoing queries for notifications

This commit is contained in:
keyan 2023-10-22 17:51:07 -05:00
parent f9f13cc752
commit 3160a3f66f
3 changed files with 22 additions and 0 deletions

View File

@ -284,6 +284,14 @@ export default {
const user = await models.user.findUnique({ where: { id: me.id } }) const user = await models.user.findUnique({ where: { id: me.id } })
const lastChecked = user.checkedNotesAt || new Date(0) const lastChecked = user.checkedNotesAt || new Date(0)
// if we've already recorded finding notes after they last checked, return true
// this saves us from rechecking notifications
if (user.foundNotesAt > lastChecked) {
return true
}
const foundNotes = () => models.user.update({ where: { id: me.id }, data: { foundNotesAt: new Date() } }).catch(console.error)
// check if any votes have been cast for them since checkedNotesAt // check if any votes have been cast for them since checkedNotesAt
if (user.noteItemSats) { if (user.noteItemSats) {
const [newSats] = await models.$queryRawUnsafe(` const [newSats] = await models.$queryRawUnsafe(`
@ -297,6 +305,7 @@ export default {
AND "Item"."userId" = $1 AND "Item"."userId" = $1
AND "ItemAct".act = 'TIP')`, me.id, lastChecked) AND "ItemAct".act = 'TIP')`, me.id, lastChecked)
if (newSats.exists) { if (newSats.exists) {
foundNotes()
return true return true
} }
} }
@ -317,6 +326,7 @@ export default {
muteClause(me) muteClause(me)
)})`, me.id, lastChecked) )})`, me.id, lastChecked)
if (newThreadSubReply.exists) { if (newThreadSubReply.exists) {
foundNotes()
return true return true
} }
@ -335,6 +345,7 @@ export default {
await filterClause(me, models), await filterClause(me, models),
muteClause(me))})`, me.id, lastChecked) muteClause(me))})`, me.id, lastChecked)
if (newUserSubs.exists) { if (newUserSubs.exists) {
foundNotes()
return true return true
} }
@ -353,6 +364,7 @@ export default {
muteClause(me) muteClause(me)
)})`, me.id, lastChecked) )})`, me.id, lastChecked)
if (newMentions.exists) { if (newMentions.exists) {
foundNotes()
return true return true
} }
} }
@ -372,6 +384,7 @@ export default {
AND "Item"."userId" <> $1 AND "Item"."userId" <> $1
AND "ItemAct".act = 'TIP')`, me.id, lastChecked) AND "ItemAct".act = 'TIP')`, me.id, lastChecked)
if (newFwdSats.exists) { if (newFwdSats.exists) {
foundNotes()
return true return true
} }
} }
@ -388,6 +401,7 @@ export default {
} }
}) })
if (job && job.statusUpdatedAt > job.createdAt) { if (job && job.statusUpdatedAt > job.createdAt) {
foundNotes()
return true return true
} }
@ -404,6 +418,7 @@ export default {
} }
}) })
if (earn) { if (earn) {
foundNotes()
return true return true
} }
} }
@ -419,6 +434,7 @@ export default {
} }
}) })
if (invoice) { if (invoice) {
foundNotes()
return true return true
} }
} }
@ -432,6 +448,7 @@ export default {
WHERE "Invite"."userId" = $1 WHERE "Invite"."userId" = $1
AND users.created_at > $2)`, me.id, lastChecked) AND users.created_at > $2)`, me.id, lastChecked)
if (newInvites.exists) { if (newInvites.exists) {
foundNotes()
return true return true
} }
@ -444,6 +461,7 @@ export default {
} }
}) })
if (referral) { if (referral) {
foundNotes()
return true return true
} }
} }
@ -459,6 +477,7 @@ export default {
}) })
if (streak) { if (streak) {
foundNotes()
return true return true
} }
} }

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "users" ADD COLUMN "foundNotesAt" TIMESTAMP(3);

View File

@ -24,6 +24,7 @@ model User {
freeComments Int @default(5) freeComments Int @default(5)
freePosts Int @default(2) freePosts Int @default(2)
checkedNotesAt DateTime? checkedNotesAt DateTime?
foundNotesAt DateTime?
pubkey String? @unique(map: "users.pubkey_unique") pubkey String? @unique(map: "users.pubkey_unique")
tipDefault Int @default(100) tipDefault Int @default(100)
bioId Int? bioId Int?