diff options
-rw-r--r-- | poezio/mam.py | 92 |
1 files changed, 66 insertions, 26 deletions
diff --git a/poezio/mam.py b/poezio/mam.py index fea0ffc7..cc40e04c 100644 --- a/poezio/mam.py +++ b/poezio/mam.py @@ -6,38 +6,78 @@ XEP-0313: Message Archive Management(MAM). """ -from datetime import datetime, timezone +import asyncio +from datetime import datetime, timedelta, timezone from poezio.theming import get_theme from poezio.text_buffer import Message, TextBuffer -async def MAM(self, remote_jid, start, end): +def add_line(text_buffer: TextBuffer, text: str, str_time: str, nick: str, top: bool): + """Adds a textual entry in the TextBuffer""" + + time = datetime.strftime(str_time, '%Y-%m-%d %H:%M:%S') + time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S') + nick = nick.split('/')[1] + color = get_theme().COLOR_OWN_NICK + top = top + text_buffer.add_message( + text, + time, + nick, + color, + True, # History + None, # User + False, # Highlight + top, #Top + None, # Identifier + None, # str_time + None, # Jid + ) + +async def MAM(self, remote_jid, start, end, top): self.remote_jid = remote_jid self.start_date = start self.end_date = end text_buffer = self._text_buffer results = self.core.xmpp.plugin['xep_0313'].retrieve(jid=self.remote_jid, - iterator=True, start=self.start_date, end=self.end_date) + iterator=True, reverse=top, start=self.start_date, end=self.end_date) + msg_count = 0 + msgs = [] async for rsm in results: - for msg in rsm['mam']['results']: - forwarded = msg['mam_result']['forwarded'] - timestamp = forwarded['delay']['stamp'] - message = forwarded['stanza'] - text = str(message['body']) - time = datetime.strftime(timestamp, '%Y-%m-%d %H:%M:%S') - time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S') - time = time.replace(tzinfo=timezone.utc).astimezone(tz=None) - nick = str(message['from']).split('/')[1] - color = get_theme().COLOR_OWN_NICK - text_buffer.add_message( - text, - time, - nick, - color, - True, # History - None, # User - False, # Highlight - None, # Identifier - None, # str_time - None, # Jid - ) - self.core.refresh_window() + if top: + for msg in rsm['mam']['results']: + msgs.append(msg) + if msg_count == 10: + return + msg_count += 1 + msgs.reverse() + for msg in msgs: + forwarded = msg['mam_result']['forwarded'] + timestamp = forwarded['delay']['stamp'] + message = forwarded['stanza'] + add_line(text_buffer, '%s' % message['body'], timestamp, str(message['from']), top) + self.text_win.scroll_up(len(self.text_win.built_lines)) + self.core.refresh_window() + else: + for msg in rsm['mam']['results']: + forwarded = msg['mam_result']['forwarded'] + timestamp = forwarded['delay']['stamp'] + message = forwarded['stanza'] + add_line(text_buffer, '%s' % message['body'], timestamp, str(message['from']), top) + self.core.refresh_window() + + +def mam_scroll(self): + remote_jid = self.jid + text_buffer = self._text_buffer + end = datetime.now() + for message in text_buffer.messages: + time = message.time + if time < end: + end = time + end = end + timedelta(seconds=-1) + end = datetime.strftime(end, '%Y-%m-%dT%H:%M:%SZ') + start = datetime.strptime(end, '%Y-%m-%dT%H:%M:%SZ') + start = start + timedelta(days=-10) + start = datetime.strftime(start, '%Y-%m-%dT%H:%M:%SZ') + top = True + asyncio.ensure_future(MAM(self, remote_jid, start, end, top))
\ No newline at end of file |