gmessages/libgm/updates_handler.go

79 lines
2.2 KiB
Go
Raw Normal View History

package libgm
import (
2023-07-15 23:24:39 +00:00
"go.mau.fi/mautrix-gmessages/libgm/events"
2023-07-17 13:51:31 +00:00
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
)
func (c *Client) handleUpdatesEvent(msg *IncomingRPCMessage) {
switch msg.Message.Action {
2023-07-17 13:51:31 +00:00
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) {
2023-07-17 13:51:31 +00:00
case *gmproto.UpdateEvents_UserAlertEvent:
c.logContent(msg, "", nil)
c.handleUserAlertEvent(msg, evt.UserAlertEvent)
2023-07-17 13:51:31 +00:00
case *gmproto.UpdateEvents_SettingsEvent:
c.logContent(msg, "", nil)
2023-07-15 23:24:39 +00:00
c.triggerEvent(evt.SettingsEvent)
2023-07-17 13:51:31 +00:00
case *gmproto.UpdateEvents_ConversationEvent:
if c.deduplicateUpdate(evt.ConversationEvent.GetData().GetConversationID(), msg) {
return
}
c.triggerEvent(evt.ConversationEvent.GetData())
2023-07-17 13:51:31 +00:00
case *gmproto.UpdateEvents_MessageEvent:
if c.deduplicateUpdate(evt.MessageEvent.GetData().GetMessageID(), msg) {
return
}
c.triggerEvent(evt.MessageEvent.GetData())
2023-07-17 13:51:31 +00:00
case *gmproto.UpdateEvents_TypingEvent:
c.logContent(msg, "", nil)
2023-07-15 23:24:39 +00:00
c.triggerEvent(evt.TypingEvent.GetData())
2023-07-17 13:43:34 +00:00
default:
2023-07-17 13:43:34 +00:00
c.Logger.Trace().Any("evt", evt).Msg("Got unknown event type")
}
default:
c.Logger.Trace().Any("response", msg).Msg("Got unexpected response")
}
}
2023-07-15 23:24:39 +00:00
func (c *Client) handleClientReady(newSessionId string) {
2023-07-17 23:57:20 +00:00
conversations, convErr := c.ListConversations(25, gmproto.ListConversationsRequest_INBOX)
2023-07-15 23:24:39 +00:00
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) {
2023-07-15 23:24:39 +00:00
alertType := data.AlertType
switch alertType {
2023-07-17 13:51:31 +00:00
case gmproto.AlertType_BROWSER_ACTIVE:
newSessionID := msg.Message.SessionID
2023-07-17 13:43:34 +00:00
c.Logger.Debug().Any("session_id", newSessionID).Msg("Got browser active notification")
if newSessionID != c.sessionHandler.sessionID {
evt := events.NewBrowserActive(newSessionID)
2023-07-15 23:24:39 +00:00
c.triggerEvent(evt)
} else {
2023-07-17 13:43:34 +00:00
go c.handleClientReady(newSessionID)
2023-07-15 23:24:39 +00:00
}
default:
c.triggerEvent(data)
}
}