-
Notifications
You must be signed in to change notification settings - Fork 1
fix: 适配跨平台多Bot实例进程 #6
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
base: feat-scope-permission
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| from dataclasses import dataclass | ||
| import json | ||
| from typing import TypeAlias | ||
|
|
||
| from nonebot_plugin_uninfo import SceneType, SupportScope | ||
|
|
||
| ENTITY_KEY_PREFIX = "limiter-entity:" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class UserKey: | ||
| scope: str | ||
| user_id: str | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class SceneKey: | ||
| scope: str | ||
| scene_id: str | ||
| scene_type: int | ||
| parent_id: str | None = None | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class SceneUserKey: | ||
| scene: SceneKey | ||
| user_id: str | ||
|
|
||
|
|
||
| EntityKey: TypeAlias = str | UserKey | SceneKey | SceneUserKey | ||
|
|
||
|
|
||
| def make_user_key(scope: SupportScope, user_id: str) -> UserKey: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 没看懂这么写包了层为了啥,直接实例化不行么
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 而且其实我目目前目测看并没有看到需要用 dataclass 的必要性, NamedTuple 完全可以解决
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你说的对( 我没仔细检查 |
||
| return UserKey(scope.value, user_id) | ||
|
|
||
|
|
||
| def make_scene_key( | ||
| scope: SupportScope, | ||
| scene_id: str, | ||
| scene_type: SceneType, | ||
| parent_id: str | None, | ||
| ) -> SceneKey: | ||
| return SceneKey(scope.value, scene_id, scene_type.value, parent_id) | ||
|
|
||
|
|
||
| def serialize_entity_key(key: EntityKey) -> str: | ||
| match key: | ||
| case str(): | ||
| return key | ||
| case UserKey(scope, user_id): | ||
| value = ["user", scope, user_id] | ||
| case SceneKey(scope, scene_id, scene_type, parent_id): | ||
| value = ["scene", scope, scene_id, scene_type, parent_id] | ||
| case SceneUserKey(SceneKey(scope, scene_id, scene_type, parent_id), user_id): | ||
| value = [ | ||
| "scene_user", | ||
| scope, | ||
| scene_id, | ||
| scene_type, | ||
| parent_id, | ||
| user_id, | ||
| ] | ||
| case _: | ||
| raise TypeError(f"unsupported entity key type: {type(key)!r}") | ||
| payload = json.dumps(value, ensure_ascii=False, separators=(",", ":")) | ||
| return f"{ENTITY_KEY_PREFIX}{payload}" | ||
|
|
||
|
|
||
| def deserialize_entity_key(value: str) -> EntityKey: | ||
| if not value.startswith(ENTITY_KEY_PREFIX): | ||
| return value | ||
|
|
||
| try: | ||
| data = json.loads(value[len(ENTITY_KEY_PREFIX) :]) | ||
| if not isinstance(data, list) or not data or not isinstance(data[0], str): | ||
| return value | ||
| kind = data[0] | ||
| if kind == "user" and len(data) == 3 and all(isinstance(item, str) for item in data[1:]): | ||
| return UserKey(data[1], data[2]) | ||
| if kind == "scene" and _is_scene_payload(data): | ||
| return SceneKey(data[1], data[2], data[3], data[4]) | ||
| if kind == "scene_user" and len(data) == 6 and _is_scene_payload(data[:5]) and isinstance(data[5], str): | ||
| scene = SceneKey(data[1], data[2], data[3], data[4]) | ||
| return SceneUserKey(scene, data[5]) | ||
| except json.JSONDecodeError: | ||
| pass | ||
| return value | ||
|
|
||
|
|
||
| def _is_scene_payload(value: list[object]) -> bool: | ||
| return ( | ||
| len(value) == 5 | ||
| and isinstance(value[1], str) | ||
| and isinstance(value[2], str) | ||
| and isinstance(value[3], int) | ||
| and not isinstance(value[3], bool) | ||
| and (value[4] is None or isinstance(value[4], str)) | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
正常的 key 我觉得完全不需要 prefix,也不存别的什么地方