Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 should able to delete subs when there are same user #696

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions nonebot_bison/config/db_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,33 @@ async def list_subs_with_all_info(self) -> Sequence[Subscribe]:

async def del_subscribe(self, user: PlatformTarget, target: str, platform_name: str):
async with create_session() as session:
user_obj = await session.scalar(select(User).where(User.user_target == model_dump(user)))
target_obj = await session.scalar(
select(Target).where(Target.platform_name == platform_name, Target.target == target)
# Delete the subscribe using JOINs
subs = await session.scalars(
select(Subscribe)
.join(User)
.join(Target)
.where(
User.user_target == model_dump(user),
Target.platform_name == platform_name,
Target.target == target,
)
)
await session.execute(delete(Subscribe).where(Subscribe.user == user_obj, Subscribe.target == target_obj))

for sub in subs:
await session.delete(sub)

# Check if target has any remaining subscriptions
target_count = await session.scalar(
select(func.count()).select_from(Subscribe).where(Subscribe.target == target_obj)
select(func.count())
.select_from(Subscribe)
.join(Target)
.where(Target.platform_name == platform_name, Target.target == target)
)

if target_count == 0:
# delete empty target
await asyncio.gather(*[hook(platform_name, T_Target(target)) for hook in self.delete_target_hook])

await session.commit()

async def update_subscribe(
Expand Down Expand Up @@ -334,7 +350,10 @@ async def add_cookie_target(self, target: T_Target, platform_name: str, cookie_i
)
# check if relation exists
cookie_target = await sess.scalar(
select(CookieTarget).where(CookieTarget.target == target_obj, CookieTarget.cookie_id == cookie_id)
select(CookieTarget).where(
CookieTarget.target == target_obj,
CookieTarget.cookie_id == cookie_id,
)
)
if cookie_target:
raise DuplicateCookieTargetException()
Expand Down
46 changes: 46 additions & 0 deletions tests/config/test_config_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,49 @@ async def test_del_subsribe(init_scheduler):
target = await sess.scalar(select(Target))
assert target
assert target.target_name == "weibo_name_new"


async def test_del_subscribe_with_same_target(init_scheduler):
from nonebot.compat import model_dump
from nonebot_plugin_datastore.db import get_engine
from nonebot_plugin_saa import TargetQQGroup
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from nonebot_bison.config.db_config import config
from nonebot_bison.config.db_model import Subscribe, Target, User
from nonebot_bison.types import Target as TTarget

async with AsyncSession(get_engine()) as sess:
user1 = User(user_target=model_dump(TargetQQGroup(group_id=123)))
sess.add(user1)

user2 = User(user_target=model_dump(TargetQQGroup(group_id=123)))
sess.add(user2)

target = Target(target="weibo_id", target_name="weibo", platform_name="weibo")
sess.add(target)

queried_user: User | None = await sess.scalar(
select(User).where(User.user_target == model_dump(TargetQQGroup(group_id=123)))
)
assert queried_user

if queried_user.id == user1.id:
sub = Subscribe(user_id=user2.id, target_id=target.id, categories=[], tags=[])
elif queried_user == user2.id:
sub = Subscribe(user_id=user1.id, target_id=target.id, categories=[], tags=[])
else:
pytest.fail("no user match")

sess.add(sub)

await sess.commit()

res = await config.list_subscribe(TargetQQGroup(group_id=123))
assert len(res) == 1

await config.del_subscribe(TargetQQGroup(group_id=123), TTarget("weibo_id"), "weibo")

res = await config.list_subscribe(TargetQQGroup(group_id=123))
assert len(res) == 0
Loading