conversation.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from flask_restful import Resource, marshal_with, reqparse
  2. from flask_restful.inputs import int_range
  3. from werkzeug.exceptions import NotFound
  4. import services
  5. from controllers.service_api import api
  6. from controllers.service_api.app.error import NotChatAppError
  7. from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token
  8. from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields
  9. from libs.helper import uuid_value
  10. from models.model import App, AppMode, EndUser
  11. from services.conversation_service import ConversationService
  12. class ConversationApi(Resource):
  13. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
  14. @marshal_with(conversation_infinite_scroll_pagination_fields)
  15. def get(self, app_model: App, end_user: EndUser):
  16. app_mode = AppMode.value_of(app_model.mode)
  17. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  18. raise NotChatAppError()
  19. parser = reqparse.RequestParser()
  20. parser.add_argument('last_id', type=uuid_value, location='args')
  21. parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
  22. args = parser.parse_args()
  23. try:
  24. return ConversationService.pagination_by_last_id(app_model, end_user, args['last_id'], args['limit'])
  25. except services.errors.conversation.LastConversationNotExistsError:
  26. raise NotFound("Last Conversation Not Exists.")
  27. class ConversationDetailApi(Resource):
  28. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  29. @marshal_with(simple_conversation_fields)
  30. def delete(self, app_model: App, end_user: EndUser, c_id):
  31. app_mode = AppMode.value_of(app_model.mode)
  32. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  33. raise NotChatAppError()
  34. conversation_id = str(c_id)
  35. try:
  36. ConversationService.delete(app_model, conversation_id, end_user)
  37. except services.errors.conversation.ConversationNotExistsError:
  38. raise NotFound("Conversation Not Exists.")
  39. return {"result": "success"}, 204
  40. class ConversationRenameApi(Resource):
  41. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  42. @marshal_with(simple_conversation_fields)
  43. def post(self, app_model: App, end_user: EndUser, c_id):
  44. app_mode = AppMode.value_of(app_model.mode)
  45. if app_mode not in [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]:
  46. raise NotChatAppError()
  47. conversation_id = str(c_id)
  48. parser = reqparse.RequestParser()
  49. parser.add_argument('name', type=str, required=False, location='json')
  50. parser.add_argument('auto_generate', type=bool, required=False, default=False, location='json')
  51. args = parser.parse_args()
  52. try:
  53. return ConversationService.rename(
  54. app_model,
  55. conversation_id,
  56. end_user,
  57. args['name'],
  58. args['auto_generate']
  59. )
  60. except services.errors.conversation.ConversationNotExistsError:
  61. raise NotFound("Conversation Not Exists.")
  62. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='conversation_name')
  63. api.add_resource(ConversationApi, '/conversations')
  64. api.add_resource(ConversationDetailApi, '/conversations/<uuid:c_id>', endpoint='conversation_detail')