[performance] add user cache and database (#879)

* go fmt

* add + use user cache and database

* fix import

* update tests

* remove unused relation
This commit is contained in:
tobi
2022-10-03 10:46:11 +02:00
committed by GitHub
parent f7af7c061c
commit 56f53a2a6f
21 changed files with 490 additions and 70 deletions

View File

@@ -52,8 +52,8 @@ func (m *Module) TokenCheck(c *gin.Context) {
log.Tracef("authenticated user %s with bearer token, scope is %s", userID, ti.GetScope())
// fetch user for this token
user := &gtsmodel.User{}
if err := m.db.GetByID(ctx, userID, user); err != nil {
user, err := m.db.GetUserByID(ctx, userID)
if err != nil {
if err != db.ErrNoEntries {
log.Errorf("database error looking for user with id %s: %s", userID, err)
return
@@ -80,22 +80,25 @@ func (m *Module) TokenCheck(c *gin.Context) {
c.Set(oauth.SessionAuthorizedUser, user)
// fetch account for this token
acct, err := m.db.GetAccountByID(ctx, user.AccountID)
if err != nil {
if err != db.ErrNoEntries {
log.Errorf("database error looking for account with id %s: %s", user.AccountID, err)
if user.Account == nil {
acct, err := m.db.GetAccountByID(ctx, user.AccountID)
if err != nil {
if err != db.ErrNoEntries {
log.Errorf("database error looking for account with id %s: %s", user.AccountID, err)
return
}
log.Warnf("no account found for userID %s", userID)
return
}
log.Warnf("no account found for userID %s", userID)
return
user.Account = acct
}
if !acct.SuspendedAt.IsZero() {
if !user.Account.SuspendedAt.IsZero() {
log.Warnf("authenticated user %s's account (accountId=%s) has been suspended", userID, user.AccountID)
return
}
c.Set(oauth.SessionAuthorizedAccount, acct)
c.Set(oauth.SessionAuthorizedAccount, user.Account)
}
// check for application token