def add_session(data): try: SessionHandler.add_session_db(data['account'], data['session']) except BaseException as e: SessionHandler.Log.exception(e) return error.handle_500() return ok_response()
def add_message(data): try: MessageHandler.add_message_db(data['type'], data['id'], data['account'], data['name'], data['message']) except BaseException as e: MessageHandler.Log.exception(e) return error.handle_500() return ok_response()
async def create_group(data): res = await get_mongo_fetch_data(data, '/user/create_group') if res['status'] == 'ok': message = { 'method': 'create_group', 'm_id': res['id'], 'name': data['name'] } MessageHandler.system_message(data['account'], message) return ok_response() else: return error.handle_500()
async def add_group(data): res = await get_mongo_fetch_data(data, '/user/user_join_group') if res['status'] == 'ok': message = { 'method': 'add_group', 'm_id': data['id'], 'name': res['name'] } MessageHandler.system_message(data['account'], message) MessageHandler.system_group_message(data['id'], '用户: ' + data['name'] + ' 加入群聊') return ok_response() else: return error.handle_500()
async def user_logout(data): send_data = {'account': data['account']} login_user_friends = await get_mongo_fetch_data(send_data, '/user/get_user_friend') now_login_users = MessageHandler.now_login_users() all_message = '用户: ' + str(data['username']) + ' 下线' MessageHandler.system_all_message(all_message) for user in now_login_users: if user in login_user_friends['friend']: friend_message = { 'method': 'remove_login_friend', 'm_id': data['account'], } MessageHandler.system_message(user, friend_message) try: session.SessionHandler.delete_session_db(data['account']) except BaseException as e: MessageHandler.Log.error(e) return error.handle_500() return ok_response()
async def add_friend(data): res = await get_mongo_fetch_data(data, '/user/user_add_friend') if res['status'] == 'ok': now_login_users = MessageHandler.now_login_users() if data['account'] in now_login_users: owner_message = { 'method': 'add_friend', 'm_id': data['id'], 'name': res['name'] } MessageHandler.system_message(data['account'], owner_message) if data['id'] in now_login_users: friend_message = { 'method': 'add_friend', 'm_id': data['account'], 'name': data['name'] } MessageHandler.system_message(data['id'], friend_message) if data['account'] in now_login_users and data['id'] in now_login_users: owner_login_friend_message = { 'method': 'add_login_friend', 'm_id': [ data['id'], ], } MessageHandler.system_message(data['account'], owner_login_friend_message) friend_login_friend_message = { 'method': 'add_login_friend', 'm_id': [ data['account'], ] } MessageHandler.system_message(data['id'], friend_login_friend_message) return ok_response() else: return error.handle_500()
def change_group(data): try: old_accounts, *args = UserHandler.get_group(data['group_id']) UserHandler.change_group(data['group_id'], data['group_name'], data['account']) if len(old_accounts) > len(data['account']): for old_account in old_accounts: if old_account not in data['account']: user_old_group = UserHandler.get_user_group(old_account) new_group = user_old_group.remove(data['group_id']) UserHandler.change_user_group(old_account, new_group) elif len(old_accounts) > len(data['account']): for new_account in data['account']: if new_account not in old_accounts: user_group = UserHandler.get_user_group(new_account) if user_group: user_group.append(data['group_id']) else: user_group = [data['group_id'], ] UserHandler.change_user_group(new_account, user_group) except BaseException as e: UserHandler.Log.exception(e) return error.handle_500() return ok_response()
async def user_login(data): send_data = {'account': data['account']} login_user_friends = await get_mongo_fetch_data(send_data, '/user/get_user_friend') now_login_users = MessageHandler.now_login_users() user_login_friends = [] all_message = '用户: ' + str(data['username']) + ' 上线' MessageHandler.system_all_message(all_message) for user in now_login_users: if user in login_user_friends['friend']: user_login_friends.append(user) friend_message = { 'method': 'add_login_friend', 'm_id': [ data['account'], ], } MessageHandler.system_message(user, friend_message) user_message = { 'method': 'add_login_friend', 'm_id': user_login_friends, } MessageHandler.system_message(data['account'], user_message) return ok_response()