Fixed UserInfo middleware

This commit is contained in:
Eduard Urbach 2017-06-23 16:58:12 +02:00
parent 389dbae5b4
commit 17b8282b5e

View File

@ -31,13 +31,11 @@ func UserInfo() aero.Middleware {
return func(ctx *aero.Context, next func()) { return func(ctx *aero.Context, next func()) {
next() next()
// This works asynchronously so it doesn't block the response // Ignore non-HTML requests
go updateUserInfo(ctx) if strings.Index(ctx.GetRequestHeader("Accept"), "text/html") == -1 {
return
} }
}
// updateUserInfo is started asynchronously so it doesn't block the request
func updateUserInfo(ctx *aero.Context) {
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
// When there's no user logged in, nothing to update // When there's no user logged in, nothing to update
@ -45,11 +43,13 @@ func updateUserInfo(ctx *aero.Context) {
return return
} }
// Ignore non-HTML requests // This works asynchronously so it doesn't block the response
if strings.Index(ctx.GetRequestHeader("Accept"), "text/html") == -1 { go updateUserInfo(ctx, user)
return
} }
}
// updateUserInfo is started asynchronously so it doesn't block the request
func updateUserInfo(ctx *aero.Context, user *arn.User) {
newIP := ctx.RealIP() newIP := ctx.RealIP()
newUserAgent := ctx.UserAgent() newUserAgent := ctx.UserAgent()
@ -69,6 +69,14 @@ func updateUserInfo(ctx *aero.Context) {
} }
if user.IP != newIP { if user.IP != newIP {
updateUserLocation(user, newIP)
}
user.LastSeen = arn.DateTimeUTC()
user.Save()
}
func updateUserLocation(user *arn.User, newIP string) {
user.IP = newIP user.IP = newIP
locationAPI := "https://api.ipinfodb.com/v3/ip-city/?key=" + apiKeys.IPInfoDB.ID + "&ip=" + user.IP + "&format=json" locationAPI := "https://api.ipinfodb.com/v3/ip-city/?key=" + apiKeys.IPInfoDB.ID + "&ip=" + user.IP + "&format=json"
@ -97,8 +105,4 @@ func updateUserInfo(ctx *aero.Context) {
user.Location.TimeZone = newLocation.TimeZone user.Location.TimeZone = newLocation.TimeZone
user.Location.ZipCode = newLocation.ZipCode user.Location.ZipCode = newLocation.ZipCode
} }
}
user.LastSeen = arn.DateTimeUTC()
user.Save()
} }