From 59338d510fd6ff8746cbca33e7ec8fef54b023b4 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Tue, 10 Oct 2023 19:30:39 -0700 Subject: [PATCH] Birthday cake emoji for the chat room --- pkg/controller/chat/chat.go | 10 ++++++++-- pkg/models/user.go | 17 +++++++++++++++++ web/templates/chat.html | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/pkg/controller/chat/chat.go b/pkg/controller/chat/chat.go index c5eb4dd..6866d9e 100644 --- a/pkg/controller/chat/chat.go +++ b/pkg/controller/chat/chat.go @@ -66,8 +66,9 @@ func Landing() http.HandlerFunc { // Are they logging into the chat room? var ( - intent = r.FormValue("intent") - isShy = currentUser.IsShy() + intent = r.FormValue("intent") + isShy = currentUser.IsShy() + birthday = r.FormValue("birthday") == "true" && currentUser.IsBirthday() ) if intent == "join" { // Maintenance mode? @@ -114,6 +115,11 @@ func Landing() http.HandlerFunc { } } + // Birthday cake emoji? + if birthday { + emoji = "🍰 It's my birthday!" + } + // Create the JWT claims. claims := Claims{ IsAdmin: currentUser.HasAdminScope(config.ScopeChatModerator), diff --git a/pkg/models/user.go b/pkg/models/user.go index b6c52ee..3ac60e4 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -606,6 +606,23 @@ func (u *User) GetDisplayAge() string { return "n/a" } +// IsBirthday returns whether it is currently the user's birthday (+- a day for time zones). +func (u *User) IsBirthday() bool { + if u.Birthdate.IsZero() { + return false + } + + // Window of time to be valid. + var ( + now, _ = time.Parse(time.DateOnly, time.Now().Format(time.DateOnly)) + bday, _ = time.Parse(time.DateOnly, fmt.Sprintf("%d-%d-%d", now.Year(), u.Birthdate.Month(), u.Birthdate.Day())) + startDate = now.Add(-24 * time.Hour) + endDate = now.Add(24 * time.Hour) + ) + + return bday.Before(endDate) && bday.After(startDate) +} + // 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/chat.html b/web/templates/chat.html index 7b50132..cf484fd 100644 --- a/web/templates/chat.html +++ b/web/templates/chat.html @@ -26,6 +26,24 @@
+ {{if .CurrentUser.IsBirthday}} +
+

🎂 Happy birthday!

+ +

+ If you would like, you may enter the chat room with a special 🍰 birthday cake emoji next to + your username instead of the usual country flag. This is totally optional and you may do this + by clicking the button below to enter the chat room: +

+ +

+ + 🍰 Join the Chat Room + +

+
+ {{end}} + {{if .IsShyUser}}
You have a Shy Account and you may not enter