Use the new receiving_chain_length endpoint

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2021-12-16 01:05:00 +01:00
parent 80cdab3ba3
commit 9e67d7d887

View file

@ -62,11 +62,6 @@ PUBLISH_OPTIONS_NODE = 'http://jabber.org/protocol/pubsub#publish-options'
PUBSUB_ERRORS = 'http://jabber.org/protocol/pubsub#errors' PUBSUB_ERRORS = 'http://jabber.org/protocol/pubsub#errors'
class ChainLengths(TypedDict):
receiving: List[Tuple[int, int]]
sending: List[Tuple[int, int]]
def b64enc(data: bytes) -> str: def b64enc(data: bytes) -> str:
return base64.b64encode(bytes(bytearray(data))).decode('ASCII') return base64.b64encode(bytes(bytearray(data))).decode('ASCII')
@ -524,23 +519,15 @@ class XEP_0384(BasePlugin):
devices = await self._omemo().getDevices(jid.bare) devices = await self._omemo().getDevices(jid.bare)
return devices.get('active', []) return devices.get('active', [])
async def _chain_lengths(self, jid: JID) -> ChainLengths: async def _chain_lengths(self, jid: JID) -> Set[Tuple[int, int]]:
""" """
Gather receiving and sending chain lengths for all devices (active Gather receiving chain lengths for all devices (active / inactive)
/ inactive) of a JID. of a JID.
Receiving chain length is used to know when to send a heartbeat to Receiving chain length is used to know when to send a heartbeat to
signal recipients our device is still active and listening. See: signal recipients our device is still active and listening. See:
https://xmpp.org/extensions/xep-0384.html#rules https://xmpp.org/extensions/xep-0384.html#rules
Sending chain length is used on the other side when a device
hasn't been sending us messages and seems inactive.
# XXX: Only the receiving part is used in this library for the
# moment.
""" """
# XXX: This method uses APIs that haven't been made public yet in the
# OMEMO library as of 0.12 (9fd7123).
bare = jid.bare bare = jid.bare
devices = await self._omemo().getDevices(bare) devices = await self._omemo().getDevices(bare)
@ -548,16 +535,10 @@ class XEP_0384(BasePlugin):
inactive = devices.get('inactive', set()) inactive = devices.get('inactive', set())
devices = active.union(inactive) devices = active.union(inactive)
lengths: ChainLengths = {'sending': [], 'receiving': []} lengths: Set[Tuple[int, int]] = set()
for did in devices: for did in devices:
session = self._omemo()._SessionManager__loadSession(bare, did) receiving = await self._omemo().receiving_chain_length(bare, did)
if session is None: lengths.add((did, receiving or 0))
continue
skr = session._DoubleRatchet__skr
sending = skr.sending_chain_length or 0
receiving = skr.receiving_chain_length or 0
lengths['sending'].append((did, sending))
lengths['receiving'].append((did, receiving))
return lengths return lengths
@ -575,8 +556,7 @@ class XEP_0384(BasePlugin):
still active. still active.
""" """
chain_lengths = await self._chain_lengths(jid) receiving_chain_lengths = await self._chain_lengths(jid)
receiving_chain_lengths = chain_lengths.get('receiving', [])
lengths = map(lambda d_l: d_l[1], receiving_chain_lengths) lengths = map(lambda d_l: d_l[1], receiving_chain_lengths)
inactive_session = max(lengths, default=0) > self.heartbeat_after inactive_session = max(lengths, default=0) > self.heartbeat_after