feat: 添加成人内容过滤功能
- 新增用户设置系统支持内容过滤开关 - 扩展类型定义支持成人内容标记 - 实现用户设置API端点(GET/PATCH/PUT) - 增强搜索API支持内容分组和过滤 - 创建AdultContentFilter UI组件 - 添加用户设置页面和认证检查 - 更新配置示例和README文档 - 实现LocalStorage和Redis存储后端 - 默认启用过滤确保安全性
This commit is contained in:
+43
-10
@@ -1,7 +1,8 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import { getAvailableApiSites, getCacheTime } from '@/lib/config';
|
||||
import { getAdultApiSites, 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';
|
||||
@@ -14,11 +15,16 @@ export async function OPTIONS() {
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const query = searchParams.get('q');
|
||||
const includeAdult = searchParams.get('include_adult') === 'true';
|
||||
const userName = searchParams.get('user'); // 用于获取用户设置
|
||||
|
||||
if (!query) {
|
||||
const cacheTime = await getCacheTime();
|
||||
const response = NextResponse.json(
|
||||
{ results: [] },
|
||||
{
|
||||
regular_results: [],
|
||||
adult_results: []
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Cache-Control': `public, max-age=${cacheTime}, s-maxage=${cacheTime}`,
|
||||
@@ -30,16 +36,36 @@ export async function GET(request: Request) {
|
||||
return addCorsHeaders(response);
|
||||
}
|
||||
|
||||
const apiSites = await getAvailableApiSites();
|
||||
const searchPromises = apiSites.map((site) => searchFromApi(site, query));
|
||||
|
||||
try {
|
||||
const results = await Promise.all(searchPromises);
|
||||
const flattenedResults = results.flat();
|
||||
const cacheTime = await getCacheTime();
|
||||
// 获取用户设置以确定是否需要过滤成人内容
|
||||
let shouldFilterAdult = true; // 默认过滤成人内容
|
||||
|
||||
if (userName) {
|
||||
const storage = getStorage();
|
||||
const userSettings = await storage.getUserSettings(userName);
|
||||
shouldFilterAdult = userSettings?.filter_adult_content !== false;
|
||||
}
|
||||
|
||||
// 获取常规资源站
|
||||
const regularSites = await getAvailableApiSites(true); // 总是过滤成人内容
|
||||
const regularSearchPromises = regularSites.map((site) => searchFromApi(site, query));
|
||||
const regularResults = (await Promise.all(regularSearchPromises)).flat();
|
||||
|
||||
let adultResults: unknown[] = [];
|
||||
|
||||
// 如果用户设置允许且明确请求包含成人内容,则搜索成人资源站
|
||||
if (!shouldFilterAdult && includeAdult) {
|
||||
const adultSites = await getAdultApiSites();
|
||||
const adultSearchPromises = adultSites.map((site) => searchFromApi(site, query));
|
||||
adultResults = (await Promise.all(adultSearchPromises)).flat();
|
||||
}
|
||||
|
||||
const cacheTime = await getCacheTime();
|
||||
const response = NextResponse.json(
|
||||
{ results: flattenedResults },
|
||||
{
|
||||
regular_results: regularResults,
|
||||
adult_results: adultResults
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Cache-Control': `public, max-age=${cacheTime}, s-maxage=${cacheTime}`,
|
||||
@@ -50,7 +76,14 @@ export async function GET(request: Request) {
|
||||
);
|
||||
return addCorsHeaders(response);
|
||||
} catch (error) {
|
||||
const response = NextResponse.json({ error: '搜索失败' }, { status: 500 });
|
||||
const response = NextResponse.json(
|
||||
{
|
||||
regular_results: [],
|
||||
adult_results: [],
|
||||
error: '搜索失败'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
return addCorsHeaders(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
import { headers } from 'next/headers';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getStorage } from '@/lib/db';
|
||||
import { UserSettings } from '@/lib/types';
|
||||
|
||||
// 获取用户设置
|
||||
export async function GET(_request: NextRequest) {
|
||||
try {
|
||||
const headersList = headers();
|
||||
const authorization = headersList.get('Authorization');
|
||||
|
||||
if (!authorization) {
|
||||
return NextResponse.json({ error: '未授权访问' }, { status: 401 });
|
||||
}
|
||||
|
||||
const userName = authorization.split(' ')[1]; // 假设格式为 "Bearer username"
|
||||
|
||||
if (!userName) {
|
||||
return NextResponse.json({ error: '用户名不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
const storage = getStorage();
|
||||
const settings = await storage.getUserSettings(userName);
|
||||
|
||||
return NextResponse.json({
|
||||
settings: settings || {
|
||||
filter_adult_content: true, // 默认开启成人内容过滤
|
||||
theme: 'auto',
|
||||
language: 'zh-CN',
|
||||
auto_play: true,
|
||||
video_quality: 'auto'
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('Error getting user settings:', error);
|
||||
return NextResponse.json({ error: '获取用户设置失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// 更新用户设置
|
||||
export async function PATCH(request: NextRequest) {
|
||||
try {
|
||||
const headersList = headers();
|
||||
const authorization = headersList.get('Authorization');
|
||||
|
||||
if (!authorization) {
|
||||
return NextResponse.json({ error: '未授权访问' }, { status: 401 });
|
||||
}
|
||||
|
||||
const userName = authorization.split(' ')[1];
|
||||
|
||||
if (!userName) {
|
||||
return NextResponse.json({ error: '用户名不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { settings } = body as { settings: Partial<UserSettings> };
|
||||
|
||||
if (!settings) {
|
||||
return NextResponse.json({ error: '设置数据不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
const storage = getStorage();
|
||||
|
||||
// 验证用户存在
|
||||
const userExists = await storage.checkUserExist(userName);
|
||||
if (!userExists) {
|
||||
return NextResponse.json({ error: '用户不存在' }, { status: 404 });
|
||||
}
|
||||
|
||||
await storage.updateUserSettings(userName, settings);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '设置更新成功'
|
||||
});
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('Error updating user settings:', error);
|
||||
return NextResponse.json({ error: '更新用户设置失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// 重置用户设置
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const headersList = headers();
|
||||
const authorization = headersList.get('Authorization');
|
||||
|
||||
if (!authorization) {
|
||||
return NextResponse.json({ error: '未授权访问' }, { status: 401 });
|
||||
}
|
||||
|
||||
const userName = authorization.split(' ')[1];
|
||||
|
||||
if (!userName) {
|
||||
return NextResponse.json({ error: '用户名不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { settings } = body as { settings: UserSettings };
|
||||
|
||||
if (!settings) {
|
||||
return NextResponse.json({ error: '设置数据不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
const storage = getStorage();
|
||||
|
||||
// 验证用户存在
|
||||
const userExists = await storage.checkUserExist(userName);
|
||||
if (!userExists) {
|
||||
return NextResponse.json({ error: '用户不存在' }, { status: 404 });
|
||||
}
|
||||
|
||||
await storage.setUserSettings(userName, settings);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '设置已重置'
|
||||
});
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('Error resetting user settings:', error);
|
||||
return NextResponse.json({ error: '重置用户设置失败' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
import { ArrowLeft, Settings, User } from 'lucide-react';
|
||||
|
||||
import AdultContentFilter from '@/components/AdultContentFilter';
|
||||
import { getAuthInfoFromBrowserCookie } from '@/lib/auth';
|
||||
|
||||
export default function UserSettingsPage() {
|
||||
const router = useRouter();
|
||||
const [authInfo, setAuthInfo] = useState<{ userName: string } | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const auth = getAuthInfoFromBrowserCookie();
|
||||
if (!auth || !auth.username) {
|
||||
// 如果用户未登录,重定向到登录页面
|
||||
router.push('/login');
|
||||
return;
|
||||
}
|
||||
setAuthInfo({ userName: auth.username });
|
||||
setIsLoading(false);
|
||||
}, [router]);
|
||||
|
||||
const handleFilterUpdate = (_enabled: boolean) => {
|
||||
// 可以在这里添加一些全局状态更新或通知逻辑
|
||||
// console.log('成人内容过滤状态已更新:', enabled);
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50 dark:bg-gray-900 flex items-center justify-center">
|
||||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!authInfo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
|
||||
<div className="max-w-4xl mx-auto p-6">
|
||||
{/* 页面头部 */}
|
||||
<div className="flex items-center justify-between mb-8">
|
||||
<div className="flex items-center space-x-4">
|
||||
<button
|
||||
onClick={() => router.back()}
|
||||
className="flex items-center justify-center w-10 h-10 rounded-full bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
|
||||
>
|
||||
<ArrowLeft className="w-5 h-5 text-gray-600 dark:text-gray-400" />
|
||||
</button>
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold text-gray-900 dark:text-white flex items-center">
|
||||
<Settings className="w-8 h-8 mr-3 text-blue-600" />
|
||||
用户设置
|
||||
</h1>
|
||||
<p className="text-gray-600 dark:text-gray-400 mt-2">
|
||||
管理您的个人偏好设置和隐私选项
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center space-x-3 px-4 py-2 bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
|
||||
<User className="w-5 h-5 text-gray-600 dark:text-gray-400" />
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
{authInfo.userName}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 设置区域 */}
|
||||
<div className="space-y-6">
|
||||
{/* 内容过滤设置 */}
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold text-gray-900 dark:text-white mb-4">
|
||||
内容过滤
|
||||
</h2>
|
||||
<AdultContentFilter
|
||||
userName={authInfo.userName}
|
||||
onUpdate={handleFilterUpdate}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 其他设置部分预留 */}
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold text-gray-900 dark:text-white mb-4">
|
||||
其他设置
|
||||
</h2>
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6">
|
||||
<p className="text-gray-500 dark:text-gray-400 text-center py-8">
|
||||
更多设置选项即将推出...
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 底部信息 */}
|
||||
<div className="mt-8 text-center text-sm text-gray-500 dark:text-gray-400">
|
||||
<p>设置会自动保存并在所有设备间同步</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user