From b958401b7371a959689a32201d3f30f95da76ce6 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 2 Aug 2023 15:07:02 +0300 Subject: [PATCH] Don't resync if session ID doesn't change --- libgm/client.go | 4 ++++ user.go | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/libgm/client.go b/libgm/client.go index 31cce8d..89aff7c 100644 --- a/libgm/client.go +++ b/libgm/client.go @@ -93,6 +93,10 @@ func NewClient(authData *AuthData, logger zerolog.Logger) *Client { return cli } +func (c *Client) CurrentSessionID() string { + return c.sessionHandler.sessionID +} + func (c *Client) SetEventHandler(eventHandler EventHandler) { c.evHandler = eventHandler } diff --git a/user.go b/user.go index 497cf72..8bb1a54 100644 --- a/user.go +++ b/user.go @@ -74,6 +74,7 @@ type User struct { mobileData bool phoneResponding bool ready bool + sessionID string batteryLowAlertSent time.Time pollErrorAlertSent bool @@ -704,12 +705,24 @@ func (user *User) handleUserAlert(v *gmproto.UserAlertEvent) { user.browserInactiveType = GMBrowserInactive becameInactive = true case gmproto.AlertType_BROWSER_ACTIVE: - // TODO check if session ID changed? + wasInactive := user.browserInactiveType != "" || !user.ready user.pollErrorAlertSent = false user.browserInactiveType = "" user.ready = true - go user.fetchAndSyncConversations() - user.sendMarkdownBridgeAlert(false, "Connected to Google Messages") + newSessionID := user.Client.CurrentSessionID() + if user.sessionID != newSessionID || wasInactive { + user.zlog.Debug(). + Str("old_session_id", user.sessionID). + Str("new_session_id", newSessionID). + Msg("Session ID changed for browser active event, resyncing") + user.sessionID = newSessionID + go user.fetchAndSyncConversations() + user.sendMarkdownBridgeAlert(false, "Connected to Google Messages") + } else { + user.zlog.Debug(). + Str("session_id", user.sessionID). + Msg("Session ID didn't change for browser active event, not resyncing") + } case gmproto.AlertType_BROWSER_INACTIVE_FROM_TIMEOUT: user.browserInactiveType = GMBrowserInactiveTimeout becameInactive = true