conversation.py 2.8 KB

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