Esempio n. 1
0
 def handle_new_notification(self, notification, callback):
     user = notification.target
     devices = RegisteredDevices.objects.filter(user=user, is_active=True)
     tokens = []
     badge = 1
     for device in devices:
         print device.id, self.waiters
         waiter = self.waiters.get(device.id)
         print waiter
         if waiter is not None and waiter.future is not None and not waiter.future.done():
             waiter.take_response(notification.dict_description())
             # badge = 0
         elif waiter is not None:
             Waiter.cache_chat(notification.dict_description(), user)
         tokens.append(device.token)
     if user.setting_center.notification_accept:
         push_notification.delay(
             user, tokens, badge, notification.apns_des(), 'notif'
         )
     if callback is not None:
         callback()
Esempio n. 2
0
def inform_of_related_waiters(message, global_message_dispatch):
    """ 根据给定的message内容,通知所有的相关的用户通知相
     :param message
    """
    # 复制一份当前的等待列表以免冲突
    if message is None:
        return
    waiters = global_message_dispatch.waiters.copy()
    if message.chat_type == "private":
        # private: 私信,目标是单一用户
        target_waiter = waiters.get(message.target_id, None)
        if target_waiter is not None:
            target_waiter.set_result([message])
        tokens = RegisteredDevices.objects.filter(user=message.target_user, is_active=True)\
            .values_list("token", flat=True)
        push_notification.delay(
            message.target_user, tokens, 1, message_body=message.message_body_des(),
            type="chat"
        )
    else:
        # group,群聊
        target_club = Club.objects.get(id=message.target_id)
        target_joins = ClubJoining.objects.filter(club=target_club)
        print "waiters", waiters
        print target_joins.values("user_id")
        for join in target_joins:
            user = join.user
            waiter = waiters.get(user.id, None)
            if message.sender == user:
                # 不需要讲消息发送给消息的发送者
                continue
            if waiter is not None:
                print message.target_id, "receive"
                waiter.set_result([message])
            else:
                print user.id, "unread"
                join.unread_chats += 1
                join.save()
Esempio n. 3
0
    def handle_new_chat(self, chat, callback):

        if chat is None:
            return
        if chat.chat_type == 'user':

            target_user = chat.target_user
            activate_devices = RegisteredDevices.objects.filter(user=target_user, is_active=True)
            entity, created = ChatEntity.objects.get_or_create(host=target_user, user=chat.sender)
            # make sure the reversed entity exists, too
            ChatEntity.objects.get_or_create(host=chat.sender, user=target_user)

            tokens = []
            badge = 1
            for device in activate_devices:
                waiter = self.waiters.get(device.id)
                if waiter is not None and waiter.future is not None and not waiter.future.done():
                    # if waiter is online
                    entity_response = entity.dict_description()
                    if waiter.cur_focused_entity == entity.id:
                        # if the message belongs to the currently focused chat, then drop the unread increase
                        badge = 0
                        entity_response["unread_num"] = 0
                    else:
                        entity_response["unread_num"] += badge

                    response = chat.dict_description(host=target_user)
                    response.update(roster=entity_response)
                    waiter.take_response(response)

                else:
                    response = chat.dict_description(host=target_user)
                    entity_response = entity.dict_description()
                    entity_response["unread_num"] += 1
                    response.update(roster=entity_response)
                    Waiter.cache_chat(response, target_user)
                tokens.append(device.token)
            if badge > 0:
                entity.unread_num += 1
                entity.save()
            elif entity.unread_num > 0:
                entity.unread_num = 0
                entity.save()
            if not entity.no_disturbing:
                push_notification.delay(
                    target_user, tokens, badge, message_body=chat.message_body_des, type='chat'
                )
        else:
            target_club = chat.target_club
            target_joins = ClubJoining.objects.select_related("user").filter(club=target_club)
            for join in target_joins:
                user = join.user
                if user.id == chat.sender_id:
                    continue
                # collect devices
                activate_devices = RegisteredDevices.objects.filter(user=user, is_active=True)
                entity, created = ChatEntity.objects.get_or_create(host=user, club=target_club)
                tokens = []
                badge = 1
                for device in activate_devices:
                    waiter = self.waiters.get(device.id)
                    if waiter is not None and waiter.future is not None and not waiter.future.done():
                        entity_response = entity.dict_description()
                        if waiter.cur_focused_entity == entity.id:
                            badge = 0
                            entity_response["unread_num"] = 0
                        else:
                            entity_response["unread_num"] += badge
                        response = chat.dict_description(host=user)
                        response.update(roster=entity_response)
                        waiter.take_response(response)
                    else:
                        response = chat.dict_description(host=user)
                        entity_response = entity.dict_description()
                        entity_response["unread_num"] += 1
                        response.update(roster=entity_response)
                        Waiter.cache_chat(response, user)
                    tokens.append(device.token)

                if badge > 0:
                    entity.unread_num += 1
                    entity.save()
                elif entity.unread_num > 0:
                    entity.unread_num = 0
                    entity.save()
                if not entity.no_disturbing:
                    push_notification.delay(
                        user, tokens, badge, message_body=chat.message_body_des, type='chat'
                    )
        if callback is not None:
            callback()