conversation.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding:utf-8 -*-
  2. from flask_restful import fields, reqparse, marshal_with
  3. from flask_restful.inputs import int_range
  4. from werkzeug.exceptions import NotFound
  5. from controllers.web import api
  6. from controllers.web.error import NotChatAppError
  7. from controllers.web.wraps import WebApiResource
  8. from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields
  9. from libs.helper import TimestampField, uuid_value
  10. from services.conversation_service import ConversationService
  11. from services.errors.conversation import LastConversationNotExistsError, ConversationNotExistsError
  12. from services.web_conversation_service import WebConversationService
  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=True, location='json')
  55. args = parser.parse_args()
  56. try:
  57. return ConversationService.rename(app_model, conversation_id, end_user, args['name'])
  58. except ConversationNotExistsError:
  59. raise NotFound("Conversation Not Exists.")
  60. class ConversationPinApi(WebApiResource):
  61. def patch(self, app_model, end_user, c_id):
  62. if app_model.mode != 'chat':
  63. raise NotChatAppError()
  64. conversation_id = str(c_id)
  65. try:
  66. WebConversationService.pin(app_model, conversation_id, end_user)
  67. except ConversationNotExistsError:
  68. raise NotFound("Conversation Not Exists.")
  69. return {"result": "success"}
  70. class ConversationUnPinApi(WebApiResource):
  71. def patch(self, app_model, end_user, c_id):
  72. if app_model.mode != 'chat':
  73. raise NotChatAppError()
  74. conversation_id = str(c_id)
  75. WebConversationService.unpin(app_model, conversation_id, end_user)
  76. return {"result": "success"}
  77. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='web_conversation_name')
  78. api.add_resource(ConversationListApi, '/conversations')
  79. api.add_resource(ConversationApi, '/conversations/<uuid:c_id>')
  80. api.add_resource(ConversationPinApi, '/conversations/<uuid:c_id>/pin')
  81. api.add_resource(ConversationUnPinApi, '/conversations/<uuid:c_id>/unpin')