Esempio n. 1
0
    def receive(self, text_data):

        text_data_json = json.loads(text_data)
        message = text_data_json['message']
        user = self.scope['user']
        Message.create_message(content=message)

        # Send message to room group
        async_to_sync(self.channel_layer.group_send)(self.room_group_name, {
            'type': 'chat_message',
            'message': message,
            'user': user.username
        })
Esempio n. 2
0
    def create_message(self, content):
        channel_name = content['channel_name']
        text = content['text']
        author_id = content['author_id']
        conversation_id = content['conversation_id']
        response_to = content['seq']

        # initialized to success values, any exception caught should change that
        error_code = ErrorEnum.OK
        error_message = ''
        message = None

        try:
            # validate if the user is allowed to do this operation
            message = Message.create_message(author_id=author_id,
                                             conversation_id=conversation_id,
                                             text=text)

        except Conversation.DoesNotExist:
            # failed
            error_code = ErrorEnum.CONVERSATION_CLOSED
            error_message = 'Conversation has closed'

        finally:
            return_content = self._create_base_return_content(
                'create_message_response', error_code, error_message,
                response_to)

            if message is not None:
                return_content['message'] = {
                    'text': message.text,
                    'conversation_id': message.conversation_id,
                    'author_id': author_id,
                    'time': time.mktime(message.time.timetuple())
                }

            async_to_sync(self.channel_layer.send)(channel_name,
                                                   return_content)