コード例 #1
0
    def test_groups_by_language(self, notify):
        author = UserFactory()
        users = [
            UserFactory(language=language)
            for language 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)
コード例 #2
0
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():
        participant = conversation.conversationparticipant_set.get(
            user=subscription.user)
        payload = ConversationSerializer(
            participant,
            context={
                'request': MockRequest(user=subscription.user)
            }).data
        send_in_channel(subscription.reply_channel, topic, payload)
コード例 #3
0
    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')
コード例 #4
0
    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.muted = True
        participant.save()
        PushSubscription.objects.create(user=user, token='', platform=PushSubscriptionPlatform.ANDROID.value)

        notify.reset_mock()
        notify_message_push_subscribers(message)
        notify.assert_not_called()