Skip to content

Commit 83ecbd0

Browse files
authored
✨ send file image if image is small
1 parent 46df202 commit 83ecbd0

File tree

7 files changed

+50
-24
lines changed

7 files changed

+50
-24
lines changed

src/plugins/github/helpers/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
@Author : yanyongyu
33
@Date : 2022-09-07 12:17:55
44
@LastEditors : yanyongyu
5-
@LastEditTime : 2023-11-13 17:35:27
5+
@LastEditTime : 2024-06-02 16:49:07
66
@Description : Helpers for github plugin
77
@GitHub : https://github.com/yanyongyu
88
"""
99

1010
__author__ = "yanyongyu"
1111

12+
from .image import qqofficial_conditional_image as qqofficial_conditional_image
13+
14+
# isort: split
15+
1216
from .rule import REPLY_PR as REPLY_PR
1317
from .rule import REPLY_ANY as REPLY_ANY
1418
from .rule import NO_GITHUB_EVENT as NO_GITHUB_EVENT
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
@Author : yanyongyu
3+
@Date : 2024-06-02 16:44:42
4+
@LastEditors : yanyongyu
5+
@LastEditTime : 2024-06-02 16:48:07
6+
@Description : None
7+
@GitHub : https://github.com/yanyongyu
8+
"""
9+
10+
__author__ = "yanyongyu"
11+
12+
from nonebot.adapters.qq import MessageSegment as QQOfficialMS
13+
14+
from src.providers.filehost import save_image
15+
16+
QQOFFICIAL_IMAGE_MAX_SIZE = 2 * 1024 * 1024
17+
18+
19+
async def qqofficial_conditional_image(image: bytes) -> QQOfficialMS:
20+
"""Get the QQ official image message segment depends on image size"""
21+
if len(image) > QQOFFICIAL_IMAGE_MAX_SIZE:
22+
return QQOfficialMS.image(await save_image(image))
23+
return QQOfficialMS.file_image(image)

src/plugins/github/plugins/github_issue/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@Author : yanyongyu
33
@Date : 2021-03-09 15:15:02
44
@LastEditors : yanyongyu
5-
@LastEditTime : 2023-12-11 12:04:57
5+
@LastEditTime : 2024-06-02 16:49:23
66
@Description : None
77
@GitHub : https://github.com/yanyongyu
88
"""
@@ -19,8 +19,6 @@
1919
from nonebot.adapters.qq import MessageSegment as QQOfficialMS
2020

2121
from src.plugins.github import config
22-
from src.providers.filehost import save_image
23-
from src.plugins.github.helpers import NO_GITHUB_EVENT, MATCH_WHEN_GROUP
2422
from src.plugins.github.libs.renderer import issue_to_image, pr_diff_to_image
2523
from src.plugins.github.cache.message_tag import (
2624
IssueTag,
@@ -33,6 +31,11 @@
3331
TargetType,
3432
extract_sent_message,
3533
)
34+
from src.plugins.github.helpers import (
35+
NO_GITHUB_EVENT,
36+
MATCH_WHEN_GROUP,
37+
qqofficial_conditional_image,
38+
)
3639
from src.plugins.github.dependencies import (
3740
ISSUE,
3841
BINDED_GROUP,
@@ -114,7 +117,7 @@ async def handle_issue(
114117
case TargetType.QQ_USER | TargetType.QQ_GROUP:
115118
result = await issue.send(QQMS.image(img))
116119
case TargetType.QQ_OFFICIAL_USER | TargetType.QQ_OFFICIAL_GROUP:
117-
result = await issue.send(QQOfficialMS.image(await save_image(img)))
120+
result = await issue.send(await qqofficial_conditional_image(img))
118121
case TargetType.QQGUILD_USER | TargetType.QQGUILD_CHANNEL:
119122
result = await issue.send(QQOfficialMS.file_image(img))
120123

@@ -164,7 +167,7 @@ async def handle_pr_diff(
164167
case TargetType.QQ_USER | TargetType.QQ_GROUP:
165168
result = await pr_diff_link.send(QQMS.image(img))
166169
case TargetType.QQ_OFFICIAL_USER | TargetType.QQ_OFFICIAL_GROUP:
167-
result = await pr_diff_link.send(QQOfficialMS.image(await save_image(img)))
170+
result = await pr_diff_link.send(await qqofficial_conditional_image(img))
168171
case TargetType.QQGUILD_USER | TargetType.QQGUILD_CHANNEL:
169172
result = await pr_diff_link.send(QQOfficialMS.file_image(img))
170173

@@ -225,7 +228,7 @@ async def handle_short(
225228
case TargetType.QQ_USER | TargetType.QQ_GROUP:
226229
result = await issue_short.send(QQMS.image(img))
227230
case TargetType.QQ_OFFICIAL_USER | TargetType.QQ_OFFICIAL_GROUP:
228-
result = await issue_short.send(QQOfficialMS.image(await save_image(img)))
231+
result = await issue_short.send(await qqofficial_conditional_image(img))
229232
case TargetType.QQGUILD_USER | TargetType.QQGUILD_CHANNEL:
230233
result = await issue_short.send(QQOfficialMS.file_image(img))
231234

src/plugins/github/plugins/github_reply/content.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@Author : yanyongyu
33
@Date : 2021-03-26 14:45:05
44
@LastEditors : yanyongyu
5-
@LastEditTime : 2023-12-11 18:11:00
5+
@LastEditTime : 2024-06-02 16:50:38
66
@Description : None
77
@GitHub : https://github.com/yanyongyu
88
"""
@@ -21,10 +21,9 @@
2121
from nonebot.adapters.qq import MessageSegment as QQOfficialMS
2222

2323
from src.plugins.github import config
24-
from src.providers.filehost import save_image
25-
from src.plugins.github.helpers import NO_GITHUB_EVENT
2624
from src.plugins.github.libs.renderer import issue_to_image
2725
from src.plugins.github.libs.github import ISSUE_REGEX, FULLREPO_REGEX
26+
from src.plugins.github.helpers import NO_GITHUB_EVENT, qqofficial_conditional_image
2827
from src.plugins.github.dependencies import (
2928
ISSUE,
3029
OPTIONAL_REPLY_TAG,
@@ -123,7 +122,7 @@ async def handle_content(
123122
case TargetType.QQ_USER | TargetType.QQ_GROUP:
124123
result = await content.send(QQMS.image(img))
125124
case TargetType.QQ_OFFICIAL_USER | TargetType.QQ_OFFICIAL_GROUP:
126-
result = await content.send(QQOfficialMS.image(await save_image(img)))
125+
result = await content.send(await qqofficial_conditional_image(img))
127126
case TargetType.QQGUILD_USER | TargetType.QQGUILD_CHANNEL:
128127
result = await content.send(QQOfficialMS.file_image(img))
129128

src/plugins/github/plugins/github_reply/diff.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@Author : yanyongyu
33
@Date : 2021-03-26 14:59:59
44
@LastEditors : yanyongyu
5-
@LastEditTime : 2023-12-15 10:37:34
5+
@LastEditTime : 2024-06-02 16:51:03
66
@Description : None
77
@GitHub : https://github.com/yanyongyu
88
"""
@@ -21,11 +21,10 @@
2121
from nonebot.adapters.qq import MessageSegment as QQOfficialMS
2222

2323
from src.plugins.github import config
24-
from src.providers.filehost import save_image
25-
from src.plugins.github.helpers import NO_GITHUB_EVENT
2624
from src.plugins.github.libs.renderer import pr_diff_to_image
2725
from src.plugins.github.libs.github import ISSUE_REGEX, FULLREPO_REGEX
2826
from src.plugins.github.cache.message_tag import PullRequestTag, create_message_tag
27+
from src.plugins.github.helpers import NO_GITHUB_EVENT, qqofficial_conditional_image
2928
from src.plugins.github.dependencies import (
3029
ISSUE,
3130
OPTIONAL_REPLY_TAG,
@@ -115,7 +114,7 @@ async def handle_diff(
115114
case TargetType.QQ_USER | TargetType.QQ_GROUP:
116115
result = await diff.send(QQMS.image(img))
117116
case TargetType.QQ_OFFICIAL_USER | TargetType.QQ_OFFICIAL_GROUP:
118-
result = await diff.send(QQOfficialMS.image(await save_image(img)))
117+
result = await diff.send(await qqofficial_conditional_image(img))
119118
case TargetType.QQGUILD_USER | TargetType.QQGUILD_CHANNEL:
120119
result = await diff.send(QQOfficialMS.file_image(img))
121120

src/plugins/github/plugins/github_reply/readme.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@Author : yanyongyu
33
@Date : 2023-10-18 17:08:37
44
@LastEditors : yanyongyu
5-
@LastEditTime : 2023-12-11 18:15:15
5+
@LastEditTime : 2024-06-02 16:51:25
66
@Description : None
77
@GitHub : https://github.com/yanyongyu
88
"""
@@ -21,11 +21,10 @@
2121
from nonebot.adapters.github import ActionFailed, ActionTimeout
2222

2323
from src.plugins.github import config
24-
from src.providers.filehost import save_image
25-
from src.plugins.github.helpers import NO_GITHUB_EVENT
2624
from src.plugins.github.libs.github import FULLREPO_REGEX
2725
from src.plugins.github.libs.renderer import readme_to_image
2826
from src.plugins.github.cache.message_tag import RepoTag, create_message_tag
27+
from src.plugins.github.helpers import NO_GITHUB_EVENT, qqofficial_conditional_image
2928
from src.plugins.github.dependencies import (
3029
REPOSITORY,
3130
OPTIONAL_REPLY_TAG,
@@ -135,7 +134,7 @@ async def render_content(
135134
case TargetType.QQ_USER | TargetType.QQ_GROUP:
136135
result = await readme.send(QQMS.image(img))
137136
case TargetType.QQ_OFFICIAL_USER | TargetType.QQ_OFFICIAL_GROUP:
138-
result = await readme.send(QQOfficialMS.image(await save_image(img)))
137+
result = await readme.send(await qqofficial_conditional_image(img))
139138
case TargetType.QQGUILD_USER | TargetType.QQGUILD_CHANNEL:
140139
result = await readme.send(QQOfficialMS.file_image(img))
141140

src/plugins/github/webhooks/_dependencies.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@Author : yanyongyu
33
@Date : 2022-11-07 08:35:10
44
@LastEditors : yanyongyu
5-
@LastEditTime : 2023-12-06 17:07:07
5+
@LastEditTime : 2024-06-02 16:52:21
66
@Description : Webhook dependencies
77
@GitHub : https://github.com/yanyongyu
88
"""
@@ -21,12 +21,11 @@
2121
from nonebot.adapters.github.utils import get_attr_or_item
2222
from nonebot.adapters.onebot.v11 import Message as QQMessage
2323
from nonebot.adapters.onebot.v11 import MessageSegment as QQMS
24-
from nonebot.adapters.qq import MessageSegment as QQOfficialMS
2524
from nonebot.adapters.qq.exception import ActionFailed as QQOfficialActionFailed
2625

2726
from src.providers.redis import redis_client
28-
from src.providers.filehost import save_image
2927
from src.plugins.github.models import Subscription
28+
from src.plugins.github.helpers import qqofficial_conditional_image
3029
from src.plugins.github.cache.message_tag import Tag, create_message_tag
3130
from src.providers.platform import TargetInfo, get_target_bot, extract_sent_message
3231
from src.providers.platform.targets import (
@@ -133,12 +132,12 @@ async def send_subscriber_image(
133132
case QQOfficialUserInfo():
134133
result = await cast(QQOfficialBot, bot).send_to_c2c(
135134
openid=target_info.qq_user_open_id,
136-
message=QQOfficialMS.image(await save_image(image)),
135+
message=await qqofficial_conditional_image(image),
137136
)
138137
case QQOfficialGroupInfo():
139138
result = await cast(QQOfficialBot, bot).send_to_group(
140139
group_openid=target_info.qq_group_open_id,
141-
message=QQOfficialMS.image(await save_image(image)),
140+
message=await qqofficial_conditional_image(image),
142141
)
143142
case QQGuildUserInfo():
144143
logger.error("Unable to send message to QQGuild User", user=target_info)

0 commit comments

Comments
 (0)