mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-06 03:46:10 +08:00
update
This commit is contained in:
parent
c6dfc9818e
commit
df23648b0f
58
src/plugins/nonebot_bison/config/db_config.py
Normal file
58
src/plugins/nonebot_bison/config/db_config.py
Normal file
@ -0,0 +1,58 @@
|
||||
import json
|
||||
from typing import Optional
|
||||
|
||||
from nonebot_bison.types import Category, Tag, Target
|
||||
from nonebot_plugin_datastore.db import create_session
|
||||
from sqlalchemy.sql.expression import select
|
||||
|
||||
from .db_model import Subscribe as MSubscribe
|
||||
from .db_model import Target as MTarget
|
||||
from .db_model import User
|
||||
|
||||
|
||||
class DBConfig:
|
||||
def __init__(self):
|
||||
self.session = create_session()
|
||||
|
||||
async def add_subscribe(
|
||||
self,
|
||||
user: int,
|
||||
user_type: str,
|
||||
target: Target,
|
||||
target_name: str,
|
||||
platform_name: str,
|
||||
cats: list[Category],
|
||||
tags: list[Tag],
|
||||
):
|
||||
db_user_stmt = (
|
||||
select(User).where(User.uid == user).where(User.type == user_type)
|
||||
)
|
||||
db_user: Optional[User] = (await self.session.scalars(db_user_stmt)).first()
|
||||
if not db_user:
|
||||
db_user = User(uid=user, type=user_type)
|
||||
self.session.add(db_user)
|
||||
db_target_stmt = (
|
||||
select(MTarget)
|
||||
.where(MTarget.platform_name == platform_name)
|
||||
.where(MTarget.target == target)
|
||||
)
|
||||
db_target: Optional[MTarget] = (
|
||||
await self.session.scalars(db_target_stmt)
|
||||
).first()
|
||||
if not db_target:
|
||||
db_target = MTarget(
|
||||
target=target, platform_name=platform_name, target_name=target_name
|
||||
)
|
||||
else:
|
||||
db_target.target_name = target_name # type: ignore
|
||||
subscribe = MSubscribe(
|
||||
categories=json.dumps(cats),
|
||||
tags=json.dumps(tags),
|
||||
user=db_user,
|
||||
target=db_target,
|
||||
)
|
||||
self.session.add(subscribe)
|
||||
await self.session.commit()
|
||||
|
||||
|
||||
config = DBConfig()
|
6
src/plugins/nonebot_bison/config/utils.py
Normal file
6
src/plugins/nonebot_bison/config/utils.py
Normal file
@ -0,0 +1,6 @@
|
||||
class NoSuchUserException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class NoSuchSubscribeException(Exception):
|
||||
pass
|
17
tests/config/test_config_operation.py
Normal file
17
tests/config/test_config_operation.py
Normal file
@ -0,0 +1,17 @@
|
||||
from nonebug.app import App
|
||||
|
||||
|
||||
async def test_add_subscrib(app: App):
|
||||
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.types import Target
|
||||
|
||||
await config.add_subscribe(
|
||||
user=123,
|
||||
user_type="group",
|
||||
target=Target("weibo_id"),
|
||||
target_name="weibo_name",
|
||||
platform_name="weibo",
|
||||
cats=[],
|
||||
tags=[],
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user