only one earn notification at a time

This commit is contained in:
keyan 2022-04-24 11:13:07 -05:00
parent 2608cbc326
commit cef122141f
5 changed files with 31 additions and 20 deletions

View File

@ -116,19 +116,6 @@ export default {
)
}
if (me.noteEarning) {
queries.push(
`(SELECT "Earn".id::text, "Earn".created_at AS "sortTime", FLOOR(msats / 1000) as "earnedSats",
'Earn' AS type
FROM "Earn"
WHERE "Earn"."userId" = $1
AND FLOOR(msats / 1000) > 0
AND created_at <= $2
ORDER BY "sortTime" DESC
LIMIT ${LIMIT}+$3)`
)
}
if (me.noteMentions) {
queries.push(
`(SELECT "Item".id::TEXT, "Mention".created_at AS "sortTime", NULL as "earnedSats",
@ -179,12 +166,28 @@ export default {
LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset)
const { checkedNotesAt } = await models.user.findUnique({ where: { id: me.id } })
let earn
if (decodedCursor.offset === 0) {
if (me.noteEarning) {
const earnings = await models.$queryRaw(
`SELECT MAX("Earn".id)::text, MAX("Earn".created_at) AS "sortTime", FLOOR(SUM(msats) / 1000) as "earnedSats",
'Earn' AS type
FROM "Earn"
WHERE "Earn"."userId" = $1
AND created_at >= $2`, me.id, checkedNotesAt)
if (earnings.length > 0 && earnings[0].earnedSats > 0) {
earn = earnings[0]
}
}
await models.user.update({ where: { id: me.id }, data: { checkedNotesAt: new Date() } })
}
console.log(decodedCursor)
return {
lastChecked: checkedNotesAt,
earn,
cursor: notifications.length === LIMIT ? nextCursorEncoded(decodedCursor) : null,
notifications
}

View File

@ -44,10 +44,11 @@ export default gql`
}
union Notification = Reply | Votification | Mention
| Invitification | JobChanged | Earn | InvoicePaid
| Invitification | Earn | JobChanged | InvoicePaid
type Notifications {
lastChecked: String
earn: Notification
cursor: String
notifications: [Notification!]!
}

View File

@ -110,11 +110,13 @@ function Notification ({ n }) {
)
}
export default function Notifications ({ notifications, cursor, lastChecked, variables }) {
export default function Notifications ({ notifications, earn, cursor, lastChecked, variables }) {
const { data, fetchMore } = useQuery(NOTIFICATIONS, { variables })
console.log(data)
if (data) {
({ notifications: { notifications, cursor } } = data)
({ notifications: { notifications, earn, cursor } } = data)
}
const [fresh, old] =
@ -128,6 +130,7 @@ export default function Notifications ({ notifications, cursor, lastChecked, var
<>
{/* XXX we shouldn't use the index but we don't have a unique id in this union yet */}
<div className='fresh'>
{earn && <Notification n={earn} key='earn' />}
{fresh.map((n, i) => (
<Notification n={n} key={i} />
))}

View File

@ -10,6 +10,13 @@ export const NOTIFICATIONS = gql`
notifications(cursor: $cursor, inc: $inc) {
cursor
lastChecked
earn {
__typename
... on Earn {
sortTime
earnedSats
}
}
notifications {
__typename
... on Mention {
@ -47,10 +54,6 @@ export const NOTIFICATIONS = gql`
...ItemFields
}
}
... on Earn {
sortTime
earnedSats
}
... on InvoicePaid {
sortTime
earnedSats

View File

@ -87,6 +87,7 @@ export default function getApolloClient () {
return {
cursor: incoming.cursor,
earn: existing?.earn || incoming.earn,
notifications: [...(existing?.notifications || []), ...incoming.notifications],
lastChecked: incoming.lastChecked
}