Fixed panic when using link share and metrics
This commit is contained in:
parent
b653278e42
commit
854fde1e4c
2 changed files with 30 additions and 29 deletions
|
@ -27,7 +27,6 @@ import (
|
|||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
// UserLogin Object to recive user credentials in JSON format
|
||||
|
@ -207,33 +206,6 @@ func GetUserFromClaims(claims jwt.MapClaims) (user *User, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// UpdateActiveUsersFromContext updates the currently active users in redis
|
||||
func UpdateActiveUsersFromContext(c echo.Context) (err error) {
|
||||
user, err := GetCurrentUser(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
allActiveUsers, err := metrics.GetActiveUsers()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var uupdated bool
|
||||
for in, u := range allActiveUsers {
|
||||
if u.UserID == user.ID {
|
||||
allActiveUsers[in].LastSeen = time.Now()
|
||||
uupdated = true
|
||||
}
|
||||
}
|
||||
|
||||
if !uupdated {
|
||||
allActiveUsers = append(allActiveUsers, &metrics.ActiveUser{UserID: user.ID, LastSeen: time.Now()})
|
||||
}
|
||||
|
||||
return metrics.SetActiveUsers(allActiveUsers)
|
||||
}
|
||||
|
||||
// CreateUser creates a new user and inserts it into the database
|
||||
func CreateUser(user *User) (newUser *User, err error) {
|
||||
|
||||
|
|
|
@ -22,8 +22,10 @@ import (
|
|||
"code.vikunja.io/api/pkg/log"
|
||||
"code.vikunja.io/api/pkg/metrics"
|
||||
"code.vikunja.io/api/pkg/models"
|
||||
v1 "code.vikunja.io/api/pkg/routes/api/v1"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func setupMetrics(a *echo.Group) {
|
||||
|
@ -91,7 +93,7 @@ func setupMetricsMiddleware(a *echo.Group) {
|
|||
return func(c echo.Context) error {
|
||||
|
||||
// Update currently active users
|
||||
if err := models.UpdateActiveUsersFromContext(c); err != nil {
|
||||
if err := updateActiveUsersFromContext(c); err != nil {
|
||||
log.Error(err)
|
||||
return next(c)
|
||||
}
|
||||
|
@ -99,3 +101,30 @@ func setupMetricsMiddleware(a *echo.Group) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
// updateActiveUsersFromContext updates the currently active users in redis
|
||||
func updateActiveUsersFromContext(c echo.Context) (err error) {
|
||||
auth, err := v1.GetAuthFromClaims(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
allActiveUsers, err := metrics.GetActiveUsers()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var uupdated bool
|
||||
for in, u := range allActiveUsers {
|
||||
if u.UserID == auth.GetID() {
|
||||
allActiveUsers[in].LastSeen = time.Now()
|
||||
uupdated = true
|
||||
}
|
||||
}
|
||||
|
||||
if !uupdated {
|
||||
allActiveUsers = append(allActiveUsers, &metrics.ActiveUser{UserID: auth.GetID(), LastSeen: time.Now()})
|
||||
}
|
||||
|
||||
return metrics.SetActiveUsers(allActiveUsers)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue