Option to hide age from profile display

This commit is contained in:
Noah Petherbridge 2023-06-15 22:05:21 -07:00
parent eed3e2f43b
commit ae8ef83115
7 changed files with 51 additions and 7 deletions

View File

@ -63,6 +63,7 @@ var (
"about_me", "about_me",
"interests", "interests",
"music_movies", "music_movies",
"hide_age",
} }
// Choices for the Contact Us subject // Choices for the Contact Us subject

View File

@ -30,7 +30,10 @@ func AgeGate() http.HandlerFunc {
// Are we POSTing? // Are we POSTing?
if r.Method == http.MethodPost { 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) birthdate, err := time.Parse("2006-01-02", dob)
if err != nil { if err != nil {
@ -82,6 +85,8 @@ func AgeGate() http.HandlerFunc {
session.FlashError(w, r, "Failed to save user to database: %s", err) 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!") session.Flash(w, r, "Thank you for entering your birthdate!")
templates.Redirect(w, "/me") templates.Redirect(w, "/me")

View File

@ -4,11 +4,13 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"strings" "strings"
"time" "time"
"code.nonshy.com/nonshy/website/pkg/config" "code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/log" "code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/utility"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -403,6 +405,15 @@ func (u *User) GetProfileField(name string) string {
return "" 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 // ProfileFieldIn checks if a substring is IN a profile field. Currently
// does a naive strings.Contains(), intended for the "here_for" field. // does a naive strings.Contains(), intended for the "here_for" field.
func (u *User) ProfileFieldIn(field, substr string) bool { func (u *User) ProfileFieldIn(field, substr string) bool {

View File

@ -54,6 +54,16 @@
</p> </p>
</div> </div>
<div class="field block">
<label class="checkbox">
<input type="checkbox"
name="hide_age"
value="true"
{{if eq (.CurrentUser.GetProfileField "hide_age") "true"}}checked{{end}}>
Don't display my age on my profile
</label>
</div>
<div class="field has-text-centered"> <div class="field has-text-centered">
<button type="submit" class="button is-success"> <button type="submit" class="button is-success">
Save and continue Save and continue

View File

@ -315,10 +315,18 @@
<strong class="is-size-7">Age:</label> <strong class="is-size-7">Age:</label>
</td> </td>
<td> <td>
<!-- The user's display age (or 'n/a' if don't have or they hide it) -->
{{.User.GetDisplayAge}}
<!-- Admin version always shows it -->
{{if .CurrentUser.IsAdmin}}
<small class="has-text-danger">
{{if not .User.Birthdate.IsZero}} {{if not .User.Birthdate.IsZero}}
{{ComputeAge .User.Birthdate}} ({{ComputeAge .User.Birthdate}})
{{else}} {{else}}
n/a (n/a)
{{end}}
</small>
{{end}} {{end}}
</td> </td>
</tr> </tr>

View File

@ -226,8 +226,8 @@
</p> </p>
{{end}} {{end}}
<p class="subtitle is-7 mb-2"> <p class="subtitle is-7 mb-2">
{{if not .Birthdate.IsZero }} {{if or (ne .GetDisplayAge "n/a")}}
<span class="mr-2">{{ComputeAge .Birthdate}}yo</span> <span class="mr-2">{{.GetDisplayAge}}</span>
{{end}} {{end}}
{{if .GetProfileField "gender"}} {{if .GetProfileField "gender"}}

View File

@ -68,6 +68,15 @@
If you entered a wrong birthdate, <a href="/contact">contact</a> support to change it. If you entered a wrong birthdate, <a href="/contact">contact</a> support to change it.
{{end}} {{end}}
</p> </p>
<!-- Don't show age checkbox -->
<label class="checkbox">
<input type="checkbox"
name="hide_age"
value="true"
{{if eq ($User.GetProfileField "hide_age") "true"}}checked{{end}}>
Don't show my age on my profile
</label>
</div> </div>
</div> </div>