conversation.py 4.6 KB

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