From db8fcf78be80d38e7d872ad5f6850e81b3476c9f Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 9 Sep 2021 11:44:01 -0500 Subject: [PATCH] fix new freebie bug and first page detection --- lib/apollo.js | 19 +++++++++++++++---- lib/cursor.js | 5 ----- .../20210908193444_tips/migration.sql | 4 ++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/apollo.js b/lib/apollo.js index 9724ea98..6934ae8e 100644 --- a/lib/apollo.js +++ b/lib/apollo.js @@ -1,5 +1,5 @@ import { ApolloClient, InMemoryCache, from, HttpLink } from '@apollo/client' -import { isFirstPage } from './cursor' +import { decodeCursor, LIMIT } from './cursor' import { RetryLink } from '@apollo/client/link/retry' const additiveLink = from([ @@ -7,6 +7,17 @@ const additiveLink = from([ new HttpLink({ uri: '/api/graphql' }) ]) +function isFirstPage (cursor, existing) { + if (cursor) { + const decursor = decodeCursor(cursor) + return decursor.offset === LIMIT + } else { + // we don't have anything cached, or our existing items are less than + // or equal to a full page TODO test for off by one + return !existing || !existing.items || existing.items.length < LIMIT + } +} + export default new ApolloClient({ uri: '/api/graphql', link: additiveLink, @@ -17,7 +28,7 @@ export default new ApolloClient({ moreItems: { keyArgs: ['sort', 'userId'], merge (existing, incoming) { - if (incoming.cursor && isFirstPage(incoming.cursor)) { + if (isFirstPage(incoming.cursor, existing)) { return incoming } @@ -30,7 +41,7 @@ export default new ApolloClient({ moreFlatComments: { keyArgs: ['userId'], merge (existing, incoming) { - if (incoming.cursor && isFirstPage(incoming.cursor)) { + if (isFirstPage(incoming.cursor, existing)) { return incoming } @@ -43,7 +54,7 @@ export default new ApolloClient({ notifications: { keyArgs: false, merge (existing, incoming) { - if (incoming.cursor && isFirstPage(incoming.cursor)) { + if (isFirstPage(incoming.cursor, existing)) { return incoming } diff --git a/lib/cursor.js b/lib/cursor.js index 0d1edb47..d9cdfd63 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -14,8 +14,3 @@ export function nextCursorEncoded (cursor) { cursor.offset += LIMIT return Buffer.from(JSON.stringify(cursor)).toString('base64') } - -export function isFirstPage (cursor) { - const decursor = decodeCursor(cursor) - return decursor.offset === LIMIT -} diff --git a/prisma/migrations/20210908193444_tips/migration.sql b/prisma/migrations/20210908193444_tips/migration.sql index a0cad864..e9aa6626 100644 --- a/prisma/migrations/20210908193444_tips/migration.sql +++ b/prisma/migrations/20210908193444_tips/migration.sql @@ -106,7 +106,7 @@ DECLARE BEGIN PERFORM ASSERT_SERIALIZED(); - SELECT (msats / 1000), id, "freePosts", "freeComments" + SELECT (msats / 1000), "freePosts", "freeComments" INTO user_sats, free_posts, free_comments FROM users WHERE id = user_id; @@ -119,7 +119,7 @@ BEGIN INSERT INTO "Item" (title, url, text, "userId", "parentId", created_at, updated_at) VALUES (title, url, text, user_id, parent_id, now_utc(), now_utc()) RETURNING * INTO item; - IF freebie THEN + IF freebie = true THEN IF parent_id IS NULL THEN UPDATE users SET "freePosts" = "freePosts" - 1 WHERE id = user_id; ELSE