Example #1
0
    def get_chats(user_id):
        """get the chats by user_id

        return the chaters' info and numbers of unread messages
        """
        qs = Chat.objects.filter(models.Q(user_1=user_id) | models.Q(user_2=user_id))
        qs = qs.order_by("-importance", "-latest_time")
        blocked = BlockHelper.get_block_list(user_id)
        chats = []
        for chat in qs:
            others = UserHelper.get_user(chat.user_1 + chat.user_2 - user_id)
            others = UserHelper.user_filter(others)
            if others is None:
                continue
            othername = others['username']
            if othername in blocked:
                continue
            unread = chat.unread_count
            if chat.latest_sender == user_id:
                unread = 0
            chats.append({
                'user' : others,
                'unread' : unread,
                'time' : date_to_string(chat.latest_time)
            })
        return chats
Example #2
0
 def get_block_list(user_id):
     """get block list (return the names of the other users in the blocked chats)
     """
     blocks = Block.objects.filter(user_id=user_id)
     chats = [block.chat_id for block in blocks]
     user_pairs = [ChatHelper.get_chat_users(chat_id) for chat_id in chats]
     user_pairs = [pair for pair in user_pairs if pair is not None]
     user_pairs = [pair for pair in user_pairs if user_id in pair]
     user_ids = [user_1 + user_2 - user_id for user_1, user_2 in user_pairs]
     users = [UserHelper.get_user(user_id) for user_id in user_ids]
     users = [user for user in users if user is not None]
     usernames = [user['username'] for user in users]
     return usernames
Example #3
0
 def get_messages(chat_id, page):
     """get messages between user_1 and user_2
     """
     messages = []
     qs = Message.objects.filter(chat_id=chat_id, valid=True)
     qs = qs.order_by('-id')
     qs = qs[(page - 1) * 20 : page * 20]
     usernames = {}
     for message in qs:
         if usernames.get(message.sender) is None:
             user = UserHelper.get_user(message.sender)
             if user is not None:
                 usernames[message.sender] = user['username']
             else:
                 usernames[message.sender] = '-'
         messages.append({
             'id' : message.id,
             'username' : usernames[message.sender],
             'content' : message.content,
             'send_time' : date_to_string(message.send_time)
         })
     return messages