Exemple #1
0
    def _calc_receive_chat_friend(self, data, req, timer):

        user = data.user.get()
        logger.notice("FRIEDNS= %s" % user.id)
        friend_index = utils.split_to_int(user.friends).index(req.friend_id)
        friend_id = FriendInfo.generate_id(data.id, friend_index)
        logger.notice("friend_id = %d" % friend_id)
        friend_msg = data.friend_list.get(friend_id)
        user_id = data.id
        message_list = data.message_list.get_all()
        friendstr = user.friends
        friend = utils.split_to_int(friendstr)
        msg_index = 1
        if not len(message_list) == 0:
            msg_index = message_list[-1].message_index + 1
        self._eliminate_msg(data, friend_id, friend_index, MAX_MSG_NUM - 1)
        message = MessageInfo.create(data.id, friend_index, msg_index,
                                     req.content, 1, 2)
        data.message_list.add(message)

        message_index = data.friend_list.get(friend_id).message_index_list
        message_index_list = utils.split_to_int(message_index)
        message_index_list.append(msg_index)
        data.friend_list.get(
            friend_id).message_index_list = utils.join_to_string(
                message_index_list)

        res = friend_pb2.ChatFriendRes()
        res.status = 0
        res.ret = friend_pb2.FRIEND_OK

        message_index = friend_msg.message_index_list
        message_index_list = utils.split_to_int(message_index)[::-1]
        num = min(len(message_index_list), 20)
        msg_index_list = message_index_list[:num]

        for msg_index in msg_index_list[::-1]:
            message_info = res.messages.add()
            message_id = MessageInfo.generate_id(user_id, friend_index,
                                                 msg_index)
            message = data.message_list.get(message_id)
            message_info.user_id = user_id
            message_info.friend_index = friend_index
            message_info.content = message.content
            message_info.status = message.status
            message_info.message_from = message.message_from

            logger.notice("message_info = %s" % message_info)

    # response = res.SerializeToString()
    # logger.notice("====================1")
    # defer = GlobalObject().root.callChild("portal", "push_chat_record", data.id, response)
    # logger.notice("====================2")

        defer = DataBase().commit(data)
        defer.addCallback(self._receive_chat_succeed, req, res, timer)
        return defer
Exemple #2
0
 def _receive_chat_failed(self, err, req, timer):
     logger.fatal("Receive chat notice failed[reason=%s]" % err)
     res = friend_pb2.ChatFriendRes()
     res.status = -1
     res.ret = friend_pb2.FRIEND_OK
     response = res.SerializeToString()
     logger.notice(
         "Receive chat notice failed[req=%s][res=%s][consume=%d]" %
         (req, res, timer.count_ms()))
     return response
Exemple #3
0
    def _pack_chat_response(self, data, req, timer):
        """封装聊天记录"""
        res = friend_pb2.ChatFriendRes()
        res.status = 0
        res.ret = friend_pb2.FRIEND_OK
        user_id = req.user_id
        friend_index = req.friend_index
        user = data.user.get()
        id = FriendInfo.generate_id(user_id, req.friend_index)
        logger.notice("friend_id = %d" % id)
        friend = data.friend_list.get(id)
        assert friend != None
        if friend == None:
            return res
        else:
            logger.notice("======================1")
            message_index = friend.message_index_list
            logger.notice("msg_index= %s" % message_index)
            message_index_list = utils.split_to_int(message_index)[::-1]
            num = min(len(message_index_list), 21)
            msg_index_list = message_index_list[:num]
            logger.notice("msg_index=%s" % msg_index_list)
            logger.notice("NUM=%d" % num)
            for msg_index in msg_index_list[::-1]:
                message_info = res.messages.add()
                message_id = MessageInfo.generate_id(user_id, friend_index,
                                                     msg_index)
                logger.notice("MSG_ID=%d" % message_id)
                message = data.message_list.get(message_id)
                message.status = 2
                message_info.user_id = user_id
                message_info.friend_index = friend_index
                message_info.content = message.content
                message_info.status = message.status
                message_info.message_from = message.message_from

                logger.notice("message_info = %s" % message_info)
        defer = DataBase().commit(data)
        return self._chat_friend_succeed(data, req, res, timer)
Exemple #4
0
 def _pack_chatwrong_response(self, data, ret):
     res = friend_pb2.ChatFriendRes()
     res.status = 0
     res.ret = ret
     return res
Exemple #5
0
    def _calc_chat_friend_info(self, proxy, data, req, timer):

        res = friend_pb2.ChatFriendRes()
        res.status = 0
        if not proxy.get_result("user", req.friend_id):
            user = data.user.get()
            friend_list = user.get_friends()
            index = friend_list.index(req.friend_id)
            friend_list[index] = 0
            user.friends = utils.join_to_string(friend_list)
            id = FriendInfo.generate_id(user.id, index)
            logger.notice("id = %d" % id)
            data.friend_list.delete(id)
            res.ret = friend_pb2.WITHOUTID
            return self._chat_friend_succeed(data, req, res, timer)
        logger.notice("content = %s" % req.content)

        message_list = data.message_list.get_all()
        for message in message_list:
            message.status = 2

        if not req.HasField("content"):
            return self._pack_chat_response(data, req, timer)
        else:
            friendstr = proxy.get_result("user", req.friend_id).friends
            friend = utils.split_to_int(friendstr)
            user = data.user.get()
            message_list = data.message_list.get_all()
            #最后一条消息的index加一
            msg_index = 1
            if not len(message_list) == 0:
                msg_index = message_list[-1].message_index + 1
            logger.notice("msg_index =%d" % msg_index)
            #当自己id 在好友的好友列表中, 将消息的index 放入自己的friend中
            logger.notice("frinds=%s" % friend)
            if req.user_id in friend:
                friend_id = FriendInfo.generate_id(data.id, req.friend_index)
                self._eliminate_msg(data, friend_id, req.friend_index,
                                    MAX_MSG_NUM - 1)
                message = MessageInfo.create(data.id, req.friend_index,
                                             msg_index, req.content, 2, 1)
                logger.notice("MSG_ID=%s" % message.content)

                data.message_list.add(message)
                #friend_id = FriendInfo.generate_id(data.id, req.friend_index)
                message_index = data.friend_list.get(
                    friend_id).message_index_list
                message_index_list = utils.split_to_int(message_index)
                message_index_list.append(msg_index)
                data.friend_list.get(
                    friend_id).message_index_list = utils.join_to_string(
                        message_index_list)
                logger.notice(
                    "message_index_list = %s" %
                    data.friend_list.get(friend_id).message_index_list)
                #将自己id 和好友id 互换
                app_req = friend_pb2.ChatFriendReq()
                app_req.friend_id = req.user_id
                app_req.user_id = req.friend_id
                app_req.content = req.content
                request = app_req.SerializeToString()
                GlobalObject().root.callChild("portal", "forward_receive_chat",
                                              req.friend_id, request)

                return self._pack_chat_response(data, req, timer)
            else:
                res.ret = friend_pb2.CANNOTCHAT
                return self._chat_friend_succeed(data, req, res, timer)