diff --git a/lib/routes/weibo/user.js b/lib/routes/weibo/user.js index 587df0d96..e4b81988f 100644 --- a/lib/routes/weibo/user.js +++ b/lib/routes/weibo/user.js @@ -1,4 +1,4 @@ -const querystring = require('querystring'); +const querystring = require('query-string'); const got = require('@/utils/got'); const weiboUtils = require('./utils'); const config = require('@/config').value; @@ -62,22 +62,14 @@ module.exports = async (ctx) => { false ); - let earliestDate; - const resultItem = await Promise.all( cards - .reverse() // reverse first to ensure the pinned card will be processed lastly + .filter((item) => item.mblog) .map(async (item) => { - if (!item.mblog && item.card_group && Array.isArray(item.card_group) && item.card_group.length > 0) { - // w/o `mblog`, it may have a `card_group` containing only 1 card - // only get the 1st card because we haven't observed that there can be multiple cards in it - item = item.card_group[0]; - } - - if (!item.mblog) { - return; // drop - } - + // TODO: unify cache key and let weiboUtils.getShowData() handle the cache? It seems safe to do so. + // Need more investigation, pending for now since the current version works fine. + // TODO: getShowData() on demand? The API seems to return most things we need since 2022/05/21. + // Need more investigation, pending for now since the current version works fine. const key = 'weibo:user:' + item.mblog.bid; const data = await ctx.cache.tryGet(key, () => weiboUtils.getShowData(uid, item.mblog.bid)); @@ -92,20 +84,10 @@ module.exports = async (ctx) => { item.mblog.created_at = timezone(item.mblog.created_at, +8); } - // drop too old pinned weibo, otherwise preserve it - if (!item.mblog.title || item.mblog.title.text !== '置顶') { - if (!earliestDate || (item.mblog.created_at && earliestDate > item.mblog.created_at)) { - earliestDate = item.mblog.created_at; - } - } else { - if (earliestDate && item.mblog.created_at && earliestDate > item.mblog.created_at) { - return; - } - } - // 转发的长微博处理 const retweet = item.mblog.retweeted_status; if (retweet && retweet.isLongText) { + // TODO: unify cache key and ... const retweetData = await ctx.cache.tryGet(`weibo:retweeted:${retweet.user.id}:${retweet.bid}`, () => weiboUtils.getShowData(retweet.user.id, retweet.bid)); if (retweetData !== undefined && retweetData.text) { item.mblog.retweeted_status.text = retweetData.text; @@ -153,11 +135,30 @@ module.exports = async (ctx) => { }) ); + // 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(); + } + ctx.state.data = { title: `${name}的微博`, - link: `http://weibo.com/${uid}/`, + link: `https://weibo.com/${uid}/`, description, image: profileImageUrl, - item: resultItem.filter((item) => item).reverse(), + item: resultItem, }; };