fix(route/weibo/user): API upgrade (#9799)

Signed-off-by: Rongrong <i@rong.moe>
This commit is contained in:
Rongrong 2022-05-22 23:02:57 +08:00 committed by GitHub
parent 42cd3c1bd9
commit b5271168f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 27 deletions

View File

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