Skip to content
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

✨ unban支持使用id #1681

Merged
merged 1 commit into from
Oct 5, 2024
Merged

✨ unban支持使用id #1681

merged 1 commit into from
Oct 5, 2024

Conversation

HibiKier
Copy link
Owner

@HibiKier HibiKier commented Oct 5, 2024

Summary by Sourcery

通过添加使用用户ID解除封禁的功能并改进解除封禁操作的反馈机制,增强封禁管理系统。重构数据检索方法以提高效率和清晰度。

新功能:

  • 添加通过命令 unban --id [idx] 使用用户ID解除封禁的支持。

增强:

  • 改进 unban 函数以返回一个表示成功与消息的元组,增强操作的反馈。
  • 重构 BanConsole 中的 _get_data 方法以简化用户或组数据的检索。
Original summary in English

Summary by Sourcery

Enhance the ban management system by adding the ability to unban users using their ID and improve the feedback mechanism for unban operations. Refactor data retrieval methods for better efficiency and clarity.

New Features:

  • Add support for unbanning users using their ID with the command unban --id [idx].

Enhancements:

  • Improve the unban function to return a tuple indicating success and a message, enhancing feedback for the operation.
  • Refactor the _get_data method in BanConsole to streamline the retrieval of user or group data.

@HibiKier HibiKier merged commit 3117dc2 into dev Oct 5, 2024
1 of 2 checks passed
@HibiKier HibiKier deleted the feature/unban branch October 5, 2024 11:07
Copy link

sourcery-ai bot commented Oct 5, 2024

审核指南由 Sourcery 提供

此拉取请求通过添加使用用户 ID 解禁的支持来增强禁用功能。它还重构并改进了现有的禁用和解禁逻辑,使代码更高效且更易于维护。

使用 ID 的解禁过程序列图

sequenceDiagram
    actor Admin
    participant System
    participant BanManage
    participant BanConsole
    Admin->>System: Initiate unban with --id [idx]
    System->>BanManage: Call unban(user_id, group_id, session, idx, is_superuser)
    BanManage->>BanConsole: Get ban data by id
    alt Ban data exists
        BanConsole-->>BanManage: Return ban data
        BanManage->>BanConsole: Delete ban data
        BanManage-->>System: Return success
        System-->>Admin: Notify unban success
    else Ban data does not exist
        BanManage-->>System: Return failure
        System-->>Admin: Notify unban failure
    end
Loading

更新后的 BanConsole 模型类图

classDiagram
    class BanConsole {
        IntField id
        CharField user_id
        CharField group_id
        IntField ban_time
        IntField duration
        CharField ban_level
        CharField operator
        +_get_data(user_id: str | None, group_id: str | None) Self | None
        +check_ban_level(user_id: str | None, group_id: str | None, user_level: int) bool
        +check_ban_time(user_id: str | None, group_id: str | None) int
        +is_ban(user_id: str | None, group_id: str | None) bool
        +ban(user_id: str, group_id: str, ban_level: str, duration: int)
        +unban(user_id: str | None, group_id: str | None)
    }
Loading

文件级更改

更改 详情 文件
添加通过 ID 解禁用户的支持
  • 为解禁命令引入了新的命令选项 '--id'
  • 修改了解禁功能以处理通过 ID 解禁
  • 更新了超级用户帮助文本以包含新的通过 ID 解禁选项
zhenxun/builtin_plugins/admin/ban/__init__.py
zhenxun/builtin_plugins/admin/ban/_data_source.py
重构并改进了禁用和解禁逻辑
  • 简化了解禁功能中的条件语句
  • 改进了解禁功能的错误处理和返回值
  • 更新了 BanConsole 模型以使用更高效的查询
  • 重构了 check_ban_time 方法以提高可读性
zhenxun/builtin_plugins/admin/ban/__init__.py
zhenxun/models/ban_console.py
zhenxun/builtin_plugins/admin/ban/_data_source.py
代码风格和导入优化
  • 重新排序导入以更好地组织
  • 简化了一些条件语句和返回逻辑
  • 更新了类型提示和注释
zhenxun/models/ban_console.py
zhenxun/builtin_plugins/admin/ban/_data_source.py

提示和命令

与 Sourcery 互动

  • 触发新审核: 在拉取请求中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审核评论。
  • 从审核评论生成 GitHub 问题: 通过回复审核评论请求 Sourcery 从中创建问题。
  • 生成拉取请求标题: 在拉取请求标题中任意位置写 @sourcery-ai 以随时生成标题。
  • 生成拉取请求摘要: 在拉取请求正文中任意位置写 @sourcery-ai summary 以随时生成 PR 摘要。您也可以使用此命令指定摘要应插入的位置。

自定义您的体验

访问您的仪表板以:

  • 启用或禁用审核功能,例如 Sourcery 生成的拉取请求摘要、审核指南等。
  • 更改审核语言。
  • 添加、删除或编辑自定义审核说明。
  • 调整其他审核设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request enhances the ban functionality by adding support for unbanning users using their ID. It also refactors and improves the existing ban and unban logic, making the code more efficient and easier to maintain.

Sequence diagram for unban process using ID

sequenceDiagram
    actor Admin
    participant System
    participant BanManage
    participant BanConsole
    Admin->>System: Initiate unban with --id [idx]
    System->>BanManage: Call unban(user_id, group_id, session, idx, is_superuser)
    BanManage->>BanConsole: Get ban data by id
    alt Ban data exists
        BanConsole-->>BanManage: Return ban data
        BanManage->>BanConsole: Delete ban data
        BanManage-->>System: Return success
        System-->>Admin: Notify unban success
    else Ban data does not exist
        BanManage-->>System: Return failure
        System-->>Admin: Notify unban failure
    end
Loading

Updated class diagram for BanConsole model

classDiagram
    class BanConsole {
        IntField id
        CharField user_id
        CharField group_id
        IntField ban_time
        IntField duration
        CharField ban_level
        CharField operator
        +_get_data(user_id: str | None, group_id: str | None) Self | None
        +check_ban_level(user_id: str | None, group_id: str | None, user_level: int) bool
        +check_ban_time(user_id: str | None, group_id: str | None) int
        +is_ban(user_id: str | None, group_id: str | None) bool
        +ban(user_id: str, group_id: str, ban_level: str, duration: int)
        +unban(user_id: str | None, group_id: str | None)
    }
Loading

File-Level Changes

Change Details Files
Added support for unbanning users by ID
  • Introduced a new command option '--id' for the unban command
  • Modified the unban function to handle unbanning by ID
  • Updated the superuser help text to include the new unban by ID option
zhenxun/builtin_plugins/admin/ban/__init__.py
zhenxun/builtin_plugins/admin/ban/_data_source.py
Refactored and improved ban and unban logic
  • Simplified conditional statements in the unban function
  • Improved error handling and return values for the unban function
  • Updated the BanConsole model to use more efficient queries
  • Refactored the check_ban_time method for better readability
zhenxun/builtin_plugins/admin/ban/__init__.py
zhenxun/models/ban_console.py
zhenxun/builtin_plugins/admin/ban/_data_source.py
Code style and import optimizations
  • Reordered imports for better organization
  • Simplified some conditional statements and return logic
  • Updated type hints and annotations
zhenxun/models/ban_console.py
zhenxun/builtin_plugins/admin/ban/_data_source.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HibiKier - 我已经审查了你的更改,看起来很棒!

这是我在审查期间查看的内容
  • 🟡 一般问题: 发现1个问题
  • 🟢 安全性: 一切看起来都很好
  • 🟢 测试: 一切看起来都很好
  • 🟡 复杂性: 发现1个问题
  • 🟢 文档: 一切看起来都很好

Sourcery对开源项目免费 - 如果你喜欢我们的审查,请考虑分享它们 ✨
帮助我变得更有用!请点击👍或👎在每个评论上,我将使用反馈来改进你的审查。
Original comment in English

Hey @HibiKier - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

is_superuser: bool = False,
) -> bool:
) -> tuple[bool, str]:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议: 考虑简化unban函数中的嵌套if-else结构

当前嵌套if-else语句的结构可以简化以提高可读性和可维护性。考虑使用提前返回或更线性的逻辑流。

Suggested change
) -> tuple[bool, str]:
) -> tuple[bool, str]:
if not user_id:
return False, "请输入正确的用户id..."
if not await BanUser.check_ban_level(user_id):
return False, f"解禁失败,{user_id}不在封禁列表中!"
await BanUser.unban(user_id)
return True, f"已将{user_id}解除封禁!"
Original comment in English

suggestion: Consider simplifying the nested if-else structure in the unban function

The current structure of nested if-else statements could be simplified to improve readability and maintainability. Consider using early returns or a more linear flow of logic.

Suggested change
) -> tuple[bool, str]:
) -> tuple[bool, str]:
if not user_id:
return False, "请输入正确的用户id..."
if not await BanUser.check_ban_level(user_id):
return False, f"解禁失败,{user_id}不在封禁列表中!"
await BanUser.unban(user_id)
return True, f"已将{user_id}解除封禁!"

@@ -238,8 +240,10 @@ async def _(
arparma: Arparma,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问题 (复杂性): 考虑重构unban函数以改善其结构和可读性。

随着新功能的添加,unban函数变得更加复杂。虽然增加的功能很有价值,但我们可以改进其结构以提高可读性和可维护性。以下是一些建议:

  1. 使用提前返回以减少嵌套:
async def unban_handler(bot: Bot, session: EventSession, arparma: Arparma, user: Match[str | At], group_id: Match[str], idx: Match[int]):
    user_id = ""
    _idx = idx.result if idx.available else None

    if not user.available and session.id1 not in bot.config.superusers:
        await MessageUtils.build_message("权限不足捏...").finish(reply_to=True)
        return

    user_id = get_user_id(user)
    gid = session.id3 or session.id2
    _group_id = group_id.result if group_id.available else None

    is_superuser = session.id1 in bot.config.superusers
    unban_target = gid if gid else _group_id

    is_unban, result = await BanManage.unban(user_id, unban_target, session, _idx, is_superuser)

    if not is_unban:
        await MessageUtils.build_message(result).finish(reply_to=True)
        return

    log_unban(is_superuser, arparma, session, unban_target, result)
    await send_unban_message(user, result, unban_target)
  1. 提取辅助函数以提高可读性:
def get_user_id(user: Match[str | At]) -> str:
    if isinstance(user.result, At):
        return user.result.target
    return user.result

def log_unban(is_superuser: bool, arparma: Arparma, session: EventSession, target: str, result: str):
    logger.info(
        "超级用户UnBan" if is_superuser else "管理员UnBan",
        arparma.header_result,
        session=session,
        target=f"{target}:{result}",
    )

async def send_unban_message(user: Match[str | At], result: str, target: str):
    at_msg = At(flag="user", target=result) if isinstance(user.result, At) else result
    message = f"对 {at_msg} 从黑屋中拉了出来并急救了一下!" if target else [
        "将 ", at_msg, " 从黑屋中拉了出来并急救了一下!"
    ]
    await MessageUtils.build_message(message).finish(reply_to=True)

这些更改将使结构扁平化,减少重复,并使代码更易于理解和维护,同时保留所有新功能。

Original comment in English

issue (complexity): Consider refactoring the unban function to improve its structure and readability.

The unban function has become more complex with the addition of new features. While the added functionality is valuable, we can improve its structure for better readability and maintainability. Here are some suggestions:

  1. Use early returns to reduce nesting:
async def unban_handler(bot: Bot, session: EventSession, arparma: Arparma, user: Match[str | At], group_id: Match[str], idx: Match[int]):
    user_id = ""
    _idx = idx.result if idx.available else None

    if not user.available and session.id1 not in bot.config.superusers:
        await MessageUtils.build_message("权限不足捏...").finish(reply_to=True)
        return

    user_id = get_user_id(user)
    gid = session.id3 or session.id2
    _group_id = group_id.result if group_id.available else None

    is_superuser = session.id1 in bot.config.superusers
    unban_target = gid if gid else _group_id

    is_unban, result = await BanManage.unban(user_id, unban_target, session, _idx, is_superuser)

    if not is_unban:
        await MessageUtils.build_message(result).finish(reply_to=True)
        return

    log_unban(is_superuser, arparma, session, unban_target, result)
    await send_unban_message(user, result, unban_target)
  1. Extract helper functions to improve readability:
def get_user_id(user: Match[str | At]) -> str:
    if isinstance(user.result, At):
        return user.result.target
    return user.result

def log_unban(is_superuser: bool, arparma: Arparma, session: EventSession, target: str, result: str):
    logger.info(
        "超级用户UnBan" if is_superuser else "管理员UnBan",
        arparma.header_result,
        session=session,
        target=f"{target}:{result}",
    )

async def send_unban_message(user: Match[str | At], result: str, target: str):
    at_msg = At(flag="user", target=result) if isinstance(user.result, At) else result
    message = f"对 {at_msg} 从黑屋中拉了出来并急救了一下!" if target else [
        "将 ", at_msg, " 从黑屋中拉了出来并急救了一下!"
    ]
    await MessageUtils.build_message(message).finish(reply_to=True)

These changes will flatten the structure, reduce duplication, and make the code easier to understand and maintain while preserving all the new functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant