diff --git a/api/resolvers/notifications.js b/api/resolvers/notifications.js index a6e000e2..9f2f3cb7 100644 --- a/api/resolvers/notifications.js +++ b/api/resolvers/notifications.js @@ -159,34 +159,44 @@ export default { LIMIT ${LIMIT}+$3)` ) } + + if (meFull.noteEarning) { + queries.push( + `SELECT id::text, created_at AS "sortTime", FLOOR(msats / 1000) as "earnedSats", + 'Earn' AS type + FROM "Earn" + WHERE "userId" = $1 + AND created_at <= $2` + ) + } } + // we do all this crazy subquery stuff to make 'reward' islands const notifications = await models.$queryRaw( - `${queries.join(' UNION ALL ')} + `SELECT MAX(id) AS id, MAX("sortTime") AS "sortTime", sum("earnedSats") AS "earnedSats", type + FROM + (SELECT *, + CASE + WHEN type = 'Earn' THEN + ROW_NUMBER() OVER(ORDER BY "sortTime" DESC) - + ROW_NUMBER() OVER(PARTITION BY type = 'Earn' ORDER BY "sortTime" DESC) + ELSE + ROW_NUMBER() OVER(ORDER BY "sortTime" DESC) + END as island + FROM + (${queries.join(' UNION ALL ')}) u + ) sub + GROUP BY type, island ORDER BY "sortTime" DESC OFFSET $3 LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset) - let earn if (decodedCursor.offset === 0) { - if (meFull.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, meFull.checkedNotesAt) - if (earnings.length > 0 && earnings[0].earnedSats > 0) { - earn = earnings[0] - } - } - await models.user.update({ where: { id: me.id }, data: { checkedNotesAt: new Date() } }) } return { lastChecked: meFull.checkedNotesAt, - earn, cursor: notifications.length === LIMIT ? nextCursorEncoded(decodedCursor) : null, notifications } diff --git a/api/typeDefs/notifications.js b/api/typeDefs/notifications.js index 8ffeeee2..89261359 100644 --- a/api/typeDefs/notifications.js +++ b/api/typeDefs/notifications.js @@ -48,7 +48,6 @@ export default gql` type Notifications { lastChecked: String - earn: Notification cursor: String notifications: [Notification!]! } diff --git a/fragments/notifications.js b/fragments/notifications.js index 81efda40..2a11d3a6 100644 --- a/fragments/notifications.js +++ b/fragments/notifications.js @@ -10,13 +10,6 @@ export const NOTIFICATIONS = gql` notifications(cursor: $cursor, inc: $inc) { cursor lastChecked - earn { - __typename - ... on Earn { - sortTime - earnedSats - } - } notifications { __typename ... on Mention { @@ -35,6 +28,10 @@ export const NOTIFICATIONS = gql` text } } + ... on Earn { + sortTime + earnedSats + } ... on Reply { sortTime item {