From e133f2c795513b7cc4aa7be48ca916ad4bdf32e4 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Tue, 14 Nov 2023 15:17:44 -0500 Subject: [PATCH] Don't repeatedly show logout notice Send users a logout notice only when they weren't already logged out. --- event/event.go | 23 +++++++++++++++++++++++ user.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 event/event.go diff --git a/event/event.go b/event/event.go new file mode 100644 index 00000000..8d0766d2 --- /dev/null +++ b/event/event.go @@ -0,0 +1,23 @@ +package event + +import ( + "reflect" + + "maunium.net/go/mautrix/event" +) + +var ( + StateBridgeConnection = event.Type{Type: "im.vector.mautrix-signalgo.connection", Class: event.AccountDataEventType} +) + +type BridgeConnectionEventContent struct { + LoggedIn bool `json:"logged_in"` +} + +func registerEventTypes() error { + event.TypeMap[StateBridgeConnection] = reflect.TypeOf(BridgeConnectionEventContent{}) + + return nil +} + +var _ = registerEventTypes() diff --git a/user.go b/user.go index 1d43513e..d76ca1e2 100644 --- a/user.go +++ b/user.go @@ -14,6 +14,7 @@ import ( "github.com/google/uuid" "github.com/rs/zerolog" "go.mau.fi/mautrix-signal/database" + bridgeEvent "go.mau.fi/mautrix-signal/event" "go.mau.fi/mautrix-signal/pkg/signalmeow" "maunium.net/go/mautrix" "maunium.net/go/mautrix/appservice" @@ -71,10 +72,16 @@ func (user *User) SetManagementRoom(roomID id.RoomID) { existing, ok := user.bridge.managementRooms[roomID] if ok { + if existing.ManagementRoom != "" { + existing.clearBridgeConnectionState(existing.ManagementRoom) + } existing.ManagementRoom = "" existing.Update() } + if user.ManagementRoom != "" { + user.clearBridgeConnectionState(user.ManagementRoom) + } user.ManagementRoom = roomID user.bridge.managementRooms[user.ManagementRoom] = user err := user.Update() @@ -331,6 +338,9 @@ func (user *User) startupTryConnect(retryCount int) { case signalmeow.SignalConnectionEventConnected: user.log.Debug().Msg("Sending Connected BridgeState") user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected}) + if managementRoomID := user.GetManagementRoomID(); managementRoomID != "" { + user.setBridgeConnectionState(managementRoomID, true) + } case signalmeow.SignalConnectionEventDisconnected: user.log.Debug().Msg("Received SignalConnectionEventDisconnected") @@ -392,8 +402,17 @@ func (user *User) startupTryConnect(retryCount int) { user.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Message: err.Error()}) } user.clearMySignalKeys() - if roomID := user.GetManagementRoomID(); roomID != "" { - user.bridge.Bot.SendNotice(roomID, defaultMessage) + if managementRoomID := user.GetManagementRoomID(); managementRoomID != "" { + var existingBridgeConnection bridgeEvent.BridgeConnectionEventContent + if err := user.bridge.Bot.GetRoomAccountData( + managementRoomID, bridgeEvent.StateBridgeConnection.Type, &existingBridgeConnection, + ); err != nil || existingBridgeConnection.LoggedIn { + if err != nil && !errors.Is(err, mautrix.MNotFound) { + user.log.Warn().Err(err).Msg("Failed to get bridge connection state from management room account data") + } + user.bridge.Bot.SendNotice(managementRoomID, defaultMessage) + user.setBridgeConnectionState(managementRoomID, false) + } } case signalmeow.SignalConnectionEventError: @@ -407,6 +426,30 @@ func (user *User) startupTryConnect(retryCount int) { }() } +func (user *User) setBridgeConnectionState(roomId id.RoomID, loggedIn bool) { + if err := user.bridge.Bot.SetRoomAccountData( + roomId, + bridgeEvent.StateBridgeConnection.Type, + bridgeEvent.BridgeConnectionEventContent{LoggedIn: loggedIn}, + ); err != nil { + user.log.Warn().Err(err).Msg( + "Failed to update bridge connection state for management room account data", + ) + } +} + +func (user *User) clearBridgeConnectionState(roomId id.RoomID) { + if err := user.bridge.Bot.SetRoomAccountData( + roomId, + bridgeEvent.StateBridgeConnection.Type, + nil, + ); err != nil { + user.log.Warn().Err(err).Msg( + "Failed to clear bridge connection state from management room account data", + ) + } +} + func (user *User) clearMySignalKeys() { // We need to clear out keys associated with the Signal device that no longer has valid credentials user.log.Debug().Msg("Clearing out Signal device keys")