diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index d8849a6d3e..65a136384b 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -1049,7 +1049,7 @@ def do_send_messages(messages_maybe_none): ) message['mention_data'] = mention_data - if message['message'].recipient.type == Recipient.STREAM: + if message['message'].is_stream_message(): stream_id = message['message'].recipient.type_id stream_topic = StreamTopicTarget( stream_id=stream_id, @@ -1173,7 +1173,7 @@ def do_send_messages(messages_maybe_none): for user_id in message['active_user_ids'] ] - if message['message'].recipient.type == Recipient.STREAM: + if message['message'].is_stream_message(): # Note: This is where authorization for single-stream # get_updates happens! We only attach stream data to the # notify new_message request if it's a public stream, @@ -1283,7 +1283,7 @@ def create_user_messages(message, um_eligible_user_ids, long_term_idle_user_ids, user_messages = [] for um in ums_to_create: if (um.user_profile_id in long_term_idle_user_ids and - message.recipient.type == Recipient.STREAM and + message.is_stream_message() and int(um.flags) == 0): continue user_messages.append(um) @@ -3289,7 +3289,7 @@ def do_update_message(user_profile, message, topic_name, propagate_mode, } # type: Dict[str, Any] changed_messages = [message] - if message.recipient.type == Recipient.STREAM: + if message.is_stream_message(): stream_id = message.recipient.type_id event['stream_name'] = Stream.objects.get(id=stream_id).name @@ -3339,7 +3339,7 @@ def do_update_message(user_profile, message, topic_name, propagate_mode, if Message.content_has_attachment(prev_content) or Message.content_has_attachment(message.content): check_attachment_reference_change(prev_content, message) - if message.recipient.type == Recipient.STREAM: + if message.is_stream_message(): if topic_name is not None: new_topic_name = topic_name else: @@ -4073,7 +4073,7 @@ def do_claim_attachments(message): path_id = attachment_url_to_path_id(url) user_profile = message.sender is_message_realm_public = False - if message.recipient.type == Recipient.STREAM: + if message.is_stream_message(): is_message_realm_public = Stream.objects.get(id=message.recipient.type_id).is_public() if not validate_attachment_request(user_profile, path_id): diff --git a/zerver/lib/notifications.py b/zerver/lib/notifications.py index 8c717c5a18..26bd2562c0 100644 --- a/zerver/lib/notifications.py +++ b/zerver/lib/notifications.py @@ -299,7 +299,7 @@ def do_send_missedmessage_events_reply_in_zulip(user_profile, missed_messages, m 'name': user_profile.full_name, 'messages': build_message_list(user_profile, missed_messages), 'message_count': message_count, - 'mention': missed_messages[0].recipient.type == Recipient.STREAM, + 'mention': missed_messages[0].is_stream_message(), 'unsubscribe_link': unsubscribe_link, }) @@ -419,7 +419,7 @@ def handle_missedmessage_emails(user_profile_id, missed_email_events): for msg_list in messages_by_recipient_subject.values(): msg = min(msg_list, key=lambda msg: msg.pub_date) - if msg.recipient.type == Recipient.STREAM: + if msg.is_stream_message(): msg_list.extend(get_context_for_message(msg)) # Send an email per recipient subject pair diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py index 1a68c858f8..59e6b2f9b5 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -392,9 +392,9 @@ def get_alert_from_message(message): return "New private group message from %s" % (sender_str,) elif message.recipient.type == Recipient.PERSONAL and message.trigger == 'private_message': return "New private message from %s" % (sender_str,) - elif message.recipient.type == Recipient.STREAM and message.trigger == 'mentioned': + elif message.is_stream_message() and message.trigger == 'mentioned': return "New mention from %s" % (sender_str,) - elif (message.recipient.type == Recipient.STREAM and + elif (message.is_stream_message() and (message.trigger == 'stream_push_notify' and message.stream_name)): return "New stream message from %s in %s" % (sender_str, message.stream_name,) else: @@ -481,7 +481,7 @@ def get_gcm_payload(user_profile, message): 'sender_avatar_url': absolute_avatar_url(message.sender), } - if message.recipient.type == Recipient.STREAM: + if message.is_stream_message(): android_data['recipient_type'] = "stream" android_data['stream'] = get_display_recipient(message.recipient) android_data['topic'] = message.subject diff --git a/zerver/models.py b/zerver/models.py index df57c8d7f3..90552d0a64 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -1124,6 +1124,17 @@ class Message(AbstractMessage): """ return self.subject + def is_stream_message(self): + # type: () -> bool + ''' + Find out whether a message is a stream message by + looking up its recipient.type. TODO: Make this + an easier operation by denormalizing the message + type onto Message, either explicity (message.type) + or implicitly (message.stream_id is not None). + ''' + return self.recipient.type == Recipient.STREAM + def get_realm(self): # type: () -> Realm return self.sender.realm diff --git a/zerver/tests/test_narrow.py b/zerver/tests/test_narrow.py index af5df98560..0143a13b34 100644 --- a/zerver/tests/test_narrow.py +++ b/zerver/tests/test_narrow.py @@ -586,8 +586,7 @@ class GetOldMessagesTest(ZulipTestCase): [self.example_email("iago"), self.example_email("cordelia")], ) personals = [m for m in get_user_messages(self.example_user('hamlet')) - if m.recipient.type == Recipient.PERSONAL or - m.recipient.type == Recipient.HUDDLE] + if not m.is_stream_message()] for personal in personals: emails = dr_emails(get_display_recipient(personal.recipient)) @@ -675,7 +674,7 @@ class GetOldMessagesTest(ZulipTestCase): self.subscribe(self.example_user("hamlet"), 'Scotland') self.send_stream_message(self.example_email("hamlet"), "Scotland") messages = get_user_messages(self.example_user('hamlet')) - stream_messages = [msg for msg in messages if msg.recipient.type == Recipient.STREAM] + stream_messages = [msg for msg in messages if msg.is_stream_message()] stream_name = get_display_recipient(stream_messages[0].recipient) stream_id = stream_messages[0].recipient.id @@ -712,7 +711,7 @@ class GetOldMessagesTest(ZulipTestCase): subdomain="zephyr") messages = get_user_messages(self.mit_user("starnine")) - stream_messages = [msg for msg in messages if msg.recipient.type == Recipient.STREAM] + stream_messages = [msg for msg in messages if msg.is_stream_message()] self.assertEqual(len(result["messages"]), 2) for i, message in enumerate(result["messages"]): @@ -751,7 +750,7 @@ class GetOldMessagesTest(ZulipTestCase): subdomain="zephyr") messages = get_user_messages(mit_user_profile) - stream_messages = [msg for msg in messages if msg.recipient.type == Recipient.STREAM] + stream_messages = [msg for msg in messages if msg.is_stream_message()] self.assertEqual(len(result["messages"]), 5) for i, message in enumerate(result["messages"]): self.assertEqual(message["type"], "stream") @@ -793,7 +792,7 @@ class GetOldMessagesTest(ZulipTestCase): subdomain="zephyr") messages = get_user_messages(mit_user_profile) - stream_messages = [msg for msg in messages if msg.recipient.type == Recipient.STREAM] + stream_messages = [msg for msg in messages if msg.is_stream_message()] self.assertEqual(len(result["messages"]), 7) for i, message in enumerate(result["messages"]): self.assertEqual(message["type"], "stream")