diff --git a/pkg/config/enum.go b/pkg/config/enum.go index 7694d47..5b52b2d 100644 --- a/pkg/config/enum.go +++ b/pkg/config/enum.go @@ -63,6 +63,7 @@ var ( "about_me", "interests", "music_movies", + "hide_age", } // Choices for the Contact Us subject diff --git a/pkg/controller/account/age_gate.go b/pkg/controller/account/age_gate.go index 3518ad9..37c1bdd 100644 --- a/pkg/controller/account/age_gate.go +++ b/pkg/controller/account/age_gate.go @@ -30,7 +30,10 @@ func AgeGate() http.HandlerFunc { // Are we POSTing? if r.Method == http.MethodPost { - var dob = r.PostFormValue("dob") + var ( + dob = r.PostFormValue("dob") + hideAge = r.PostFormValue("hide_age") + ) birthdate, err := time.Parse("2006-01-02", dob) if err != nil { @@ -82,6 +85,8 @@ func AgeGate() http.HandlerFunc { session.FlashError(w, r, "Failed to save user to database: %s", err) } + user.SetProfileField("hide_age", hideAge) + session.Flash(w, r, "Thank you for entering your birthdate!") templates.Redirect(w, "/me") diff --git a/pkg/models/user.go b/pkg/models/user.go index fe12b71..00d47eb 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -4,11 +4,13 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "strings" "time" "code.nonshy.com/nonshy/website/pkg/config" "code.nonshy.com/nonshy/website/pkg/log" + "code.nonshy.com/nonshy/website/pkg/utility" "golang.org/x/crypto/bcrypt" "gorm.io/gorm" ) @@ -403,6 +405,15 @@ func (u *User) GetProfileField(name string) string { return "" } +// GetDisplayAge returns the user's age dependent on their hide-my-age setting. +func (u *User) GetDisplayAge() string { + if !u.Birthdate.IsZero() && u.GetProfileField("hide_age") != "true" { + return fmt.Sprintf("%dyo", utility.Age(u.Birthdate)) + } + + return "n/a" +} + // ProfileFieldIn checks if a substring is IN a profile field. Currently // does a naive strings.Contains(), intended for the "here_for" field. func (u *User) ProfileFieldIn(field, substr string) bool { diff --git a/web/templates/account/age_gate.html b/web/templates/account/age_gate.html index 124786a..e169d8f 100644 --- a/web/templates/account/age_gate.html +++ b/web/templates/account/age_gate.html @@ -54,6 +54,16 @@
+