conversation.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, 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. if app_model.mode != 'chat':
  17. raise NotChatAppError()
  18. parser = reqparse.RequestParser()
  19. parser.add_argument('last_id', type=uuid_value, location='args')
  20. parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
  21. args = parser.parse_args()
  22. try:
  23. return ConversationService.pagination_by_last_id(app_model, end_user, args['last_id'], args['limit'])
  24. except services.errors.conversation.LastConversationNotExistsError:
  25. raise NotFound("Last Conversation Not Exists.")
  26. class ConversationDetailApi(Resource):
  27. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  28. @marshal_with(simple_conversation_fields)
  29. def delete(self, app_model: App, end_user: EndUser, c_id):
  30. if app_model.mode != 'chat':
  31. raise NotChatAppError()
  32. conversation_id = str(c_id)
  33. try:
  34. ConversationService.delete(app_model, conversation_id, end_user)
  35. except services.errors.conversation.ConversationNotExistsError:
  36. raise NotFound("Conversation Not Exists.")
  37. return {"result": "success"}, 204
  38. class ConversationRenameApi(Resource):
  39. @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.JSON))
  40. @marshal_with(simple_conversation_fields)
  41. def post(self, app_model: App, end_user: EndUser, c_id):
  42. if app_model.mode != 'chat':
  43. raise NotChatAppError()
  44. conversation_id = str(c_id)
  45. parser = reqparse.RequestParser()
  46. parser.add_argument('name', type=str, required=False, location='json')
  47. parser.add_argument('auto_generate', type=bool, required=False, default=False, location='json')
  48. args = parser.parse_args()
  49. try:
  50. return ConversationService.rename(
  51. app_model,
  52. conversation_id,
  53. end_user,
  54. args['name'],
  55. args['auto_generate']
  56. )
  57. except services.errors.conversation.ConversationNotExistsError:
  58. raise NotFound("Conversation Not Exists.")
  59. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='conversation_name')
  60. api.add_resource(ConversationApi, '/conversations')
  61. api.add_resource(ConversationDetailApi, '/conversations/<uuid:c_id>', endpoint='conversation_detail')