Ejemplo n.º 1
0
 def add(params,user_info):
     ''' 添加通讯录 '''
     if params['focused_user_id'] == user_info['data']['id']:
         return Utils.formatError(Code.BAD_REQUEST,msg='无法添加自己为新朋友')
     filters = {
         AddressBook.focused_user_id == params['focused_user_id'],
         AddressBook.be_focused_user_id == user_info['data']['id']
     }
     addressBookData = AddressBook().getOne(filters)
     if(addressBookData == None):
         # 加入房间号
         room_uuid = Utils.unique_id()
         # 建立通讯录关系
         status = AddressBook.addRoomAndAddressBook(
             room_uuid, params['focused_user_id'], user_info['data']['id'])
         if status == False:
             return Utils.formatError(Code.BAD_REQUEST,msg='添加失败')
     
         #回复被添加用户
         socketio.emit('beg', Utils.formatBody({
             'action':'beg_add_success',
             'focused_user_id' : user_info['data']['id']
         }), namespace='/room', room='@broadcast.'+str(params['focused_user_id']))
         #添加后同步房间
         addressBookData = AddressBook.get(room_uuid)
         for item in addressBookData:
             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))   
         return Utils.formatBody({},msg='添加成功')
     return Utils.formatError(Code.BAD_REQUEST,msg='已添加')
Ejemplo n.º 2
0
 def delete(params, user_info):
     filters = {Room.room_uuid == params['room_uuid']}
     roomData = Room().getOne(filters)
     if roomData['type'] == 0:
         address_book_data = Utils.db_l_to_d(
             AddressBook.get(params['room_uuid']))
         filters = {AddressBook.room_uuid == params['room_uuid']}
         AddressBook().delete(filters)
         filters = {Room.room_uuid == params['room_uuid']}
         Room().delete(filters)
         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']))
             roomList = AddressBook.getRoomList(
                 item['focused_user_id'])['data']
             socketio.emit('room',
                           Utils.formatBody(roomList),
                           namespace="/room",
                           room='@broadcast.' +
                           str(item['focused_user_id']))
     else:
         user_room_relation_data = Utils.db_l_to_d(
             UserRoomRelation.get(params['room_uuid']))
         filters = {
             UserRoomRelation.room_uuid == params['room_uuid'],
             UserRoomRelation.user_id == user_info['data']['id']
         }
         data = UserRoomRelation().delete(filters)
         filters = {Room.room_uuid == params['room_uuid']}
         Room().delete(filters)
         filters = {UserRoomRelation.room_uuid == params['room_uuid']}
         UserRoomRelation().delete(filters)
         for item in user_room_relation_data:
             roomList = UserRoomRelation.getRoomList(
                 item['user_id'])['data']
             socketio.emit('groupRoom',
                           Utils.formatBody(roomList),
                           namespace='/room',
                           room='@broadcast.' + str(item['user_id']))
     return Utils.formatBody()
Ejemplo n.º 3
0
 def adminCreateRoom(message):
     admin_user_info = UsersAuthJWT().adminIdentify(
         message['Authorization'])
     if isinstance(admin_user_info, str):
         return Utils.formatError(CONST['CODE']['ERROR_AUTH_CHECK_TOKEN_FAIL']['value'], admin_user_info)
     filters = {
         Admin.id == admin_user_info['data']['id'],
     }
     admin_user_info = Admin().getOne(filters)
     filters = {
         AddressBook.be_focused_user_id == message['user_id'],
         AddressBook.focused_user_id == admin_user_info['id'],
         AddressBook.type == 1
     }
     addressBookInfo = AddressBook().getOne(filters)
     if addressBookInfo == None:
         room_uuid = Utils.unique_id()
         # 建立通讯录关系
         status = AddressBook.adminAddRoomAndAddressBook(
             room_uuid, admin_user_info, message['user_id'])
         if status == False:
             return Utils.formatError(CONST['CODE']['BAD_REQUEST']['value'], msg='添加失败')
         # 添加后同步房间
         addressBookData = AddressBook.get(room_uuid)
         for item in addressBookData:
             roomList = AddressBook.getRoomList(
                 item.be_focused_user_id)['list']
             if item['type'] == CONST['ADDRESSBOOK']['ADMIN']['value']:
                 socketio.emit('room', Utils.formatBody(
                     roomList), namespace="/api", room='@broadcast.'+str(item.be_focused_user_id))
             else:
                 socketio.emit('room', Utils.formatBody(
                     roomList), namespace="/api", room='@broadcast.'+str(item.be_focused_user_id))
     else:
         room_uuid = addressBookInfo['room_uuid']
     return Utils.formatBody({'room_uuid': room_uuid})
Ejemplo n.º 4
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"})
Ejemplo n.º 5
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})