From 2d69a3c42f8c4e98e78f521fedf18e2bb91d8c35 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 16 Jul 2023 02:24:39 +0300 Subject: [PATCH] Remove some unnecessary event wrappers --- libgm/events/ready.go | 6 ++--- libgm/events/settings.go | 21 ---------------- libgm/events/typing.go | 50 -------------------------------------- libgm/events/useralerts.go | 6 ++--- libgm/settings_handler.go | 13 ---------- libgm/typing_handler.go | 26 -------------------- libgm/updates_handler.go | 37 ++++++++++++++++++++++++++-- libgm/useralert_handler.go | 40 ------------------------------ 8 files changed, 41 insertions(+), 158 deletions(-) delete mode 100644 libgm/events/settings.go delete mode 100644 libgm/events/typing.go delete mode 100644 libgm/settings_handler.go delete mode 100644 libgm/typing_handler.go delete mode 100644 libgm/useralert_handler.go diff --git a/libgm/events/ready.go b/libgm/events/ready.go index ece3e76..d81c854 100644 --- a/libgm/events/ready.go +++ b/libgm/events/ready.go @@ -8,13 +8,13 @@ import ( ) type ClientReady struct { - SessionId string + SessionID string Conversations []*binary.Conversation } -func NewClientReady(sessionId string, conversationList *binary.Conversations) *ClientReady { +func NewClientReady(sessionID string, conversationList *binary.Conversations) *ClientReady { return &ClientReady{ - SessionId: sessionId, + SessionID: sessionID, Conversations: conversationList.Conversations, } } diff --git a/libgm/events/settings.go b/libgm/events/settings.go deleted file mode 100644 index c5c6a34..0000000 --- a/libgm/events/settings.go +++ /dev/null @@ -1,21 +0,0 @@ -package events - -import "go.mau.fi/mautrix-gmessages/libgm/binary" - -type SettingEvent interface { - GetSettings() *binary.Settings -} - -type SETTINGS_UPDATED struct { - Settings *binary.Settings -} - -func (su *SETTINGS_UPDATED) GetSettings() *binary.Settings { - return su.Settings -} - -func NewSettingsUpdated(settings *binary.Settings) SettingEvent { - return &SETTINGS_UPDATED{ - Settings: settings, - } -} diff --git a/libgm/events/typing.go b/libgm/events/typing.go deleted file mode 100644 index 09ae0d0..0000000 --- a/libgm/events/typing.go +++ /dev/null @@ -1,50 +0,0 @@ -package events - -import "go.mau.fi/mautrix-gmessages/libgm/binary" - -type TypingEvent interface { - GetConversation() string -} - -type User struct { - Field1 int64 - Number string -} - -type STARTED_TYPING struct { - ConversationId string - User User -} - -func (t *STARTED_TYPING) GetConversation() string { - return t.ConversationId -} - -func NewStartedTyping(data *binary.TypingData) TypingEvent { - return &STARTED_TYPING{ - ConversationId: data.ConversationID, - User: User{ - Field1: data.User.Field1, - Number: data.User.Number, - }, - } -} - -type STOPPED_TYPING struct { - ConversationId string - User User -} - -func (t *STOPPED_TYPING) GetConversation() string { - return t.ConversationId -} - -func NewStoppedTyping(data *binary.TypingData) TypingEvent { - return &STOPPED_TYPING{ - ConversationId: data.ConversationID, - User: User{ - Field1: data.User.Field1, - Number: data.User.Number, - }, - } -} diff --git a/libgm/events/useralerts.go b/libgm/events/useralerts.go index 989adfb..602abb5 100644 --- a/libgm/events/useralerts.go +++ b/libgm/events/useralerts.go @@ -1,11 +1,11 @@ package events type BrowserActive struct { - SessionId string + SessionID string } -func NewBrowserActive(sessionId string) *BrowserActive { +func NewBrowserActive(sessionID string) *BrowserActive { return &BrowserActive{ - SessionId: sessionId, + SessionID: sessionID, } } diff --git a/libgm/settings_handler.go b/libgm/settings_handler.go deleted file mode 100644 index 6dd4d0a..0000000 --- a/libgm/settings_handler.go +++ /dev/null @@ -1,13 +0,0 @@ -package libgm - -import ( - "go.mau.fi/mautrix-gmessages/libgm/pblite" - - "go.mau.fi/mautrix-gmessages/libgm/binary" - "go.mau.fi/mautrix-gmessages/libgm/events" -) - -func (c *Client) handleSettingsEvent(res *pblite.Response, data *binary.Settings) { - evt := events.NewSettingsUpdated(data) - c.triggerEvent(evt) -} diff --git a/libgm/typing_handler.go b/libgm/typing_handler.go deleted file mode 100644 index 7d4aebd..0000000 --- a/libgm/typing_handler.go +++ /dev/null @@ -1,26 +0,0 @@ -package libgm - -import ( - "go.mau.fi/mautrix-gmessages/libgm/pblite" - - "go.mau.fi/mautrix-gmessages/libgm/binary" - "go.mau.fi/mautrix-gmessages/libgm/events" -) - -func (c *Client) handleTypingEvent(res *pblite.Response, data *binary.TypingData) { - typingType := data.Type - - var evt events.TypingEvent - switch typingType { - case binary.TypingTypes_STARTED_TYPING: - evt = events.NewStartedTyping(data) - case binary.TypingTypes_STOPPED_TYPING: - evt = events.NewStoppedTyping(data) - default: - c.Logger.Debug().Any("data", data).Msg("got unknown TypingData evt") - } - - if evt != nil { - c.triggerEvent(evt) - } -} diff --git a/libgm/updates_handler.go b/libgm/updates_handler.go index f8389a3..1d91af3 100644 --- a/libgm/updates_handler.go +++ b/libgm/updates_handler.go @@ -1,6 +1,7 @@ package libgm import ( + "go.mau.fi/mautrix-gmessages/libgm/events" "go.mau.fi/mautrix-gmessages/libgm/pblite" "go.mau.fi/mautrix-gmessages/libgm/binary" @@ -22,7 +23,7 @@ func (c *Client) handleUpdatesEvent(res *pblite.Response) { case *binary.UpdateEvents_SettingsEvent: c.rpc.logContent(res) - c.handleSettingsEvent(res, evt.SettingsEvent) + c.triggerEvent(evt.SettingsEvent) case *binary.UpdateEvents_ConversationEvent: if c.rpc.deduplicateUpdate(res) { @@ -38,7 +39,7 @@ func (c *Client) handleUpdatesEvent(res *pblite.Response) { case *binary.UpdateEvents_TypingEvent: c.rpc.logContent(res) - c.handleTypingEvent(res, evt.TypingEvent.GetData()) + c.triggerEvent(evt.TypingEvent.GetData()) default: c.Logger.Debug().Any("evt", evt).Any("res", res).Msg("Got unknown event type") } @@ -47,3 +48,35 @@ func (c *Client) handleUpdatesEvent(res *pblite.Response) { c.Logger.Error().Any("response", res).Msg("ignoring response.") } } + +func (c *Client) handleClientReady(newSessionId string) { + c.Logger.Info().Any("sessionId", newSessionId).Msg("Client is ready!") + conversations, convErr := c.ListConversations(25, binary.ListConversationsPayload_INBOX) + if convErr != nil { + panic(convErr) + } + c.Logger.Debug().Any("conversations", conversations).Msg("got conversations") + notifyErr := c.NotifyDittoActivity() + if notifyErr != nil { + panic(notifyErr) + } + readyEvt := events.NewClientReady(newSessionId, conversations) + c.triggerEvent(readyEvt) +} + +func (c *Client) handleUserAlertEvent(res *pblite.Response, data *binary.UserAlertEvent) { + alertType := data.AlertType + switch alertType { + case binary.AlertType_BROWSER_ACTIVE: + newSessionId := res.Data.RequestID + c.Logger.Info().Any("sessionId", newSessionId).Msg("[NEW_BROWSER_ACTIVE] Opened new browser connection") + 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/useralert_handler.go b/libgm/useralert_handler.go deleted file mode 100644 index e29d129..0000000 --- a/libgm/useralert_handler.go +++ /dev/null @@ -1,40 +0,0 @@ -package libgm - -import ( - "go.mau.fi/mautrix-gmessages/libgm/pblite" - - "go.mau.fi/mautrix-gmessages/libgm/binary" - "go.mau.fi/mautrix-gmessages/libgm/events" -) - -func (c *Client) handleClientReady(newSessionId string) { - c.Logger.Info().Any("sessionId", newSessionId).Msg("Client is ready!") - conversations, convErr := c.ListConversations(25, binary.ListConversationsPayload_INBOX) - if convErr != nil { - panic(convErr) - } - c.Logger.Debug().Any("conversations", conversations).Msg("got conversations") - notifyErr := c.NotifyDittoActivity() - if notifyErr != nil { - panic(notifyErr) - } - readyEvt := events.NewClientReady(newSessionId, conversations) - c.triggerEvent(readyEvt) -} - -func (c *Client) handleUserAlertEvent(res *pblite.Response, data *binary.UserAlertEvent) { - alertType := data.AlertType - switch alertType { - case binary.AlertType_BROWSER_ACTIVE: - newSessionId := res.Data.RequestID - c.Logger.Info().Any("sessionId", newSessionId).Msg("[NEW_BROWSER_ACTIVE] Opened new browser connection") - if newSessionId != c.sessionHandler.sessionID { - evt := events.NewBrowserActive(newSessionId) - c.triggerEvent(evt) - } else { - go c.handleClientReady(newSessionId) - } - default: - c.triggerEvent(data) - } -}