From 46be6f6199b0da48d11af76600a0289d7dd8f5bc Mon Sep 17 00:00:00 2001 From: Rongrong Date: Fri, 17 Jun 2022 00:27:34 +0800 Subject: [PATCH] fix(route)(weibo/user): multiple pinned weibo (#9967) Signed-off-by: Rongrong --- lib/routes/weibo/user.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/routes/weibo/user.js b/lib/routes/weibo/user.js index e4b81988f..f1b0de2ca 100644 --- a/lib/routes/weibo/user.js +++ b/lib/routes/weibo/user.js @@ -62,7 +62,7 @@ module.exports = async (ctx) => { false ); - const resultItem = await Promise.all( + let resultItems = await Promise.all( cards .filter((item) => item.mblog) .map(async (item) => { @@ -131,27 +131,18 @@ module.exports = async (ctx) => { link, pubDate, author: item.mblog.user.screen_name, + isPinned: item.profile_type_id?.startsWith('proweibotop'), }; }) ); - // remove pinned weibo if it is too old (older than all rest weibo). - // if pinned weibo exists, it will always be the first item and the total item count will be 11 (otherwise 10). - // if < 11, either there is no pinned weibo or the user sent < 11 weibo in total, no need to remove anything. - // there is still another characteristic of a pinned weibo: card.profile_type_id.startsWith('proweibotop'), - // but it can be changed in the future, so here we do not rely on it. - if ( - resultItem.length >= 11 && - resultItem[0].pubDate && - resultItem[0].pubDate < - Math.min( - ...resultItem - .slice(1) - .map((item) => item.pubDate) - .filter((item) => item) - ) - ) { - resultItem.shift(); + // remove pinned weibo if they are too old (older than all the rest weibo) + // the character of pinned weibo is `card.profile_type_id.startsWith('proweibotop')` + // there can be 1 or 2 (WHAT A FANTASTIC BRAIN THE PM HAS?) pinned weibo at the same time + const pinnedItems = resultItems.filter((item) => item.isPinned); + const ordinaryItems = resultItems.filter((item) => !item.isPinned); + if (pinnedItems.length > 0 && ordinaryItems.length > 0 && Math.max(...pinnedItems.map((i) => i.pubDate).filter(Boolean)) < Math.min(...ordinaryItems.map((i) => i.pubDate).filter(Boolean))) { + resultItems = ordinaryItems; } ctx.state.data = { @@ -159,6 +150,6 @@ module.exports = async (ctx) => { link: `https://weibo.com/${uid}/`, description, image: profileImageUrl, - item: resultItem, + item: resultItems, }; };