Example #1
0
    def render_message(self, message, channel_id=None):
        is_app = False
        subtype = message.get('subtype')

        if subtype == SCLACK_SUBTYPE:
            message = Message(
                message['ts'],
                '',
                User('1', 'sclack'),
                MarkdownText(message['text']),
                Indicators(False, False)
            )
            urwid.connect_signal(message, 'go_to_sidebar', self.go_to_sidebar)
            urwid.connect_signal(message, 'quit_application', self.quit_application)
            urwid.connect_signal(message, 'set_insert_mode', self.set_insert_mode)
            urwid.connect_signal(message, 'mark_read', self.handle_mark_read)

            return message

        message_text = message['text']
        files = message.get('files', [])

        # Files uploaded
        if len(files) > 0:
            file_links = ['"{}" <{}>'.format(file.get('title'), file.get('url_private')) for file in message.get('files')]
            file_upload_text = 'File{} uploaded'.format('' if len(files) == 1 else 's')
            file_text = '{} {}'.format(file_upload_text ,', '.join(file_links))

            if message_text == '':
                message_text = file_text
            else:
                message_text = '{}\n{}'.format(message_text, file_text)

        if subtype == 'bot_message':
            bot = (self.store.find_user_by_id(message['bot_id'])
                or self.store.find_or_load_bot(message['bot_id']))
            if bot:
                user_id = message['bot_id']
                user_name = bot.get('profile', {}).get('display_name') or bot.get('name')
                color = bot.get('color')
                is_app = 'app_id' in bot
            else:
                return None
        elif subtype == 'file_comment':
            user = self.store.find_user_by_id(message['comment']['user'])

            # A temporary fix for a null pointer exception for truncated or deleted users
            if user is None:
                return None

            user_id = user['id']
            user_name = user['profile']['display_name'] or user.get('name')
            color = user.get('color')
            if message.get('file'):
                message['file'] = None
        else:
            user = self.store.find_user_by_id(message['user'])

            # A temporary fix for a null pointer exception for truncated or deleted users
            if user is None:
                return None

            user_id = user['id']
            user_name = user['profile']['display_name'] or user.get('name')
            color = user.get('color')

        user = User(user_id, user_name, color, is_app)
        text = MarkdownText(message_text)
        indicators = Indicators('edited' in message, message.get('is_starred', False))
        reactions = [
            Reaction(reaction['name'], reaction['count'])
            for reaction in message.get('reactions', [])
        ]

        attachments = []
        for attachment in message.get('attachments', []):
            attachment_widget = Attachment(
                service_name=attachment.get('service_name'),
                title=attachment.get('title'),
                from_url=attachment.get('from_url'),
                fields=attachment.get('fields'),
                color=attachment.get('color'),
                author_name=attachment.get('author_name') or attachment.get('author_subname'),
                pretext=attachment.get('pretext'),
                text=message_text,
                attachment_text=attachment.get('text'),
                ts=attachment.get('ts'),
                footer=attachment.get('footer')
            )
            image_url = attachment.get('image_url')
            if image_url and self.config['features']['pictures']:
                loop.create_task(self.load_picture_async(
                    image_url,
                    attachment.get('image_width', 500),
                    attachment_widget,
                    auth=False
                ))
            attachments.append(attachment_widget)

        file = message.get('file')

        if file:
            files.append(file)

        message_channel = channel_id if channel_id is not None else message.get('channel')

        message = Message(
            message['ts'],
            message_channel,
            user,
            text,
            indicators,
            attachments=attachments,
            reactions=reactions
        )

        self.lazy_load_images(files, message)

        urwid.connect_signal(message, 'edit_message', self.edit_message)
        urwid.connect_signal(message, 'get_permalink', self.get_permalink)
        urwid.connect_signal(message, 'go_to_profile', self.go_to_profile)
        urwid.connect_signal(message, 'go_to_sidebar', self.go_to_sidebar)
        urwid.connect_signal(message, 'delete_message', self.delete_message)
        urwid.connect_signal(message, 'quit_application', self.quit_application)
        urwid.connect_signal(message, 'set_insert_mode', self.set_insert_mode)
        urwid.connect_signal(message, 'mark_read', self.handle_mark_read)

        return message
Example #2
0
File: app.py Project: budnik/sclack
    def render_message(self, message):
        is_app = False
        subtype = message.get('subtype')
        if subtype == 'bot_message':
            bot = (self.store.find_user_by_id(message['bot_id'])
                   or self.store.find_or_load_bot(message['bot_id']))
            if bot:
                user_id = message['bot_id']
                user_name = bot.get('profile',
                                    {}).get('display_name') or bot.get('name')
                color = bot.get('color')
                is_app = 'app_id' in bot
            else:
                return None
        elif subtype == 'file_comment':
            user = self.store.find_user_by_id(message['comment']['user'])
            user_id = user['id']
            user_name = user['profile']['display_name'] or user.get('name')
            color = user.get('color')
            if message.get('file'):
                message['file'] = None
        else:
            user = self.store.find_user_by_id(message['user'])
            user_id = user['id']
            user_name = user['profile']['display_name'] or user.get('name')
            color = user.get('color')

        user = User(user_id, user_name, color, is_app)
        text = MarkdownText(message['text'])
        indicators = Indicators('edited' in message,
                                message.get('is_starred', False))
        reactions = [
            Reaction(reaction['name'], reaction['count'])
            for reaction in message.get('reactions', [])
        ]
        file = message.get('file')
        attachments = []
        for attachment in message.get('attachments', []):
            attachment_widget = Attachment(
                service_name=attachment.get('service_name'),
                title=attachment.get('title'),
                fields=attachment.get('fields'),
                color=attachment.get('color'),
                author_name=attachment.get('author_name'),
                pretext=attachment.get('pretext'),
                text=attachment.get('text'),
                footer=attachment.get('footer'))
            image_url = attachment.get('image_url')
            if image_url and self.config['features']['pictures']:
                loop.create_task(
                    self.load_picture_async(image_url,
                                            attachment.get('image_width', 500),
                                            attachment_widget,
                                            auth=False))
            attachments.append(attachment_widget)
        message = Message(message['ts'],
                          user,
                          text,
                          indicators,
                          attachments=attachments,
                          reactions=reactions)
        if (file and file.get('filetype')
                in ('bmp', 'gif', 'jpeg', 'jpg', 'png')
                and self.config['features']['pictures']):
            loop.create_task(
                self.load_picture_async(file['url_private'],
                                        file.get('original_w', 500), message))
        urwid.connect_signal(message, 'edit_message', self.edit_message)
        urwid.connect_signal(message, 'go_to_profile', self.go_to_profile)
        urwid.connect_signal(message, 'delete_message', self.delete_message)
        urwid.connect_signal(message, 'set_insert_mode', self.set_insert_mode)
        return message