def get(self, request, chat): """Retrieve all the messages in the given chat, sorted descending by time (most recent first). --- tags: - Chat parameters: - in: path name: chatid description: ID of chat type: integer - in: query name: take description: Number of messages to retrieve type: integer - in: query name: before_id description: The newest retrieved message is the first earlier one than this ID type: integer responses: 200: description: Result message set schema: id: result_messages type: object required: - messages properties: messages: type: array items: $ref: '#/definitions/message' 403: description: The logged in user is not part of the conversation schema: id: result_error_forbidden type: object required: - reason properties: reason: type: string 404: description: the chat does not exist ... :type request: HttpRequest :type chat: ChatModel """ messages = MessageModel.objects \ .filter(in_conversation=chat.id) \ .order_by('-created_at') take = request.GET.get('take') before_id = request.GET.get('before_id') if take is None and before_id is None: messages = messages.all() if before_id is not None: before_time = MessageModel.objects.get( id=int(before_id)).created_at messages = messages.filter(created_at__lt=before_time).all() if take is not None: messages = messages[:int(take)] return self.success({ 'messages': [serializers.message(message) for message in messages] })
def get(self, request, chat): """Retrieve all the messages in the given chat, sorted descending by time (most recent first). --- tags: - Chat parameters: - in: path name: chatid description: ID of chat type: integer - in: query name: take description: Number of messages to retrieve type: integer - in: query name: before_id description: The newest retrieved message is the first earlier one than this ID type: integer responses: 200: description: Result message set schema: id: result_messages type: object required: - messages properties: messages: type: array items: $ref: '#/definitions/message' 403: description: The logged in user is not part of the conversation schema: id: result_error_forbidden type: object required: - reason properties: reason: type: string 404: description: the chat does not exist ... :type request: HttpRequest :type chat: ChatModel """ messages = MessageModel.objects \ .filter(in_conversation=chat.id) \ .order_by('-created_at') take = request.GET.get('take') before_id = request.GET.get('before_id') if take is None and before_id is None: messages = messages.all() if before_id is not None: before_time = MessageModel.objects.get(id=int(before_id)).created_at messages = messages.filter(created_at__lt=before_time).all() if take is not None: messages = messages[:int(take)] return self.success({'messages': [serializers.message(message) for message in messages]})
def post(self, request, chat): """ Send a new message in given chat --- tags: - Chat parameters: - in: path name: chatid description: ID of chat type: integer - in: body name: body schema: id: send_message required: - type - content properties: type: type: string enum: [TEXT, IMAGE] description: Type of this message example: TEXT content: type: string example: Hi Peter, how are you? description: Content, e.g. utf8-formatted plain text or image id received by the upload endpoint responses: 201: description: Chat message added schema: id: message type: object required: - type - content - sender - created_at - id properties: type: type: string enum: [TEXT, IMAGE] description: Type of this message example: TEXT content: type: string example: Hi Peter, how are you? description: Content, e.g. utf8-formatted plain text or image id received by the upload endpoint sender: type: integer description: Userid of sender of message example: 82 created_at: type: string description: ISO 8601 formatted timestring in UTC timezone (YYYY-MM-DDTHH:MM:SS) example: 2007-12-24T18:21:00.003423 id: type: integer description: Unique identifier of this message example: 124624 403: description: The logged in user is not part of the conversation schema: $ref: '#/definitions/result_error_forbidden' 404: description: the chat does not exist ... :type request: HttpRequest :type chat: ChatModel """ message = MessageModel.objects.create( sent_by_id=request.user.id, in_conversation_id=chat.id, type=request.body['type'], content=request.body['content'], ) return self.created(serializers.message(message))