🐛 Fix Account Info Not Completely Remove from Database

This commit is contained in:
洛水居室 2022-12-25 15:56:52 +08:00
parent 0218ac7648
commit a8d1fe19ff
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
2 changed files with 17 additions and 7 deletions

View File

@ -1,6 +1,7 @@
from typing import cast, List from typing import cast, List
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.exc import NoResultFound
from sqlmodel.ext.asyncio.session import AsyncSession from sqlmodel.ext.asyncio.session import AsyncSession
from core.base.mysql import MySQL from core.base.mysql import MySQL
@ -100,9 +101,9 @@ class CookiesRepository:
else: else:
raise RegionNotFoundError(region.name) raise RegionNotFoundError(region.name)
results = await session.execute(statement) results = await session.execute(statement)
db_cookies = results.unique().scalar_one() try:
if db_cookies: db_cookies = results.unique().scalar_one()
await session.delete(db_cookies) except NoResultFound as exc:
await session.commit() raise CookiesNotFoundError(user_id) from exc
else: await session.delete(db_cookies)
raise CookiesNotFoundError(user_id) await session.commit()

View File

@ -8,6 +8,7 @@ from core.cookies import CookiesService
from core.cookies.error import CookiesNotFoundError from core.cookies.error import CookiesNotFoundError
from core.cookies.models import Cookies from core.cookies.models import Cookies
from core.plugin import Plugin, handler, conversation from core.plugin import Plugin, handler, conversation
from core.sign import SignServices
from core.user import UserService from core.user import UserService
from core.user.error import UserNotFoundError from core.user.error import UserNotFoundError
from utils.decorators.error import error_callable from utils.decorators.error import error_callable
@ -30,9 +31,11 @@ class UserPlugin(Plugin.Conversation, BasePlugin.Conversation):
self, self,
user_service: UserService = None, user_service: UserService = None,
cookies_service: CookiesService = None, cookies_service: CookiesService = None,
sign_service: SignServices = None,
): ):
self.cookies_service = cookies_service self.cookies_service = cookies_service
self.user_service = user_service self.user_service = user_service
self.sign_service = sign_service
@conversation.entry_point @conversation.entry_point
@handler.command(command="deluser", filters=filters.ChatType.PRIVATE, block=True) @handler.command(command="deluser", filters=filters.ChatType.PRIVATE, block=True)
@ -62,9 +65,11 @@ class UserPlugin(Plugin.Conversation, BasePlugin.Conversation):
if user_info.region == RegionEnum.HYPERION: if user_info.region == RegionEnum.HYPERION:
uid = user_info.yuanshen_uid uid = user_info.yuanshen_uid
region_str = "米游社" region_str = "米游社"
del_user_command_data.region = RegionEnum.HYPERION
elif user_info.region == RegionEnum.HOYOLAB: elif user_info.region == RegionEnum.HOYOLAB:
uid = user_info.genshin_uid uid = user_info.genshin_uid
region_str = "HoYoLab" region_str = "HoYoLab"
del_user_command_data.region = RegionEnum.HOYOLAB
else: else:
await message.reply_text("数据非法") await message.reply_text("数据非法")
return ConversationHandler.END return ConversationHandler.END
@ -90,6 +95,10 @@ class UserPlugin(Plugin.Conversation, BasePlugin.Conversation):
return ConversationHandler.END return ConversationHandler.END
elif message.text == "确认": elif message.text == "确认":
del_user_command_data: DelUserCommandData = context.chat_data.get("del_user_command_data") del_user_command_data: DelUserCommandData = context.chat_data.get("del_user_command_data")
sign = await self.sign_service.get_by_user_id(user.id)
if sign:
await self.sign_service.remove(sign)
logger.success("用户 %s[%s] 从数据库删除定时签到成功", user.full_name, user.id)
try: try:
await self.user_service.del_user_by_id(user.id) await self.user_service.del_user_by_id(user.id)
except UserNotFoundError: except UserNotFoundError:
@ -102,7 +111,7 @@ class UserPlugin(Plugin.Conversation, BasePlugin.Conversation):
try: try:
await self.cookies_service.del_cookies(user.id, del_user_command_data.region) await self.cookies_service.del_cookies(user.id, del_user_command_data.region)
except CookiesNotFoundError: except CookiesNotFoundError:
logger.info("用户 %s[%s] Cookies 不存在", user.full_name, user.id) logger.warning("用户 %s[%s] Cookies 不存在", user.full_name, user.id)
else: else:
logger.success("用户 %s[%s] 从数据库删除Cookies成功", user.full_name, user.id) logger.success("用户 %s[%s] 从数据库删除Cookies成功", user.full_name, user.id)
await message.reply_text("删除成功") await message.reply_text("删除成功")