conversation.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from flask_restful import marshal_with, reqparse
  2. from flask_restful.inputs import int_range
  3. from werkzeug.exceptions import NotFound
  4. from controllers.web import api
  5. from controllers.web.error import NotChatAppError
  6. from controllers.web.wraps import WebApiResource
  7. from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields
  8. from libs.helper import uuid_value
  9. from services.conversation_service import ConversationService
  10. from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError
  11. from services.web_conversation_service import WebConversationService
  12. class ConversationListApi(WebApiResource):
  13. @marshal_with(conversation_infinite_scroll_pagination_fields)
  14. def get(self, app_model, end_user):
  15. if app_model.mode != 'chat':
  16. raise NotChatAppError()
  17. parser = reqparse.RequestParser()
  18. parser.add_argument('last_id', type=uuid_value, location='args')
  19. parser.add_argument('limit', type=int_range(1, 100), required=False, default=20, location='args')
  20. parser.add_argument('pinned', type=str, choices=['true', 'false', None], location='args')
  21. args = parser.parse_args()
  22. pinned = None
  23. if 'pinned' in args and args['pinned'] is not None:
  24. pinned = True if args['pinned'] == 'true' else False
  25. try:
  26. return WebConversationService.pagination_by_last_id(
  27. app_model=app_model,
  28. user=end_user,
  29. last_id=args['last_id'],
  30. limit=args['limit'],
  31. pinned=pinned
  32. )
  33. except LastConversationNotExistsError:
  34. raise NotFound("Last Conversation Not Exists.")
  35. class ConversationApi(WebApiResource):
  36. def delete(self, app_model, end_user, c_id):
  37. if app_model.mode != 'chat':
  38. raise NotChatAppError()
  39. conversation_id = str(c_id)
  40. try:
  41. ConversationService.delete(app_model, conversation_id, end_user)
  42. except ConversationNotExistsError:
  43. raise NotFound("Conversation Not Exists.")
  44. WebConversationService.unpin(app_model, conversation_id, end_user)
  45. return {"result": "success"}, 204
  46. class ConversationRenameApi(WebApiResource):
  47. @marshal_with(simple_conversation_fields)
  48. def post(self, app_model, end_user, c_id):
  49. if app_model.mode != 'chat':
  50. raise NotChatAppError()
  51. conversation_id = str(c_id)
  52. parser = reqparse.RequestParser()
  53. parser.add_argument('name', type=str, required=False, location='json')
  54. parser.add_argument('auto_generate', type=bool, required=False, default=False, location='json')
  55. args = parser.parse_args()
  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 ConversationNotExistsError:
  65. raise NotFound("Conversation Not Exists.")
  66. class ConversationPinApi(WebApiResource):
  67. def patch(self, app_model, end_user, c_id):
  68. if app_model.mode != 'chat':
  69. raise NotChatAppError()
  70. conversation_id = str(c_id)
  71. try:
  72. WebConversationService.pin(app_model, conversation_id, end_user)
  73. except ConversationNotExistsError:
  74. raise NotFound("Conversation Not Exists.")
  75. return {"result": "success"}
  76. class ConversationUnPinApi(WebApiResource):
  77. def patch(self, app_model, end_user, c_id):
  78. if app_model.mode != 'chat':
  79. raise NotChatAppError()
  80. conversation_id = str(c_id)
  81. WebConversationService.unpin(app_model, conversation_id, end_user)
  82. return {"result": "success"}
  83. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='web_conversation_name')
  84. api.add_resource(ConversationListApi, '/conversations')
  85. api.add_resource(ConversationApi, '/conversations/<uuid:c_id>')
  86. api.add_resource(ConversationPinApi, '/conversations/<uuid:c_id>/pin')
  87. api.add_resource(ConversationUnPinApi, '/conversations/<uuid:c_id>/unpin')