From 498f210e1007a21dc732de646f22d43d7f8486a0 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 19 Jul 2023 23:30:27 +0300 Subject: [PATCH] Move fetching initial conversations to bridge code --- libgm/event_handler.go | 42 ++++++++++++++++++++++ libgm/events/ready.go | 7 ---- libgm/updates_handler.go | 78 ---------------------------------------- libgm/util/config.go | 2 +- user.go | 34 +++++++++++------- 5 files changed, 64 insertions(+), 99 deletions(-) delete mode 100644 libgm/updates_handler.go diff --git a/libgm/event_handler.go b/libgm/event_handler.go index a1558dc..51da2b8 100644 --- a/libgm/event_handler.go +++ b/libgm/event_handler.go @@ -156,3 +156,45 @@ func (c *Client) HandleRPCMsg(rawMsg *gmproto.IncomingRPCMessage) { c.handleUpdatesEvent(msg) } } + +func (c *Client) handleUpdatesEvent(msg *IncomingRPCMessage) { + switch msg.Message.Action { + case gmproto.ActionType_GET_UPDATES: + data, ok := msg.DecryptedMessage.(*gmproto.UpdateEvents) + if !ok { + c.Logger.Error().Type("data_type", msg.DecryptedMessage).Msg("Unexpected data type in GET_UPDATES event") + return + } + + switch evt := data.Event.(type) { + case *gmproto.UpdateEvents_UserAlertEvent: + c.logContent(msg, "", nil) + c.triggerEvent(evt.UserAlertEvent) + + case *gmproto.UpdateEvents_SettingsEvent: + c.logContent(msg, "", nil) + c.triggerEvent(evt.SettingsEvent) + + case *gmproto.UpdateEvents_ConversationEvent: + if c.deduplicateUpdate(evt.ConversationEvent.GetData().GetConversationID(), msg) { + return + } + c.triggerEvent(evt.ConversationEvent.GetData()) + + case *gmproto.UpdateEvents_MessageEvent: + if c.deduplicateUpdate(evt.MessageEvent.GetData().GetMessageID(), msg) { + return + } + c.triggerEvent(evt.MessageEvent.GetData()) + + case *gmproto.UpdateEvents_TypingEvent: + c.logContent(msg, "", nil) + c.triggerEvent(evt.TypingEvent.GetData()) + + default: + c.Logger.Trace().Any("evt", evt).Msg("Got unknown event type") + } + default: + c.Logger.Trace().Any("response", msg).Msg("Got unexpected response") + } +} diff --git a/libgm/events/ready.go b/libgm/events/ready.go index 25d42d1..a45175d 100644 --- a/libgm/events/ready.go +++ b/libgm/events/ready.go @@ -12,13 +12,6 @@ type ClientReady struct { Conversations []*gmproto.Conversation } -func NewClientReady(sessionID string, conversationList *gmproto.ListConversationsResponse) *ClientReady { - return &ClientReady{ - SessionID: sessionID, - Conversations: conversationList.Conversations, - } -} - type AuthTokenRefreshed struct{} type HTTPError struct { diff --git a/libgm/updates_handler.go b/libgm/updates_handler.go deleted file mode 100644 index 48ba3d4..0000000 --- a/libgm/updates_handler.go +++ /dev/null @@ -1,78 +0,0 @@ -package libgm - -import ( - "go.mau.fi/mautrix-gmessages/libgm/events" - "go.mau.fi/mautrix-gmessages/libgm/gmproto" -) - -func (c *Client) handleUpdatesEvent(msg *IncomingRPCMessage) { - switch msg.Message.Action { - case gmproto.ActionType_GET_UPDATES: - data, ok := msg.DecryptedMessage.(*gmproto.UpdateEvents) - if !ok { - c.Logger.Error().Type("data_type", msg.DecryptedMessage).Msg("Unexpected data type in GET_UPDATES event") - return - } - - switch evt := data.Event.(type) { - case *gmproto.UpdateEvents_UserAlertEvent: - c.logContent(msg, "", nil) - c.handleUserAlertEvent(msg, evt.UserAlertEvent) - - case *gmproto.UpdateEvents_SettingsEvent: - c.logContent(msg, "", nil) - c.triggerEvent(evt.SettingsEvent) - - case *gmproto.UpdateEvents_ConversationEvent: - if c.deduplicateUpdate(evt.ConversationEvent.GetData().GetConversationID(), msg) { - return - } - c.triggerEvent(evt.ConversationEvent.GetData()) - - case *gmproto.UpdateEvents_MessageEvent: - if c.deduplicateUpdate(evt.MessageEvent.GetData().GetMessageID(), msg) { - return - } - c.triggerEvent(evt.MessageEvent.GetData()) - - case *gmproto.UpdateEvents_TypingEvent: - c.logContent(msg, "", nil) - c.triggerEvent(evt.TypingEvent.GetData()) - - default: - c.Logger.Trace().Any("evt", evt).Msg("Got unknown event type") - } - default: - c.Logger.Trace().Any("response", msg).Msg("Got unexpected response") - } -} - -func (c *Client) handleClientReady(newSessionId string) { - conversations, convErr := c.ListConversations(25, gmproto.ListConversationsRequest_INBOX) - if convErr != nil { - panic(convErr) - } - notifyErr := c.NotifyDittoActivity() - if notifyErr != nil { - panic(notifyErr) - } - readyEvt := events.NewClientReady(newSessionId, conversations) - c.triggerEvent(readyEvt) -} - -func (c *Client) handleUserAlertEvent(msg *IncomingRPCMessage, data *gmproto.UserAlertEvent) { - alertType := data.AlertType - switch alertType { - case gmproto.AlertType_BROWSER_ACTIVE: - newSessionID := msg.Message.SessionID - c.Logger.Debug().Any("session_id", newSessionID).Msg("Got browser active notification") - if newSessionID != c.sessionHandler.sessionID { - evt := events.NewBrowserActive(newSessionID) - c.triggerEvent(evt) - } else { - go c.handleClientReady(newSessionID) - } - default: - c.triggerEvent(data) - } -} diff --git a/libgm/util/config.go b/libgm/util/config.go index 2deef7d..2273978 100644 --- a/libgm/util/config.go +++ b/libgm/util/config.go @@ -7,7 +7,7 @@ import ( var ConfigMessage = &gmproto.ConfigVersion{ Year: 2023, Month: 7, - Day: 10, + Day: 13, V1: 4, V2: 6, } diff --git a/user.go b/user.go index 5f06084..c30a70d 100644 --- a/user.go +++ b/user.go @@ -71,6 +71,7 @@ type User struct { browserInactiveType status.BridgeStateErrorCode batteryLow bool mobileData bool + ready bool loginInProgress atomic.Bool pairSuccessChan chan struct{} @@ -628,17 +629,6 @@ func (user *User) HandleEvent(event interface{}) { case *gmproto.Message: portal := user.GetPortalByID(v.GetConversationID()) portal.messages <- PortalMessage{evt: v, source: user} - case *events.ClientReady: - user.browserInactiveType = "" - user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected}) - go func() { - for _, conv := range v.Conversations { - user.syncConversation(conv) - } - }() - case *events.BrowserActive: - user.zlog.Trace().Any("data", v).Msg("Browser active") - user.browserInactiveType = "" case *gmproto.UserAlertEvent: user.handleUserAlert(v) default: @@ -663,14 +653,28 @@ func (user *User) aggressiveSetActive() { } } +func (user *User) fetchAndSyncConversations() { + resp, err := user.Client.ListConversations(25, gmproto.ListConversationsRequest_INBOX) + if err != nil { + user.zlog.Err(err).Msg("Failed to get conversation list") + return + } + for _, conv := range resp.GetConversations() { + user.syncConversation(conv) + } +} + func (user *User) handleUserAlert(v *gmproto.UserAlertEvent) { - user.zlog.Debug().Any("data", v).Msg("Got user alert event") + user.zlog.Debug().Str("alert_type", v.GetAlertType().String()).Msg("Got user alert event") becameInactive := false switch v.GetAlertType() { case gmproto.AlertType_BROWSER_INACTIVE: - // TODO aggressively reactivate if configured to do so user.browserInactiveType = GMBrowserInactive becameInactive = true + case gmproto.AlertType_BROWSER_ACTIVE: + user.browserInactiveType = "" + user.ready = true + go user.fetchAndSyncConversations() case gmproto.AlertType_BROWSER_INACTIVE_FROM_TIMEOUT: user.browserInactiveType = GMBrowserInactiveTimeout becameInactive = true @@ -706,6 +710,10 @@ func (user *User) FillBridgeState(state status.BridgeState) status.BridgeState { state.Info["battery_low"] = user.batteryLow state.Info["mobile_data"] = user.mobileData state.Info["browser_active"] = user.browserInactiveType == "" + if !user.ready { + state.StateEvent = status.StateConnecting + state.Error = GMConnecting + } if user.longPollingError != nil { state.StateEvent = status.StateTransientDisconnect state.Error = GMListenError