fix(whatsapp): normalize neonize message timestamps to seconds

Signed-off-by: jipeng6036-del <241105014+jipeng6036-del@users.noreply.github.com>
This commit is contained in:
jipeng6036-del authored and chengyongru committed 2026-10-10 10:33:55 +08:00
1 parent 2748b647aa
commit e5a69544e8
2 files changed
+36 -1

No files matched your search

+2 -1
View File
@@ -679,7 +679,8 @@ class WhatsAppChannel(BaseChannel):
if chat_jid == "status@broadcast":
return
timestamp = float(_safe_attr(info, "Timestamp", 0) or 0)
# Neonize encodes MessageInfo.Timestamp as Unix milliseconds.
timestamp = float(_safe_attr(info, "Timestamp", 0) or 0) / 1000
if self._started_at and timestamp and timestamp < self._started_at:
return
@@ -588,6 +588,40 @@ async def test_group_allow_from_does_not_allow_same_participant_in_other_group()
assert bus.inbound_size == 0
@pytest.mark.asyncio
async def test_message_before_startup_is_dropped_with_millisecond_timestamp() -> None:
bus = MessageBus()
ch = WhatsAppChannel({"enabled": True, "allowFrom": ["*"]}, bus)
ch._started_at = 1_700_000_000.250
ch._send_read_receipt = AsyncMock()
await ch._handle_neonize_message(
SimpleNamespace(download_any=AsyncMock()),
_event(message=_Proto(conversation="old message"), timestamp=1_700_000_000_249),
)
assert bus.inbound_size == 0
ch._send_read_receipt.assert_not_awaited()
@pytest.mark.parametrize("timestamp", [1_700_000_000_251, 1_700_000_001_987])
@pytest.mark.asyncio
async def test_message_after_startup_has_timestamp_in_seconds(timestamp: int) -> None:
bus = MessageBus()
ch = WhatsAppChannel({"enabled": True, "allowFrom": ["*"]}, bus)
ch._started_at = 1_700_000_000.250
await ch._handle_neonize_message(
SimpleNamespace(download_any=AsyncMock()),
_event(message=_Proto(conversation="new message"), timestamp=timestamp),
)
assert bus.inbound_size == 1
msg = await bus.consume_inbound()
assert msg.content == "new message"
assert msg.metadata["timestamp"] == timestamp // 1000
@pytest.mark.asyncio
async def test_read_receipt_is_requested_once_after_dedup() -> None:
ch = _make_channel()