Merge pull request #3998 from omnivore-app/fix/home-feed

fix/home API
This commit is contained in:
Hongbo Wu
2024-05-28 17:18:00 +08:00
committed by GitHub
2 changed files with 14 additions and 7 deletions

View File

@ -209,7 +209,7 @@ const rankCandidates = async (
return candidates
}
const redisKey = (userId: string) => `just-read-feed:${userId}`
const redisKey = (userId: string) => `home:${userId}`
const MAX_FEED_ITEMS = 500
export const getHomeSections = async (
@ -251,7 +251,7 @@ export const getHomeSections = async (
const appendSectionsToHome = async (
userId: string,
sections: Array<Section>,
cursor = Date.now()
cursor?: number
) => {
const redisClient = redisDataSource.redisClient
if (!redisClient) {
@ -263,11 +263,14 @@ const appendSectionsToHome = async (
// store candidates in redis sorted set
const pipeline = redisClient.pipeline()
const offset = sections.length + 86_400_000
cursor = cursor - offset
// sections expire in 24 hours
const ttl = 86_400_000
const batchSize = sections.length
const savedAt = cursor ? cursor - batchSize - ttl : Date.now()
const scoreMembers = sections.flatMap((section, index) => [
cursor + index + 86_400_000, // sections expire in 24 hours
savedAt + index + ttl, // score for the section is the savedAt + index + ttl
JSON.stringify(section),
])

View File

@ -56,6 +56,8 @@ export const homeResolver = authorized<
}
}
const endCursor = sections[sections.length - 1].score.toString()
const edges = sections.map((section) => ({
cursor: section.score.toString(),
node: section.member,
@ -64,8 +66,10 @@ export const homeResolver = authorized<
return {
edges,
pageInfo: {
hasPreviousPage: true, // there is always a previous page for new items
hasNextPage: true, // there is always a next page for old items
startCursor: after,
endCursor,
hasPreviousPage: true, // there is always a previous page for newer items
hasNextPage: true, // there is always a next page for older items
},
}
})