fix: discussion parent message count and last message time out of sync with the discussion
Proposed changes (including videos or screenshots)
Root cause 1: the refresh reads outdated values (lag of one message). In #33034 we moved the flush of the roomUpdater out of the notifyUsersOnMessage callback and into afterSaveMessage, where it now runs after the whole callback chain. Previously the room record was already updated by the MEDIUM priority notifyUsersOnMessage callback and then freshly consumed by PropagateDiscussionMetadata (LOW priority); nowadays that callback reads the room before the flush, so it propagates the pre-increment msgs and the previous lm.
Root cause 2: the refresh never runs for system messages. Message.saveSystemMessage does not run the afterSaveMessage callbacks at all: it inserts the message, increments the room counter and notifies the message and room changes. Since PropagateDiscussionMetadata is one of those callbacks, system messages added to a discussion leave the parent message outdated until the next user message triggers the refresh. This one is not a regression from #33034, it has been like that since discussions were introduced.
Solution 1: use the roomUpdater received by the callback to read the update filter operations staged in memory (getRawUpdateFilter()) and apply them on top of the room fetched from the database, so the discussion metadata is computed against the state the room is about to have.
Solution 2: refresh the discussion metadata from saveSystemMessage itself. The Rooms.incMsgCountById call it already performs is replaced with a new findOneAndIncMsgCountById, which returns the updated room from the increment already in flight, so the refresh costs no extra round trip.
Issue(s)
Steps to test or reproduce
Lag of one message
- Go to any channel and create a new discussion.
- Send the first message inside the newly created discussion.
- Look at the parent message in the original channel: it says "Reply / No messages yet" (
dcountis 0,dlmis not set). - Send a second message inside the discussion: it says "1 message", with the timestamp of the first one.
Expected after the fix: the parent message reads "1 message" right after step 2, with the timestamp
of that message.
System messages
- Create a discussion and send a message inside it.
- Add a user to the discussion, or change its topic, so a system message is added to it.
- Look at the parent message: the count and the timestamp do not change until a regular message is sent inside the discussion.
Expected after the fix: the parent message reflects the new message as soon as the system message is
added.
Further comments
Summary by CodeRabbit
-
Bug Fixes
- Fixed discussion parent message counts so they stay synchronized when regular or system messages are added.
- Ensured the latest discussion activity timestamp is accurately reflected on the parent message.
- Improved consistency of discussion metadata after activity.
-
Tests
- Added end-to-end coverage for message counts and activity timestamps, including discussion rename system messages.