fix(route)(weibo/user): multiple pinned weibo (#9967)

Signed-off-by: Rongrong <i@rong.moe>
This commit is contained in:
Rongrong 2022-06-17 00:27:34 +08:00 committed by GitHub
parent 3cfad37145
commit 46be6f6199
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 19 deletions

View File

@ -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,
};
};