fix: TweetDB update too late

This commit is contained in:
xtaodada 2024-01-22 13:57:27 +08:00
parent 7765f33700
commit 961b6abb40
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 18 additions and 5 deletions

View File

@ -26,6 +26,12 @@ class TweetDB:
tweets.append(tid) tweets.append(tid)
db[f"{TweetDB.prefix}{username}"] = tweets db[f"{TweetDB.prefix}{username}"] = tweets
@staticmethod
def remove(username: str, tid: int) -> None:
tweets = TweetDB.get_all(username)
tweets.remove(tid)
db[f"{TweetDB.prefix}{username}"] = tweets
class UserDB: class UserDB:
@staticmethod @staticmethod

View File

@ -95,10 +95,7 @@ async def send_username_changed(user: str):
async def send_check(user_data: User) -> bool: async def send_check(user_data: User) -> bool:
need_send_tweets = [ need_send_tweets = user_data.tweets
tweet for tweet in user_data.tweets
if not TweetDB.check_id(user_data.username, tweet.id)
]
if len(need_send_tweets): if len(need_send_tweets):
logs.info(f"{user_data.name} (@{user_data.username}) 需要推送 {len(need_send_tweets)} 条推文") logs.info(f"{user_data.name} (@{user_data.username}) 需要推送 {len(need_send_tweets)} 条推文")
for tweet in need_send_tweets: for tweet in need_send_tweets:
@ -106,14 +103,24 @@ async def send_check(user_data: User) -> bool:
await send_to_user(user_data, tweet) await send_to_user(user_data, tweet)
except Exception: except Exception:
logs.error(f"推送 {user_data.name} 的推文 {tweet.id} 失败") logs.error(f"推送 {user_data.name} 的推文 {tweet.id} 失败")
TweetDB.add(user_data.username, tweet.id) TweetDB.remove(user_data.username, tweet.id)
return len(need_send_tweets) > 0 return len(need_send_tweets) > 0
def filter_tweets(username: str, data: List[Tweet]) -> List[Tweet]:
tweets = []
for tweet in data:
if not TweetDB.check_id(username, tweet.id):
TweetDB.add(username, tweet.id)
tweets.append(tweet)
return tweets
async def async_get_user(user_data: Dict, username: str) -> None: async def async_get_user(user_data: Dict, username: str) -> None:
try: try:
data = await get_user(username) data = await get_user(username)
if data: if data:
data.tweets = filter_tweets(username, data.tweets)
user_data[username] = data user_data[username] = data
else: else:
user_data[username] = None user_data[username] = None