async def allow_bot_for_user( allow_bot: BotAccessRequest, background_tasks: BackgroundTasks, bot: str = Path(default=None, description="bot id", example="613f63e87a1d435607c3c183"), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS)): """ Allows user to access a bot. """ bot_name, url = AccountProcessor.allow_bot_and_generate_invite_url( bot, allow_bot.email, current_user.get_user(), current_user.account, allow_bot.role) if Utility.email_conf["email"]["enable"]: background_tasks.add_task( Utility.format_and_send_mail, mail_type='add_member', email=allow_bot.email, url=url, first_name=f'{current_user.first_name} {current_user.last_name}', bot_name=bot_name, role=allow_bot.role) return Response(message='An invitation has been sent to the user') else: return {"message": "User added"}
async def update_ui_config(request_data: DictData, current_user: User = Depends( Authentication.get_current_user)): """ Add/update ui configuration for user. """ AccountProcessor.update_ui_config(request_data.data, current_user.get_user()) return {"message": "Config saved!"}
async def feedback(request_data: FeedbackRequest, current_user: User = Depends( Authentication.get_current_user)): """ Receive feedback from user. """ AccountProcessor.add_feedback(request_data.rating, current_user.get_user(), request_data.scale, request_data.feedback) return {"message": "Thanks for your feedback!"}
async def add_bot(request: TextData, current_user: User = Depends( Authentication.get_current_user)): """ Add new bot in a account. """ AccountProcessor.add_bot(request.data, current_user.account, current_user.get_user()) return {'message': 'Bot created'}
async def list_active_bot_invites(current_user: User = Security( Authentication.get_current_user)): """ Lists active bot invites. """ return Response( data={ 'active_invites': list(AccountProcessor.list_active_invites(current_user.get_user())) })
async def update_integration_token( request: IntegrationRequest, current_user: User = Security(Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS), ): """ Enable/disable/delete an integration. """ Authentication.update_integration_token(request.name, current_user.get_bot(), current_user.get_user(), int_status=request.status) return {"message": "Integration status updated!"}
async def transfer_ownership( request_data: TextData, bot: str = Path(default=None, description="bot id", example="613f63e87a1d435607c3c183"), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=OWNER_ACCESS)): """ Transfers ownership to provided user. """ AccountProcessor.transfer_ownership(current_user.account, bot, current_user.get_user(), request_data.data) return Response(message='Ownership transferred')
async def update_bot_access_for_user( allow_bot: BotAccessRequest, bot: str = Path(default=None, description="bot id", example="613f63e87a1d435607c3c183"), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS)): """ Updates user's role or status. """ AccountProcessor.update_bot_access(bot, allow_bot.email, current_user.get_user(), allow_bot.role, allow_bot.activity_status) return Response(message='User access updated')
async def delete_bot_conversations_history( background_tasks: BackgroundTasks, month: int = Query(default=3, ge=1, le=6), current_user: User = Security(Authentication.get_current_user_and_bot, scopes=TESTER_ACCESS)): """ Deletes bot chat history for all users up to certain months min 1 month max 6 months """ HistoryDeletionLogProcessor.is_event_in_progress( bot=current_user.get_bot()) ChatHistoryUtils.validate_history_endpoint(bot=current_user.get_bot()) background_tasks.add_task(EventsTrigger.trigger_history_deletion, bot=current_user.get_bot(), user=current_user.get_user(), month=month) return { "message": "Delete chat history initiated. It may take a while. Check logs!" }
async def delete_user_chat_history(background_tasks: BackgroundTasks, sender: Text, month: int = Query(default=3, ge=1, le=6), current_user: User = Security( Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS)): """ Deletes user chat history up to certain months min 3 month max 6 months """ HistoryDeletionLogProcessor.is_event_in_progress( bot=current_user.get_bot()) ChatHistoryUtils.validate_history_endpoint(bot=current_user.get_bot()) background_tasks.add_task(EventsTrigger.trigger_history_deletion, bot=current_user.get_bot(), user=current_user.get_user(), month=month, sender_id=sender) return { "message": "Delete user history initiated. It may take a while. Check logs!" }
async def generate_integration_token( request: IntegrationRequest, current_user: User = Security(Authentication.get_current_user_and_bot, scopes=ADMIN_ACCESS), ): """ Generates an access token for api integration. """ access_token = Authentication.generate_integration_token( current_user.get_bot(), current_user.get_user(), expiry=request.expiry_minutes, name=request.name, access_limit=request.access_list, role=request.role) return { "data": { "access_token": access_token, "token_type": "bearer" }, "message": """This token will be shown only once. Please copy this somewhere safe. It is your responsibility to keep the token secret. If leaked, others may have access to your system.""" }
async def get_ui_config(current_user: User = Depends( Authentication.get_current_user)): """ Get ui configuration for user. """ return {'data': AccountProcessor.get_ui_config(current_user.get_user())}