Skip to content

Commit

Permalink
✨ 去除MessageFactory和MessageSegmentFactory的泛型,补充成员方法
Browse files Browse the repository at this point in the history
  • Loading branch information
AzideCupric committed Dec 27, 2023
1 parent c589265 commit 39c880e
Show file tree
Hide file tree
Showing 15 changed files with 626 additions and 65 deletions.
309 changes: 293 additions & 16 deletions nonebot_plugin_saa/abstract_factories.py

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions nonebot_plugin_saa/adapters/dodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -91,8 +90,8 @@ async def _image(image: Image, bot: BaseBot) -> MessageSegment:

@register_dodo(Reply)
def _reply(reply: Reply) -> MessageSegment:
assert isinstance(reply.data, DodoMessageId)
return MessageSegment.reference(reply.data.message_id)
assert isinstance(mid := reply.data["message_id"], DodoMessageId)
return MessageSegment.reference(mid.message_id)

@register_dodo(Mention)
def _mention(mention: Mention) -> MessageSegment:
Expand Down Expand Up @@ -181,10 +180,13 @@ async def pin(self, is_cancel: bool = False):
def raw(self) -> str:
return self.message_id

def extract_message_id(self) -> DodoMessageId:
return DodoMessageId(message_id=self.message_id)

Check warning on line 184 in nonebot_plugin_saa/adapters/dodo.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/dodo.py#L184

Added line #L184 was not covered by tests

@register_sender(adapter)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
16 changes: 10 additions & 6 deletions nonebot_plugin_saa/adapters/feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ..types import Text, Image, Reply, Mention
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -78,8 +77,8 @@ def _mention(m: Mention) -> MessageSegment:

@register_feishu(Reply)
def _reply(r: Reply) -> MessageSegment:
assert isinstance(r.data, FeishuMessageId)
return MessageSegment("reply", {"message_id": r.data.message_id})
assert isinstance(mid := r.data["message_id"], FeishuMessageId)
return MessageSegment("reply", {"message_id": mid.message_id})

@register_target_extractor(PrivateMessageEvent)
def _extract_private_msg_event(event: Event) -> TargetFeishuPrivate:
Expand All @@ -105,6 +104,9 @@ async def revoke(self):
def raw(self) -> Any:
return self.data

def extract_message_id(self) -> FeishuMessageId:
return FeishuMessageId(message_id=self.message_id)

Check warning on line 108 in nonebot_plugin_saa/adapters/feishu.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/feishu.py#L108

Added line #L108 was not covered by tests

@register_message_id_getter(MessageEvent)
def _(event: Event) -> FeishuMessageId:
assert isinstance(event, MessageEvent)
Expand All @@ -113,7 +115,7 @@ def _(event: Event) -> FeishuMessageId:
@register_sender(adapter)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down Expand Up @@ -142,8 +144,10 @@ async def send(
message_to_send = Message()
for message_segment_factory in full_msg:
if isinstance(message_segment_factory, Reply):
assert isinstance(message_segment_factory.data, FeishuMessageId)
reply_to_message_id = message_segment_factory.data.message_id
assert isinstance(
mid := message_segment_factory.data["message_id"], FeishuMessageId
)
reply_to_message_id = mid.message_id
continue

message_segment = await message_segment_factory.build(bot)
Expand Down
11 changes: 7 additions & 4 deletions nonebot_plugin_saa/adapters/kaiheila.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -84,8 +83,8 @@ def _mention(m: Mention) -> MessageSegment:

@register_kaiheila(Reply)
def _reply(r: Reply) -> MessageSegment:
assert isinstance(r.data, KaiheilaMessageId)
return MessageSegment.quote(r.data.message_id)
assert isinstance(mid := r.data["message_id"], KaiheilaMessageId)
return MessageSegment.quote(mid.message_id)

@register_target_extractor(PrivateMessageEvent)
def _extract_private_msg_event(event: Event) -> TargetKaiheilaPrivate:
Expand Down Expand Up @@ -125,6 +124,10 @@ async def revoke(self):
def raw(self) -> MessageCreateReturn:
return self.data

def extract_message_id(self) -> MessageId:
assert self.data.msg_id
return KaiheilaMessageId(message_id=self.data.msg_id)

Check warning on line 129 in nonebot_plugin_saa/adapters/kaiheila.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/kaiheila.py#L128-L129

Added lines #L128 - L129 were not covered by tests

@register_message_id_getter(MessageEvent)
def _(event: Event) -> KaiheilaMessageId:
assert isinstance(event, MessageEvent)
Expand All @@ -133,7 +136,7 @@ def _(event: Event) -> KaiheilaMessageId:
@register_sender(SupportedAdapters.kaiheila)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
10 changes: 6 additions & 4 deletions nonebot_plugin_saa/adapters/onebot_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
AggregatedMessageFactory,
register_ms_adapter,
assamble_message_factory,
Expand Down Expand Up @@ -71,8 +70,8 @@ async def _mention(m: Mention) -> MessageSegment:

@register_onebot_v11(Reply)
async def _reply(r: Reply) -> MessageSegment:
assert isinstance(r.data, OB11MessageId)
return MessageSegment.reply(r.data.message_id)
assert isinstance(mid := r.data["message_id"], OB11MessageId)
return MessageSegment.reply(mid.message_id)

@register_target_extractor(PrivateMessageEvent)
def _extract_private_msg_event(event: Event) -> TargetQQPrivate:
Expand Down Expand Up @@ -170,10 +169,13 @@ async def revoke(self):
def raw(self) -> Any:
return self.message_id

def extract_message_id(self) -> OB11MessageId:
return OB11MessageId(message_id=self.message_id)

Check warning on line 173 in nonebot_plugin_saa/adapters/onebot_v11.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/onebot_v11.py#L173

Added line #L173 was not covered by tests

@register_sender(SupportedAdapters.onebot_v11)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
10 changes: 6 additions & 4 deletions nonebot_plugin_saa/adapters/onebot_v12.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -97,8 +96,8 @@ async def _mention(m: Mention) -> MessageSegment:

@register_onebot_v12(Reply)
async def _reply(r: Reply) -> MessageSegment:
assert isinstance(r.data, OB12MessageId)
return MessageSegment.reply(r.data.message_id)
assert isinstance(mid := r.data["message_id"], OB12MessageId)
return MessageSegment.reply(mid.message_id)

@register_target_extractor(PrivateMessageEvent)
def _extract_private_msg_event(event: Event) -> PlatformTarget:
Expand Down Expand Up @@ -254,10 +253,13 @@ async def revoke(self):
def raw(self):
return self.message_id

def extract_message_id(self) -> OB12MessageId:
return OB12MessageId(message_id=self.message_id)

Check warning on line 257 in nonebot_plugin_saa/adapters/onebot_v12.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/onebot_v12.py#L257

Added line #L257 was not covered by tests

@register_sender(SupportedAdapters.onebot_v12)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
13 changes: 9 additions & 4 deletions nonebot_plugin_saa/adapters/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ..auto_select_bot import register_list_targets
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -73,8 +72,8 @@ def _mention(m: Mention) -> MessageSegment:

@register_qq(Reply)
def _reply(r: Reply) -> MessageSegment:
assert isinstance(r.data, QQMessageId)
return MessageSegment.reference(r.data.message_id)
assert isinstance(mid := r.data["message_id"], QQMessageId)
return MessageSegment.reference(mid.message_id)

Check warning on line 76 in nonebot_plugin_saa/adapters/qq.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/qq.py#L75-L76

Added lines #L75 - L76 were not covered by tests

@register_target_extractor(GuildMessageEvent)
def extract_message_event(event: Event) -> PlatformTarget:
Expand Down Expand Up @@ -138,10 +137,16 @@ async def revoke(self, hidetip=False):
def raw(self):
return self.msg_return

def extract_message_id(self) -> QQMessageId:
assert hasattr(self.msg_return, "id")
id = getattr(self.msg_return, "id")
assert isinstance(id, str)
return QQMessageId(message_id=id)

Check warning on line 144 in nonebot_plugin_saa/adapters/qq.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/qq.py#L141-L144

Added lines #L141 - L144 were not covered by tests

@register_sender(SupportedAdapters.qq)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target: PlatformTarget,
event: Optional[Event],
at_sender: bool,
Expand Down
11 changes: 7 additions & 4 deletions nonebot_plugin_saa/adapters/qqguild.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ..auto_select_bot import register_list_targets
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -63,8 +62,8 @@ def _mention(m: Mention) -> MessageSegment:

@register_qqguild(Reply)
def _reply(r: Reply) -> MessageSegment:
assert isinstance(r.data, QQGuildMessageId)
return MessageSegment.reference(r.data.message_id)
assert isinstance(mid := r.data["message_id"], QQGuildMessageId)
return MessageSegment.reference(mid.message_id)

Check warning on line 66 in nonebot_plugin_saa/adapters/qqguild.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/qqguild.py#L65-L66

Added lines #L65 - L66 were not covered by tests

@register_target_extractor(MessageEvent)
def extract_message_event(event: Event) -> PlatformTarget:
Expand Down Expand Up @@ -108,10 +107,14 @@ async def revoke(self, hidetip=False):
def raw(self):
return self.sent_msg

def extract_message_id(self) -> QQGuildMessageId:
assert self.sent_msg.id
return QQGuildMessageId(message_id=self.sent_msg.id)

Check warning on line 112 in nonebot_plugin_saa/adapters/qqguild.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/qqguild.py#L111-L112

Added lines #L111 - L112 were not covered by tests

@register_sender(SupportedAdapters.qqguild)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
18 changes: 12 additions & 6 deletions nonebot_plugin_saa/adapters/red.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..utils import SupportedAdapters, SupportedPlatform
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
AggregatedMessageFactory,
register_ms_adapter,
assamble_message_factory,
Expand Down Expand Up @@ -72,11 +71,11 @@ async def _mention(m: Mention) -> MessageSegment:

@register_red(Reply)
async def _reply(r: Reply) -> MessageSegment:
assert isinstance(r.data, RedMessageId)
assert isinstance(mid := r.data["message_id"], RedMessageId)
return MessageSegment.reply(
message_seq=r.data.message_seq,
message_id=r.data.message_id,
sender_uin=r.data.sender_uin,
message_seq=mid.message_seq,
message_id=mid.message_id,
sender_uin=mid.sender_uin,
)

@register_target_extractor(PrivateMessageEvent)
Expand Down Expand Up @@ -123,10 +122,17 @@ async def revoke(self):
def raw(self) -> MessageModel:
return self.message

def extract_message_id(self) -> RedMessageId:
return RedMessageId(

Check warning on line 126 in nonebot_plugin_saa/adapters/red.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/red.py#L126

Added line #L126 was not covered by tests
message_seq=self.message.msgSeq,
message_id=self.message.msgId,
sender_uin=self.message.senderUin,
)

@register_sender(SupportedAdapters.red)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down
16 changes: 10 additions & 6 deletions nonebot_plugin_saa/adapters/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..types import Text, Image, Reply, Mention
from ..abstract_factories import (
MessageFactory,
MessageSegmentFactory,
register_ms_adapter,
assamble_message_factory,
)
Expand Down Expand Up @@ -71,8 +70,8 @@ async def _mention(m: Mention) -> MessageSegment:

@register_telegram(Reply)
async def _reply(r: Reply) -> MessageSegment:
assert isinstance(r.data, TelegramMessageId)
return MessageSegment("reply", {"message_id": str(r.data.message_id)})
assert isinstance(mid := r.data["message_id"], TelegramMessageId)
return MessageSegment("reply", {"message_id": str(mid.message_id)})

@register_target_extractor(PrivateMessageEvent)
@register_target_extractor(GroupMessageEvent)
Expand Down Expand Up @@ -129,6 +128,9 @@ async def revoke(self):
def raw(self):
return self.messages

def extract_message_id(self) -> List[TelegramMessageId]:
return [TelegramMessageId(message_id=x.message_id) for x in self.messages]

Check warning on line 132 in nonebot_plugin_saa/adapters/telegram.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/adapters/telegram.py#L132

Added line #L132 was not covered by tests

@register_message_id_getter(MessageEvent)
def _(event: Event):
assert isinstance(event, MessageEvent)
Expand All @@ -137,7 +139,7 @@ def _(event: Event):
@register_sender(SupportedAdapters.telegram)
async def send(
bot,
msg: MessageFactory[MessageSegmentFactory],
msg: MessageFactory,
target,
event,
at_sender: bool,
Expand Down Expand Up @@ -166,8 +168,10 @@ async def send(
message_to_send = Message()
for message_segment_factory in full_msg:
if isinstance(message_segment_factory, Reply):
assert isinstance(message_segment_factory.data, TelegramMessageId)
reply_to_message_id = message_segment_factory.data.message_id
assert isinstance(
mid := message_segment_factory.data["message_id"], TelegramMessageId
)
reply_to_message_id = mid.message_id
continue

if (
Expand Down
3 changes: 2 additions & 1 deletion nonebot_plugin_saa/registries/message_id.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from abc import ABC
from typing_extensions import Annotated
from typing import Dict, Type, Callable, Optional

Expand All @@ -8,7 +9,7 @@
from ..utils import SupportedAdapters


class MessageId(SerializationMeta):
class MessageId(SerializationMeta, ABC):
_index_key = "adapter_name"

adapter_name: SupportedAdapters
Expand Down
7 changes: 7 additions & 0 deletions nonebot_plugin_saa/registries/receipt.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Any
from abc import abstractmethod

from nonebot import get_bot
from nonebot.adapters import Bot

from .message_id import MessageId
from .meta import SerializationMeta
from ..utils import SupportedAdapters

Expand All @@ -22,3 +24,8 @@ async def revoke(self):
@property
def raw(self) -> Any:
...

@abstractmethod
def extract_message_id(self) -> MessageId:
"""从 Receipt 中提取 MessageId"""
...

Check warning on line 31 in nonebot_plugin_saa/registries/receipt.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/registries/receipt.py#L31

Added line #L31 was not covered by tests
Loading

0 comments on commit 39c880e

Please sign in to comment.