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

* Move makeUserID into userutil and prevent code duplication

* Correct ungraceful merge
This commit is contained in:
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 (
"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))
}

View File

@ -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))
}

View File

@ -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))
}