diff --git a/internal/auth/repository/eventsourcing/eventstore/user_session.go b/internal/auth/repository/eventsourcing/eventstore/user_session.go index 9be553e6f1..3092044b2d 100644 --- a/internal/auth/repository/eventsourcing/eventstore/user_session.go +++ b/internal/auth/repository/eventsourcing/eventstore/user_session.go @@ -22,6 +22,6 @@ func (repo *UserSessionRepo) GetMyUserSessions(ctx context.Context) ([]*usr_mode } func (repo *UserSessionRepo) ActiveUserSessionCount() int64 { - userSessions, _ := repo.View.ActiveUserSessions() - return int64(len(userSessions)) + userSessions, _ := repo.View.ActiveUserSessionsCount() + return int64(userSessions) } diff --git a/internal/auth/repository/eventsourcing/view/user_session.go b/internal/auth/repository/eventsourcing/view/user_session.go index 988f79dca7..03c47e7a50 100644 --- a/internal/auth/repository/eventsourcing/view/user_session.go +++ b/internal/auth/repository/eventsourcing/view/user_session.go @@ -24,7 +24,7 @@ func (v *View) UserSessionsByAgentID(agentID string) ([]*model.UserSessionView, return view.UserSessionsByAgentID(v.Db, userSessionTable, agentID) } -func (v *View) ActiveUserSessions() ([]*model.UserSessionView, error) { +func (v *View) ActiveUserSessionsCount() (uint64, error) { return view.ActiveUserSessions(v.Db, userSessionTable) } diff --git a/internal/user/repository/view/user_session_view.go b/internal/user/repository/view/user_session_view.go index 7a49e0c270..1ed3610e3c 100644 --- a/internal/user/repository/view/user_session_view.go +++ b/internal/user/repository/view/user_session_view.go @@ -1,14 +1,14 @@ package view import ( + "github.com/jinzhu/gorm" + auth_model "github.com/caos/zitadel/internal/auth_request/model" "github.com/caos/zitadel/internal/domain" caos_errs "github.com/caos/zitadel/internal/errors" - "github.com/caos/zitadel/internal/view/repository" - "github.com/jinzhu/gorm" - usr_model "github.com/caos/zitadel/internal/user/model" "github.com/caos/zitadel/internal/user/repository/view/model" + "github.com/caos/zitadel/internal/view/repository" ) func UserSessionByIDs(db *gorm.DB, table, agentID, userID string) (*model.UserSessionView, error) { @@ -59,8 +59,7 @@ func UserSessionsByAgentID(db *gorm.DB, table, agentID string) ([]*model.UserSes return userSessions, err } -func ActiveUserSessions(db *gorm.DB, table string) ([]*model.UserSessionView, error) { - userSessions := make([]*model.UserSessionView, 0) +func ActiveUserSessions(db *gorm.DB, table string) (uint64, error) { activeQuery := &usr_model.UserSessionSearchQuery{ Key: usr_model.UserSessionSearchKeyState, Method: domain.SearchMethodEquals, @@ -69,8 +68,7 @@ func ActiveUserSessions(db *gorm.DB, table string) ([]*model.UserSessionView, er query := repository.PrepareSearchQuery(table, model.UserSessionSearchRequest{ Queries: []*usr_model.UserSessionSearchQuery{activeQuery}, }) - _, err := query(db, &userSessions) - return userSessions, err + return query(db, nil) } func PutUserSession(db *gorm.DB, table string, session *model.UserSessionView) error { diff --git a/internal/view/repository/query.go b/internal/view/repository/query.go index 410339735b..65fa3ebb9c 100644 --- a/internal/view/repository/query.go +++ b/internal/view/repository/query.go @@ -2,11 +2,12 @@ package repository import ( "fmt" - "github.com/caos/zitadel/internal/domain" - caos_errs "github.com/caos/zitadel/internal/errors" "github.com/jinzhu/gorm" "github.com/lib/pq" + + "github.com/caos/zitadel/internal/domain" + caos_errs "github.com/caos/zitadel/internal/errors" ) type SearchRequest interface { @@ -48,6 +49,9 @@ func PrepareSearchQuery(table string, request SearchRequest) func(db *gorm.DB, r } query = query.Count(&count) + if res == nil { + return count, nil + } if request.GetLimit() != 0 { query = query.Limit(request.GetLimit()) }