Merge fixes

This commit is contained in:
Gabe Kangas 2022-04-30 21:51:55 -07:00
parent 3bfa260e5e
commit a507eba350
No known key found for this signature in database
GPG key ID: 9A56337728BC81EA

View file

@ -17,7 +17,10 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var _datastore *data.Datastore var (
_datastore *data.Datastore
_cachedGetUserByTokenStatement *sql.Stmt
)
const ( const (
moderatorScopeKey = "MODERATOR" moderatorScopeKey = "MODERATOR"
@ -139,14 +142,14 @@ func create(user *User) error {
defer _datastore.DbLock.Unlock() defer _datastore.DbLock.Unlock()
if createUserStatement == nil { if createUserStatement == nil {
stmt, err := _datastore.DB.Prepare("INSERT INTO users(id, access_token, display_name, display_color, previous_names, created_at) values(?, ?, ?, ?, ?, ?)") stmt, err := _datastore.DB.Prepare("INSERT INTO users(id, display_name, display_color, previous_names, created_at) values(?, ?, ?, ?, ?, ?)")
if err != nil { if err != nil {
return errors.Wrap(err, "error preparing create user statement") return errors.Wrap(err, "error preparing create user statement")
} }
createUserStatement = stmt createUserStatement = stmt
} }
_, err := createUserStatement.Exec(user.ID, user.AccessToken, user.DisplayName, user.DisplayColor, user.DisplayName, user.CreatedAt) _, err := createUserStatement.Exec(user.ID, user.DisplayName, user.DisplayColor, user.DisplayName, user.CreatedAt)
if err != nil { if err != nil {
return errors.Wrap(err, "error creating user") return errors.Wrap(err, "error creating user")
} }
@ -186,8 +189,6 @@ func SetEnabled(userID string, enabled bool) error {
return tx.Commit() return tx.Commit()
} }
var cachedGUserByTokenStatement *sql.Stmt
// GetUserByToken will return a user by an access token. // GetUserByToken will return a user by an access token.
func GetUserByToken(token string) *User { func GetUserByToken(token string) *User {
u, err := _datastore.GetQueries().GetUserByAccessToken(context.Background(), token) u, err := _datastore.GetQueries().GetUserByAccessToken(context.Background(), token)
@ -195,15 +196,19 @@ func GetUserByToken(token string) *User {
return nil return nil
} }
if cachedGUserByTokenStatement == nil { if _cachedGetUserByTokenStatement == nil {
stmt, err := _datastore.DB.Prepare("SELECT id, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at, scopes FROM users WHERE access_token = ?") stmt, err := _datastore.DB.Prepare("SELECT id, display_name, display_color, created_at, disabled_at, previous_names, namechanged_at, scopes FROM users WHERE access_token = ?")
if err != nil { if err != nil {
log.Errorln(err) log.Errorln(err)
return nil return nil
} }
cachedGUserByTokenStatement = stmt _cachedGetUserByTokenStatement = stmt
}
var scopes []string
if u.Scopes.Valid {
scopes = strings.Split(u.Scopes.String, ",")
} }
row := cachedGUserByTokenStatement.QueryRow(token)
var disabledAt *time.Time var disabledAt *time.Time
if u.DisabledAt.Valid { if u.DisabledAt.Valid {