def remove_participant(sender, instance, **kwargs): """When a user is removed from a conversation we will notify them.""" user = instance.user conversation = instance.conversation for subscription in ChannelSubscription.objects.recent().filter(user=user): send_in_channel(subscription.reply_channel, topic='conversations:leave', payload={'id': conversation.id})
def send_application_updates(sender, instance, **kwargs): application = instance group = application.group payload = ApplicationSerializer(application).data q = Q(user__in=group.members.all()) | Q(user=application.user) for subscription in ChannelSubscription.objects.recent().filter(q).distinct(): send_in_channel(subscription.reply_channel, topic='applications:update', payload=payload)
def send_activity_participant_updates(sender, instance, **kwargs): activity = instance.activity payload = ActivitySerializer(activity).data for subscription in ChannelSubscription.objects.recent().filter( user__in=activity.place.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='activities:activity', payload=payload)
def notification_meta_saved(sender, instance, **kwargs): meta = instance payload = NotificationMetaSerializer(meta).data for subscription in ChannelSubscription.objects.recent().filter( user=meta.user): send_in_channel(subscription.reply_channel, topic='notifications:meta', payload=payload)
def notification_deleted(sender, instance, **kwargs): notification = instance payload = NotificationSerializer(notification).data for subscription in ChannelSubscription.objects.recent().filter( user=notification.user): send_in_channel(subscription.reply_channel, topic='notifications:notification_deleted', payload=payload)
def send_history_updates(sender, instance, **kwargs): history = instance payload = HistorySerializer(history).data for subscription in ChannelSubscription.objects.recent().filter( user__in=history.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='history:history', payload=payload)
def send_offer_delete(sender, instance, **kwargs): offer = instance payload = OfferSerializer(offer).data for subscription in ChannelSubscription.objects.recent().filter( user__in=offer.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='offers:offer_deleted', payload=payload)
def send_pickup_collector_updates(sender, instance, **kwargs): pickup = instance.pickupdate payload = PickupDateSerializer(pickup).data for subscription in ChannelSubscription.objects.recent().filter( user__in=pickup.place.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='pickups:pickupdate', payload=payload)
def send_pickup_series_delete(sender, instance, **kwargs): series = instance payload = PickupDateSeriesSerializer(series).data for subscription in ChannelSubscription.objects.recent().filter( user__in=series.place.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='pickups:series_deleted', payload=payload)
def send_notification_status_update(user): count = unseen_notification_count(user) for subscription in ChannelSubscription.objects.recent().filter(user=user): send_in_channel( subscription.reply_channel, topic='status', payload={ 'unseen_notification_count': count, } )
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)
def send_invitation_accept(sender, instance, **kwargs): invitation = instance payload = InvitationSerializer(invitation).data for subscription in ChannelSubscription.objects.recent().filter( user__in=invitation.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='invitations:invitation_accept', payload=payload)
def send_activity_type_delete(sender, instance, **kwargs): activity_type = instance payload = ActivityTypeSerializer(activity_type).data for subscription in ChannelSubscription.objects.recent().filter( user__in=activity_type.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='activities:type_deleted', payload=payload)
def send_activity_series_updates(sender, instance, **kwargs): series = instance payload = ActivitySeriesSerializer(series).data for subscription in ChannelSubscription.objects.recent().filter( user__in=series.place.group.members.all()).distinct(): send_in_channel(subscription.reply_channel, topic='activities:series', payload=payload)
def community_feed_meta_saved(sender, instance, **kwargs): meta = instance payload = CommunityFeedMetaSerializer(meta).data for subscription in ChannelSubscription.objects.recent().filter( user=meta.user): send_in_channel(subscription.reply_channel, topic='community_feed:meta', payload=payload)
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)
def send_feedback_possible_count(user): groups = defaultdict(dict) for group_id, count in get_feedback_possible(user): groups[group_id]['feedback_possible_count'] = count payload = {'groups': groups} for subscription in ChannelSubscription.objects.recent().filter(user=user): send_in_channel(subscription.reply_channel, topic='status', payload=payload)
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)
def send_activity_updates(sender, instance, **kwargs): activity = instance if activity.is_done: # doesn't change serialized data return payload = ActivitySerializer(activity).data for subscription in ChannelSubscription.objects.recent().filter(user__in=activity.place.group.members.all() ).distinct(): send_in_channel(subscription.reply_channel, topic='activities:activity', payload=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_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 application_saved(sender, instance, **kwargs): application = instance for user, subscriptions in groupby(sorted(list( ChannelSubscription.objects.recent().filter(user__in=application.group.members.all())), key=lambda x: x.user.id), key=lambda x: x.user): groups = defaultdict(dict) for group_id, count in pending_applications(user): groups[group_id]['pending_application_count'] = count for subscription in subscriptions: send_in_channel(subscription.reply_channel, topic='status', payload={'groups': groups})
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)
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_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 send_offer_updates(sender, instance, created, **kwargs): offer = instance payload = OfferSerializer(offer).data for subscription in ChannelSubscription.objects.recent().filter(user__in=offer.group.members.all()).distinct(): if offer.status == OfferStatus.ACTIVE.value or \ offer.user == subscription.user or \ subscription.user.conversation_set.filter(id=offer.conversation.id).exists(): send_in_channel(subscription.reply_channel, topic='offers:offer', payload=payload) elif not created: # if the user cannot see it, it's deleted from their point of view! send_in_channel(subscription.reply_channel, topic='offers:offer_deleted', payload=payload) if created: tasks.notify_new_offer_push_subscribers(offer)
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)
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)
def feedback_saved(sender, instance, created, **kwargs): feedback = instance if not created: return user = feedback.given_by groups = defaultdict(dict) for group_id, count in get_feedback_possible(user): groups[group_id]['feedback_possible_count'] = count payload = {'groups': groups} for subscription in ChannelSubscription.objects.recent().filter(user=user): send_in_channel(subscription.reply_channel, topic='status', payload=payload)