summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--poezio/mam.py113
1 files changed, 8 insertions, 105 deletions
diff --git a/poezio/mam.py b/poezio/mam.py
index 468d12e7..56dad6bf 100644
--- a/poezio/mam.py
+++ b/poezio/mam.py
@@ -1,20 +1,17 @@
-#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
-
"""
Query and control an archive of messages stored on a server using
XEP-0313: Message Archive Management(MAM).
"""
+from __future__ import annotations
+
import asyncio
import logging
-import random
from datetime import datetime, timedelta, timezone
from hashlib import md5
from typing import (
Any,
AsyncIterable,
- Callable,
Dict,
List,
Optional,
@@ -24,13 +21,10 @@ from slixmpp import JID, Message as SMessage
from slixmpp.exceptions import IqError, IqTimeout
from poezio.theming import get_theme
from poezio import tabs
-from poezio import xhtml, colors
-from poezio.config import config
+from poezio import colors
from poezio.common import to_utc
-from poezio.text_buffer import TextBuffer, HistoryGap
from poezio.ui.types import (
BaseMessage,
- EndOfArchive,
Message,
)
@@ -48,6 +42,7 @@ def make_line(
time: datetime,
jid: JID,
identifier: str = '',
+ nick: str = ''
) -> Message:
"""Adds a textual entry in the TextBuffer"""
@@ -71,11 +66,13 @@ def make_line(
color = theme.LIST_COLOR_NICKNAMES[nick_pos]
else:
if jid.bare == tab.core.xmpp.boundjid.bare:
- nick = tab.core.own_nick
+ if not nick:
+ nick = tab.core.own_nick
color = get_theme().COLOR_OWN_NICK
else:
color = get_theme().COLOR_REMOTE_USER
- nick = tab.get_nick()
+ if not nick:
+ nick = tab.get_nick()
return Message(
txt=text,
identifier=identifier,
@@ -193,97 +190,3 @@ async def fetch_history(tab: tabs.ChatTab,
reverse=True,
)
return await retrieve_messages(tab, mam_iterator, amount)
-
-async def fill_missing_history(tab: tabs.ChatTab, gap: HistoryGap) -> None:
- start = gap.last_timestamp_before_leave
- end = gap.first_timestamp_after_join
- if start:
- start = start + timedelta(seconds=1)
- if end:
- end = end - timedelta(seconds=1)
- try:
- messages = await fetch_history(tab, start=start, end=end, amount=999)
- tab._text_buffer.add_history_messages(messages, gap=gap)
- if messages:
- tab.core.refresh_window()
- except (NoMAMSupportException, MAMQueryException, DiscoInfoException):
- return
- finally:
- tab.query_status = False
-
-async def on_new_tab_open(tab: tabs.ChatTab) -> None:
- """Called when opening a new tab"""
- amount = 2 * tab.text_win.height
- end = datetime.now()
- for message in tab._text_buffer.messages:
- if isinstance(message, Message) and to_utc(message.time) < to_utc(end):
- end = message.time
- break
- end = end - timedelta(microseconds=1)
- try:
- messages = await fetch_history(tab, end=end, amount=amount)
- tab._text_buffer.add_history_messages(messages)
- if messages:
- tab.core.refresh_window()
- except (NoMAMSupportException, MAMQueryException, DiscoInfoException):
- return None
- finally:
- tab.query_status = False
-
-
-def schedule_tab_open(tab: tabs.ChatTab) -> None:
- """Set the query status and schedule a MAM query"""
- tab.query_status = True
- asyncio.ensure_future(on_tab_open(tab))
-
-
-async def on_tab_open(tab: tabs.ChatTab) -> None:
- gap = tab._text_buffer.find_last_gap_muc()
- if gap is None or not gap.leave_message:
- await on_new_tab_open(tab)
- else:
- await fill_missing_history(tab, gap)
-
-
-def schedule_scroll_up(tab: tabs.ChatTab) -> None:
- """Set query status and schedule a scroll up"""
- tab.query_status = True
- asyncio.ensure_future(on_scroll_up(tab))
-
-
-async def on_scroll_up(tab: tabs.ChatTab) -> None:
- tw = tab.text_win
-
- # If position in the tab is < two screen pages, then fetch MAM, so that we
- # keep some prefetched margin. A first page should also be prefetched on
- # join if not already available.
- total, pos, height = len(tw.built_lines), tw.pos, tw.height
- rest = (total - pos) // height
-
- if rest > 1:
- tab.query_status = False
- return None
-
- try:
- # XXX: Do we want to fetch a possibly variable number of messages?
- # (InfoTab changes height depending on the type of messages, see
- # `information_buffer_popup_on`).
- messages = await fetch_history(tab, amount=height)
- last_message_exists = False
- if tab._text_buffer.messages:
- last_message = tab._text_buffer.messages[0]
- last_message_exists = True
- if not messages and last_message_exists and not isinstance(last_message, EndOfArchive):
- time = tab._text_buffer.messages[0].time
- messages = [EndOfArchive('End of archive reached', time=time)]
- tab._text_buffer.add_history_messages(messages)
- if messages:
- tab.core.refresh_window()
- except NoMAMSupportException:
- tab.core.information('MAM not supported for %r' % tab.jid, 'Info')
- return None
- except (MAMQueryException, DiscoInfoException):
- tab.core.information('An error occured when fetching MAM for %r' % tab.jid, 'Error')
- return None
- finally:
- tab.query_status = False