Skip to content

Commit

Permalink
v2: use improved NewLogin function
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 7, 2024
1 parent b2c1efc commit 85f3c15
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ require (
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1
go.mau.fi/util v0.5.1-0.20240626184357-b3f4d78c25cf
go.mau.fi/util v0.5.1-0.20240702170310-bd1da3c069eb
golang.org/x/crypto v0.24.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/net v0.26.0
google.golang.org/protobuf v1.34.2
maunium.net/go/mautrix v0.19.0-beta.1.0.20240629212545-63c49bf8400d
maunium.net/go/mautrix v0.19.0-beta.1.0.20240706124659-b4057a26c3ed
nhooyr.io/websocket v1.8.11
)

Expand All @@ -40,7 +40,7 @@ require (
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/yuin/goldmark v1.7.2 // indirect
github.com/yuin/goldmark v1.7.4 // indirect
go.mau.fi/zeroconfig v0.1.2 // indirect
golang.org/x/sys v0.21.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/yuin/goldmark v1.7.2 h1:NjGd7lO7zrUn/A7eKwn5PEOt4ONYGqpxSEeZuduvgxc=
github.com/yuin/goldmark v1.7.2/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.mau.fi/util v0.5.1-0.20240626184357-b3f4d78c25cf h1:ceXQTB6IqjqGBGhzOTEBGbxQu7xDyuT9YR06gxr9Ncw=
go.mau.fi/util v0.5.1-0.20240626184357-b3f4d78c25cf/go.mod h1:DsJzUrJAG53lCZnnYvq9/mOyLuPScWwYhvETiTrpdP4=
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.mau.fi/util v0.5.1-0.20240702170310-bd1da3c069eb h1:VZPo2pvfjNj6fkFv5e9FyTYx96BLwwYNA19WYaY+KN8=
go.mau.fi/util v0.5.1-0.20240702170310-bd1da3c069eb/go.mod h1:DsJzUrJAG53lCZnnYvq9/mOyLuPScWwYhvETiTrpdP4=
go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto=
go.mau.fi/zeroconfig v0.1.2/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
Expand All @@ -93,7 +93,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240629212545-63c49bf8400d h1:hKmeOEHr6woCQCDhq8nzT0ekuCRGelx3k5FDo2+DD34=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240629212545-63c49bf8400d/go.mod h1:eu/C1dTewrW7yiFNiCKGm4zuWJANyt7zPjaY5g3f3r4=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240706124659-b4057a26c3ed h1:3F4YHSFaUJ9N0l4zNGeXZvnTBIHC9PDVOWOFiOvNn3Y=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240706124659-b4057a26c3ed/go.mod h1:bNQrvIftiwJ+7OjSh+Gza5xcncq1ooHk6oyDWq4B4sg=
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
48 changes: 14 additions & 34 deletions pkg/connector/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"

"github.com/google/uuid"
"maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"

Expand Down Expand Up @@ -157,42 +156,23 @@ func (qr *QRLogin) processingWait(ctx context.Context) (*bridgev2.LoginStep, err
return nil, ctx.Err()
}

ul, err := qr.Main.Bridge.GetExistingUserLoginByID(ctx, newLoginID)
if err != nil {
return nil, fmt.Errorf("failed to get existing login: %w", err)
}
if ul != nil && ul.UserMXID != qr.User.MXID {
ul.Delete(ctx, status.BridgeState{StateEvent: status.StateLoggedOut, Error: "overridden-by-another-user"}, false)
ul = nil
}
if ul == nil {
ul, err = qr.User.NewLogin(ctx, &database.UserLogin{
ID: newLoginID,
Metadata: database.UserLoginMetadata{
StandardUserLoginMetadata: database.StandardUserLoginMetadata{
RemoteName: qr.ProvData.Number,
},
Extra: map[string]any{
"phone": qr.ProvData.Number,
},
ul, err := qr.User.NewLogin(ctx, &database.UserLogin{
ID: newLoginID,
Metadata: database.UserLoginMetadata{
StandardUserLoginMetadata: database.StandardUserLoginMetadata{
RemoteName: qr.ProvData.Number,
},
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to save new login: %w", err)
}
} else {
ul.Metadata.Extra["phone"] = qr.ProvData.Number
ul.Metadata.RemoteName = qr.ProvData.Number
err = ul.Save(ctx)
if err != nil {
return nil, fmt.Errorf("failed to update existing login: %w", err)
}
}
backgroundCtx := ul.Log.WithContext(context.Background())
err = qr.Main.LoadUserLogin(backgroundCtx, ul)
Extra: map[string]any{
"phone": qr.ProvData.Number,
},
},
}, &bridgev2.NewLoginParams{
DeleteOnConflict: true,
})
if err != nil {
return nil, fmt.Errorf("failed to prepare connection after login: %w", err)
return nil, fmt.Errorf("failed to create user login: %w", err)
}
backgroundCtx := ul.Log.WithContext(context.Background())
err = ul.Client.Connect(backgroundCtx)
if err != nil {
return nil, fmt.Errorf("failed to connect after login: %w", err)
Expand Down

0 comments on commit 85f3c15

Please sign in to comment.