mirror of
https://github.com/Xtao-Labs/QQ-GitHub-Bot.git
synced 2025-02-07 10:59:44 +00:00
♻️ separate redis funcs
This commit is contained in:
parent
9a3508ed7a
commit
66729a193d
@ -4,7 +4,7 @@
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-09 16:30:16
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-16 00:16:40
|
||||
@LastEditTime : 2021-03-23 00:35:49
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
@ -43,7 +43,9 @@ def get_auth_link(username: str) -> str:
|
||||
"client_id":
|
||||
config.github_client_id,
|
||||
"redirect_uri":
|
||||
urllib.parse.urljoin(config.github_self_host, "/api/github/auth"),
|
||||
urllib.parse.urljoin(
|
||||
config.github_self_host, # type: ignore
|
||||
"/api/github/auth"),
|
||||
"scope":
|
||||
"admin:repo_hook,repo",
|
||||
"state":
|
||||
|
@ -1,82 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-14 10:53:42
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-16 00:57:20
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
__author__ = "yanyongyu"
|
||||
|
||||
from typing import Optional
|
||||
from datetime import timedelta
|
||||
|
||||
from .. import redis
|
||||
|
||||
GITHUB_BIND_FORMAT = "github_bind_{group_id}"
|
||||
USER_STATE_FORMAT = "github_state_{state}"
|
||||
USER_TOKEN_FORMAT = "github_token_{user_id}"
|
||||
REPO_HOOK_FORMAT = "github_hook_{repo_name}"
|
||||
|
||||
|
||||
def set_group_bind_repo(group_id: int, full_name: str) -> Optional[bool]:
|
||||
return redis.set(GITHUB_BIND_FORMAT.format(group_id=group_id), full_name)
|
||||
|
||||
|
||||
def delete_group_bind_repo(group_id: int) -> int:
|
||||
return redis.delete(GITHUB_BIND_FORMAT.format(group_id=group_id))
|
||||
|
||||
|
||||
def exists_group_bind_repo(group_id: int) -> int:
|
||||
return redis.exists(GITHUB_BIND_FORMAT.format(group_id=group_id))
|
||||
|
||||
|
||||
def get_group_bind_repo(group_id: int) -> Optional[str]:
|
||||
value = redis.get(GITHUB_BIND_FORMAT.format(group_id=group_id))
|
||||
return value if value is None else value.decode()
|
||||
|
||||
|
||||
def set_state_bind_user(user_id: str, state: int) -> Optional[bool]:
|
||||
return redis.set(USER_STATE_FORMAT.format(state=state), user_id,
|
||||
timedelta(minutes=5))
|
||||
|
||||
|
||||
def get_state_bind_user(state: int) -> Optional[str]:
|
||||
value = redis.get(USER_STATE_FORMAT.format(state=state))
|
||||
return value if value is None else value.decode()
|
||||
|
||||
|
||||
def set_user_token(user_id: str, token: str) -> Optional[bool]:
|
||||
return redis.set(USER_TOKEN_FORMAT.format(user_id=user_id), token)
|
||||
|
||||
|
||||
def delete_user_token(user_id: str) -> int:
|
||||
return redis.delete(USER_TOKEN_FORMAT.format(user_id=user_id))
|
||||
|
||||
|
||||
def exists_user_token(user_id: str) -> int:
|
||||
return redis.exists(USER_TOKEN_FORMAT.format(user_id=user_id))
|
||||
|
||||
|
||||
def get_user_token(user_id: str) -> Optional[str]:
|
||||
value = redis.get(USER_TOKEN_FORMAT.format(user_id=user_id))
|
||||
return value if value is None else value.decode()
|
||||
|
||||
|
||||
def set_repo_hook(hook_id: str, full_name: str) -> Optional[bool]:
|
||||
return redis.set(REPO_HOOK_FORMAT.format(repo_name=full_name), hook_id)
|
||||
|
||||
|
||||
def delete_repo_hook(full_name: str) -> int:
|
||||
return redis.delete(REPO_HOOK_FORMAT.format(repo_name=full_name))
|
||||
|
||||
|
||||
def exists_repo_hook(full_name: str) -> int:
|
||||
return redis.exists(REPO_HOOK_FORMAT.format(repo_name=full_name))
|
||||
|
||||
|
||||
def get_repo_hook(full_name: str) -> Optional[str]:
|
||||
value = redis.get(REPO_HOOK_FORMAT.format(repo_name=full_name))
|
||||
return value if value is None else value.decode()
|
19
src/plugins/github/libs/redis/__init__.py
Normal file
19
src/plugins/github/libs/redis/__init__.py
Normal file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-14 10:53:42
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-23 00:55:34
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
__author__ = "yanyongyu"
|
||||
|
||||
from ... import redis
|
||||
|
||||
from .state import set_state_bind_user, get_state_bind_user
|
||||
from .hook import set_repo_hook, get_repo_hook, delete_repo_hook, exists_repo_hook
|
||||
from .token import set_user_token, get_user_token, delete_user_token, exists_user_token
|
||||
from .message import set_message_info, get_message_info, delete_message_info, exists_message_info
|
||||
from .bind import set_group_bind_repo, get_group_bind_repo, delete_group_bind_repo, exists_group_bind_repo
|
34
src/plugins/github/libs/redis/bind.py
Normal file
34
src/plugins/github/libs/redis/bind.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-23 00:17:23
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-23 00:38:10
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
__author__ = "yanyongyu"
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from . import redis
|
||||
|
||||
GITHUB_BIND_FORMAT = "github_bind_{group_id}"
|
||||
|
||||
|
||||
def set_group_bind_repo(group_id: str, full_name: str) -> Optional[bool]:
|
||||
return redis.set(GITHUB_BIND_FORMAT.format(group_id=group_id), full_name)
|
||||
|
||||
|
||||
def delete_group_bind_repo(group_id: str) -> int:
|
||||
return redis.delete(GITHUB_BIND_FORMAT.format(group_id=group_id))
|
||||
|
||||
|
||||
def exists_group_bind_repo(group_id: str) -> int:
|
||||
return redis.exists(GITHUB_BIND_FORMAT.format(group_id=group_id))
|
||||
|
||||
|
||||
def get_group_bind_repo(group_id: str) -> Optional[str]:
|
||||
value = redis.get(GITHUB_BIND_FORMAT.format(group_id=group_id))
|
||||
return value if value is None else value.decode()
|
34
src/plugins/github/libs/redis/hook.py
Normal file
34
src/plugins/github/libs/redis/hook.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-23 00:22:01
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-23 00:22:23
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
__author__ = "yanyongyu"
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from . import redis
|
||||
|
||||
REPO_HOOK_FORMAT = "github_hook_{repo_name}"
|
||||
|
||||
|
||||
def set_repo_hook(hook_id: str, full_name: str) -> Optional[bool]:
|
||||
return redis.set(REPO_HOOK_FORMAT.format(repo_name=full_name), hook_id)
|
||||
|
||||
|
||||
def delete_repo_hook(full_name: str) -> int:
|
||||
return redis.delete(REPO_HOOK_FORMAT.format(repo_name=full_name))
|
||||
|
||||
|
||||
def exists_repo_hook(full_name: str) -> int:
|
||||
return redis.exists(REPO_HOOK_FORMAT.format(repo_name=full_name))
|
||||
|
||||
|
||||
def get_repo_hook(full_name: str) -> Optional[str]:
|
||||
value = redis.get(REPO_HOOK_FORMAT.format(repo_name=full_name))
|
||||
return value if value is None else value.decode()
|
51
src/plugins/github/libs/redis/message.py
Normal file
51
src/plugins/github/libs/redis/message.py
Normal file
@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-23 00:30:58
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-23 00:54:40
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
__author__ = "yanyongyu"
|
||||
|
||||
import json
|
||||
from typing import Optional
|
||||
from datetime import timedelta
|
||||
from dataclasses import dataclass
|
||||
|
||||
from . import redis
|
||||
|
||||
MESSAGE_ID_FORMAT = "github_message_{message_id}"
|
||||
|
||||
|
||||
@dataclass
|
||||
class MessageInfo:
|
||||
owner: str
|
||||
repo: str
|
||||
number: int
|
||||
|
||||
|
||||
def set_message_info(message_id: str, owner: str, repo: str,
|
||||
number: int) -> Optional[bool]:
|
||||
return redis.set(
|
||||
MESSAGE_ID_FORMAT.format(message_id=message_id),
|
||||
json.dumps({
|
||||
"owner": owner,
|
||||
"repo": repo,
|
||||
"number": number
|
||||
}), timedelta(days=3))
|
||||
|
||||
|
||||
def delete_message_info(message_id: str) -> int:
|
||||
return redis.delete(MESSAGE_ID_FORMAT.format(message_id=message_id))
|
||||
|
||||
|
||||
def exists_message_info(message_id: str) -> int:
|
||||
return redis.exists(MESSAGE_ID_FORMAT.format(message_id=message_id))
|
||||
|
||||
|
||||
def get_message_info(message_id: str) -> Optional[MessageInfo]:
|
||||
value = redis.get(MESSAGE_ID_FORMAT.format(message_id=message_id))
|
||||
return value if value is None else MessageInfo(**json.loads(value))
|
28
src/plugins/github/libs/redis/state.py
Normal file
28
src/plugins/github/libs/redis/state.py
Normal file
@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-23 00:19:12
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-23 00:19:53
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
__author__ = "yanyongyu"
|
||||
|
||||
from typing import Optional
|
||||
from datetime import timedelta
|
||||
|
||||
from . import redis
|
||||
|
||||
USER_STATE_FORMAT = "github_state_{state}"
|
||||
|
||||
|
||||
def set_state_bind_user(user_id: str, state: int) -> Optional[bool]:
|
||||
return redis.set(USER_STATE_FORMAT.format(state=state), user_id,
|
||||
timedelta(minutes=5))
|
||||
|
||||
|
||||
def get_state_bind_user(state: int) -> Optional[str]:
|
||||
value = redis.get(USER_STATE_FORMAT.format(state=state))
|
||||
return value if value is None else value.decode()
|
34
src/plugins/github/libs/redis/token.py
Normal file
34
src/plugins/github/libs/redis/token.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-23 00:20:51
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-23 00:21:18
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
__author__ = "yanyongyu"
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from . import redis
|
||||
|
||||
USER_TOKEN_FORMAT = "github_token_{user_id}"
|
||||
|
||||
|
||||
def set_user_token(user_id: str, token: str) -> Optional[bool]:
|
||||
return redis.set(USER_TOKEN_FORMAT.format(user_id=user_id), token)
|
||||
|
||||
|
||||
def delete_user_token(user_id: str) -> int:
|
||||
return redis.delete(USER_TOKEN_FORMAT.format(user_id=user_id))
|
||||
|
||||
|
||||
def exists_user_token(user_id: str) -> int:
|
||||
return redis.exists(USER_TOKEN_FORMAT.format(user_id=user_id))
|
||||
|
||||
|
||||
def get_user_token(user_id: str) -> Optional[str]:
|
||||
value = redis.get(USER_TOKEN_FORMAT.format(user_id=user_id))
|
||||
return value if value is None else value.decode()
|
@ -4,7 +4,7 @@
|
||||
@Author : yanyongyu
|
||||
@Date : 2021-03-12 15:03:23
|
||||
@LastEditors : yanyongyu
|
||||
@LastEditTime : 2021-03-15 22:07:08
|
||||
@LastEditTime : 2021-03-23 00:38:38
|
||||
@Description : None
|
||||
@GitHub : https://github.com/yanyongyu
|
||||
"""
|
||||
@ -59,7 +59,7 @@ async def process_repo(bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
await bind.reject(f"仓库名 {owner}/{repo_name} 不存在!请重新发送或取消")
|
||||
return
|
||||
|
||||
set_group_bind_repo(event.group_id, repo.full_name)
|
||||
set_group_bind_repo(str(event.group_id), repo.full_name)
|
||||
await bind.finish(f"本群成功绑定仓库 {repo.full_name} !")
|
||||
|
||||
|
||||
@ -75,8 +75,8 @@ unbind.__doc__ = """
|
||||
|
||||
@unbind.handle()
|
||||
async def process_unbind(bot: Bot, event: GroupMessageEvent):
|
||||
if exists_group_bind_repo(event.group_id):
|
||||
delete_group_bind_repo(event.group_id)
|
||||
if exists_group_bind_repo(str(event.group_id)):
|
||||
delete_group_bind_repo(str(event.group_id))
|
||||
await unbind.finish("成功解绑仓库!")
|
||||
else:
|
||||
await unbind.finish("尚未绑定仓库!")
|
||||
|
Loading…
Reference in New Issue
Block a user