diff --git a/slixmpp_omemo/__init__.py b/slixmpp_omemo/__init__.py index 841a46a..e736017 100644 --- a/slixmpp_omemo/__init__.py +++ b/slixmpp_omemo/__init__.py @@ -16,6 +16,7 @@ from typing import Any, Dict, List, Optional, Set, Tuple, Union # Not available in Python 3.7, and slixmpp already imports the right things # for me from slixmpp.types import TypedDict +from functools import reduce import os import json @@ -533,10 +534,12 @@ class XEP_0384(BasePlugin): for did in devices: session = self._omemo._SessionManager__loadSession(bare, did) if session is None: - break + continue skr = session._DoubleRatchet__skr - lengths['sending'].append((did, skr.sending_chain_length)) - lengths['receiving'].append((did, skr.receiving_chain_length)) + sending = skr.sending_chain_length or -1 + receiving = skr.receiving_chain_length or -1 + lengths['sending'].append((did, sending)) + lengths['receiving'].append((did, receiving)) return lengths @@ -545,11 +548,16 @@ class XEP_0384(BasePlugin): Returns whether we should send a heartbeat message for JID. See notes about heartbeat in https://xmpp.org/extensions/xep-0384.html#rules. + + This method will return True when a session among all of the + sessions for this JID is not yet confirmed, or if one of the + sessions hasn't been answered in a while. """ receiving_chain_lengths = self._chain_lengths(jid).get('receiving', []) lengths = map(lambda d_l: d_l[1], receiving_chain_lengths) - return max(lengths, default=0) > self.heartbeat_after + min_length = reduce(lambda x, d_l: min(x, d_l[1]), receiving_chain_lengths, 0) == -1 + return min_length or max(lengths, default=0) > self.heartbeat_after async def make_heartbeat(self, jid: JID) -> Message: """