mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-10 18:57:56 +08:00
♻️ cookie 组件不再与 user 关联
This commit is contained in:
@@ -259,14 +259,9 @@ class DBConfig:
|
||||
)
|
||||
return res
|
||||
|
||||
async def get_cookie(
|
||||
self, user: PlatformTarget = None, platform_name: str = None, target: T_Target = None
|
||||
) -> list[Cookie]:
|
||||
async def get_cookie(self, platform_name: str = None, target: T_Target = None) -> list[Cookie]:
|
||||
async with create_session() as sess:
|
||||
query = select(Cookie).distinct().join(User)
|
||||
if user:
|
||||
user_id = await sess.scalar(select(User.id).where(User.user_target == model_dump(user)))
|
||||
query = query.where(Cookie.user_id == user_id)
|
||||
query = select(Cookie).distinct()
|
||||
if platform_name:
|
||||
query = query.where(Cookie.platform_name == platform_name)
|
||||
query = query.outerjoin(CookieTarget).options(selectinload(Cookie.targets))
|
||||
@@ -277,24 +272,9 @@ class DBConfig:
|
||||
res = [cookie for cookie in res if cookie.id in ids]
|
||||
return res
|
||||
|
||||
async def get_cookie_by_user_and_platform(self, user: PlatformTarget, platform_name: str) -> list[Cookie]:
|
||||
async def add_cookie(self, platform_name: str, content: str) -> int:
|
||||
async with create_session() as sess:
|
||||
res = await sess.scalar(
|
||||
select(User)
|
||||
.where(User.user_target == model_dump(user))
|
||||
.join(Cookie)
|
||||
.where(Cookie.platform_name == platform_name)
|
||||
.outerjoin(CookieTarget)
|
||||
.options(selectinload(User.cookies))
|
||||
)
|
||||
if not res:
|
||||
return []
|
||||
return res.cookies
|
||||
|
||||
async def add_cookie(self, user: PlatformTarget, platform_name: str, content: str) -> int:
|
||||
async with create_session() as sess:
|
||||
user_obj = await sess.scalar(select(User).where(User.user_target == model_dump(user)))
|
||||
cookie = Cookie(user=user_obj, platform_name=platform_name, content=content)
|
||||
cookie = Cookie(platform_name=platform_name, content=content)
|
||||
sess.add(cookie)
|
||||
await sess.commit()
|
||||
await sess.refresh(cookie)
|
||||
|
||||
@@ -20,7 +20,6 @@ class User(Model):
|
||||
user_target: Mapped[dict] = mapped_column(JSON().with_variant(JSONB, "postgresql"))
|
||||
|
||||
subscribes: Mapped[list["Subscribe"]] = relationship(back_populates="user")
|
||||
cookies: Mapped[list["Cookie"]] = relationship(back_populates="user")
|
||||
|
||||
@property
|
||||
def saa_target(self) -> PlatformTarget:
|
||||
@@ -73,14 +72,12 @@ class Subscribe(Model):
|
||||
|
||||
class Cookie(Model):
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("nonebot_bison_user.id"))
|
||||
platform_name: Mapped[str] = mapped_column(String(20))
|
||||
content: Mapped[str] = mapped_column(String(1024))
|
||||
last_usage: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime(1970, 1, 1))
|
||||
status: Mapped[str] = mapped_column(String(20), default="")
|
||||
tags: Mapped[dict[str, Any]] = mapped_column(JSON().with_variant(JSONB, "postgresql"), default={})
|
||||
|
||||
user: Mapped[User] = relationship(back_populates="cookies")
|
||||
targets: Mapped[list["CookieTarget"]] = relationship(back_populates="cookie")
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 590dc2911ea7
|
||||
Revises: f9baef347cc8
|
||||
Create Date: 2024-08-31 23:06:02.123932
|
||||
|
||||
"""
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy import Text
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "590dc2911ea7"
|
||||
down_revision = "f9baef347cc8"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"nonebot_bison_cookie",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("platform_name", sa.String(length=20), nullable=False),
|
||||
sa.Column("content", sa.String(length=1024), nullable=False),
|
||||
sa.Column("last_usage", sa.DateTime(), nullable=False),
|
||||
sa.Column("status", sa.String(length=20), nullable=False),
|
||||
sa.Column("tags", sa.JSON().with_variant(postgresql.JSONB(astext_type=Text()), "postgresql"), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_nonebot_bison_cookie")),
|
||||
)
|
||||
op.create_table(
|
||||
"nonebot_bison_cookietarget",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("target_id", sa.Integer(), nullable=False),
|
||||
sa.Column("cookie_id", sa.Integer(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
["cookie_id"],
|
||||
["nonebot_bison_cookie.id"],
|
||||
name=op.f("fk_nonebot_bison_cookietarget_cookie_id_nonebot_bison_cookie"),
|
||||
ondelete="CASCADE",
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["target_id"],
|
||||
["nonebot_bison_target.id"],
|
||||
name=op.f("fk_nonebot_bison_cookietarget_target_id_nonebot_bison_target"),
|
||||
ondelete="CASCADE",
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_nonebot_bison_cookietarget")),
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table("nonebot_bison_cookietarget")
|
||||
op.drop_table("nonebot_bison_cookie")
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user