From d4ee8676f97c17f09f9115a0e8094f282b524864 Mon Sep 17 00:00:00 2001 From: Duy Nguyen Date: Thu, 30 Aug 2018 11:15:59 +0700 Subject: [PATCH] Fix slack message attachment --- app.py | 5 ++++- sclack/components.py | 33 ++++++++++++++++++++++++++++----- sclack/utils/message.py | 19 +++++++++++++++++++ 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 sclack/utils/message.py diff --git a/app.py b/app.py index 1c867c3..494cbac 100755 --- a/app.py +++ b/app.py @@ -447,11 +447,14 @@ def render_message(self, message, channel_id=None): 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'), + 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') diff --git a/sclack/components.py b/sclack/components.py index 9abc37f..96733bc 100644 --- a/sclack/components.py +++ b/sclack/components.py @@ -11,6 +11,7 @@ from .markdown import MarkdownText from .store import Store from sclack.utils.channel import is_group, is_channel, is_dm +from sclack.utils.message import format_date_time MARK_READ_ALARM_PERIOD = 3 @@ -36,35 +37,57 @@ def __init__(self, service_name=None, title=None, title_link=None, + from_url=None, author_name=None, pretext=None, text=None, fields=None, + attachment_text=None, + ts=None, footer=None): body = [] if not color: color = 'CCCCCC' color = '#{}'.format(shorten_hex(color)) + self._image_index = 0 + self.from_url = from_url + if service_name: body.append(urwid.Text(('attachment_title', service_name))) self._image_index = self._image_index + 1 + if title: - body.append(urwid.Text(('attachment_title', title))) + body.append(urwid.Text(('attachment_title', title.strip()))) self._image_index = self._image_index + 1 + if author_name: body.append(urwid.Text(('attachment_title', author_name))) self._image_index = self._image_index + 1 + if pretext: body.append(urwid.Text(MarkdownText(pretext).markup)) self._image_index = self._image_index + 1 - if text: - body.append(urwid.Text(MarkdownText(text).markup)) + + text_display = attachment_text if attachment_text is not None else text + if text_display: + body.append(urwid.Text(MarkdownText(text_display.strip()).markup)) + if fields: body.append(Fields(fields)) - if footer: - body.append(urwid.Text(MarkdownText(footer).markup)) + + if footer or ts: + footer_parts = [] + if footer: + footer_parts.append(footer) + if ts: + footer_parts.append(format_date_time(ts)) + + footer_text = ' | '.join(footer_parts) + body.append(urwid.Text(MarkdownText(footer_text).markup)) + self.pile = urwid.Pile(body) + super(Attachment, self).__init__(self.pile, color) @property diff --git a/sclack/utils/message.py b/sclack/utils/message.py new file mode 100644 index 0000000..e784826 --- /dev/null +++ b/sclack/utils/message.py @@ -0,0 +1,19 @@ +from datetime import datetime + + +def format_date_time(ts): + """ + Format date time for message + :param ts: + :return: + """ + message_datetime = datetime.fromtimestamp(float(ts)) + message_date = message_datetime.date() + today = datetime.today().date() + + if message_date == today: + date_text = message_datetime.strftime('Today at %I:%M%p') + else: + date_text = message_datetime.strftime('%b %d, %Y at %I:%M%p') + + return date_text