From 05be8d1c99daa4fe6da645946616ff5a3bc1c6c8 Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Thu, 31 May 2018 19:51:13 +0530 Subject: [PATCH] Move makeUserID into userutil and prevent code duplication (#475) * Move makeUserID into userutil and prevent code duplication * Correct ungraceful merge --- .../auth/storage/accounts/accounts_table.go | 10 +++------- .../auth/storage/devices/devices_table.go | 14 +++++--------- .../dendrite/clientapi/userutil/userutil.go | 6 ++++++ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/accounts_table.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/accounts_table.go index 8ea2d517f..3b4e6bd50 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/accounts_table.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/accounts_table.go @@ -17,10 +17,10 @@ package accounts import ( "context" "database/sql" - "fmt" "time" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/gomatrixserverlib" ) @@ -97,7 +97,7 @@ func (s *accountsStatements) insertAccount( return &authtypes.Account{ Localpart: localpart, - UserID: makeUserID(localpart, s.serverName), + UserID: userutil.MakeUserID(localpart, s.serverName), ServerName: s.serverName, AppServiceID: appserviceID, }, nil @@ -116,12 +116,8 @@ func (s *accountsStatements) selectAccountByLocalpart( stmt := s.selectAccountByLocalpartStmt err = stmt.QueryRowContext(ctx, localpart).Scan(&acc.Localpart, &acc.AppServiceID) if err == nil { - acc.UserID = makeUserID(localpart, s.serverName) + acc.UserID = userutil.MakeUserID(localpart, s.serverName) acc.ServerName = s.serverName } return } - -func makeUserID(localpart string, server gomatrixserverlib.ServerName) string { - return fmt.Sprintf("@%s:%s", localpart, string(server)) -} diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/devices_table.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/devices_table.go index 9512acfc0..96d6521d8 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/devices_table.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/devices/devices_table.go @@ -17,12 +17,12 @@ package devices import ( "context" "database/sql" - "fmt" "time" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" + "github.com/matrix-org/dendrite/clientapi/userutil" "github.com/matrix-org/gomatrixserverlib" ) @@ -126,7 +126,7 @@ func (s *devicesStatements) insertDevice( } return &authtypes.Device{ ID: id, - UserID: makeUserID(localpart, s.serverName), + UserID: userutil.MakeUserID(localpart, s.serverName), AccessToken: accessToken, }, nil } @@ -163,7 +163,7 @@ func (s *devicesStatements) selectDeviceByToken( stmt := s.selectDeviceByTokenStmt err := stmt.QueryRowContext(ctx, accessToken).Scan(&dev.ID, &localpart) if err == nil { - dev.UserID = makeUserID(localpart, s.serverName) + dev.UserID = userutil.MakeUserID(localpart, s.serverName) dev.AccessToken = accessToken } return &dev, err @@ -178,7 +178,7 @@ func (s *devicesStatements) selectDeviceByID( err := stmt.QueryRowContext(ctx, localpart, deviceID).Scan(&created) if err == nil { dev.ID = deviceID - dev.UserID = makeUserID(localpart, s.serverName) + dev.UserID = userutil.MakeUserID(localpart, s.serverName) } return &dev, err } @@ -200,13 +200,9 @@ func (s *devicesStatements) selectDevicesByLocalpart( if err != nil { return devices, err } - dev.UserID = makeUserID(localpart, s.serverName) + dev.UserID = userutil.MakeUserID(localpart, s.serverName) devices = append(devices, dev) } return devices, nil } - -func makeUserID(localpart string, server gomatrixserverlib.ServerName) string { - return fmt.Sprintf("@%s:%s", localpart, string(server)) -} diff --git a/src/github.com/matrix-org/dendrite/clientapi/userutil/userutil.go b/src/github.com/matrix-org/dendrite/clientapi/userutil/userutil.go index de2d1959f..4cea3c18e 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/userutil/userutil.go +++ b/src/github.com/matrix-org/dendrite/clientapi/userutil/userutil.go @@ -14,6 +14,7 @@ package userutil import ( "errors" + "fmt" "strings" "github.com/matrix-org/gomatrixserverlib" @@ -41,3 +42,8 @@ func ParseUsernameParam(usernameParam string, expectedServerName *gomatrixserver } 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)) +}