Skip to content

Commit

Permalink
Adjust hacky GET_UPDATES behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Apr 5, 2024
1 parent 7167eff commit e2c8d92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion libgm/longpoll.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (dp *dittoPinger) Ping(pingID uint64, timeout time.Duration, timeoutCount i
}
}

const DefaultBugleDefaultCheckInterval = 1*time.Hour + 55*time.Minute
const DefaultBugleDefaultCheckInterval = 2*time.Hour + 55*time.Minute

func (dp *dittoPinger) Loop() {
for {
Expand Down
40 changes: 22 additions & 18 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ type User struct {
phoneNotRespondingAlertSent bool
didHackySetActive bool
noDataReceivedRecentlyStart time.Time
lastNoDataResync time.Time

loginInProgress atomic.Bool
pairSuccessChan chan struct{}
Expand Down Expand Up @@ -875,22 +874,33 @@ func (user *User) aggressiveSetActive() {
}
}

func (user *User) fetchAndSyncConversations(noDataReceivedStart time.Time) {
func (user *User) fetchAndSyncConversations(noDataReceivedStart time.Time, minimalSync bool) {
user.zlog.Info().Msg("Fetching conversation list")
resp, err := user.Client.ListConversations(user.bridge.Config.Bridge.InitialChatSyncCount, gmproto.ListConversationsRequest_INBOX)
if err != nil {
user.zlog.Err(err).Msg("Failed to get conversation list")
return
}
user.zlog.Info().Int("count", len(resp.GetConversations())).Msg("Syncing conversations")
for _, conv := range resp.GetConversations() {
lastMessageTS := time.UnixMicro(conv.GetLastMessageTimestamp())
if !noDataReceivedStart.IsZero() && lastMessageTS.After(noDataReceivedStart) {
user.zlog.Warn().
Time("last_message_ts", lastMessageTS).
Time("no_data_received_start", noDataReceivedStart).
Msg("Conversation's last message is newer than no data received start time")
if !noDataReceivedStart.IsZero() {
for _, conv := range resp.GetConversations() {
lastMessageTS := time.UnixMicro(conv.GetLastMessageTimestamp())
if lastMessageTS.After(noDataReceivedStart) {
user.zlog.Warn().
Time("last_message_ts", lastMessageTS).
Time("no_data_received_start", noDataReceivedStart).
Msg("Conversation's last message is newer than no data received start time")
minimalSync = false
}
}
} else if minimalSync {
user.zlog.Warn().Msg("Minimal sync called without no data received start time")
}
if minimalSync {
user.zlog.Debug().Msg("Minimal sync with no recent messages, not syncing conversations")
return
}
for _, conv := range resp.GetConversations() {
user.syncConversation(conv, "sync")
}
}
Expand Down Expand Up @@ -932,14 +942,8 @@ func (user *User) handleUserAlert(v *gmproto.UserAlertEvent) {
user.browserInactiveType = ""
user.ready = true
newSessionID := user.Client.CurrentSessionID()
if hadNoDataReceived && time.Since(user.lastNoDataResync) < 5*time.Hour {
user.zlog.Warn().Time("last_resync", user.lastNoDataResync).Msg("Frequent no data received resyncs")
hadNoDataReceived = false
}
if user.sessionID != newSessionID || wasInactive || hadNoDataReceived {
if hadNoDataReceived {
user.lastNoDataResync = time.Now()
}
sessionIDChanged := user.sessionID != newSessionID
if sessionIDChanged || wasInactive || hadNoDataReceived {
user.zlog.Debug().
Str("old_session_id", user.sessionID).
Str("new_session_id", newSessionID).
Expand All @@ -948,7 +952,7 @@ func (user *User) handleUserAlert(v *gmproto.UserAlertEvent) {
Time("no_data_received_start", noDataReceivedStart).
Msg("Session ID changed for browser active event, resyncing")
user.sessionID = newSessionID
go user.fetchAndSyncConversations(noDataReceivedStart)
go user.fetchAndSyncConversations(noDataReceivedStart, !sessionIDChanged && !wasInactive)
go user.sendMarkdownBridgeAlert(ctx, false, "Connected to Google Messages")
} else {
user.zlog.Debug().
Expand Down

0 comments on commit e2c8d92

Please sign in to comment.