website/pkg/templates/template_vars.go
Noah fb0e3651b0 iPad Friendly Nav Bar + Mobile NavBar Notifications
* iPad in landscape mode was "desktop" size so got the full nav bar but
  the "More" drop-down was unusable. Add work-arounds for large touch
  devices to make the nav bar functional.
* "Click" on the "More" button will pin it open so that the drop-down
  doesn't rely solely on mouseover events. Clicking off the open
  drop-down or clicking again on "More" toggles it hidden.
* The logged-in user menu now drops its menu on hover like "More" did.
* The logged-in user menu adds "TouchStart" events: touching the menu
  button toggles its drop-down to appear, canceling the link to "/me"
  that clicking the menu button does on desktops. Clicking off the open
  drop-down closes it.
* Add notification indicators for "mobile" devices which only showed the
  brand and hamburger menu by default. Next to the hamburger button will
  be badges for number of friend requests or messages, with icons. Click
  the badge to go to the relevant page, or it hints that there are
  notifications in the drop-down.
2022-08-21 21:35:01 -07:00

96 lines
2.6 KiB
Go

package templates
import (
"net/http"
"time"
"git.kirsle.net/apps/gosocial/pkg/config"
"git.kirsle.net/apps/gosocial/pkg/log"
"git.kirsle.net/apps/gosocial/pkg/models"
"git.kirsle.net/apps/gosocial/pkg/session"
)
// MergeVars mixes in globally available template variables. The http.Request is optional.
func MergeVars(r *http.Request, m map[string]interface{}) {
m["Title"] = config.Title
m["Subtitle"] = config.Subtitle
m["YYYY"] = time.Now().Year()
if r == nil {
return
}
m["Request"] = r
}
// MergeUserVars mixes in global template variables: LoggedIn and CurrentUser. The http.Request is optional.
func MergeUserVars(r *http.Request, m map[string]interface{}) {
// Defaults
m["LoggedIn"] = false
m["CurrentUser"] = nil
m["SessionImpersonated"] = false
// User notification counts for nav bar.
m["NavUnreadMessages"] = 0 // New messages
m["NavFriendRequests"] = 0 // Friend requests
m["NavTotalNotifications"] = 0 // Total of above
// Admin notification counts for nav bar.
m["NavCertificationPhotos"] = 0 // Cert. photos needing approval
m["NavAdminFeedback"] = 0 // Unacknowledged feedback
m["NavAdminNotifications"] = 0 // Total of above
if r == nil {
return
}
m["SessionImpersonated"] = session.Impersonated(r)
if user, err := session.CurrentUser(r); err == nil {
m["LoggedIn"] = true
m["CurrentUser"] = user
// Collect notification counts.
var (
// For users
countMessages int64
countFriendReqs int64
// For admins
countCertPhotos int64
countFeedback int64
)
// Get unread message count.
if count, err := models.CountUnreadMessages(user.ID); err == nil {
m["NavUnreadMessages"] = count
countMessages = count
} else {
log.Error("MergeUserVars: couldn't CountUnreadMessages for %d: %s", user.ID, err)
}
// Get friend request count.
if count, err := models.CountFriendRequests(user.ID); err == nil {
m["NavFriendRequests"] = count
countFriendReqs = count
} else {
log.Error("MergeUserVars: couldn't CountFriendRequests for %d: %s", user.ID, err)
}
// Are we admin?
if user.IsAdmin {
// Any pending certification photos or feedback?
countCertPhotos = models.CountCertificationPhotosNeedingApproval()
countFeedback = models.CountUnreadFeedback()
m["NavCertificationPhotos"] = countCertPhotos
m["NavAdminFeedback"] = countFeedback
// Total notification count for admin actions.
m["NavAdminNotifications"] = countCertPhotos + countFeedback
}
// Total count for user notifications.
m["NavTotalNotifications"] = countMessages + countFriendReqs + countCertPhotos + countFeedback
}
}