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)
Example #2
0
def send_user_updates(sender, instance, **kwargs):
    """Send profile updates to everyone except the user"""
    user = instance
    payload = UserSerializer(user, context={'request': MockRequest()}).data
    user_groups = user.groups.values('id')
    for subscription in ChannelSubscription.objects.recent().filter(user__groups__in=user_groups).exclude(user=user
                                                                                                          ).distinct():
        send_in_channel(subscription.reply_channel, topic='users:user', payload=payload)
Example #3
0
def send_conversation_update(sender, instance, **kwargs):
    # Update conversations object for user after updating their participation
    # (important for seen_up_to and unread_message_count)
    participant = instance

    topic = 'conversations:conversation'
    payload = ConversationSerializer(participant, context={'request': MockRequest(user=participant.user)}).data

    for subscription in ChannelSubscription.objects.recent().filter(user=instance.user):
        send_in_channel(subscription.reply_channel, topic, payload)
Example #4
0
def send_group_detail(group, user=None):
    qs = ChannelSubscription.objects.recent().distinct()
    if user:
        qs = qs.filter(user=user)
    else:
        qs = qs.filter(user__in=group.members.all())

    for subscription in qs:
        payload = GroupDetailSerializer(group, context={'request': MockRequest(user=subscription.user)}).data
        send_in_channel(subscription.reply_channel, topic='groups:group_detail', payload=payload)
Example #5
0
def send_reaction_update(sender, instance, **kwargs):
    reaction = instance
    message = reaction.message
    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)
def send_issue_updates(sender, issue, **kwargs):
    for subscription in ChannelSubscription.objects.recent().filter(
            user__issueparticipant__issue=issue).distinct():
        payload = IssueSerializer(issue,
                                  context={
                                      'request':
                                      MockRequest(user=subscription.user)
                                  }).data
        send_in_channel(subscription.reply_channel,
                        topic='issues:issue',
                        payload=payload)
def send_auth_user_updates(sender, instance, **kwargs):
    """Send full details to the user"""
    user = instance
    payload = AuthUserSerializer(user,
                                 context={
                                     'request': MockRequest(user=user)
                                 }).data
    for subscription in ChannelSubscription.objects.recent().filter(user=user):
        send_in_channel(subscription.reply_channel,
                        topic='auth:user',
                        payload=payload)
def send_feedback_updates(sender, instance, **kwargs):
    feedback = instance
    for subscription in ChannelSubscription.objects.recent().filter(
            user__in=feedback.about.place.group.members.all()).distinct():
        payload = FeedbackSerializer(feedback,
                                     context={
                                         'request':
                                         MockRequest(user=subscription.user)
                                     }).data
        send_in_channel(subscription.reply_channel,
                        topic='feedback:feedback',
                        payload=payload)
def place_subscription_updated(sender, instance, **kwargs):
    place = instance.place
    user = instance.user
    payload = PlaceSerializer(place,
                              context={
                                  'request': MockRequest(user=user)
                              }).data
    for subscription in ChannelSubscription.objects.recent().filter(
            user=user).distinct():
        send_in_channel(subscription.reply_channel,
                        topic='places:place',
                        payload=payload)
def send_place_updates(sender, instance, **kwargs):
    place = instance
    for subscription in ChannelSubscription.objects.recent().filter(
            user__in=place.group.members.all()).distinct():
        payload = PlaceSerializer(place,
                                  context={
                                      'request':
                                      MockRequest(user=subscription.user)
                                  }).data
        send_in_channel(subscription.reply_channel,
                        topic='places:place',
                        payload=payload)
Example #11
0
def send_participant_joined(sender, instance, created, **kwargs):
    """Notify other participants when someone joins"""
    if not created:
        return

    conversation = instance.conversation

    topic = 'conversations:conversation'

    for subscription in ChannelSubscription.objects.recent() \
            .filter(user__in=conversation.participants.all()) \
            .exclude(user=instance.user).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)
Example #12
0
def send_thread_update(sender, instance, created, **kwargs):
    # Update thread object for user after updating their participation
    # (important for seen_up_to and unread_message_count)

    # Saves a few unnecessary messages if we only send on modify
    if created:
        return

    thread = instance.thread

    topic = 'conversations:message'
    payload = ConversationMessageSerializer(thread, context={'request': MockRequest(user=instance.user)}).data

    for subscription in ChannelSubscription.objects.recent().filter(user=instance.user):
        send_in_channel(subscription.reply_channel, topic, payload)
Example #13
0
def send_participant_left(sender, instance, **kwargs):
    """Notify conversation participants when someone leaves"""
    conversation = instance.conversation

    topic = 'conversations:conversation'

    # TODO send to all group members?

    for subscription in ChannelSubscription.objects.recent() \
            .filter(user__in=conversation.participants.all()).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)

    participant = conversation.make_participant()
    payload = ConversationSerializer(participant).data
    for subscription in ChannelSubscription.objects.recent().filter(user=instance.user).distinct():
        send_in_channel(subscription.reply_channel, topic, payload)