Remove some unnecessary event wrappers

This commit is contained in:
Tulir Asokan 2023-07-16 02:24:39 +03:00
parent 8302bc95ee
commit 2d69a3c42f
8 changed files with 41 additions and 158 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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