Skip to content

Commit

Permalink
don't record satori message-deleted and message-updated
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq committed Sep 9, 2024
1 parent c192d10 commit 171345a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
5 changes: 3 additions & 2 deletions nonebot_plugin_chatrecorder/adapters/satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
from ..utils import format_platform, record_type, remove_timezone

try:
from nonebot.adapters.satori import Bot, Message, MessageEvent
from nonebot.adapters.satori import Bot, Message
from nonebot.adapters.satori.event import MessageCreatedEvent
from nonebot.adapters.satori.models import MessageObject

adapter = SupportedAdapter.satori

@event_postprocessor
async def record_recv_msg(bot: Bot, event: MessageEvent):
async def record_recv_msg(bot: Bot, event: MessageCreatedEvent):
session = extract_session(bot, event)
session_persist_id = await get_session_persist_id(session)

Expand Down
64 changes: 63 additions & 1 deletion tests/test_satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from nonebot.adapters.satori.event import (
PrivateMessageCreatedEvent,
PublicMessageCreatedEvent,
PublicMessageDeletedEvent,
PublicMessageUpdatedEvent,
)
from nonebot.adapters.satori.models import (
Channel,
Expand All @@ -20,7 +22,7 @@
from nonebot.compat import type_validate_python
from nonebug.app import App

from .utils import check_record
from .utils import assert_no_record, check_record


def fake_public_message_created_event(
Expand Down Expand Up @@ -65,6 +67,54 @@ def fake_private_message_created_event(
)


def fake_public_message_deleted_event(
content: str, msg_id: str
) -> PublicMessageDeletedEvent:
return type_validate_python(
PublicMessageDeletedEvent,
{
"id": 1,
"type": "message-deleted",
"platform": "kook",
"self_id": "2233",
"timestamp": 17000000000,
"channel": {"id": "6677", "type": 0, "name": "test"},
"guild": {"id": "5566", "name": "test"},
"user": {"id": "3344", "nick": "test"},
"member": {
"user": {"id": "3344", "nick": "test"},
"nick": "test",
"joined_at": None,
},
"message": {"id": msg_id, "content": content},
},
)


def fake_public_message_updated_event(
content: str, msg_id: str
) -> PublicMessageUpdatedEvent:
return type_validate_python(
PublicMessageUpdatedEvent,
{
"id": 1,
"type": "message-updated",
"platform": "kook",
"self_id": "2233",
"timestamp": 17000000000,
"channel": {"id": "6677", "type": 0, "name": "test"},
"guild": {"id": "5566", "name": "test"},
"user": {"id": "3344", "nick": "test"},
"member": {
"user": {"id": "3344", "nick": "test"},
"nick": "test",
"joined_at": None,
},
"message": {"id": msg_id, "content": content},
},
)


async def test_record_recv_msg(app: App):
"""测试记录收到的消息"""
from nonebot_plugin_chatrecorder.message import serialize_message
Expand All @@ -75,6 +125,9 @@ async def test_record_recv_msg(app: App):
private_msg = "test private message created"
private_msg_id = "56163f81-de30-4c39-b4c4-3a205d0be9db"

msg_deleted_id = "56163f81-de30-4c39-b4c4-3a205d0be9dc"
msg_updated_id = "56163f81-de30-4c39-b4c4-3a205d0be9dd"

async with app.test_matcher() as ctx:
adapter = get_driver()._adapters[Adapter.get_name()]
bot = ctx.create_bot(
Expand All @@ -100,6 +153,12 @@ async def test_record_recv_msg(app: App):
event = fake_private_message_created_event(private_msg, private_msg_id)
ctx.receive_event(bot, event)

event = fake_public_message_deleted_event("msg deleted", msg_deleted_id)
ctx.receive_event(bot, event)

event = fake_public_message_updated_event("msg updated", msg_updated_id)
ctx.receive_event(bot, event)

await check_record(
"2233",
"Satori",
Expand Down Expand Up @@ -130,6 +189,9 @@ async def test_record_recv_msg(app: App):
private_msg,
)

await assert_no_record(msg_deleted_id)
await assert_no_record(msg_updated_id)


async def test_record_send_msg(app: App):
"""测试记录发送的消息"""
Expand Down
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ async def check_record(
assert record.time == remove_timezone(time)
assert record.message == message
assert record.plain_text == plain_text


async def assert_no_record(message_id: str):
from nonebot_plugin_orm import get_session
from sqlalchemy import select

from nonebot_plugin_chatrecorder.model import MessageRecord

statement = select(MessageRecord).where(MessageRecord.message_id == message_id)
async with get_session() as db_session:
records = (await db_session.scalars(statement)).all()

assert len(records) == 0

0 comments on commit 171345a

Please sign in to comment.