From 66729a193daf2aa712ba8aa36ba4febae8597932 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Tue, 23 Mar 2021 00:56:55 +0800 Subject: [PATCH] :recycle: separate redis funcs --- src/plugins/github/libs/auth.py | 6 +- src/plugins/github/libs/redis.py | 82 ------------------- src/plugins/github/libs/redis/__init__.py | 19 +++++ src/plugins/github/libs/redis/bind.py | 34 ++++++++ src/plugins/github/libs/redis/hook.py | 34 ++++++++ src/plugins/github/libs/redis/message.py | 51 ++++++++++++ src/plugins/github/libs/redis/state.py | 28 +++++++ src/plugins/github/libs/redis/token.py | 34 ++++++++ .../github/plugins/github_bind/__init__.py | 8 +- 9 files changed, 208 insertions(+), 88 deletions(-) delete mode 100644 src/plugins/github/libs/redis.py create mode 100644 src/plugins/github/libs/redis/__init__.py create mode 100644 src/plugins/github/libs/redis/bind.py create mode 100644 src/plugins/github/libs/redis/hook.py create mode 100644 src/plugins/github/libs/redis/message.py create mode 100644 src/plugins/github/libs/redis/state.py create mode 100644 src/plugins/github/libs/redis/token.py diff --git a/src/plugins/github/libs/auth.py b/src/plugins/github/libs/auth.py index 2060d5e..e70b6d9 100644 --- a/src/plugins/github/libs/auth.py +++ b/src/plugins/github/libs/auth.py @@ -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": diff --git a/src/plugins/github/libs/redis.py b/src/plugins/github/libs/redis.py deleted file mode 100644 index 6fee04f..0000000 --- a/src/plugins/github/libs/redis.py +++ /dev/null @@ -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() diff --git a/src/plugins/github/libs/redis/__init__.py b/src/plugins/github/libs/redis/__init__.py new file mode 100644 index 0000000..5cfd27a --- /dev/null +++ b/src/plugins/github/libs/redis/__init__.py @@ -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 diff --git a/src/plugins/github/libs/redis/bind.py b/src/plugins/github/libs/redis/bind.py new file mode 100644 index 0000000..5196232 --- /dev/null +++ b/src/plugins/github/libs/redis/bind.py @@ -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() diff --git a/src/plugins/github/libs/redis/hook.py b/src/plugins/github/libs/redis/hook.py new file mode 100644 index 0000000..e474f94 --- /dev/null +++ b/src/plugins/github/libs/redis/hook.py @@ -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() diff --git a/src/plugins/github/libs/redis/message.py b/src/plugins/github/libs/redis/message.py new file mode 100644 index 0000000..33ae32c --- /dev/null +++ b/src/plugins/github/libs/redis/message.py @@ -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)) diff --git a/src/plugins/github/libs/redis/state.py b/src/plugins/github/libs/redis/state.py new file mode 100644 index 0000000..58b5981 --- /dev/null +++ b/src/plugins/github/libs/redis/state.py @@ -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() diff --git a/src/plugins/github/libs/redis/token.py b/src/plugins/github/libs/redis/token.py new file mode 100644 index 0000000..cd6ada9 --- /dev/null +++ b/src/plugins/github/libs/redis/token.py @@ -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() diff --git a/src/plugins/github/plugins/github_bind/__init__.py b/src/plugins/github/plugins/github_bind/__init__.py index c224781..9ac7d67 100644 --- a/src/plugins/github/plugins/github_bind/__init__.py +++ b/src/plugins/github/plugins/github_bind/__init__.py @@ -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("尚未绑定仓库!")