Skip to content

Commit

Permalink
Add hacky persistence of missing registration provider state
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 20, 2023
1 parent 7c700e3 commit 5139781
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions database/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (

KVNACServToken = "nacserv_override_token"
KVNACServURL = "nacserv_override_url"

KVHackyNACErrorPersistence = "hacky_nac_error_persistence"
)

func (kvq *KeyValueQuery) Get(key string) (value string) {
Expand Down
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main
import (
"context"
_ "embed"
"encoding/json"
"errors"
"sync"

Expand Down Expand Up @@ -114,7 +115,11 @@ func (br *IMBridge) Start() {
err := user.Start()
if errors.Is(err, ErrNoNAC) {
user.zlog.Warn().Msg("NAC not configured, logging out user")
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Error: "im-nacserv-not-configured"})
state := status.BridgeState{StateEvent: status.StateUnknownError, Error: "im-nacserv-not-configured"}
stateForDB := state.Fill(user)
user.BridgeState.Send(state)
data, _ := json.Marshal(&stateForDB)
br.DB.KV.Set(database.KVHackyNACErrorPersistence, string(data))
user.AppleRegistration = nil
// TODO don't ignore errors
user.Update(context.TODO())
Expand All @@ -123,7 +128,14 @@ func (br *IMBridge) Start() {
}
}
if !startedAnyUsers {
br.SendGlobalBridgeState(status.BridgeState{StateEvent: status.StateUnconfigured})
if hackyNACErrorPersistence := br.DB.KV.Get(database.KVHackyNACErrorPersistence); hackyNACErrorPersistence != "" {
var state status.BridgeState
_ = json.Unmarshal([]byte(hackyNACErrorPersistence), &state)
br.ZLog.Debug().Interface("bridge_state", state).Msg("Sending cached NAC error state")
br.GetUserByMXID(state.UserID).BridgeState.Send(state)
} else {
br.SendGlobalBridgeState(status.BridgeState{StateEvent: status.StateUnconfigured})
}
}

br.WaitWebsocketConnected()
Expand Down
1 change: 1 addition & 0 deletions provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ func (prov *ProvisioningAPI) SetRelay(w http.ResponseWriter, r *http.Request) {
// Now that we know the relay exists, save it to the database
prov.bridge.DB.KV.Set(database.KVNACServURL, req.URL)
prov.bridge.DB.KV.Set(database.KVNACServToken, req.Token)
user.bridge.DB.KV.Delete(database.KVHackyNACErrorPersistence)

if user.IM != nil {
user.IM.NACServ = user.makeNACClient()
Expand Down
2 changes: 2 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (user *User) Start() error {
if cfg == nil {
cfg = &ids.Config{}
user.AppleRegistration = cfg
user.bridge.DB.KV.Delete(database.KVHackyNACErrorPersistence)
}
ctx := user.zlog.WithContext(context.TODO())
err := user.IM.Configure(ctx, cfg, false)
Expand Down Expand Up @@ -314,6 +315,7 @@ func (user *User) Logout() {
user.zlog.Warn().Err(err).Msg("Failed to logout")
}
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateLoggedOut})
user.bridge.DB.KV.Delete(database.KVHackyNACErrorPersistence)
user.Stop()
user.AppleRegistration = nil
// TODO don't ignore errors
Expand Down

0 comments on commit 5139781

Please sign in to comment.