def send_messages(sender, instance, created, **kwargs): """When there is a message in a conversation we need to send it to any subscribed participants.""" message = instance conversation = message.conversation topic = 'conversations:message' for subscription in ChannelSubscription.objects.recent().filter( user__in=conversation.participants.all()).distinct(): payload = ConversationMessageSerializer( message, context={ 'request': MockRequest(user=subscription.user) }).data send_in_channel(subscription.reply_channel, topic, payload) if created and message.is_thread_reply( ) and subscription.user != message.author: payload = ConversationMessageSerializer( message.thread, context={ 'request': MockRequest(user=subscription.user) }).data send_in_channel(subscription.reply_channel, topic, payload) # Send push notification and conversation updates when a message is created, but not when it is modified if not created: return tasks.notify_message_push_subscribers(message) # Send conversations object to participants after sending a message # (important for unread_message_count) # Exclude the author because their seen_up_to status gets updated, # so they will receive the `send_conversation_update` message topic = 'conversations:conversation' # Can be skipped for thread replies, as they don't alter the conversations object if message.is_thread_reply(): return for subscription in ChannelSubscription.objects.recent()\ .filter(user__in=conversation.participants.all())\ .exclude(user=message.author).distinct(): payload = ConversationSerializer( conversation, context={ 'request': MockRequest(user=subscription.user) }).data send_in_channel(subscription.reply_channel, topic, payload)
def test_message_notification(self, notify): author = UserFactory() user = UserFactory() group = GroupFactory(members=[author, user]) conversation = Conversation.objects.get_or_create_for_target(group) message = conversation.messages.create(author=author, content='bla') subscriptions = [ PushSubscription.objects.create( user=user, token='', platform=PushSubscriptionPlatform.ANDROID.value) ] notify.reset_mock() notify_message_push_subscribers(message) notify.assert_called_once_with(message, subscriptions, 'en')
def test_no_message_notification_if_muted(self, notify): author = UserFactory() user = UserFactory() group = GroupFactory(members=[author, user]) conversation = Conversation.objects.get_or_create_for_target(group) message = conversation.messages.create(author=author, content='bla') participant = ConversationParticipant.objects.get( user=user, conversation=conversation) participant.email_notifications = False participant.save() PushSubscription.objects.create( user=user, token='', platform=PushSubscriptionPlatform.ANDROID.value) notify.reset_mock() notify_message_push_subscribers(message) notify.assert_not_called()
def test_groups_by_language(self, notify): author = UserFactory() users = [UserFactory(language=l) for l in ('de', 'de', 'en', 'fr')] group = GroupFactory(members=[author, *users]) conversation = Conversation.objects.get_or_create_for_target(group) message = conversation.messages.create(author=author, content='bla') subscriptions = [ PushSubscription.objects.create( user=u, token='', platform=PushSubscriptionPlatform.ANDROID.value) for u in users ] notify.reset_mock() notify_message_push_subscribers(message) notify.assert_has_calls([ call(message, subscriptions[:2], 'de'), call(message, subscriptions[2:3], 'en'), call(message, subscriptions[3:4], 'fr'), ]) self.assertEqual(len(notify.call_args_list), 3)
def test_no_reply_notification_if_muted(self, notify): author = UserFactory() reply_author = UserFactory() group = GroupFactory(members=[author, reply_author]) conversation = Conversation.objects.get_or_create_for_target(group) message = conversation.messages.create(author=author, content='bla') reply = ConversationMessage.objects.create(author=reply_author, conversation=conversation, thread=message, content='reply') participant = ConversationThreadParticipant.objects.get( user=author, thread=reply.thread) participant.muted = True participant.save() PushSubscription.objects.create( user=author, token='', platform=PushSubscriptionPlatform.ANDROID.value) notify.reset_mock() notify_message_push_subscribers(reply) notify.assert_not_called()