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",
"interests",
"music_movies",
"hide_age",
}
// Choices for the Contact Us subject

View File

@ -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")

View File

@ -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 {

View File

@ -54,6 +54,16 @@
</p>
</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">
<button type="submit" class="button is-success">
Save and continue

View File

@ -315,10 +315,18 @@
<strong class="is-size-7">Age:</label>
</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}}
{{ComputeAge .User.Birthdate}}
({{ComputeAge .User.Birthdate}})
{{else}}
n/a
(n/a)
{{end}}
</small>
{{end}}
</td>
</tr>

View File

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

View File

@ -68,6 +68,15 @@
If you entered a wrong birthdate, <a href="/contact">contact</a> support to change it.
{{end}}
</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>