messages: Fix unread_msgs accounting of wildcard mentions.

Previously, the unread_msgs data structure accounting (used for both
the web and mobile apps to determine the "Unread mentions" count
displayed in the UI) did not include wildcard mentions at all.

We fix this by adding the logic required to include properly that
data, with tests.  As discussed in #6040, it makes sense to include
muted streams and topics for the purpose of this calculation.

Fixes part of #6040.
This commit is contained in:
Tim Abbott 2019-08-25 20:11:18 -07:00
parent 625eb53b08
commit 5e19546c14
2 changed files with 89 additions and 1 deletions

View File

@ -845,9 +845,19 @@ def get_raw_unread_data(user_profile: UserProfile) -> RawUnreadMessagesResult:
user_ids_string=user_ids_string,
)
# TODO: Add support for alert words here as well.
is_mentioned = (row['flags'] & UserMessage.flags.mentioned) != 0
is_wildcard_mentioned = (row['flags'] & UserMessage.flags.wildcard_mentioned) != 0
if is_mentioned:
mentions.add(message_id)
if is_wildcard_mentioned:
if msg_type == Recipient.STREAM:
stream_id = row['message__recipient__type_id']
topic = row[MESSAGE__TOPIC]
if not is_row_muted(stream_id, recipient_id, topic):
mentions.add(message_id)
else: # nocoverage # TODO: Test wildcard mentions in PMs.
mentions.add(message_id)
return dict(
pm_dict=pm_dict,
@ -957,6 +967,9 @@ def apply_unread_message_event(user_profile: UserProfile,
if 'mentioned' in flags:
state['mentions'].add(message_id)
if 'wildcard_mentioned' in flags:
if message_id in state['unmuted_stream_msgs']:
state['mentions'].add(message_id)
def remove_message_id_from_unread_mgs(state: RawUnreadMessagesResult,
message_id: int) -> None:

View File

@ -622,6 +622,16 @@ class EventsRegisterTest(ZulipTestCase):
)
def test_wildcard_mentioned_send_message_events(self) -> None:
for i in range(3):
content = 'mentioning... @**all** hello ' + str(i)
self.do_test(
lambda: self.send_stream_message(self.example_email('cordelia'),
"Verona",
content)
)
def test_pm_send_message_events(self) -> None:
self.do_test(
lambda: self.send_personal_message(self.example_email('cordelia'),
@ -3082,10 +3092,75 @@ class GetUnreadMsgsTest(ZulipTestCase):
)
um.flags |= UserMessage.flags.mentioned
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [stream_message_id])
um.flags = UserMessage.flags.has_alert_word
um.save()
result = get_unread_data()
# TODO: This should change when we make alert words work better.
self.assertEqual(result['mentions'], [])
um.flags = UserMessage.flags.wildcard_mentioned
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [stream_message_id])
um.flags = 0
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [])
# Test with a muted stream
um = UserMessage.objects.get(
user_profile_id=user_profile.id,
message_id=muted_stream_message_id
)
um.flags = UserMessage.flags.mentioned
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [muted_stream_message_id])
um.flags = UserMessage.flags.has_alert_word
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [])
um.flags = UserMessage.flags.wildcard_mentioned
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [])
um.flags = 0
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [])
# Test with a muted topic
um = UserMessage.objects.get(
user_profile_id=user_profile.id,
message_id=muted_topic_message_id
)
um.flags = UserMessage.flags.mentioned
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [muted_topic_message_id])
um.flags = UserMessage.flags.has_alert_word
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [])
um.flags = UserMessage.flags.wildcard_mentioned
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [])
um.flags = 0
um.save()
result = get_unread_data()
self.assertEqual(result['mentions'], [])
class ClientDescriptorsTest(ZulipTestCase):
def test_get_client_info_for_all_public_streams(self) -> None:
hamlet = self.example_user('hamlet')