From d452ad31e00f4d57425696ccf93061ae88182f8c Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Wed, 1 Jan 2020 15:27:14 +0000 Subject: [PATCH] server: Sort user_ids in recent PM conversations. This change should prevent test flakes, plus it's more deterministic behavior for clients, who will generally comma-join the ids into a key for their internal data structures. I was able to verify test coverage on this by making the sort reversed, which would cause test_huddle_send_message_events to fail. --- zerver/lib/events.py | 6 +++--- zerver/lib/message.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/zerver/lib/events.py b/zerver/lib/events.py index b3409a06fa..11dc46ccef 100644 --- a/zerver/lib/events.py +++ b/zerver/lib/events.py @@ -424,9 +424,9 @@ def apply_event(state: Dict[str, Any], if recipient_id not in conversations: conversations[recipient_id] = dict( - user_ids=[user_dict['id'] for user_dict in - event['message']['display_recipient'] if - user_dict['id'] != user_profile.id] + user_ids=sorted([user_dict['id'] for user_dict in + event['message']['display_recipient'] if + user_dict['id'] != user_profile.id]) ) conversations[recipient_id]['max_message_id'] = event['message']['id'] return diff --git a/zerver/lib/message.py b/zerver/lib/message.py index 46fa7202ef..2e4ec344ed 100644 --- a/zerver/lib/message.py +++ b/zerver/lib/message.py @@ -1133,4 +1133,9 @@ def get_recent_private_conversations(user_profile: UserProfile) -> Dict[int, Dic user_profile_id=user_profile.id).values_list( "recipient_id", "user_profile_id"): recipient_map[recipient_id]['user_ids'].append(user_profile_id) + + # Sort to prevent test flakes and client bugs. + for rec in recipient_map.values(): + rec['user_ids'].sort() + return recipient_map