mirror of https://github.com/Mai-with-u/MaiBot.git
Merge cabc8fba57 into f44598a331
commit
1ff7b11c87
|
|
@ -2,12 +2,13 @@
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from typing import Optional, List, Annotated
|
from typing import Optional, List, Annotated
|
||||||
from fastapi import APIRouter, HTTPException, Query
|
from fastapi import APIRouter, HTTPException, Query, Header , Cookie
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from peewee import fn
|
from peewee import fn
|
||||||
|
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
from src.common.database.database_model import Jargon, ChatStreams
|
from src.common.database.database_model import Jargon, ChatStreams
|
||||||
|
from src.webui.core import get_token_manager, verify_auth_token_from_cookie_or_header
|
||||||
|
|
||||||
logger = get_logger("webui.jargon")
|
logger = get_logger("webui.jargon")
|
||||||
|
|
||||||
|
|
@ -177,6 +178,12 @@ class ChatListResponse(BaseModel):
|
||||||
success: bool = True
|
success: bool = True
|
||||||
data: List[ChatInfoResponse]
|
data: List[ChatInfoResponse]
|
||||||
|
|
||||||
|
def verify_auth_token(
|
||||||
|
maibot_session: Optional[str] = None,
|
||||||
|
authorization: Optional[str] = None,
|
||||||
|
) -> bool:
|
||||||
|
"""验证认证 Token,支持 Cookie 和 Header"""
|
||||||
|
return verify_auth_token_from_cookie_or_header(maibot_session, authorization)
|
||||||
|
|
||||||
# ==================== 工具函数 ====================
|
# ==================== 工具函数 ====================
|
||||||
|
|
||||||
|
|
@ -216,9 +223,13 @@ async def get_jargon_list(
|
||||||
chat_id: Optional[str] = Query(None, description="按聊天ID筛选"),
|
chat_id: Optional[str] = Query(None, description="按聊天ID筛选"),
|
||||||
is_jargon: Optional[bool] = Query(None, description="按是否是黑话筛选"),
|
is_jargon: Optional[bool] = Query(None, description="按是否是黑话筛选"),
|
||||||
is_global: Optional[bool] = Query(None, description="按是否全局筛选"),
|
is_global: Optional[bool] = Query(None, description="按是否全局筛选"),
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
):
|
):
|
||||||
"""获取黑话列表"""
|
"""获取黑话列表"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
# 构建查询
|
# 构建查询
|
||||||
query = Jargon.select()
|
query = Jargon.select()
|
||||||
|
|
||||||
|
|
@ -273,9 +284,14 @@ async def get_jargon_list(
|
||||||
|
|
||||||
|
|
||||||
@router.get("/chats", response_model=ChatListResponse)
|
@router.get("/chats", response_model=ChatListResponse)
|
||||||
async def get_chat_list():
|
async def get_chat_list(
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
|
):
|
||||||
"""获取所有有黑话记录的聊天列表"""
|
"""获取所有有黑话记录的聊天列表"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
# 获取所有不同的 chat_id
|
# 获取所有不同的 chat_id
|
||||||
chat_ids = Jargon.select(Jargon.chat_id).distinct().where(Jargon.chat_id.is_null(False))
|
chat_ids = Jargon.select(Jargon.chat_id).distinct().where(Jargon.chat_id.is_null(False))
|
||||||
|
|
||||||
|
|
@ -320,9 +336,14 @@ async def get_chat_list():
|
||||||
|
|
||||||
|
|
||||||
@router.get("/stats/summary", response_model=JargonStatsResponse)
|
@router.get("/stats/summary", response_model=JargonStatsResponse)
|
||||||
async def get_jargon_stats():
|
async def get_jargon_stats(
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
|
):
|
||||||
"""获取黑话统计数据"""
|
"""获取黑话统计数据"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
# 总数量
|
# 总数量
|
||||||
total = Jargon.select().count()
|
total = Jargon.select().count()
|
||||||
|
|
||||||
|
|
@ -373,9 +394,15 @@ async def get_jargon_stats():
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{jargon_id}", response_model=JargonDetailResponse)
|
@router.get("/{jargon_id}", response_model=JargonDetailResponse)
|
||||||
async def get_jargon_detail(jargon_id: int):
|
async def get_jargon_detail(
|
||||||
|
jargon_id: int,
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
|
):
|
||||||
"""获取黑话详情"""
|
"""获取黑话详情"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
jargon = Jargon.get_or_none(Jargon.id == jargon_id)
|
jargon = Jargon.get_or_none(Jargon.id == jargon_id)
|
||||||
if not jargon:
|
if not jargon:
|
||||||
raise HTTPException(status_code=404, detail="黑话不存在")
|
raise HTTPException(status_code=404, detail="黑话不存在")
|
||||||
|
|
@ -390,9 +417,15 @@ async def get_jargon_detail(jargon_id: int):
|
||||||
|
|
||||||
|
|
||||||
@router.post("/", response_model=JargonCreateResponse)
|
@router.post("/", response_model=JargonCreateResponse)
|
||||||
async def create_jargon(request: JargonCreateRequest):
|
async def create_jargon(
|
||||||
|
request: JargonCreateRequest,
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
|
):
|
||||||
"""创建黑话"""
|
"""创建黑话"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
# 检查是否已存在相同内容的黑话
|
# 检查是否已存在相同内容的黑话
|
||||||
existing = Jargon.get_or_none((Jargon.content == request.content) & (Jargon.chat_id == request.chat_id))
|
existing = Jargon.get_or_none((Jargon.content == request.content) & (Jargon.chat_id == request.chat_id))
|
||||||
if existing:
|
if existing:
|
||||||
|
|
@ -426,9 +459,16 @@ async def create_jargon(request: JargonCreateRequest):
|
||||||
|
|
||||||
|
|
||||||
@router.patch("/{jargon_id}", response_model=JargonUpdateResponse)
|
@router.patch("/{jargon_id}", response_model=JargonUpdateResponse)
|
||||||
async def update_jargon(jargon_id: int, request: JargonUpdateRequest):
|
async def update_jargon(
|
||||||
|
jargon_id: int,
|
||||||
|
request: JargonUpdateRequest,
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
|
):
|
||||||
"""更新黑话(增量更新)"""
|
"""更新黑话(增量更新)"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
jargon = Jargon.get_or_none(Jargon.id == jargon_id)
|
jargon = Jargon.get_or_none(Jargon.id == jargon_id)
|
||||||
if not jargon:
|
if not jargon:
|
||||||
raise HTTPException(status_code=404, detail="黑话不存在")
|
raise HTTPException(status_code=404, detail="黑话不存在")
|
||||||
|
|
@ -457,9 +497,14 @@ async def update_jargon(jargon_id: int, request: JargonUpdateRequest):
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{jargon_id}", response_model=JargonDeleteResponse)
|
@router.delete("/{jargon_id}", response_model=JargonDeleteResponse)
|
||||||
async def delete_jargon(jargon_id: int):
|
async def delete_jargon(jargon_id: int,
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
|
):
|
||||||
"""删除黑话"""
|
"""删除黑话"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
jargon = Jargon.get_or_none(Jargon.id == jargon_id)
|
jargon = Jargon.get_or_none(Jargon.id == jargon_id)
|
||||||
if not jargon:
|
if not jargon:
|
||||||
raise HTTPException(status_code=404, detail="黑话不存在")
|
raise HTTPException(status_code=404, detail="黑话不存在")
|
||||||
|
|
@ -483,9 +528,15 @@ async def delete_jargon(jargon_id: int):
|
||||||
|
|
||||||
|
|
||||||
@router.post("/batch/delete", response_model=JargonDeleteResponse)
|
@router.post("/batch/delete", response_model=JargonDeleteResponse)
|
||||||
async def batch_delete_jargons(request: BatchDeleteRequest):
|
async def batch_delete_jargons(
|
||||||
|
request: BatchDeleteRequest,
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
|
):
|
||||||
"""批量删除黑话"""
|
"""批量删除黑话"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
if not request.ids:
|
if not request.ids:
|
||||||
raise HTTPException(status_code=400, detail="ID列表不能为空")
|
raise HTTPException(status_code=400, detail="ID列表不能为空")
|
||||||
|
|
||||||
|
|
@ -510,9 +561,13 @@ async def batch_delete_jargons(request: BatchDeleteRequest):
|
||||||
async def batch_set_jargon_status(
|
async def batch_set_jargon_status(
|
||||||
ids: Annotated[List[int], Query(description="黑话ID列表")],
|
ids: Annotated[List[int], Query(description="黑话ID列表")],
|
||||||
is_jargon: Annotated[bool, Query(description="是否是黑话")],
|
is_jargon: Annotated[bool, Query(description="是否是黑话")],
|
||||||
|
maibot_session: Optional[str] = Cookie(None),
|
||||||
|
authorization: Optional[str] = Header(None),
|
||||||
):
|
):
|
||||||
"""批量设置黑话状态"""
|
"""批量设置黑话状态"""
|
||||||
try:
|
try:
|
||||||
|
verify_auth_token(maibot_session, authorization)
|
||||||
|
|
||||||
if not ids:
|
if not ids:
|
||||||
raise HTTPException(status_code=400, detail="ID列表不能为空")
|
raise HTTPException(status_code=400, detail="ID列表不能为空")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue