feat: 实现成人内容过滤设置的动态处理,优化搜索API的缓存控制
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import { getCacheTime, getFilteredApiSites } from '@/lib/config';
|
||||
import { getAvailableApiSites,getCacheTime } from '@/lib/config';
|
||||
import { addCorsHeaders, handleOptionsRequest } from '@/lib/cors';
|
||||
import { getStorage } from '@/lib/db';
|
||||
import { searchFromApi } from '@/lib/downstream';
|
||||
|
||||
export const runtime = 'edge';
|
||||
@@ -43,8 +44,30 @@ export async function GET(request: Request) {
|
||||
}
|
||||
|
||||
try {
|
||||
// 使用新的动态过滤方法,根据用户设置自动过滤成人内容源
|
||||
const availableSites = await getFilteredApiSites(userName);
|
||||
// 检查是否明确要求包含成人内容(用于关闭过滤时的明确请求)
|
||||
const includeAdult = searchParams.get('include_adult') === 'true';
|
||||
|
||||
// 获取用户的成人内容过滤设置
|
||||
let shouldFilterAdult = true; // 默认过滤
|
||||
if (userName) {
|
||||
try {
|
||||
const storage = getStorage();
|
||||
const userSettings = await storage.getUserSettings(userName);
|
||||
// 如果用户设置存在且明确设为false,则不过滤;否则默认过滤
|
||||
shouldFilterAdult = userSettings?.filter_adult_content !== false;
|
||||
} catch (error) {
|
||||
// 出错时默认过滤成人内容
|
||||
shouldFilterAdult = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 根据用户设置和明确请求决定最终的过滤策略
|
||||
const finalShouldFilter = shouldFilterAdult || !includeAdult;
|
||||
|
||||
// 使用动态过滤方法,但不依赖缓存,实时获取设置
|
||||
const availableSites = finalShouldFilter
|
||||
? await getAvailableApiSites(true) // 过滤成人内容
|
||||
: await getAvailableApiSites(false); // 不过滤成人内容
|
||||
|
||||
if (!availableSites || availableSites.length === 0) {
|
||||
const cacheTime = await getCacheTime();
|
||||
|
||||
@@ -34,6 +34,12 @@ export async function GET(_request: NextRequest) {
|
||||
auto_play: true,
|
||||
video_quality: 'auto'
|
||||
}
|
||||
}, {
|
||||
headers: {
|
||||
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
||||
'Pragma': 'no-cache',
|
||||
'Expires': '0'
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
@@ -78,6 +84,12 @@ export async function PATCH(request: NextRequest) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '设置更新成功'
|
||||
}, {
|
||||
headers: {
|
||||
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
||||
'Pragma': 'no-cache',
|
||||
'Expires': '0'
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
|
||||
@@ -180,9 +180,16 @@ function SearchPageClient() {
|
||||
}
|
||||
|
||||
// 简化的搜索请求 - 成人内容过滤现在在API层面自动处理
|
||||
// 添加时间戳参数避免缓存问题
|
||||
const timestamp = Date.now();
|
||||
const response = await fetch(
|
||||
`/api/search?q=${encodeURIComponent(query.trim())}`,
|
||||
{ headers }
|
||||
`/api/search?q=${encodeURIComponent(query.trim())}&t=${timestamp}`,
|
||||
{
|
||||
headers: {
|
||||
...headers,
|
||||
'Cache-Control': 'no-cache, no-store, must-revalidate'
|
||||
}
|
||||
}
|
||||
);
|
||||
const data = await response.json();
|
||||
|
||||
|
||||
@@ -68,6 +68,18 @@ const AdultContentFilter: React.FC<AdultContentFilterProps> = ({
|
||||
if (response.ok) {
|
||||
const newState = !isEnabled;
|
||||
setIsEnabled(newState);
|
||||
|
||||
// 强制刷新用户设置缓存 - 向搜索API发送一个空请求来刷新设置
|
||||
try {
|
||||
await fetch('/api/search?q=_cache_refresh_', {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${userName}`,
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
// 忽略刷新缓存的错误
|
||||
}
|
||||
|
||||
onUpdate?.(newState);
|
||||
} else {
|
||||
const errorData = await response.json();
|
||||
|
||||
Reference in New Issue
Block a user