d99kris / nchat

Terminal-based Telegram / WhatsApp client for Linux and macOS
MIT License
716 stars 48 forks source link

Messages can appear in the wrong order in history due to low time precision #282

Open fulalas opened 2 months ago

fulalas commented 2 months ago

If I send 2 messages in a row really quick or forward (on mobile app) 2 messages at once, sometimes nchat show them in the wrong order. I presume this happens because nchat is using low time precision (maybe just seconds, not milliseconds?).

This is not a critical bug and this is not a regression.

nchat-5.1.29

d99kris commented 2 months ago

Hi @fulalas - thanks for reporting a bug. I can see the issue here too, if I forward two messages using WhatsApp from the phone on a different account, and receive them in nchat. I assume you also see this only for WhatsApp, right?

The WhatsApp glue layer implementation has some (likely unecessary) time stamp rounding to seconds (like you guessed), I will look into switching to millisecond timestamps (which is what nchat generally uses).

fulalas commented 2 months ago

I just confirmed it also happens using Telegram protocol.

d99kris commented 2 months ago

Thanks for sharing. I checked the WhatsApp (whatsmeow) and Telegram (tdlib) API's, and the message timestamp nchat receives is only at second-resolution. Internally nchat converts the timestamp to ms-resolution and uses a message id hash for the "ms-part", this is to have a consistent sorting order (even if it's wrong at least it's not random).

Anyway, the official Telegram and WhatsApp clients manage to list messages in correct order despite the timestamp resolution, so they likely sort based on some other data. I believe Telegram might sort based on message id, and for WhatsApp perhaps the messages are just sorted based on order received by the client (just a guess).

I will need to do a bit more research to see how this can be fixed. Just wanted to share a brief update on the troubleshooting done so far.

fulalas commented 2 months ago

Thanks a lot, man! Very kind of you :)