♻️ 整理代码

This commit is contained in:
suyiiyii 2024-09-08 13:03:47 +08:00
parent a6227828e3
commit 61dcf879ce
4 changed files with 52 additions and 10 deletions

View File

@ -259,7 +259,7 @@ class DBConfig:
)
return res
async def get_cookie(self, platform_name: str = None, target: T_Target = None) -> list[Cookie]:
async def get_cookie(self, platform_name: str = None, target: T_Target = None) -> Sequence[Cookie]:
"""根据平台名和订阅名获取 cookie不会返回匿名cookie"""
async with create_session() as sess:
query = select(Cookie).distinct().where(Cookie.is_universal == False) # noqa: E712
@ -273,7 +273,7 @@ class DBConfig:
res = [cookie for cookie in res if cookie.id in ids]
return res
async def get_unviersal_cookie(self, platform_name: str = None, target: T_Target = None) -> list[Cookie]:
async def get_unviersal_cookie(self, platform_name: str = None) -> Sequence[Cookie]:
async with create_session() as sess:
query = select(Cookie).distinct().where(Cookie.is_universal == True) # noqa: E712
if platform_name:
@ -312,7 +312,7 @@ class DBConfig:
await sess.execute(delete(Cookie).where(Cookie.id == cookie_id))
await sess.commit()
async def get_cookie_by_target(self, target: T_Target, platform_name: str) -> list[Cookie]:
async def get_cookie_by_target(self, target: T_Target, platform_name: str) -> Sequence[Cookie]:
async with create_session() as sess:
query = (
select(Cookie)
@ -320,16 +320,16 @@ class DBConfig:
.join(Target)
.where(Target.platform_name == platform_name, Target.target == target)
)
return list((await sess.scalars(query)).all())
return (await sess.scalars(query)).all()
async def get_universal_cookie(self, platform_name: str) -> list[Cookie]:
async def get_universal_cookie(self, platform_name: str) -> Sequence[Cookie]:
async with create_session() as sess:
query = (
select(Cookie)
.where(Cookie.platform_name == platform_name)
.where(Cookie.is_universal == True) # noqa: E712
)
return list((await sess.scalars(query)).all())
return (await sess.scalars(query)).all()
async def add_cookie_target(self, target: T_Target, platform_name: str, cookie_id: int):
async with create_session() as sess:
@ -358,5 +358,10 @@ class DBConfig:
)
await sess.commit()
async def get_cookie_target(self) -> Sequence[CookieTarget]:
async with create_session() as sess:
query = select(CookieTarget).outerjoin(Target).options(selectinload(CookieTarget.target))
return (await sess.scalars(query)).all()
config = DBConfig()

View File

@ -15,7 +15,9 @@ from .add_sub import do_add_sub
from .del_sub import do_del_sub
from .query_sub import do_query_sub
from .add_cookie import do_add_cookie
from .del_cookie import do_del_cookie
from .add_cookie_target import do_add_cookie_target
from .del_cookie_target import do_del_cookie_target
from .utils import common_platform, admin_permission, gen_handle_cancel, configurable_to_me, set_target_user_info
add_sub_matcher = on_command(
@ -45,23 +47,40 @@ do_del_sub(del_sub_matcher)
add_cookie_matcher = on_command(
"添加cookie",
rule=configurable_to_me,
permission=admin_permission(),
permission=SUPERUSER,
priority=5,
block=True,
)
add_cookie_matcher.handle()(set_target_user_info)
do_add_cookie(add_cookie_matcher)
add_cookie_target_matcher = on_command(
"关联cookie",
rule=configurable_to_me,
permission=admin_permission(),
permission=SUPERUSER,
priority=5,
block=True,
)
add_cookie_target_matcher.handle()(set_target_user_info)
do_add_cookie_target(add_cookie_target_matcher)
del_cookie_target_matcher = on_command(
"取消关联cookie",
rule=configurable_to_me,
permission=SUPERUSER,
priority=5,
block=True,
)
do_del_cookie_target(del_cookie_target_matcher)
del_cookie_matcher = on_command(
"删除cookie",
rule=configurable_to_me,
permission=SUPERUSER,
priority=5,
block=True,
)
do_del_cookie(del_cookie_matcher)
group_manage_matcher = on_command("群管理", rule=to_me(), permission=SUPERUSER, priority=4, block=True)
group_handle_cancel = gen_handle_cancel(group_manage_matcher, "已取消")

View File

@ -0,0 +1,9 @@
from nonebot.matcher import Matcher
from .utils import ensure_user_info, gen_handle_cancel
def do_del_cookie(del_cookie: type[Matcher]):
handle_cancel = gen_handle_cancel(del_cookie, "删除中止")
del_cookie.handle()(ensure_user_info(del_cookie))

View File

@ -0,0 +1,9 @@
from nonebot.matcher import Matcher
from .utils import ensure_user_info, gen_handle_cancel
def do_del_cookie_target(del_cookie_target: type[Matcher]):
handle_cancel = gen_handle_cancel(del_cookie_target, "删除中止")
del_cookie_target.handle()(ensure_user_info(del_cookie_target))