conversation.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from flask import request
  2. from flask_restful import marshal_with, reqparse
  3. from flask_restful.inputs import int_range
  4. from werkzeug.exceptions import NotFound
  5. import services
  6. from controllers.service_api import api
  7. from controllers.service_api.app import create_or_update_end_user_for_user_id
  8. from controllers.service_api.app.error import NotChatAppError
  9. from controllers.service_api.wraps import AppApiResource
  10. from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields
  11. from libs.helper import uuid_value
  12. from services.conversation_service import ConversationService
  13. class ConversationApi(AppApiResource):
  14. @marshal_with(conversation_infinite_scroll_pagination_fields)
  15. def get(self, app_model, end_user):
  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. parser.add_argument('user', type=str, location='args')
  22. args = parser.parse_args()
  23. if end_user is None and args['user'] is not None:
  24. end_user = create_or_update_end_user_for_user_id(app_model, args['user'])
  25. try:
  26. return ConversationService.pagination_by_last_id(app_model, end_user, args['last_id'], args['limit'])
  27. except services.errors.conversation.LastConversationNotExistsError:
  28. raise NotFound("Last Conversation Not Exists.")
  29. class ConversationDetailApi(AppApiResource):
  30. @marshal_with(simple_conversation_fields)
  31. def delete(self, app_model, end_user, c_id):
  32. if app_model.mode != 'chat':
  33. raise NotChatAppError()
  34. conversation_id = str(c_id)
  35. user = request.get_json().get('user')
  36. if end_user is None and user is not None:
  37. end_user = create_or_update_end_user_for_user_id(app_model, user)
  38. try:
  39. ConversationService.delete(app_model, conversation_id, end_user)
  40. except services.errors.conversation.ConversationNotExistsError:
  41. raise NotFound("Conversation Not Exists.")
  42. return {"result": "success"}, 204
  43. class ConversationRenameApi(AppApiResource):
  44. @marshal_with(simple_conversation_fields)
  45. def post(self, app_model, end_user, c_id):
  46. if app_model.mode != 'chat':
  47. raise NotChatAppError()
  48. conversation_id = str(c_id)
  49. parser = reqparse.RequestParser()
  50. parser.add_argument('name', type=str, required=False, location='json')
  51. parser.add_argument('user', type=str, location='json')
  52. parser.add_argument('auto_generate', type=bool, required=False, default=False, location='json')
  53. args = parser.parse_args()
  54. if end_user is None and args['user'] is not None:
  55. end_user = create_or_update_end_user_for_user_id(app_model, args['user'])
  56. try:
  57. return ConversationService.rename(
  58. app_model,
  59. conversation_id,
  60. end_user,
  61. args['name'],
  62. args['auto_generate']
  63. )
  64. except services.errors.conversation.ConversationNotExistsError:
  65. raise NotFound("Conversation Not Exists.")
  66. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='conversation_name')
  67. api.add_resource(ConversationApi, '/conversations')
  68. api.add_resource(ConversationDetailApi, '/conversations/<uuid:c_id>', endpoint='conversation_detail')