Skip to content

Commit

Permalink
Don't repeatedly show logout notice
Browse files Browse the repository at this point in the history
Send users a logout notice only when they weren't already logged out.
  • Loading branch information
AndrewFerr committed Nov 15, 2023
1 parent b8657cf commit 850c26e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ COPY pkg/libsignalgo/* pkg/libsignalgo/
COPY pkg/libsignalgo/resources/. pkg/libsignalgo/resources/.
COPY config/. config/.
COPY database/. database/.
COPY event/. event/.
COPY .git .git
COPY Makefile .
COPY docker-run.sh .
Expand Down
23 changes: 23 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
@@ -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()
47 changes: 45 additions & 2 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand Down

0 comments on commit 850c26e

Please sign in to comment.