def test_integration_data_to_users(self): """ This is a RealtimeClient integration test. For current implementation, subscribe to the notifications topic to see it working: redis-cli subscribe notifications """ RealtimeClientData.set_user_session('123', 3) RealtimeClientData.send_to_users([3], RealtimeClientData.Types.CHAT_MESSAGE, {'msg': 'hello'})
def test_integration_data_to_users(self): """ This is a RealtimeClient integration test. For current implementation, subscribe to the notifications topic to see it working: redis-cli subscribe notifications """ RealtimeClientData.set_user_session('123', 3) RealtimeClientData.send_to_users([3], {'msg': 'hello'})
def create(self, validated_data): message = MessageModel.objects.create( author_id=self.context['request'].user.id, in_conversation_id=self.context['request'].data['in_conversation_id'], **validated_data) serialized_message = self.to_representation(message) payload = {'conversation_id': message.in_conversation_id, 'message': serialized_message} RealtimeClientData.send_to_users(list(message.in_conversation.participants.values_list('id', flat=True)), RealtimeClientData.Types.CONVERSATION_MESSAGE, payload) return message
def post(self, request): """log out the user --- tags: - Authentication responses: 200: description: User logged out ... :type request: HttpRequest """ RealtimeClientData.destroy_user_session(request.session.session_key) logout(request) return self.success()
def create(self, validated_data): message = MessageModel.objects.create( author_id=self.context['request'].user.id, in_conversation_id=self.context['request']. data['in_conversation_id'], **validated_data) serialized_message = self.to_representation(message) payload = { 'conversation_id': message.in_conversation_id, 'message': serialized_message } RealtimeClientData.send_to_users( list( message.in_conversation.participants.values_list('id', flat=True)), RealtimeClientData.Types.CONVERSATION_MESSAGE, payload) return message
def test_session_key(self): self.assertEqual(RealtimeClientData.session_key('123'), 'session-store-123')
def test_set_get_django_redis(self): RealtimeClientData.set_user_session('123', 3) self.assertEqual(RealtimeClientData.get_user_by_session('123'), '3') RealtimeClientData.set_user_session('123', 4) self.assertEqual(RealtimeClientData.get_user_by_session('123'), '4')
def post(self, request, chat): """ Send a new message in given chat --- tags: - Chat parameters: - in: path name: chat description: ID of chat type: integer - in: body name: body schema: id: send_message required: - type - content properties: content: type: string example: Hi Peter, how are you? description: Content, e.g. utf8-formatted plain text responses: 201: description: Chat message added schema: id: message type: object required: - content - sender - created_at - id properties: 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: ConversationModel """ message = MessageModel.objects.create( sent_by_id=request.user.id, in_conversation_id=chat.id, content=request.body['content'], ) serialized_message = serializers.conversation_message(message) payload = {'chat_id': chat.id, 'message': serialized_message} RealtimeClientData.send_to_users(list(chat.participants.values_list('id', flat=True)), RealtimeClientData.Types.CHAT_MESSAGE, payload) return self.created(serialized_message)