更新检测改为后台定时执行

This commit is contained in:
mtvpls
2025-12-22 22:44:21 +08:00
parent 6f05f91a80
commit 02a76f83ba
2 changed files with 23 additions and 51 deletions

View File

@@ -1,10 +1,9 @@
/* eslint-disable no-console,@typescript-eslint/no-explicit-any */
import * as crypto from 'crypto';
import { NextRequest, NextResponse } from 'next/server';
import { getConfig, refineConfig } from '@/lib/config';
import { db } from '@/lib/db';
import { db, getStorage } from '@/lib/db';
import { fetchVideoDetail } from '@/lib/fetchVideoDetail';
import { refreshLiveChannels } from '@/lib/live';
import { SearchResult } from '@/lib/types';
@@ -156,6 +155,7 @@ async function refreshRecordAndFavorites() {
for (const user of users) {
console.log(`开始处理用户: ${user}`);
const storage = getStorage();
// 播放记录
try {
@@ -216,6 +216,7 @@ async function refreshRecordAndFavorites() {
);
const totalFavorites = Object.keys(favorites).length;
let processedFavorites = 0;
const now = Date.now();
for (const [key, fav] of Object.entries(favorites)) {
try {
@@ -245,6 +246,26 @@ async function refreshRecordAndFavorites() {
console.log(
`更新收藏: ${fav.title} (${fav.total_episodes} -> ${favEpisodeCount})`
);
// 创建通知
const notification = {
id: `fav_update_${source}_${id}_${now}`,
type: 'favorite_update' as const,
title: '收藏更新',
message: `${fav.title}》有新集数更新!从 ${fav.total_episodes} 集更新到 ${favEpisodeCount}`,
timestamp: now,
read: false,
metadata: {
source,
id,
title: fav.title,
old_episodes: fav.total_episodes,
new_episodes: favEpisodeCount,
},
};
await storage.addNotification(user, notification);
console.log(`已为用户 ${user} 创建收藏更新通知: ${fav.title}`);
}
processedFavorites++;

View File

@@ -57,55 +57,6 @@ function HomeClient() {
}
}, [announcement]);
// 首次进入时检查收藏更新(带前端冷却检查)
useEffect(() => {
const checkFavoriteUpdates = async () => {
try {
// 检查冷却时间(前端 localStorage
const COOLDOWN_TIME = 30 * 60 * 1000; // 30分钟
const lastCheckTime = localStorage.getItem('lastFavoriteCheckTime');
const now = Date.now();
if (lastCheckTime) {
const timeSinceLastCheck = now - parseInt(lastCheckTime, 10);
if (timeSinceLastCheck < COOLDOWN_TIME) {
const remainingMinutes = Math.ceil((COOLDOWN_TIME - timeSinceLastCheck) / 1000 / 60);
console.log(`收藏更新检查冷却中,还需等待 ${remainingMinutes} 分钟`);
return;
}
}
console.log('开始检查收藏更新...');
const response = await fetch('/api/favorites/check-updates', {
method: 'POST',
});
if (response.ok) {
// 更新本地检查时间
localStorage.setItem('lastFavoriteCheckTime', now.toString());
const data = await response.json();
if (data.updates && data.updates.length > 0) {
console.log(`发现 ${data.updates.length} 个收藏更新`);
// 触发通知更新事件
window.dispatchEvent(new Event('notificationsUpdated'));
} else {
console.log('没有收藏更新');
}
}
} catch (error) {
console.error('检查收藏更新失败:', error);
}
};
// 延迟3秒后检查避免影响首页加载
const timer = setTimeout(() => {
checkFavoriteUpdates();
}, 3000);
return () => clearTimeout(timer);
}, []);
// 收藏夹数据
type FavoriteItem = {
id: string;