Move makeUserID into userutil and prevent code duplication (#475)

* Move makeUserID into userutil and prevent code duplication

* Correct ungraceful merge
main
Anant Prakash 2018-05-31 19:51:13 +05:30 committed by Andrew Morgan
parent 7ac1efc4b3
commit 05be8d1c99
3 changed files with 14 additions and 16 deletions

View File

@ -17,10 +17,10 @@ package accounts
import ( import (
"context" "context"
"database/sql" "database/sql"
"fmt"
"time" "time"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/clientapi/userutil"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
@ -97,7 +97,7 @@ func (s *accountsStatements) insertAccount(
return &authtypes.Account{ return &authtypes.Account{
Localpart: localpart, Localpart: localpart,
UserID: makeUserID(localpart, s.serverName), UserID: userutil.MakeUserID(localpart, s.serverName),
ServerName: s.serverName, ServerName: s.serverName,
AppServiceID: appserviceID, AppServiceID: appserviceID,
}, nil }, nil
@ -116,12 +116,8 @@ func (s *accountsStatements) selectAccountByLocalpart(
stmt := s.selectAccountByLocalpartStmt stmt := s.selectAccountByLocalpartStmt
err = stmt.QueryRowContext(ctx, localpart).Scan(&acc.Localpart, &acc.AppServiceID) err = stmt.QueryRowContext(ctx, localpart).Scan(&acc.Localpart, &acc.AppServiceID)
if err == nil { if err == nil {
acc.UserID = makeUserID(localpart, s.serverName) acc.UserID = userutil.MakeUserID(localpart, s.serverName)
acc.ServerName = s.serverName acc.ServerName = s.serverName
} }
return return
} }
func makeUserID(localpart string, server gomatrixserverlib.ServerName) string {
return fmt.Sprintf("@%s:%s", localpart, string(server))
}

View File

@ -17,12 +17,12 @@ package devices
import ( import (
"context" "context"
"database/sql" "database/sql"
"fmt"
"time" "time"
"github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/clientapi/userutil"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
@ -126,7 +126,7 @@ func (s *devicesStatements) insertDevice(
} }
return &authtypes.Device{ return &authtypes.Device{
ID: id, ID: id,
UserID: makeUserID(localpart, s.serverName), UserID: userutil.MakeUserID(localpart, s.serverName),
AccessToken: accessToken, AccessToken: accessToken,
}, nil }, nil
} }
@ -163,7 +163,7 @@ func (s *devicesStatements) selectDeviceByToken(
stmt := s.selectDeviceByTokenStmt stmt := s.selectDeviceByTokenStmt
err := stmt.QueryRowContext(ctx, accessToken).Scan(&dev.ID, &localpart) err := stmt.QueryRowContext(ctx, accessToken).Scan(&dev.ID, &localpart)
if err == nil { if err == nil {
dev.UserID = makeUserID(localpart, s.serverName) dev.UserID = userutil.MakeUserID(localpart, s.serverName)
dev.AccessToken = accessToken dev.AccessToken = accessToken
} }
return &dev, err return &dev, err
@ -178,7 +178,7 @@ func (s *devicesStatements) selectDeviceByID(
err := stmt.QueryRowContext(ctx, localpart, deviceID).Scan(&created) err := stmt.QueryRowContext(ctx, localpart, deviceID).Scan(&created)
if err == nil { if err == nil {
dev.ID = deviceID dev.ID = deviceID
dev.UserID = makeUserID(localpart, s.serverName) dev.UserID = userutil.MakeUserID(localpart, s.serverName)
} }
return &dev, err return &dev, err
} }
@ -200,13 +200,9 @@ func (s *devicesStatements) selectDevicesByLocalpart(
if err != nil { if err != nil {
return devices, err return devices, err
} }
dev.UserID = makeUserID(localpart, s.serverName) dev.UserID = userutil.MakeUserID(localpart, s.serverName)
devices = append(devices, dev) devices = append(devices, dev)
} }
return devices, nil return devices, nil
} }
func makeUserID(localpart string, server gomatrixserverlib.ServerName) string {
return fmt.Sprintf("@%s:%s", localpart, string(server))
}

View File

@ -14,6 +14,7 @@ package userutil
import ( import (
"errors" "errors"
"fmt"
"strings" "strings"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -41,3 +42,8 @@ func ParseUsernameParam(usernameParam string, expectedServerName *gomatrixserver
} }
return localpart, nil return localpart, nil
} }
// MakeUserID generates user ID from localpart & server name
func MakeUserID(localpart string, server gomatrixserverlib.ServerName) string {
return fmt.Sprintf("@%s:%s", localpart, string(server))
}