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='已添加')
def getByRoomUuid(params, user_info): """ 获取群组或单聊信息 """ filters = { Room.room_uuid == params['room_uuid'] } roomData = Room().getOne(filters) if roomData['type'] == 0: filters = { AddressBook.room_uuid == params['room_uuid'] } data = AddressBook().getList( filters, AddressBook.updated_at.desc()) filters.add( AddressBook.be_focused_user_id == user_info['data']['id'] ) data['room'] = AddressBook().getOne( filters) else: filters = { UserRoomRelation.room_uuid == params['room_uuid'] } data = UserRoomRelation().getList( filters, UserRoomRelation.updated_at.desc()) filters.add( UserRoomRelation.user_id == user_info['data']['id'] ) data['room'] = UserRoomRelation().getOne( filters) return Utils.formatBody(data)
def input(params, user_info): filters = { AddressBook.focused_user_id == user_info['data']['id'], AddressBook.room_uuid == params['room_uuid'] } AddressBook().edit({'is_input': 1}, filters) # 发送消息 data = AddressBook().getOne(filters) data['even'] = params['even'] emit('input', Utils.formatBody(data), room='@broadcast.'+str(data['be_focused_user_id'])) return Utils.formatBody({'action': "input", "data": data})
def userRoomRelationGetByRoomUuid(params, user_info): """ 获取群组或单聊信息 """ filters = {Room.room_uuid == params['room_uuid']} roomData = Room().getOne(filters) if roomData['type'] == 0: filters = {AddressBook.room_uuid == params['room_uuid']} data = AddressBook().getList(filters, 'updated_at desc') filters.add(AddressBook.be_focused_user_id == user_info['data']['id']) data['room'] = AddressBook().getOne(filters) else: filters = {UserRoomRelation.room_uuid == params['room_uuid']} data = UserRoomRelation().getList(filters, 'updated_at desc') filters.add(UserRoomRelation.user_id == user_info['data']['id']) data['room'] = UserRoomRelation().getOne(filters) return BaseController().successData(data)
def updateAlert(params, user_info): """ 更新对否提醒 """ filters = { Room.room_uuid == params['room_uuid'] } roomData = Room().getOne(filters) if roomData['type'] == 0: filters = { AddressBook.room_uuid == params['room_uuid'], AddressBook.be_focused_user_id == user_info['data']['id'] } data = { 'is_alert': params['is_alert'] } status = AddressBook().edit(data, filters) else: filters = { UserRoomRelation.room_uuid == params['room_uuid'], UserRoomRelation.user_id == user_info['data']['id'] } data = { 'is_alert': params['is_alert'] } status = UserRoomRelation().edit(data, filters) if status: return Utils.formatBody()
def updateSaveAction(params, user_info): """ 更新是否云端保存 """ filters = { Room.room_uuid == params['room_uuid'] } roomData = Room().getOne(filters) if roomData['type'] == CONST['ROOM']['ALONE']['value']: filters = { AddressBook.room_uuid == params['room_uuid'], AddressBook.be_focused_user_id == user_info['data']['id'] } data = { 'save_action': params['save_action'] } status = AddressBook().edit(data, filters) else: filters = { UserRoomRelation.room_uuid == params['room_uuid'], UserRoomRelation.user_id == user_info['data']['id'] } data = { 'save_action': params['save_action'] } status = UserRoomRelation().edit(data, filters) if status: return Utils.formatBody()
def userRoomRelationUpdateSaveAction(params, user_info): """ 更新是否云端保存 """ filters = { Room.room_uuid == params['room_uuid'] } roomData = Room().getOne(filters) if roomData['type'] == 0: filters = { AddressBook.room_uuid == params['room_uuid'], AddressBook.be_focused_user_id == user_info['data']['id'] } data = { 'save_action': params['save_action'] } status = AddressBook().edit(data, filters) else: filters = { UserRoomRelation.room_uuid == params['room_uuid'], UserRoomRelation.user_id == user_info['data']['id'] } data = { 'save_action': params['save_action'] } status = UserRoomRelation().edit(data, filters) if status: return BaseController().successData()
def beg(params, user_info): if user_info['data']['id'] == params['be_focused_user_id']: return Utils.formatError(CONST['CODE']['BAD_REQUEST']['value'], '无法添加自己为新朋友') addressBookdata = AddressBook.getAdddressBookByFocusedUserId( user_info['data']['id'], params['be_focused_user_id']) if addressBookdata != None: return Utils.formatError(CONST['CODE']['BAD_REQUEST']['value'], '已添加') msg_uuid = Utils.unique_id() userData = Users().getOne({Users.id == user_info['data']['id']}) data = { 'data': userData, 'action': 'beg_add', 'msg_uuid': msg_uuid, 'be_focused_user_id': params['be_focused_user_id'], 'focused_user_id': user_info['data']['id'] } socketio.emit('beg', Utils.formatBody(data), namespace='/api', room='@broadcast.' + str(params['be_focused_user_id'])) #,callback=success) #只缓存最新的一条 cache.set('@broadcast.beg' + str(params['be_focused_user_id']), data) return Utils.formatBody({}, msg='发送成功')
def roomDel(user_info, params): filters = { Room.room_uuid == params['room_uuid'] } roomData = Room().getOne(filters) if roomData['type'] == 0: filters = { AddressBook.room_uuid == params['room_uuid'] } AddressBook().delete(filters) filters = { Room.room_uuid == params['room_uuid'] } Room().delete(filters) else: filters = { UserRoomRelation.room_uuid == params['room_uuid'], UserRoomRelation.user_id == params['user_id'] } data = UserRoomRelation().delete(filters) filters = { Room.room_uuid == params['room_uuid'], Room.user_id == params['user_id'] } Room().delete(filters) return BaseController().successData()
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()
def addressBookAdd(params): if params['focused_user_id'] == params['be_focused_user_id']: return BaseController().error(msg='无法添加自己为新朋友') filters = { AddressBook.focused_user_id == params['focused_user_id'], AddressBook.be_focused_user_id == params['be_focused_user_id'] } addressBookData = AddressBook().getOne(filters) if(addressBookData == None): # 加入房间号 room_uuid = Utils.unique_id() # 建立通讯录关系 status = AddressBook.addRoomAndAddressBook( room_uuid, params['focused_user_id'], params['be_focused_user_id']) if status == False: return BaseController().error(msg='添加失败') #回复被添加用户 socketio.emit('beg', Utils.formatBody({ 'action':'beg_add_success' }), namespace='/room', room='@broadcast.'+str(params['focused_user_id'])) return BaseController().successData(msg='添加成功') return BaseController().error(msg='已添加')
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})
def addressBookBeg(params): if params['focused_user_id'] == params['be_focused_user_id']: return BaseController().error(msg='无法添加自己为新朋友') addressBookdata = AddressBook.getAdddressBookByFocusedUserId(params['focused_user_id'], params['be_focused_user_id']) if addressBookdata != None: return BaseController().error(msg='已添加') msg_uuid = Utils.unique_id() userData = Users().getOne({Users.id == params['focused_user_id']}) socketio.emit('beg', Utils.formatBody({ 'data':userData, 'action':'beg_add', 'msg_uuid': msg_uuid, 'be_focused_user_id': params['be_focused_user_id'] }), namespace='/room', room='@broadcast.'+str(params['be_focused_user_id']), callback=success) return BaseController().successData(msg='发送成功')
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"})
def roomGet(user_info): data = AddressBook.getRoomList(user_info['data']['id']) return BaseController().json(data)
def adminAddressBookDelete(*args, **kwargs): """ 删除通讯录 """ params = kwargs['params'] filters = {AddressBook.id == params['id']} AddressBook().delete(filters) return BaseController().successData()
def adminAddressBookList(*args, **kwargs): """ 获取通讯录列表 """ params = kwargs['params'] data = AddressBook().getList({}, params['orderBy'] + " " + params['order'], (), params['page_no'], params['per_page']) return BaseController().successData(data)
def addressBookGet(params, user_info): filters = { AddressBook.be_focused_user_id == user_info['data']['id'] } data = AddressBook.rawGetList(params['page_no'], params['per_page'], filters) return BaseController().json(data)
def get(params, user_info): """ 获取房间列表 """ return AddressBook.getRoomList(user_info['data']['id'])
def adminAddressBookList(*args, **kwargs): """ 获取通讯录列表 """ params = kwargs['params'] data = AddressBook().getList({}, AddressBook.updated_at.desc(), (), params['page_no'], params['per_page']) return BaseController().successData(data)
def get(params, user_info): """ 获取通讯录列表 """ filters = { AddressBook.be_focused_user_id == user_info['data']['id'] } return AddressBook.rawGetList(params['page_no'], params['per_page'], filters)
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})