conversation.py 3.6 KB

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