conversation.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding:utf-8 -*-
  2. from flask import request
  3. from flask_restful import fields, marshal_with, reqparse
  4. from flask_restful.inputs import int_range
  5. from werkzeug.exceptions import NotFound
  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 libs.helper import TimestampField, uuid_value
  11. import services
  12. from services.conversation_service import ConversationService
  13. conversation_fields = {
  14. 'id': fields.String,
  15. 'name': fields.String,
  16. 'inputs': fields.Raw,
  17. 'status': fields.String,
  18. 'introduction': fields.String,
  19. 'created_at': TimestampField
  20. }
  21. conversation_infinite_scroll_pagination_fields = {
  22. 'limit': fields.Integer,
  23. 'has_more': fields.Boolean,
  24. 'data': fields.List(fields.Nested(conversation_fields))
  25. }
  26. class ConversationApi(AppApiResource):
  27. @marshal_with(conversation_infinite_scroll_pagination_fields)
  28. def get(self, app_model, end_user):
  29. if app_model.mode != 'chat':
  30. raise NotChatAppError()
  31. parser = reqparse.RequestParser()
  32. parser.add_argument('last_id', type=uuid_value, location='args')
  33. parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
  34. parser.add_argument('user', type=str, location='args')
  35. args = parser.parse_args()
  36. if end_user is None and args['user'] is not None:
  37. end_user = create_or_update_end_user_for_user_id(app_model, args['user'])
  38. try:
  39. return ConversationService.pagination_by_last_id(app_model, end_user, args['last_id'], args['limit'])
  40. except services.errors.conversation.LastConversationNotExistsError:
  41. raise NotFound("Last Conversation Not Exists.")
  42. class ConversationDetailApi(AppApiResource):
  43. @marshal_with(conversation_fields)
  44. def delete(self, app_model, end_user, c_id):
  45. if app_model.mode != 'chat':
  46. raise NotChatAppError()
  47. conversation_id = str(c_id)
  48. user = request.get_json().get('user')
  49. if end_user is None and user is not None:
  50. end_user = create_or_update_end_user_for_user_id(app_model, user)
  51. try:
  52. ConversationService.delete(app_model, conversation_id, end_user)
  53. except services.errors.conversation.ConversationNotExistsError:
  54. raise NotFound("Conversation Not Exists.")
  55. return {"result": "success"}, 204
  56. class ConversationRenameApi(AppApiResource):
  57. @marshal_with(conversation_fields)
  58. def post(self, app_model, end_user, c_id):
  59. if app_model.mode != 'chat':
  60. raise NotChatAppError()
  61. conversation_id = str(c_id)
  62. parser = reqparse.RequestParser()
  63. parser.add_argument('name', type=str, required=True, location='json')
  64. parser.add_argument('user', type=str, location='json')
  65. args = parser.parse_args()
  66. if end_user is None and args['user'] is not None:
  67. end_user = create_or_update_end_user_for_user_id(app_model, args['user'])
  68. try:
  69. return ConversationService.rename(app_model, conversation_id, end_user, args['name'])
  70. except services.errors.conversation.ConversationNotExistsError:
  71. raise NotFound("Conversation Not Exists.")
  72. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='conversation_name')
  73. api.add_resource(ConversationApi, '/conversations')
  74. api.add_resource(ConversationApi, '/conversations/<uuid:c_id>', endpoint='conversation')
  75. api.add_resource(ConversationDetailApi, '/conversations/<uuid:c_id>', endpoint='conversation_detail')