From b5d2cc0465b59dfabc560d6b33329ac504f74c0d Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 23 Jun 2017 19:22:39 +0200 Subject: [PATCH] Refactor --- main.go | 3 +-- middleware/UserInfo.go | 14 +++++++++++++- utils/production.go | 17 ----------------- utils/user.go | 20 ++------------------ 4 files changed, 16 insertions(+), 38 deletions(-) delete mode 100644 utils/production.go diff --git a/main.go b/main.go index 4dfe6162..edc47ead 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,6 @@ import ( "github.com/animenotifier/notify.moe/pages/user" "github.com/animenotifier/notify.moe/pages/users" "github.com/animenotifier/notify.moe/pages/webdev" - "github.com/animenotifier/notify.moe/utils" ) var app = aero.New() @@ -84,7 +83,7 @@ func configure(app *aero.Application) *aero.Application { api.Install(app) // Domain - if utils.IsDevelopment() { + if arn.IsDevelopment() { app.Config.Domain = "beta.notify.moe" } diff --git a/middleware/UserInfo.go b/middleware/UserInfo.go index 3867e8d6..9f1a8429 100644 --- a/middleware/UserInfo.go +++ b/middleware/UserInfo.go @@ -32,7 +32,14 @@ func UserInfo() aero.Middleware { next() // Ignore non-HTML requests - if strings.Index(ctx.GetRequestHeader("Accept"), "text/html") == -1 { + if ctx.IsMediaResponse() { + return + } + + // Ignore API requests + // Note that API requests can filter data (privacy) and we might accidentally save the filtered data. + // That's why it's very important to ignore all API requests and not call user.Save() in this context. + if strings.HasPrefix(ctx.URI(), "/api/") { return } @@ -43,6 +50,11 @@ func UserInfo() aero.Middleware { return } + // Let's be 100% sure we really do not accidentally save filtered data. + if user.Email == "" && user.IP == "" && user.FirstName == "" { + return + } + // This works asynchronously so it doesn't block the response go updateUserInfo(ctx, user) } diff --git a/utils/production.go b/utils/production.go deleted file mode 100644 index 334a0991..00000000 --- a/utils/production.go +++ /dev/null @@ -1,17 +0,0 @@ -package utils - -import ( - "os" - "strings" -) - -// IsProduction returns true if the hostname contains "arn". -func IsProduction() bool { - host, _ := os.Hostname() - return strings.Contains(host, "arn") -} - -// IsDevelopment returns true if the hostname does not contain "arn". -func IsDevelopment() bool { - return !IsProduction() -} diff --git a/utils/user.go b/utils/user.go index ff52a7df..3e665f7e 100644 --- a/utils/user.go +++ b/utils/user.go @@ -5,23 +5,7 @@ import ( "github.com/animenotifier/arn" ) -// GetUser ... +// GetUser returns the logged in user for the given context. func GetUser(ctx *aero.Context) *arn.User { - if !ctx.HasSession() { - return nil - } - - userID := ctx.Session().GetString("userId") - - if userID == "" { - return nil - } - - user, err := arn.GetUser(userID) - - if err != nil { - return nil - } - - return user + return arn.GetUserFromContext(ctx) }