Make event handling synchronous

This commit is contained in:
Tulir Asokan 2023-08-10 15:40:43 +03:00
parent 3fc26339d8
commit efb0008ca0
3 changed files with 20 additions and 20 deletions

View file

@ -146,7 +146,7 @@ func (c *Client) HandleRPCMsg(rawMsg *gmproto.IncomingRPCMessage) {
} }
switch msg.BugleRoute { switch msg.BugleRoute {
case gmproto.BugleRoute_PairEvent: case gmproto.BugleRoute_PairEvent:
go c.handlePairingEvent(msg) c.handlePairingEvent(msg)
case gmproto.BugleRoute_DataEvent: case gmproto.BugleRoute_DataEvent:
if c.skipCount > 0 { if c.skipCount > 0 {
c.skipCount-- c.skipCount--

View file

@ -59,10 +59,12 @@ func (c *Client) completePairing(data *gmproto.PairedData) {
c.triggerEvent(&events.PairSuccessful{PairedData: data}) c.triggerEvent(&events.PairSuccessful{PairedData: data})
err := c.Reconnect() go func() {
if err != nil { err := c.Reconnect()
c.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to reconnect after pair success: %w", err)}) if err != nil {
} c.triggerEvent(&events.ListenFatalError{Error: fmt.Errorf("failed to reconnect after pair success: %w", err)})
}
}()
} }
func (c *Client) RegisterPhoneRelay() (*gmproto.RegisterPhoneRelayResponse, error) { func (c *Client) RegisterPhoneRelay() (*gmproto.RegisterPhoneRelayResponse, error) {

28
user.go
View file

@ -409,10 +409,6 @@ func (user *User) createClient(sess *libgm.AuthData) {
user.Client.SetEventHandler(user.syncHandleEvent) user.Client.SetEventHandler(user.syncHandleEvent)
} }
func (user *User) syncHandleEvent(ev any) {
go user.HandleEvent(ev)
}
type qrChannelItem struct { type qrChannelItem struct {
success bool success bool
qr string qr string
@ -593,10 +589,10 @@ func (user *User) sendMarkdownBridgeAlert(important bool, formatString string, a
} }
} }
func (user *User) HandleEvent(event interface{}) { func (user *User) syncHandleEvent(event any) {
switch v := event.(type) { switch v := event.(type) {
case *events.ListenFatalError: case *events.ListenFatalError:
user.Logout(status.BridgeState{ go user.Logout(status.BridgeState{
StateEvent: status.StateUnknownError, StateEvent: status.StateUnknownError,
Error: GMFatalError, Error: GMFatalError,
Info: map[string]any{"go_error": v.Error.Error()}, Info: map[string]any{"go_error": v.Error.Error()},
@ -629,7 +625,6 @@ func (user *User) HandleEvent(event interface{}) {
case *events.PairSuccessful: case *events.PairSuccessful:
user.Session = user.Client.AuthData user.Session = user.Client.AuthData
user.PhoneID = v.GetMobile().GetSourceID() user.PhoneID = v.GetMobile().GetSourceID()
user.tryAutomaticDoublePuppeting()
user.addToPhoneMap() user.addToPhoneMap()
err := user.Update(context.TODO()) err := user.Update(context.TODO())
if err != nil { if err != nil {
@ -638,20 +633,23 @@ func (user *User) HandleEvent(event interface{}) {
if ch := user.pairSuccessChan; ch != nil { if ch := user.pairSuccessChan; ch != nil {
close(ch) close(ch)
} }
go user.tryAutomaticDoublePuppeting()
case *gmproto.RevokePairData: case *gmproto.RevokePairData:
user.zlog.Info().Any("revoked_device", v.GetRevokedDevice()).Msg("Got pair revoked event") user.zlog.Info().Any("revoked_device", v.GetRevokedDevice()).Msg("Got pair revoked event")
user.Logout(status.BridgeState{ go user.Logout(status.BridgeState{
StateEvent: status.StateBadCredentials, StateEvent: status.StateBadCredentials,
Error: GMUnpaired, Error: GMUnpaired,
}, false) }, false)
user.sendMarkdownBridgeAlert(true, "Unpaired from Google Messages. Log in again to continue using the bridge.") go user.sendMarkdownBridgeAlert(true, "Unpaired from Google Messages. Log in again to continue using the bridge.")
case *events.AuthTokenRefreshed: case *events.AuthTokenRefreshed:
err := user.Update(context.TODO()) go func() {
if err != nil { err := user.Update(context.TODO())
user.zlog.Err(err).Msg("Failed to update session in database") if err != nil {
} user.zlog.Err(err).Msg("Failed to update session in database")
}
}()
case *gmproto.Conversation: case *gmproto.Conversation:
user.syncConversation(v, "event") go user.syncConversation(v, "event")
case *gmproto.Message: case *gmproto.Message:
portal := user.GetPortalByID(v.GetConversationID()) portal := user.GetPortalByID(v.GetConversationID())
portal.messages <- PortalMessage{evt: v, source: user} portal.messages <- PortalMessage{evt: v, source: user}
@ -717,7 +715,7 @@ func (user *User) handleUserAlert(v *gmproto.UserAlertEvent) {
Msg("Session ID changed for browser active event, resyncing") Msg("Session ID changed for browser active event, resyncing")
user.sessionID = newSessionID user.sessionID = newSessionID
go user.fetchAndSyncConversations() go user.fetchAndSyncConversations()
user.sendMarkdownBridgeAlert(false, "Connected to Google Messages") go user.sendMarkdownBridgeAlert(false, "Connected to Google Messages")
} else { } else {
user.zlog.Debug(). user.zlog.Debug().
Str("session_id", user.sessionID). Str("session_id", user.sessionID).