def on_message(self, message): json_data = get_cleaned_json_data_websocket(message, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] if opt == 'update_recent_user_list': current_user_list = ChatMessage.get_recent_user_list( self.current_user) current_user_list['code'] = 'update_recent_user_list' self.write_message(json_result(0, current_user_list)) elif opt == 'update_recent_user_list_and_open': current_user_list = ChatMessage.get_recent_user_list( self.current_user) current_user_list['code'] = 'update_recent_user_list_and_open' self.write_message(json_result(0, current_user_list)) elif opt == 'send_message': other_id = data['user_id'] other = User.get(User.id == other_id) content = data['content'] cl = ChatMessage.create(sender=self.current_user, receiver=other, content=content) other_websocket = WebsocketChatHandler.is_online(other.username) self.write_message( json_result( 0, { 'code': 'receive_message', 'other_id': other.id, 'msg': ['>', cl.content, TimeUtil.datetime_delta(cl.time)] })) other_websocket.write_message( json_result( 0, { 'code': 'receive_message', 'other_id': self.current_user.id, 'msg': ['<', cl.content, TimeUtil.datetime_delta(cl.time)] })) elif opt == 'recent_chat_message': other_id = data['user_id'] other = User.get(User.id == other_id) recent_message = ChatMessage.get_recent_chat_message( self.current_user, other) recent_message['code'] = 'recent_chat_message' self.write_message(json_result(0, recent_message))
def get_recent_chat_message(current_user, other): ''' 获取双方对话的聊天记录 :param userA: :param userB: :return: ''' result = {} result['name'] = other.username result['id'] = other.id result['avatar'] = other.avatar result['msg'] = ChatMessage.get_unread_message(current_user, other) result['msg'] = [] # import pdb;pdb.set_trace() # () 注意需要全包 recent_messages = (ChatMessage.select().where(( (ChatMessage.sender == current_user) & (ChatMessage.receiver == other)) | ( (ChatMessage.sender == other) & (ChatMessage.receiver == current_user))).order_by( ChatMessage.time).limit(10)) for msg in recent_messages: d = '>' if msg.sender == current_user else '<' result['msg'].append( [d, msg.content, TimeUtil.datetime_delta(msg.time)]) return result
def get_recent_chat_message(current_user, other): """ get the recent message between `current_user` and `other` :param current_user: :param other: :return: """ result = {} result['name'] = other.username result['id'] = other.id result['avatar'] = other.avatar result['msg'] = ChatMessage.get_unread_message(current_user, other) result['msg'] = [] recent_messages = (ChatMessage.select().where(( (ChatMessage.sender == current_user) & (ChatMessage.receiver == other)) | ( (ChatMessage.sender == other) & (ChatMessage.receiver == current_user))).order_by( ChatMessage.time).limit(10)) for msg in recent_messages: d = '>' if msg.sender == current_user else '<' result['msg'].append( [d, msg.content, TimeUtil.datetime_delta(msg.time)]) return result
def get_unread_message(current_user, other): tmp = [] unread_messages = ChatMessage.select().where( ChatMessage.receiver == current_user, ChatMessage.sender == other, ChatMessage.is_read == False).order_by(ChatMessage.time) for msg in unread_messages: tmp.append(['<', msg.content, TimeUtil.datetime_delta(msg.time)]) return tmp
def datetime_delta(handler, t): """ display friendly time :param handler: :param t: :return: """ if not t: return '???' return TimeUtil.datetime_delta(t)
def datetime_delta(handler, t): ''' 时间友好显示 :param handler: :param t: :return: ''' if not t: return '???' return TimeUtil.datetime_delta(t)
def get_recent_user_list(current_user): """ get the recent user who chat with `current_user` :param current_user: :return: """ recent_user_list = {} user_id_list = [] recent_user_list['user_id_list'] = user_id_list tmp_recent_user_list = [] one_day_ago = TimeUtil.get_ago(60 * 60 * 24 * 10 * 10) ol = ChatMessage.select( ChatMessage.sender, ChatMessage.receiver).where( ((ChatMessage.sender == current_user) | (ChatMessage.receiver == current_user)) & (ChatMessage.time > one_day_ago)).group_by( ChatMessage.sender, ChatMessage.receiver).limit(10) for u in ol: if u.sender == current_user: other = u.receiver else: other = u.sender if other.id in user_id_list: continue user_id_list.append(other.id) unread_count = ChatMessage.select().where( ChatMessage.sender == u.sender, ChatMessage.receiver == u.receiver).count() recent_user_list[other.id] = {} recent_user_list[other.id]['other_name'] = other.username recent_user_list[other.id]['other_id'] = other.id recent_user_list[other.id]['other_avatar'] = other.avatar tmp_recent_user_list.append({ 'id': other.id, 'avatar': other.avatar, 'name': other.username }) recent_user_list['user_id_list'] = user_id_list return tmp_recent_user_list
def get_unread_message(current_user, other): """ get the unread message between `current_user` and `other` :param current_user: :param other: :return: """ tmp = [] unread_messages = ChatMessage.select().where( ChatMessage.receiver == current_user, ChatMessage.sender == other, ChatMessage.is_read == False).order_by(ChatMessage.time) for msg in unread_messages: tmp.append(['<', msg.content, TimeUtil.datetime_delta(msg.time)]) return tmp
def on_message(self, message): json_data = get_cleaned_json_data_websocket(message, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] if opt == 'update_recent_user_list': logger.debug('update_recent_user_list...') recent_user_list = ChatMessage.get_recent_user_list(self.current_user) self.write_message(json_result(0,{'code': 'recent_user_list', 'data': recent_user_list})) elif opt == 'update_recent_user_list_and_open': recent_user_list = ChatMessage.get_recent_user_list(self.current_user) self.write_message(json_result(0,recent_user_list)) elif opt == 'send_message': other_id = data['user_id'] other = User.get(User.id == other_id) content = data['content'] cl = ChatMessage.create(sender=self.current_user, receiver=other, content=content) self.write_message(json_result(0, {'code': 'receive_a_message', 'data': { 'id': other.id, 'name': other.username, 'avatar': other.avatar, 'msg': ['>', cl.content, TimeUtil.datetime_delta(cl.time)]}})) # send to other user other_websocket = WebsocketChatHandler.is_online(other.username) if other_websocket: other_websocket.write_message(json_result(0, {'code': 'receive_a_message', 'data': { 'id': self.current_user.id, 'avatar': self.current_user.avatar, 'name': self.current_user.username, 'msg': ['<', cl.content, TimeUtil.datetime_delta(cl.time)]}})) elif opt == 'update_recent_message_list': other_id = data['user_id'] other = User.get(User.id == other_id) recent_message = ChatMessage.get_recent_chat_message(self.current_user, other) logger.debug(recent_message) self.write_message(json_result(0,{'code': 'recent_message_list', 'data':recent_message}))
def get_recent_user_list(current_user): ''' 获取所有未读的消息(都是发送给我的) :param me: :return: ''' recent_user_list = {} user_id_list = [] recent_user_list['user_id_list'] = user_id_list recent_message = ChatMessage.select().where(ChatMessage.receiver == current_user, ChatMessage.is_read == False).order_by(ChatMessage.time) for msg in recent_message: sender = msg.sender if sender.id not in recent_user_list.keys(): user_id_list.append(sender.id) recent_user_list[sender.id]={} recent_user_list[sender.id]['other_name'] = sender.username recent_user_list[sender.id]['other_id'] = sender.id recent_user_list[sender.id]['other_avatar'] = sender.avatar recent_user_list[sender.id]['msg'] = [] recent_user_list[sender.id]['msg'].append(['<', msg.content, TimeUtil.datetime_delta(msg.time)]) recent_user_list[sender.id]['update_time'] = str(msg.time) recent_user_list['user_id_list'] = user_id_list return recent_user_list
def on_message(self, message): json_data = get_cleaned_json_data_websocket(message, ['opt', 'data']) data = json_data['data'] opt = json_data['opt'] if opt == 'update_recent_user_list': logger.debug('update_recent_user_list...') recent_user_list = ChatMessage.get_recent_user_list( self.current_user) self.write_message( json_result(0, { 'code': 'recent_user_list', 'data': recent_user_list })) elif opt == 'update_recent_user_list_and_open': recent_user_list = ChatMessage.get_recent_user_list( self.current_user) self.write_message(json_result(0, recent_user_list)) elif opt == 'send_message': other_id = data['user_id'] other = User.get(User.id == other_id) content = data['content'] cl = ChatMessage.create(sender=self.current_user, receiver=other, content=content) self.write_message( json_result( 0, { 'code': 'receive_a_message', 'data': { 'id': other.id, 'name': other.username, 'avatar': other.avatar, 'msg': [ '>', cl.content, TimeUtil.datetime_delta(cl.time) ] } })) # send to other user other_websocket = WebsocketChatHandler.is_online(other.username) if other_websocket: other_websocket.write_message( json_result( 0, { 'code': 'receive_a_message', 'data': { 'id': self.current_user.id, 'avatar': self.current_user.avatar, 'name': self.current_user.username, 'msg': [ '<', cl.content, TimeUtil.datetime_delta(cl.time) ] } })) elif opt == 'update_recent_message_list': other_id = data['user_id'] other = User.get(User.id == other_id) recent_message = ChatMessage.get_recent_chat_message( self.current_user, other) logger.debug(recent_message) self.write_message( json_result(0, { 'code': 'recent_message_list', 'data': recent_message }))
def get_recent_user_list(current_user): ''' 获取最近用户列表 :param me: :return: ''' recent_user_list = {} user_id_list = [] recent_user_list['user_id_list'] = user_id_list tmp_recent_user_list = [] # other send to me # recent_message = ChatMessage.select().where(ChatMessage.receiver == current_user, ChatMessage.is_read == False).order_by(ChatMessage.time) # for msg in recent_message: # sender = msg.sender # if sender.id not in recent_user_list.keys(): # user_id_list.append(sender.id) # recent_user_list[sender.id]={} # recent_user_list[sender.id]['other_name'] = sender.username # recent_user_list[sender.id]['other_id'] = sender.id # recent_user_list[sender.id]['other_avatar'] = sender.avatar # recent_user_list[sender.id]['msg'] = [] # recent_user_list[sender.id]['msg'].append(['<', msg.content, TimeUtil.datetime_delta(msg.time)]) # recent_user_list[sender.id]['update_time'] = str(msg.time) one_day_ago = TimeUtil.get_ago(60 * 60 * 24 * 10 * 10) ol = ChatMessage.select( ChatMessage.sender, ChatMessage.receiver).where( ((ChatMessage.sender == current_user) | (ChatMessage.receiver == current_user)) & (ChatMessage.time > one_day_ago)).group_by( ChatMessage.sender, ChatMessage.receiver).limit(10) for u in ol: if u.sender == current_user: other = u.receiver else: other = u.sender if other.id in user_id_list: continue user_id_list.append(other.id) unread_count = ChatMessage.select().where( ChatMessage.sender == u.sender, ChatMessage.receiver == u.receiver).count() recent_user_list[other.id] = {} recent_user_list[other.id]['other_name'] = other.username recent_user_list[other.id]['other_id'] = other.id recent_user_list[other.id]['other_avatar'] = other.avatar #--- tmp_recent_user_list.append({ 'id': other.id, 'avatar': other.avatar, 'name': other.username }) recent_user_list['user_id_list'] = user_id_list return tmp_recent_user_list