Move fetching initial conversations to bridge code

This commit is contained in:
Tulir Asokan 2023-07-19 23:30:27 +03:00
parent 0e3f211118
commit 498f210e10
5 changed files with 64 additions and 99 deletions

View file

@ -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")
}
}

View file

@ -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 {

View file

@ -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)
}
}

View file

@ -7,7 +7,7 @@ import (
var ConfigMessage = &gmproto.ConfigVersion{
Year: 2023,
Month: 7,
Day: 10,
Day: 13,
V1: 4,
V2: 6,
}

34
user.go
View file

@ -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