bugfix:修复notice事件扩散问题 (#2132)

* bugfix:修复notice事件扩散问题

* 优化并发调度

* bugfix:修复签到样式

* bugfix:功能调用统计修复

* bugfix:修复私聊时功能调用统计显示已退群问题

* 提高插件适配兼容性

* 优化发送队列

* 修改权限检查设计

* 继续修改权限检查设计

* 完善权限检查设计

* 优化sqlite配置

* 优化数据库初始化

* 代码整理,无用代码清理

* bugfix:修复启动时数据库校验问题

* bugfix:修复预算裁剪过于激进问题
This commit is contained in:
Copaan
2026-05-28 22:57:28 +08:00
committed by GitHub
parent 12fc5663fb
commit 5596497947
57 changed files with 7557 additions and 1522 deletions
+64 -35
View File
@@ -1,11 +1,12 @@
from datetime import datetime, timedelta
from typing import ClassVar, Literal
from typing import Any, ClassVar, Literal
from typing_extensions import Self
from tortoise import fields
from tortoise.functions import Count
from tortoise.expressions import Q
from zhenxun.services.db_context import Model
from zhenxun.services.db_context.schema_ops import AlterColumnType, RenameColumn
class ChatHistory(Model):
@@ -35,6 +36,41 @@ class ChatHistory(Model):
("user_id", "group_id"),
]
@classmethod
def _platform_from_scope(cls, platform_scope: str | None) -> str | None:
"""Map the new fine-grained scope back to the legacy platform column."""
if not platform_scope:
return None
scope = str(platform_scope).lower()
if scope in {"qq", "qq_client", "qq_api"} or scope.startswith("qq_"):
return "qq"
if "onebot" in scope:
return "qq"
return scope
@classmethod
def scoped_query(cls, platform_scope: str | None = None, **filters: Any):
"""Return a chat-history query compatible with platform_scope callers.
chat_history currently stores the coarse legacy ``platform`` column rather
than a dedicated ``platform_scope`` column, so this method intentionally
stays as a thin compatibility shim.
"""
query = cls.filter(**filters)
if not platform_scope:
return query
if "platform" in filters or any(k.startswith("platform__") for k in filters):
return query
platform = cls._platform_from_scope(platform_scope)
if not platform:
return query
if platform == "qq" and str(platform_scope).lower() in {"qq", "qq_client"}:
return query.filter(
Q(platform=platform) | Q(platform__isnull=True) | Q(platform="")
)
return query.filter(platform=platform)
@classmethod
async def get_group_msg_rank(
cls,
@@ -42,7 +78,7 @@ class ChatHistory(Model):
limit: int = 10,
order: str = "DESC",
date_scope: tuple[datetime, datetime] | None = None,
) -> list[Self]:
) -> list[tuple[str, int]]:
"""获取排行数据
参数:
@@ -51,18 +87,9 @@ class ChatHistory(Model):
order: 排序类型,desc,des
date_scope: 日期范围
"""
o = "-" if order == "DESC" else ""
query = cls.filter(group_id=gid) if gid else cls
if date_scope:
filter_scope = (date_scope[0].isoformat(" "), date_scope[1].isoformat(" "))
query = query.filter(create_time__range=filter_scope)
return list(
await query.annotate(count=Count("user_id"))
.order_by(f"{o}count")
.group_by("user_id")
.limit(limit)
.values_list("user_id", "count")
) # type: ignore
from zhenxun.services.hot_query_cache import get_chat_history_rank_cached
return await get_chat_history_rank_cached(cls, gid, limit, order, date_scope)
@classmethod
async def get_group_first_msg_datetime(
@@ -73,22 +100,21 @@ class ChatHistory(Model):
参数:
group_id: 群组id
"""
if group_id:
message = (
await cls.filter(group_id=group_id).order_by("create_time").first()
)
else:
message = await cls.all().order_by("create_time").first()
return message.create_time if message else None
from zhenxun.services.hot_query_cache import (
get_chat_history_first_msg_datetime_cached,
)
return await get_chat_history_first_msg_datetime_cached(cls, group_id)
@classmethod
async def get_message(
cls,
uid: str,
gid: str,
uid: str | None,
gid: str | None,
type_: Literal["user", "group"],
msg_type: Literal["private", "group"] | None = None,
days: int | tuple[datetime, datetime] | None = None,
platform_scope: str | None = None,
) -> list[Self]:
"""获取消息查询query
@@ -98,15 +124,16 @@ class ChatHistory(Model):
type_: 类型,私聊或群聊
msg_type: 消息类型,用户或群聊
days: 限制日期
platform_scope: 兼容细粒度平台作用域
"""
if type_ == "user":
query = cls.filter(user_id=uid)
query = cls.scoped_query(platform_scope=platform_scope, user_id=uid)
if msg_type == "private":
query = query.filter(group_id__isnull=True)
elif msg_type == "group":
query = query.filter(group_id__not_isnull=True)
else:
query = cls.filter(group_id=gid)
query = cls.scoped_query(platform_scope=platform_scope, group_id=gid)
if uid:
query = query.filter(user_id=uid)
if days:
@@ -128,13 +155,15 @@ class ChatHistory(Model):
# 允许 plain_text 为空
"alter table chat_history alter plain_text drop not null;",
# 将user_id改为user_id
"ALTER TABLE chat_history RENAME COLUMN user_qq TO user_id;",
"ALTER TABLE chat_history "
"ALTER COLUMN user_id TYPE character varying(255);",
"ALTER TABLE chat_history "
"ALTER COLUMN group_id TYPE character varying(255);",
# 添加bot_id字段
"ALTER TABLE chat_history ADD bot_id VARCHAR(255);",
"ALTER TABLE chat_history ALTER COLUMN bot_id TYPE character varying(255);",
"ALTER TABLE chat_history ADD COLUMN platform character varying(255);",
RenameColumn("chat_history", "user_qq", "user_id"),
AlterColumnType(
"chat_history",
"user_id",
{"postgres": "character varying(255)", "mysql": "VARCHAR(255)"},
),
AlterColumnType(
"chat_history",
"group_id",
{"postgres": "character varying(255)", "mysql": "VARCHAR(255)"},
),
]