Skip to content

Commit

Permalink
适配新版session插件与access-control插件
Browse files Browse the repository at this point in the history
  • Loading branch information
ssttkkl committed Nov 3, 2023
1 parent 447a44b commit a6bb546
Show file tree
Hide file tree
Showing 6 changed files with 1,020 additions and 773 deletions.
1,690 changes: 967 additions & 723 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-pixivbot"
version = "2.0.5"
version = "2.1.0"
description = "Nonebot Plugin PixivBot"
authors = ["ssttkkl <[email protected]>"]
readme = "README.md"
Expand All @@ -14,8 +14,10 @@ packages = [
python = "^3.9"
nonebot2 = "^2.0.0"
nonebot_plugin_apscheduler = ">=0.3.0"
nonebot-plugin-access-control = ">=0.5.5"
nonebot-plugin-session = ">=0.1.0, <0.3.0"
nonebot-plugin-access-control = "^1.0.0"
nonebot-plugin-session = "^0.2.0"
nonebot-plugin-session-orm = "^0.1.1"
nonebot-plugin-session-saa = "^0.1.0"
nonebot-plugin-send-anything-anywhere = ">=0.3.0"
nonebot-plugin-localstore = "^0.5.1"
PixivPy-Async = "^1.2.14"
Expand All @@ -29,6 +31,8 @@ asyncache = ">=0.3.1"
shortuuid = ">=1.0.9"
ssttkkl-nonebot-utils = ">=0.1.14"
aiofiles = ">=23.2.1"
SQLAlchemy = { version = "^2.0.0", extras = ["asyncio"] }
aiosqlite = ">=0.18.0"

[tool.poetry.group.dev.dependencies]
nb-cli = "^1.0.5"
Expand All @@ -41,6 +45,10 @@ nonebot-adapter-kaiheila = "^0.2.0"
nonebot-adapter-qqguild = "^0.2.2"
autopep8 = "^2.0.4"

[tool.nonebot]
plugins = ["nonebot_plugin_pixivbot"]
plugin_dirs = []

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
2 changes: 2 additions & 0 deletions src/nonebot_plugin_pixivbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
require("nonebot_plugin_apscheduler")
require("nonebot_plugin_access_control")
require("nonebot_plugin_session")
require("nonebot_plugin_session_orm")
require("nonebot_plugin_session_saa")
require("nonebot_plugin_saa")
require("nonebot_plugin_localstore")
require("ssttkkl_nonebot_utils")
Expand Down
18 changes: 7 additions & 11 deletions src/nonebot_plugin_pixivbot/data/nb_session.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
from typing import Optional

from nonebot_plugin_datastore.db import get_engine
from nonebot_plugin_session import Session
from nonebot_plugin_session.model import get_or_add_session_model, SessionModel
from sqlalchemy.ext.asyncio import AsyncSession
from nonebot_plugin_session_orm import get_session_persist_id, get_session_by_persist_id
from sqlalchemy.exc import NoResultFound

from ..global_context import context


@context.register_singleton()
class NbSessionRepo:
async def get_id(self, session: Session) -> int:
async with AsyncSession(get_engine()) as db_sess:
model = await get_or_add_session_model(session, db_sess)
return model.id
return await get_session_persist_id(session)

async def get_session(self, session_id: int) -> Optional[Session]:
async with AsyncSession(get_engine()) as db_sess:
model = await db_sess.get(SessionModel, session_id)
if model is None:
return None
return model.session
try:
return await get_session_by_persist_id(session_id)
except NoResultFound:
return None
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import json
from typing import Union

from nonebot_plugin_datastore.db import get_engine
from nonebot_plugin_session import Session, SessionLevel
from nonebot_plugin_session.model import get_or_add_session_model
from nonebot_plugin_session_orm import get_session_persist_id
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession
from sqlalchemy.ext.asyncio import AsyncConnection

from nonebot_plugin_pixivbot import Config
from nonebot_plugin_pixivbot.global_context import context
Expand Down Expand Up @@ -119,24 +118,23 @@ async def migrate(self, conn: AsyncConnection):
);
"""))

async with AsyncSession(get_engine()) as db_sess:
result = await conn.execute(
text("select id, subscriber, code, type, kwargs, bot, schedule, tz from subscription_old;"))
result = await conn.execute(
text("select id, subscriber, code, type, kwargs, bot, schedule, tz from subscription_old;"))

for id, subscriber, code, type, kwargs, bot, schedule, tz in result.fetchall():
session = convert_session(bot, subscriber)
session_id = (await get_or_add_session_model(session, db_sess)).id
for id, subscriber, code, type, kwargs, bot, schedule, tz in result.fetchall():
session = convert_session(bot, subscriber)
session_id = await get_session_persist_id(session)

kwargs = convert_kwargs(kwargs)
if not isinstance(kwargs, str):
kwargs = json.dumps(kwargs)
kwargs = convert_kwargs(kwargs)
if not isinstance(kwargs, str):
kwargs = json.dumps(kwargs)

await conn.execute(
text("insert into subscription(id, session_id, code, type, kwargs, bot_id, schedule, tz) "
"values (:id, :session_id, :code, :type, :kwargs, :bot_id, :schedule, :tz);"),
dict(id=id, session_id=session_id, code=code, type=type, kwargs=kwargs, bot_id=session.bot_id,
schedule=schedule, tz=tz)
)
await conn.execute(
text("insert into subscription(id, session_id, code, type, kwargs, bot_id, schedule, tz) "
"values (:id, :session_id, :code, :type, :kwargs, :bot_id, :schedule, :tz);"),
dict(id=id, session_id=session_id, code=code, type=type, kwargs=kwargs, bot_id=session.bot_id,
schedule=schedule, tz=tz)
)

await conn.execute(text("drop table subscription_old;"))

Expand Down Expand Up @@ -175,27 +173,26 @@ async def migrate(self, conn: AsyncConnection):
);
"""))

async with AsyncSession(get_engine()) as db_sess:
result = await conn.execute(
text("select id, subscriber, code, type, kwargs, bot, checkpoint from watch_task_old;"))
result = await conn.execute(
text("select id, subscriber, code, type, kwargs, bot, checkpoint from watch_task_old;"))

for id, subscriber, code, type, kwargs, bot, checkpoint in result.fetchall():
session = convert_session(bot, subscriber)
session_id = (await get_or_add_session_model(session, db_sess)).id
for id, subscriber, code, type, kwargs, bot, checkpoint in result.fetchall():
session = convert_session(bot, subscriber)
session_id = await get_session_persist_id(session)

kwargs = convert_kwargs(kwargs)
kwargs = convert_kwargs(kwargs)

if not isinstance(kwargs, str):
kwargs = json.dumps(kwargs)
if not isinstance(kwargs, str):
kwargs = json.dumps(kwargs)

if not isinstance(schedule, str):
schedule = json.dumps(schedule)
if not isinstance(schedule, str):
schedule = json.dumps(schedule)

await conn.execute(
text("insert into watch_task(id, session_id, code, type, kwargs, bot_id, checkpoint) "
"values (:id, :session_id, :code, :type, :kwargs, :bot_id, :checkpoint);"),
dict(id=id, session_id=session_id, code=code, type=type, kwargs=kwargs, bot_id=session.bot_id,
checkpoint=checkpoint)
)
await conn.execute(
text("insert into watch_task(id, session_id, code, type, kwargs, bot_id, checkpoint) "
"values (:id, :session_id, :code, :type, :kwargs, :bot_id, :checkpoint);"),
dict(id=id, session_id=session_id, code=code, type=type, kwargs=kwargs, bot_id=session.bot_id,
checkpoint=checkpoint)
)

await conn.execute(text("drop table watch_task_old;"))
2 changes: 1 addition & 1 deletion src/nonebot_plugin_pixivbot/service/postman.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from nonebot.internal.adapter import Event
from nonebot_plugin_saa import MessageFactory, Text, Image, AggregatedMessageFactory
from nonebot_plugin_session import Session
from nonebot_plugin_session.saa import get_saa_target
from nonebot_plugin_session_saa import get_saa_target

from ..config import Config
from ..enums import BlockAction
Expand Down

0 comments on commit a6bb546

Please sign in to comment.