website/pkg/templates/template_vars.go
Noah Petherbridge 481bd0ae61 Deactivate Account; Friends Lists on Profiles
* Add a way for users to temporarily deactivate their accounts, in a
  recoverable way should they decide to return later.
* A deactivated account may log in but have limited options: to
  reactivate their account, permanently delete it, or log out.
* Fix several bugs around the display of comments, messages and
  forum threads for disabled, banned, or blocked users:
  * Messages (inbox and sentbox) will be hidden and the unread indicator
    will not count unread messages the user can't access.
  * Comments on photos and forum posts are hidden, and top-level threads
    on the "Newest" tab will show "[unavailable]" for their text and
    username.
  * Your historical notifications will hide users who are blocked, banned
    or disabled.
* Add a "Friends" tab to user profile pages, to see other users' friends.
  * The page is Certification Required so non-cert users can't easily
    discover any members on the site.
2023-10-22 15:02:24 -07:00

119 lines
3.4 KiB
Go

package templates
import (
"net/http"
"time"
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/worker"
)
// 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["BuildHash"] = config.RuntimeBuild
m["BuildDate"] = config.RuntimeBuildDate
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["NavUnreadNotifications"] = 0 // general notifications
m["NavTotalNotifications"] = 0 // Total of above
m["NavChatStatistics"] = worker.GetChatStatistics()
// 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
// Get user recent notifications.
/*notifPager := &models.Pagination{
Page: 1,
PerPage: 10,
}
if notifs, err := models.PaginateNotifications(user, notifPager); err == nil {
m["Notifications"] = notifs
}*/
// Collect notification counts.
var (
// For users
countMessages int64
countFriendReqs int64
countNotifications int64
// For admins
countCertPhotos int64
countFeedback int64
)
// Get unread message count.
if count, err := models.CountUnreadMessages(user); 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)
}
// Count other notifications.
if count, err := models.CountUnreadNotifications(user); err == nil {
m["NavUnreadNotifications"] = count
countNotifications = 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 + countNotifications + countCertPhotos + countFeedback
}
}