1
0
Fork 0

Don't update presence status if not needed

Status messages are sent with every presence update by some XMPP
clients, which would previously result in corresponding changes being
made on the WhatsApp side, where no effective change in status has been
made since.

This commit implements naive caching for presence status in order to
avoid most double-sets, but which however will clear across restarts.
This commit is contained in:
Alex Palaistras 2024-04-29 21:36:29 +01:00
parent f2b4997473
commit 1275ef55d1
2 changed files with 10 additions and 4 deletions

View File

@ -100,7 +100,7 @@ func (s *Session) Login() error {
select {
case <-timer.C:
if presence == PresenceAvailable {
s.GetContacts(false)
_, _ = s.GetContacts(false)
timer, timerStopped = newTimer(presenceRefreshInterval), false
} else {
timerStopped = true
@ -112,7 +112,7 @@ func (s *Session) Login() error {
}
return
} else if timerStopped && p == PresenceAvailable {
s.GetContacts(false)
_, _ = s.GetContacts(false)
timer, timerStopped = newTimer(presenceRefreshInterval), false
}
presence = p

View File

@ -77,6 +77,7 @@ class Session(BaseSession[str, Recipient]):
self.whatsapp.SetEventHandler(self._handle_event)
self._connected = self.xmpp.loop.create_future()
self.user_phone: Optional[str] = None
self._presence_status: str = ""
self._lock = Lock()
async def login(self):
@ -318,9 +319,14 @@ class Session(BaseSession[str, Recipient]):
if merged_resource["show"] in ["chat", ""]
else whatsapp.PresenceUnavailable
)
self.whatsapp.SendPresence(
presence, merged_resource["status"] if merged_resource["status"] else ""
status = (
merged_resource["status"]
if self._presence_status != merged_resource["status"]
else ""
)
if status:
self._presence_status = status
self.whatsapp.SendPresence(presence, status)
async def on_active(self, c: Recipient, thread=None):
"""