Skip to content

Commit

Permalink
获取不到用户信息时用 id 作为初始用户名
Browse files Browse the repository at this point in the history
  • Loading branch information
he0119 committed Sep 13, 2023
1 parent 8e47bd8 commit 5bf4d35
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
17 changes: 8 additions & 9 deletions src/plugins/user/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@

from nonebot.matcher import Matcher
from nonebot.params import Depends
from nonebot_plugin_session import SessionLevel
from nonebot_plugin_session import Session, SessionLevel, extract_session
from nonebot_plugin_userinfo import EventUserInfo, UserInfo

from src.utils.annotated import MyUserInfo, Session

from . import utils
from .models import User

Check warning on line 10 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L9-L10

Added lines #L9 - L10 were not covered by tests


async def get_or_create_user(

Check warning on line 13 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L13

Added line #L13 was not covered by tests
matcher: Matcher,
session: Session,
session: Session = Depends(extract_session),
user_info: UserInfo | None = EventUserInfo(),
):
"""获取一个用户,如果不存在则创建"""
if (

Check warning on line 19 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L19

Added line #L19 was not covered by tests
session.platform == "unknown"
or user_info is None
or session.level == SessionLevel.LEVEL0
or not session.id1
):
Expand All @@ -31,16 +28,18 @@ async def get_or_create_user(
user = await utils.get_user(session.id1, session.platform)
except ValueError:
user = await utils.create_user(

Check warning on line 30 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L27-L30

Added lines #L27 - L30 were not covered by tests
session.id1, session.platform, user_info.user_name
session.id1,
session.platform,
user_info and user_info.user_name or session.id1,
)

return user

Check warning on line 36 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L36

Added line #L36 was not covered by tests


@dataclass
class UserSession:
session: Session
info: MyUserInfo
session: Session = Depends(extract_session)
info: UserInfo | None = EventUserInfo()
user: User = Depends(get_or_create_user)

Check warning on line 43 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L39-L43

Added lines #L39 - L43 were not covered by tests

@property
Expand All @@ -56,7 +55,7 @@ def name(self) -> str:
@property
def created_at(self) -> datetime:

Check warning on line 56 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L55-L56

Added lines #L55 - L56 were not covered by tests
"""用户创建日期"""
return self.user.created_at
return self.user.created_at.astimezone()

Check warning on line 58 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L58

Added line #L58 was not covered by tests

@property
def pid(self) -> str:

Check warning on line 61 in src/plugins/user/depends.py

View check run for this annotation

Codecov / codecov/patch

src/plugins/user/depends.py#L60-L61

Added lines #L60 - L61 were not covered by tests
Expand Down
6 changes: 0 additions & 6 deletions src/utils/annotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

from nonebot.params import Depends
from nonebot_plugin_datastore import get_session
from nonebot_plugin_session import Session as _Session
from nonebot_plugin_session import extract_session
from nonebot_plugin_userinfo import EventUserInfo
from nonebot_plugin_userinfo import UserInfo as _MyUserInfo
from sqlalchemy.ext.asyncio import AsyncSession as _AsyncSession

from .depends import (
Expand All @@ -28,5 +24,3 @@
OptionalPlainTextArgs = Annotated[str | None, Depends(get_plaintext_args)]
Platform = Annotated[str, Depends(get_platform)]
OptionalPlatform = Annotated[str | None, Depends(get_platform)]
MyUserInfo = Annotated[_MyUserInfo, EventUserInfo()]
Session = Annotated[_Session, Depends(extract_session)]

0 comments on commit 5bf4d35

Please sign in to comment.