diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2023-07-11 01:07:10 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2024-02-18 11:32:53 +0100 |
commit | e6afe041876394a6f75cbb3e8671faa3dd91b0cc (patch) | |
tree | 1f43efb24d26455c553db7440e9910664b12be31 | |
parent | 675d28844004a1acb3bb573c52f69efe5e094f07 (diff) | |
download | wee-slack-e6afe041876394a6f75cbb3e8671faa3dd91b0cc.tar.gz |
Show correct date/time for messages
-rw-r--r-- | slack/slack_conversation.py | 19 | ||||
-rw-r--r-- | slack/slack_message.py | 24 |
2 files changed, 31 insertions, 12 deletions
diff --git a/slack/slack_conversation.py b/slack/slack_conversation.py index e4504cd..1e8728d 100644 --- a/slack/slack_conversation.py +++ b/slack/slack_conversation.py @@ -8,7 +8,7 @@ from typing import TYPE_CHECKING, List, Optional import weechat from slack.shared import shared -from slack.slack_message import SlackMessage +from slack.slack_message import SlackMessage, SlackTs from slack.task import gather from slack.util import get_callback_name @@ -39,7 +39,7 @@ class SlackConversation: self.workspace = workspace self._info = info self._members: Optional[List[str]] = None - self._messages: OrderedDict[str, SlackMessage] = OrderedDict() + self._messages: OrderedDict[SlackTs, SlackMessage] = OrderedDict() # TODO: buffer_pointer may be accessed by buffer_switch before it's initialized self.buffer_pointer: str = "" self.is_loading = False @@ -162,12 +162,10 @@ class SlackConversation: sender_user_ids = [m.sender_user_id for m in messages if m.sender_user_id] await self.workspace.users.initialize_items(sender_user_ids) - messages_rendered = await gather( - *(message.render_message() for message in messages) - ) + await gather(*(message.render() for message in messages)) - for rendered in reversed(messages_rendered): - weechat.prnt(self.buffer_pointer, rendered) + for message in reversed(messages): + self.print_message(message, await message.render()) print(f"history w/o fetch took: {time.time() - start}") self.history_filled = True @@ -176,13 +174,16 @@ class SlackConversation: async def add_message(self, message: SlackMessage): self._messages[message.ts] = message if self.history_filled: - message_rendered = await message.render_message() - weechat.prnt(self.buffer_pointer, message_rendered) + rendered = await message.render() + self.print_message(message, rendered) else: weechat.buffer_set( self.buffer_pointer, "hotlist", str(message.priority.value) ) + def print_message(self, message: SlackMessage, rendered: str): + weechat.prnt_date_tags(self.buffer_pointer, message.ts.major, "", rendered) + def _buffer_input_cb(self, data: str, buffer: str, input_data: str) -> int: weechat.prnt(buffer, "Text: %s" % input_data) return weechat.WEECHAT_RC_OK diff --git a/slack/slack_message.py b/slack/slack_message.py index 1a3365f..bc667d7 100644 --- a/slack/slack_message.py +++ b/slack/slack_message.py @@ -25,11 +25,25 @@ class MessagePriority(Enum): HIGHLIGHT = 3 +class SlackTs: + def __init__(self, ts: str): + self.major, self.minor = [int(x) for x in ts.split(".", 1)] + + def __eq__(self, other: object) -> bool: + if not isinstance(other, SlackTs): + return False + return self.major == other.major and self.minor == other.minor + + def __hash__(self) -> int: + return hash((self.major, self.minor)) + + class SlackMessage: def __init__(self, conversation: SlackConversation, message_json: SlackMessageDict): self._message_json = message_json + self._rendered = None self.conversation = conversation - self.ts = message_json["ts"] + self.ts = SlackTs(message_json["ts"]) @property def workspace(self) -> SlackWorkspace: @@ -43,11 +57,15 @@ class SlackMessage: def priority(self) -> MessagePriority: return MessagePriority.MESSAGE - async def render_message(self) -> str: + async def render(self) -> str: + if self._rendered is not None: + return self._rendered + prefix_coro = self._prefix() message_coro = self._unfurl_refs(self._message_json["text"]) prefix, message = await gather(prefix_coro, message_coro) - return f"{prefix}\t{message}" + self._rendered = f"{prefix}\t{message}" + return self._rendered async def _prefix(self) -> str: if ( |