conversation.py 4.2 KB

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