show long read items before quick links

This commit is contained in:
Hongbo Wu
2024-05-27 17:55:19 +08:00
parent f603c84b27
commit b24d8ca413
2 changed files with 40 additions and 45 deletions

View File

@ -221,7 +221,7 @@ export const getJustReadFeedSections = async (
const key = redisKey(userId)
// get feed items from redis sorted set in descending order
// with score greater than minScore
// with score smalled than maxScore
// limit to the first `limit` items
// response is an array of [member1, score1, member2, score2, ...]
const results = await redisClient.zrevrangebyscore(
@ -335,34 +335,29 @@ const mixFeedItems = (rankedFeedItems: Array<Candidate>): Array<Section> => {
}
}
// distribute longer items first and then shorter items
distributeItems(longItems, batches)
// distribute quick link items first
distributeItems(shortItems, batches)
distributeItems(longItems, batches)
// convert batches to sections
const sections = []
for (const batch of batches) {
// create a section for each long item
for (let i = 0; i < 5; i++) {
const section: Section = {
items: [
{
id: batch[i].id,
type: batch[i].type,
},
],
layout: 'long',
}
sections.push(section)
}
// create a section for short items
// create a section for all quick links
sections.push({
items: batch.slice(5).map((item) => ({
items: batch.slice(0, 5).map((item) => ({
id: item.id,
type: item.type,
})),
layout: 'quick links',
})
// create a section for each long item
sections.push(
...batch.slice(5).map((item) => ({
items: [{ id: item.id, type: item.type }],
layout: 'long',
}))
)
}
return sections

View File

@ -23,33 +23,33 @@ export type ScoreApiResponse = Record<string, number> // item_id -> score
export const getScores = async (
data: ScoreApiRequestBody
): Promise<ScoreApiResponse> => {
// const API_URL = 'http://127.0.0.1:5000/predictions'
// const token = process.env.SCORE_API_TOKEN
const API_URL = 'http://127.0.0.1:5000/predictions'
const token = process.env.SCORE_API_TOKEN
// if (!token) {
// throw new Error('No score API token found')
// }
// const response = await fetch(API_URL, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// Authorization: `Bearer ${token}`,
// },
// body: JSON.stringify(data),
// })
// if (!response.ok) {
// throw new Error(`Failed to score candidates: ${response.statusText}`)
// }
// const scores = (await response.json()) as ScoreApiResponse
// return scores
// fake random scores
const scores: ScoreApiResponse = {}
for (const itemId of Object.keys(data.item_features)) {
scores[itemId] = Math.random()
if (!token) {
throw new Error('No score API token found')
}
return Promise.resolve(scores)
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
})
if (!response.ok) {
throw new Error(`Failed to score candidates: ${response.statusText}`)
}
const scores = (await response.json()) as ScoreApiResponse
return scores
// // fake random scores
// const scores: ScoreApiResponse = {}
// for (const itemId of Object.keys(data.item_features)) {
// scores[itemId] = Math.random()
// }
// return Promise.resolve(scores)
}