From ae8ef83115fa91b151343f39910707ab92433837 Mon Sep 17 00:00:00 2001
From: Noah Petherbridge
Date: Thu, 15 Jun 2023 22:05:21 -0700
Subject: [PATCH] Option to hide age from profile display
---
pkg/config/enum.go | 1 +
pkg/controller/account/age_gate.go | 7 ++++++-
pkg/models/user.go | 11 +++++++++++
web/templates/account/age_gate.html | 10 ++++++++++
web/templates/account/profile.html | 16 ++++++++++++----
web/templates/account/search.html | 4 ++--
web/templates/account/settings.html | 9 +++++++++
7 files changed, 51 insertions(+), 7 deletions(-)
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 @@
+
+
+
+