-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
116 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from typing import Annotated | ||
|
||
from nonebot.params import Depends | ||
|
||
from .depends import UserSession as _UserSession | ||
from .depends import get_or_create_user | ||
from .models import User as _User | ||
|
||
User = Annotated[_User, Depends(get_or_create_user)] | ||
UserSession = Annotated[_UserSession, Depends(_UserSession)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
|
||
from nonebot.matcher import Matcher | ||
from nonebot.params import Depends | ||
from nonebot_plugin_session import SessionLevel | ||
|
||
from src.utils.annotated import MyUserInfo, Session | ||
|
||
from . import utils | ||
from .models import User | ||
|
||
|
||
async def get_or_create_user(matcher: Matcher, session: Session, user_info: MyUserInfo): | ||
"""获取一个用户,如果不存在则创建""" | ||
if ( | ||
session.platform == "unknown" | ||
or session.level == SessionLevel.LEVEL0 | ||
or not session.id1 | ||
): | ||
await matcher.finish("用户相关功能暂不支持当前平台") | ||
return | ||
|
||
try: | ||
user = await utils.get_user(session.id1, session.platform) | ||
except ValueError: | ||
user = await utils.create_user( | ||
session.id1, session.platform, user_info.user_name | ||
) | ||
|
||
return user | ||
|
||
|
||
@dataclass | ||
class UserSession: | ||
session: Session | ||
info: MyUserInfo | ||
user: User = Depends(get_or_create_user) | ||
|
||
@property | ||
def uid(self) -> int: | ||
"""用户 ID""" | ||
return self.user.id | ||
|
||
@property | ||
def name(self) -> str: | ||
"""用户名""" | ||
return self.user.name | ||
|
||
@property | ||
def created_at(self) -> datetime: | ||
"""用户创建日期""" | ||
return self.user.created_at | ||
|
||
@property | ||
def pid(self) -> str: | ||
"""用户所在平台 ID""" | ||
assert self.session.id1 | ||
return self.session.id1 | ||
|
||
@property | ||
def platform(self) -> str: | ||
"""用户所在平台""" | ||
return self.session.platform | ||
|
||
@property | ||
def level(self) -> SessionLevel: | ||
"""用户会话级别""" | ||
return self.session.level |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters