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 create(params, user_info): # 加入房间号 data = ChatService().groupChatCreate(params, user_info) if data: #添加后同步房间 user_room_relation_data = Utils.db_l_to_d(UserRoomRelation.get(data['room_uuid'])) 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(data, msg='创建成功') return Utils.formatError(CONST['CODE']['BAD_REQUEST']['value'],msg='创建失败')
def create(user_info, params): # 加入房间号 data = ChatService().groupChatCreate(user_info, params) if data: #添加后同步房间 user_room_relation_data = Utils.db_l_to_d(UserRoomRelation.get(data['room_uuid'])) 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(data, msg='创建成功') return Utils.formatError(Code.BAD_REQUEST,msg='创建失败')
def groupChatCreate(user_info, params): # 加入房间号 data = ChatService().groupChatCreate(user_info, params) if data: #添加后同步房间 user_room_relation_data = Utils.db_l_to_d( UserRoomRelation.get(data['room_uuid'])) 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 BaseController().successData(data, msg='创建成功') return BaseController().error(msg='创建失败')
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 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 userRoomRelationGet(user_info): """ 获取通讯录列表 """ filters = { UserRoomRelation.user_id == user_info['data']['id'] } data = UserRoomRelation().getAll(filters, 'updated_at desc') return BaseController().successData(data)
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 groupChatCreate(self, user_info, params): """ 创建聊天群组 @Param dict userInfo @param dict params @return bool """ room_uuid = Utils.unique_id() name = '' now_item = int(time.time()) for id in params['ids']: user_data = Users().getOne({Users.id == id}) name = name + ',' + user_data['nick_name'] userRoomRelationData = { 'user_id': id, 'room_uuid': room_uuid, 'is_alert': 0, 'unread_number': 0, 'updated_at': now_item, 'created_at': now_item } UserRoomRelation().add(userRoomRelationData) room_data = { 'room_uuid': room_uuid, 'last_msg': '', 'type': 1, 'updated_at': now_item, 'created_at': now_item, 'name': name.strip(','), 'user_id': user_info['data']['id'] } room = Room().addByClass(room_data) return {'room_uuid': room_uuid}
def roomDetails(user_info, params): """ 获取群聊用户信息 """ filters = { UserRoomRelation.room_uuid == params['room_uuid'] } data = UserRoomRelation().getAll(filters, UserRoomRelation.created_at.desc) return 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 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 get(params, user_info): """ 获取通讯录列表 """ filters = { UserRoomRelation.user_id == user_info['data']['id'] } data = UserRoomRelation().getAll(filters, 'updated_at desc') return Utils.formatBody(data)
def adminRoomDelete(*args, **kwargs): """ 删除房间 """ params = kwargs['params'] filters = {Room.room_uuid == params['room_uuid']} Room().delete(filters) filters = {UserRoomRelation.room_uuid == params['room_uuid']} UserRoomRelation().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 groupChatCreate(params, user_info): """ 创建聊天群组,userInfo.id是群主 @Param dict userInfo @param dict params @return bool """ room_uuid = Utils.unique_id() name = '' userRoomRelationData = { 'user_id': user_info['data']['id'], 'room_uuid': room_uuid, 'is_alert': 0, 'unread_number': 0, 'type':CONST['GROUP']['MASTER']['value'] } UserRoomRelation().add(userRoomRelationData) for id in params['ids']: user_data = Users().getOne({Users.id == id}) name = name + ',' + user_data['nick_name'] userRoomRelationData = { 'user_id': id, 'room_uuid': room_uuid, 'is_alert': 0, 'unread_number': 0 } UserRoomRelation().add(userRoomRelationData) room_data = { 'room_uuid': room_uuid, 'last_msg': '', 'type': CONST['CHAT']['TEXT']['value'], 'name': name.strip(','), 'user_id': user_info['data']['id'] } Room().addByClass(room_data) return {'room_uuid': room_uuid, 'name': name.strip(',')}
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 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})