Skip to content

Commit

Permalink
Use new typed db metadata structs
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 13, 2024
1 parent ab6b77e commit cb9a2ae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/rs/zerolog v1.33.0
github.com/twilio/twilio-go v1.22.2
go.mau.fi/util v0.5.1-0.20240702170310-bd1da3c069eb
maunium.net/go/mautrix v0.19.0-beta.1.0.20240706124659-b4057a26c3ed
maunium.net/go/mautrix v0.19.0-beta.1.0.20240713090952-9fdf94132a3d
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ 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.20240706124659-b4057a26c3ed h1:3F4YHSFaUJ9N0l4zNGeXZvnTBIHC9PDVOWOFiOvNn3Y=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240706124659-b4057a26c3ed/go.mod h1:bNQrvIftiwJ+7OjSh+Gza5xcncq1ooHk6oyDWq4B4sg=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240713090952-9fdf94132a3d h1:Fxpvg1sOmkKKsYqbTLEIf6La9+qeBFKmnwkZfcxa8eM=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240713090952-9fdf94132a3d/go.mod h1:bNQrvIftiwJ+7OjSh+Gza5xcncq1ooHk6oyDWq4B4sg=
62 changes: 38 additions & 24 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,33 @@ func (tc *TwilioConnector) GetConfig() (example string, data any, upgrader confi
return "", nil, nil
}

func (tc *TwilioConnector) GetDBMetaTypes() database.MetaTypes {
return database.MetaTypes{
Portal: nil,
Ghost: nil,
Message: nil,
Reaction: nil,
UserLogin: func() any {
return &UserLoginMetadata{}
},
}
}

type UserLoginMetadata struct {
Phone string `json:"phone"`
PhoneSID string `json:"phone_sid"`
AuthToken string `json:"auth_token"`
AccountSID string `json:"account_sid"`
}

func (tc *TwilioConnector) LoadUserLogin(ctx context.Context, login *bridgev2.UserLogin) error {
accountSID := login.Metadata.Extra["account_sid"].(string)
authToken := login.Metadata.Extra["auth_token"].(string)
meta := login.Metadata.(*UserLoginMetadata)
restClient := twilio.NewRestClientWithParams(twilio.ClientParams{
Username: accountSID,
Password: authToken,
AccountSid: accountSID,
Username: meta.AccountSID,
Password: meta.AuthToken,
AccountSid: meta.AccountSID,
})
validator := tclient.NewRequestValidator(authToken)
validator := tclient.NewRequestValidator(meta.AuthToken)
login.Client = &TwilioClient{
UserLogin: login,
Twilio: restClient,
Expand All @@ -145,16 +163,16 @@ func (tc *TwilioClient) Connect(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to list phone numbers: %w", err)
}
numberInUse := tc.UserLogin.Metadata.Extra["phone"].(string)
meta := tc.UserLogin.Metadata.(*UserLoginMetadata)
var numberFound bool
for _, number := range phoneNumbers {
if number.PhoneNumber != nil && *number.PhoneNumber == numberInUse {
if number.PhoneNumber != nil && *number.PhoneNumber == meta.Phone {
numberFound = true
break
}
}
if !numberFound {
return fmt.Errorf("phone number %s not found on account", numberInUse)
return fmt.Errorf("phone number %s not found on account", meta.Phone)
}
return nil
}
Expand Down Expand Up @@ -186,8 +204,8 @@ func makeUserLoginID(accountSID, phoneSID string) networkid.UserLoginID {
}

func (tc *TwilioClient) IsThisUser(ctx context.Context, userID networkid.UserID) bool {
phoneNum := tc.UserLogin.Metadata.Extra["phone"].(string)
return makeUserID(phoneNum) == userID
meta := tc.UserLogin.Metadata.(*UserLoginMetadata)
return makeUserID(meta.Phone) == userID
}

func (tc *TwilioClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal) (*bridgev2.ChatInfo, error) {
Expand All @@ -198,7 +216,7 @@ func (tc *TwilioClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal
{
EventSender: bridgev2.EventSender{
IsFromMe: true,
Sender: makeUserID(tc.UserLogin.Metadata.Extra["phone"].(string)),
Sender: makeUserID(tc.UserLogin.Metadata.(*UserLoginMetadata).Phone),
},
// This could be omitted, but leave it in to be explicit.
Membership: event.MembershipJoin,
Expand Down Expand Up @@ -267,7 +285,7 @@ func (tc *TwilioClient) convertMessage(ctx context.Context, portal *bridgev2.Por
func (tc *TwilioClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage) (message *bridgev2.MatrixMessageResponse, err error) {
resp, err := tc.Twilio.Api.CreateMessage(&openapi.CreateMessageParams{
To: ptr.Ptr(fmt.Sprintf("+%s", msg.Portal.ID)),
From: ptr.Ptr(tc.UserLogin.Metadata.Extra["phone"].(string)),
From: ptr.Ptr(tc.UserLogin.Metadata.(*UserLoginMetadata).Phone),
Body: ptr.Ptr(msg.Content.Body),
})
if err != nil {
Expand Down Expand Up @@ -442,17 +460,13 @@ func (tl *TwilioLogin) submitChosenPhoneNumber(ctx context.Context, input map[st

func (tl *TwilioLogin) finishLogin(ctx context.Context, phoneNumber twilioPhoneNumber) (*bridgev2.LoginStep, error) {
ul, err := tl.User.NewLogin(ctx, &database.UserLogin{
ID: makeUserLoginID(tl.AccountSID, phoneNumber.SID),
Metadata: database.UserLoginMetadata{
StandardUserLoginMetadata: database.StandardUserLoginMetadata{
RemoteName: phoneNumber.PrettyNumber,
},
Extra: map[string]any{
"phone": phoneNumber.Number,
"phone_sid": phoneNumber.SID,
"auth_token": tl.AuthToken,
"account_sid": tl.AccountSID,
},
ID: makeUserLoginID(tl.AccountSID, phoneNumber.SID),
RemoteName: phoneNumber.PrettyNumber,
Metadata: &UserLoginMetadata{
Phone: phoneNumber.Number,
PhoneSID: phoneNumber.SID,
AuthToken: tl.AuthToken,
AccountSID: tl.AccountSID,
},
}, &bridgev2.NewLoginParams{
LoadUserLogin: func(ctx context.Context, login *bridgev2.UserLogin) error {
Expand Down

0 comments on commit cb9a2ae

Please sign in to comment.