conversation.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding:utf-8 -*-
  2. from flask import request
  3. from flask_restful import fields, marshal_with, reqparse
  4. from flask_restful.inputs import int_range
  5. from werkzeug.exceptions import NotFound
  6. from controllers.service_api import api
  7. from controllers.service_api.app import create_or_update_end_user_for_user_id
  8. from controllers.service_api.app.error import NotChatAppError
  9. from controllers.service_api.wraps import AppApiResource
  10. from fields.conversation_fields import conversation_infinite_scroll_pagination_fields, simple_conversation_fields
  11. from libs.helper import TimestampField, uuid_value
  12. import services
  13. from services.conversation_service import ConversationService
  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=True, location='json')
  52. parser.add_argument('user', type=str, location='json')
  53. args = parser.parse_args()
  54. if end_user is None and args['user'] is not None:
  55. end_user = create_or_update_end_user_for_user_id(app_model, args['user'])
  56. try:
  57. return ConversationService.rename(app_model, conversation_id, end_user, args['name'])
  58. except services.errors.conversation.ConversationNotExistsError:
  59. raise NotFound("Conversation Not Exists.")
  60. api.add_resource(ConversationRenameApi, '/conversations/<uuid:c_id>/name', endpoint='conversation_name')
  61. api.add_resource(ConversationApi, '/conversations')
  62. api.add_resource(ConversationApi, '/conversations/<uuid:c_id>', endpoint='conversation')
  63. api.add_resource(ConversationDetailApi, '/conversations/<uuid:c_id>', endpoint='conversation_detail')