-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Prevent stale reactions when usernames change or users are deleted #39514
Description
Description
While reviewing the reaction implementation, I noticed that reactions are currently stored using usernames directly inside the message document.
For example, in setReaction a reaction entry is created as:
message.reactions[reaction] = { usernames: [], };
and the reacting user is stored like this:
message.reactions[reaction].usernames.push(user.username)
Because reactions reference usernames rather than userIds, this can lead to stale data in a few scenarios:
If a user changes their username, existing reactions still reference the old username.
If a user deletes their account, the username may remain in the message’s reaction list.
In both cases, the message document may end up referencing users that no longer exist or have changed identity.
Potential Improvement
A full redesign of the reaction schema would likely require a migration across all message documents, which may not be desirable for large deployments.
However, a smaller incremental improvement could improve correctness while keeping the current structure compatible.
Possible steps:
1. Store userId alongside username for new reactions.
Example:
message.reactions[reaction] = { usernames: [], userIds: [], }
2. When a username is changed, update reaction usernames by matching the stored userId.
3. When a user account is deleted, remove their reactions from messages using the stored userId.
This would allow improving reaction consistency while maintaining compatibility with existing message documents.
Related Discussion
Related: #20425
Contribution
If this aligns with the current architecture, I’d be happy to start with a small improvement (for example storing userId alongside username for new reactions) and iterate from there.