RocketChat / RocketChat/EmbeddedChat

Bug: Broken Message Insertion Logic in messageListHelpers

Open
#1,082 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
165
Forks
381
Avg merge
1d 2h
Merged PRs (30d)
1

Description

A bug exists in the message insertion logic that causes messages to be inserted at incorrect positions in the message list. The insertMessage function assumes a descending (newest-first) sort order, while the rest of the application renders messages in ascending (oldest-first) order.

This mismatch leads to messages appearing out of chronological order in the UI.


Observed Behavior

The insertMessage function attempts to keep messages sorted by timestamp (ts) using the following logic:

messages.findIndex((m) => new Date(m.ts) < new Date(message.ts))

If the message list is stored in ascending order (oldest to newest), this condition matches the first message in the list, causing the new message to be inserted at the beginning.

This results in an incorrect and scrambled message order.


Steps to Reproduce

  1. Start with an existing message list:
[10:00, 10:01, 10:03]
  1. Insert a new message with timestamp 10:02.

  2. The logic evaluates:

10:00 < 10:02  -> true (index 0)
  1. The new message is unshifted to the beginning:
[10:02, 10:00, 10:01, 10:03]

The messages are now out of chronological order.


Expected Behavior

The insertion logic should match the render order.

For an ascending message list, the function should:

  • Find the first message with a timestamp greater than the new message
  • Insert the new message before that item
  • Append to the end if no such message exists

This ensures messages remain correctly ordered in the UI.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search for messageListHelpers and inspect the insertMessage function and its callers to confirm the list's ascending render order. Reproduce insertion with timestamps such as 10:00, 10:01, 10:03, and 10:02; done means the new message is inserted before the first later timestamp or appended when it is newest.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.