示例#1
0
 def chat(self, message, user_info):
     """
         @param  dict message
         @param  dict user_info
         @return dict 
     """
     msg = message['data']['msg']
     room_uuid = message['data']['room_uuid']
     Type = message['data']['type']
     room_data = Room.get(room_uuid)
     room_type = room_data.type
     created_at = message['data']['created_at']
     user_data = Users().getOne({Users.id == user_info['data']['id']})
     if room_data != None and room_type == 0:
         address_book_data = AddressBook.get(room_uuid)
         #发送消息
         emit('chat',
              Utils.formatBody({
                  'msg': msg,
                  'name': user_data['nick_name'],
                  'user_id': user_data['id'],
                  'type': Type,
                  'head_img': user_data['head_img'],
                  'room_uuid': room_uuid,
                  'created_at': created_at
              }),
              room=room_uuid)
         #聊天时同步房间信息
         Room.updateLastMsgRoom(room_uuid, msg)
         #更新聊天提示数字
         AddressBook.updateUnreadNumber(room_uuid, user_data['id'])
         AddressBook.cleanUnreadNumber(room_uuid, user_data['id'])
         #更新客户端房间信息
         for item in address_book_data:
             roomList = AddressBook.getRoomList(
                 item.be_focused_user_id)['data']
             socketio.emit('room',
                           Utils.formatBody(roomList),
                           namespace='/room',
                           room='@broadcast.' +
                           str(item.be_focused_user_id))
     elif room_data != None and room_type == 1:
         user_room_relation_data = UserRoomRelation.get(room_uuid)
         #发送消息
         emit('chat',
              Utils.formatBody({
                  'msg': msg,
                  'name': user_data['nick_name'],
                  'user_id': user_data['id'],
                  'type': Type,
                  'head_img': user_data['head_img'],
                  'room_uuid': room_uuid,
                  'created_at': created_at
              }),
              room=room_uuid)
         #聊天时同步房间信息
         Room.updateLastMsgRoom(room_uuid, msg)
         #更新聊天提示数字
         UserRoomRelation.updateUnreadNumber(room_uuid, user_data['id'])
         UserRoomRelation.cleanUnreadNumber(room_uuid, user_data['id'])
         #更新客户端房间信息
         for item in user_room_relation_data:
             #if item.user_id != user_id:
             roomList = UserRoomRelation.getRoomList(item.user_id)['data']
             socketio.emit('groupRoom',
                           Utils.formatBody(roomList),
                           namespace='/room',
                           room='@broadcast.' + str(item.user_id))
     return Utils.formatBody({'action': "chat"})
示例#2
0
 def sendChatMessage(msg, room_uuid, Type, room_data, room_type, created_at, save_action, user_data, user_type=0):
     data = {
         'msg': msg,
         'name': user_data['nick_name'],
         'user_id': user_data['id'],
         'type':  Type,
         'head_img': user_data['head_img'],
         'room_uuid': room_uuid,
         'created_at': created_at,
         'read_status': 0,
         'user_type': user_type
         }
     if room_data != None and room_type == CONST['ROOM']['ADMIN']['value']:
         address_book_data = AddressBook.get(room_uuid)
         # 发送消息
         socketio.emit('chat',  Utils.formatBody(data),
                       namespace='/api', room=room_uuid)
         # 视频消息则直接返回
         if Type ==  CONST['CHAT']['VIDEO']['value']:
             return
         # 如果是云端存储则记录,这边判断不判断都存储
         # if save_action == CONST['SAVE']['CLOUD']['value']:
         res = Msg().getOne({Msg.room_uuid == room_uuid, Msg.created_at ==
                             created_at, Msg.user_id == user_data['id']})
         if res == None:
             copy_data = data.copy()
             copy_data['msg'] = json.dumps(msg)
             copy_data['send_status'] = CONST['STATUS']['SUCCESS']['value']
             Msg().add(copy_data)
         else:
             copy_data = data.copy()
             copy_data['msg'] = json.dumps(msg)
             copy_data['send_status'] = CONST['STATUS']['SUCCESS']['value']
             filters = {
                 Msg.name == copy_data['name'],
                 Msg.created_at == copy_data['created_at'],
                 Msg.room_uuid == room_uuid
             }
             Msg().edit(copy_data, filters)
         # 聊天时同步房间信息
         Room.updateLastMsgRoom(room_uuid, data, created_at)
         # 更新聊天提示数字
         AddressBook.updateUnreadNumber(room_uuid, user_data['id'])
         AddressBook.cleanUnreadNumber(room_uuid, user_data['id'])
         # 更新客户端房间信息
         for item in address_book_data:
             roomList = AddressBook.getRoomList(item.be_focused_user_id)
             socketio.emit('room', Utils.formatBody(
                 roomList), namespace='/api', room='@broadcast.'+str(item.be_focused_user_id))
     if room_data != None and room_type == CONST['ROOM']['ALONE']['value']:
         address_book_data = AddressBook.get(room_uuid)
         # 视频消息则直接返回
         if Type ==  CONST['CHAT']['VIDEO']['value']:
             videoData = data
             videoData["room_type"] = room_type
             # 发送消息
             for item in address_book_data:
                 if item['be_focused_user_id'] != user_data['id']:
                     emit('video',  Utils.formatBody(videoData), room='@broadcast.'+str(item.be_focused_user_id))
             return 
         # 发送消息
         emit('chat',  Utils.formatBody(data), room=room_uuid)
         # 如果是云端存储则记录,这边判断不判断都存储
         # if save_action == CONST['SAVE']['CLOUD']['value']:
         res = Msg().getOne({Msg.room_uuid == room_uuid, Msg.created_at ==
                             created_at, Msg.user_id == user_data['id']})
         if res == None:
             copy_data = data.copy()
             copy_data['msg'] = json.dumps(msg)
             copy_data['send_status'] = CONST['STATUS']['SUCCESS']['value']
             Msg().add(copy_data)
         else:
             copy_data = data.copy()
             copy_data['msg'] = json.dumps(msg)
             copy_data['send_status'] = CONST['STATUS']['SUCCESS']['value']
             filters = {
                 Msg.name == copy_data['name'],
                 Msg.created_at == copy_data['created_at'],
                 Msg.room_uuid == room_uuid
             }
             Msg().edit(copy_data, filters)
         # 聊天时同步房间信息
         Room.updateLastMsgRoom(room_uuid, data, created_at)
         # 更新聊天提示数字
         AddressBook.updateUnreadNumber(room_uuid, user_data['id'])
         AddressBook.cleanUnreadNumber(room_uuid, user_data['id'])
         # 更新客户端房间信息
         for item in address_book_data:
             print(AddressBook,item)
             roomList = AddressBook.getRoomList(item.be_focused_user_id)
             socketio.emit('room', Utils.formatBody(
                 roomList), namespace='/api', room='@broadcast.'+str(item.be_focused_user_id))
     elif room_data != None and room_type == CONST['ROOM']['GROUP']['value']:
         # 获取用户的权限,如果禁言状态返回错误
         filters = {
             UserRoomRelation.user_id == user_data['id'],
             UserRoomRelation.room_uuid == room_uuid
         }
         selfUserRoomRelationData = UserRoomRelation().getOne(filters)
         if selfUserRoomRelationData['status'] == CONST['GROUP']['BLOCK']['value']:
             return Utils.formatError(CONST['CODE']['ERROR']['value'], '禁言中')
         # 获取群组内用户
         user_room_relation_data = UserRoomRelation.get(room_uuid)
         # 发送消息
         emit('chat', Utils.formatBody(data), room=room_uuid)
         # 如果是云端存储则记录
         # if save_action == CONST['SAVE']['CLOUD']['value']:
         res = Msg().getOne({Msg.room_uuid == room_uuid, Msg.created_at ==
                             created_at, Msg.user_id == user_data['id']})
         if res == None:
             copy_data = data.copy()
             copy_data['msg'] = json.dumps(msg)
             copy_data['send_status'] = CONST['STATUS']['SUCCESS']['value']
             Msg().add(copy_data)
         else:
             copy_data = data.copy()
             copy_data['msg'] = json.dumps(msg)
             copy_data['send_status'] = CONST['STATUS']['SUCCESS']['value']
             filters = {
                 Msg.name == copy_data['name'],
                 Msg.created_at == copy_data['created_at'],
                 Msg.room_uuid == room_uuid
             }
             Msg().edit(copy_data, filters)
         # 聊天时同步房间信息
         Room.updateLastMsgRoom(room_uuid, data, created_at)
         # 更新聊天提示数字
         UserRoomRelation.updateUnreadNumber(room_uuid, user_data['id'])
         UserRoomRelation.cleanUnreadNumber(room_uuid, user_data['id'])
         # 更新客户端房间信息
         for item in user_room_relation_data:
             roomList = UserRoomRelation.getRoomList(item.user_id)
             socketio.emit('groupRoom', Utils.formatBody(
                 roomList), namespace='/api', room='@broadcast.'+str(item.user_id))
     return Utils.formatBody({'action': "chat", "data": data})