2023-07-09 11:16:52 +00:00
|
|
|
package libgm
|
|
|
|
|
|
|
|
import (
|
2023-07-15 23:24:39 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/events"
|
2023-07-09 11:16:52 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/pblite"
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
|
2023-07-09 11:16:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) handleUpdatesEvent(res *pblite.Response) {
|
|
|
|
switch res.Data.Action {
|
2023-07-17 13:51:31 +00:00
|
|
|
case gmproto.ActionType_GET_UPDATES:
|
|
|
|
data, ok := res.Data.Decrypted.(*gmproto.UpdateEvents)
|
2023-07-09 11:16:52 +00:00
|
|
|
if !ok {
|
2023-07-17 13:43:34 +00:00
|
|
|
c.Logger.Error().Type("data_type", res.Data.Decrypted).Msg("Unexpected data type in GET_UPDATES event")
|
2023-07-09 11:16:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch evt := data.Event.(type) {
|
2023-07-17 13:51:31 +00:00
|
|
|
case *gmproto.UpdateEvents_UserAlertEvent:
|
2023-07-15 13:17:02 +00:00
|
|
|
c.rpc.logContent(res)
|
2023-07-09 11:16:52 +00:00
|
|
|
c.handleUserAlertEvent(res, evt.UserAlertEvent)
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
case *gmproto.UpdateEvents_SettingsEvent:
|
2023-07-15 13:17:02 +00:00
|
|
|
c.rpc.logContent(res)
|
2023-07-15 23:24:39 +00:00
|
|
|
c.triggerEvent(evt.SettingsEvent)
|
2023-07-09 11:16:52 +00:00
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
case *gmproto.UpdateEvents_ConversationEvent:
|
2023-07-10 22:20:50 +00:00
|
|
|
if c.rpc.deduplicateUpdate(res) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.triggerEvent(evt.ConversationEvent.GetData())
|
2023-07-09 11:16:52 +00:00
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
case *gmproto.UpdateEvents_MessageEvent:
|
2023-07-10 22:20:50 +00:00
|
|
|
if c.rpc.deduplicateUpdate(res) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.triggerEvent(evt.MessageEvent.GetData())
|
2023-07-09 11:16:52 +00:00
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
case *gmproto.UpdateEvents_TypingEvent:
|
2023-07-15 13:17:02 +00:00
|
|
|
c.rpc.logContent(res)
|
2023-07-15 23:24:39 +00:00
|
|
|
c.triggerEvent(evt.TypingEvent.GetData())
|
2023-07-17 13:43:34 +00:00
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
default:
|
2023-07-17 13:43:34 +00:00
|
|
|
c.Logger.Trace().Any("evt", evt).Msg("Got unknown event type")
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
|
|
|
default:
|
2023-07-17 13:43:34 +00:00
|
|
|
c.Logger.Trace().Any("response", res).Msg("Got unexpected response")
|
2023-07-09 11:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-15 23:24:39 +00:00
|
|
|
|
|
|
|
func (c *Client) handleClientReady(newSessionId string) {
|
2023-07-17 13:51:31 +00:00
|
|
|
conversations, convErr := c.ListConversations(25, gmproto.ListConversationsPayload_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)
|
|
|
|
}
|
|
|
|
|
2023-07-17 13:51:31 +00:00
|
|
|
func (c *Client) handleUserAlertEvent(res *pblite.Response, 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:
|
2023-07-17 13:43:34 +00:00
|
|
|
newSessionID := res.Data.RequestID
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|