2022-08-10 05:10:47 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"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"
|
2023-06-08 04:59:15 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/worker"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2022-08-25 04:17:34 +00:00
|
|
|
m["BuildHash"] = config.RuntimeBuild
|
|
|
|
m["BuildDate"] = config.RuntimeBuildDate
|
2022-08-10 05:10:47 +00:00
|
|
|
m["Subtitle"] = config.Subtitle
|
|
|
|
m["YYYY"] = time.Now().Year()
|
|
|
|
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-08-13 06:11:36 +00:00
|
|
|
|
|
|
|
m["Request"] = r
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2022-08-14 23:27:57 +00:00
|
|
|
m["SessionImpersonated"] = false
|
2022-08-10 05:10:47 +00:00
|
|
|
|
2022-08-22 04:24:36 +00:00
|
|
|
// User notification counts for nav bar.
|
2022-08-25 04:17:34 +00:00
|
|
|
m["NavUnreadMessages"] = 0 // New messages
|
|
|
|
m["NavFriendRequests"] = 0 // Friend requests
|
|
|
|
m["NavUnreadNotifications"] = 0 // general notifications
|
|
|
|
m["NavTotalNotifications"] = 0 // Total of above
|
2023-06-08 04:59:15 +00:00
|
|
|
m["NavChatStatistics"] = worker.GetChatStatistics()
|
2022-08-22 04:24:36 +00:00
|
|
|
|
|
|
|
// Admin notification counts for nav bar.
|
|
|
|
m["NavCertificationPhotos"] = 0 // Cert. photos needing approval
|
|
|
|
m["NavAdminFeedback"] = 0 // Unacknowledged feedback
|
|
|
|
m["NavAdminNotifications"] = 0 // Total of above
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-14 23:27:57 +00:00
|
|
|
m["SessionImpersonated"] = session.Impersonated(r)
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
if user, err := session.CurrentUser(r); err == nil {
|
|
|
|
m["LoggedIn"] = true
|
|
|
|
m["CurrentUser"] = user
|
2022-08-14 00:42:42 +00:00
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// Get user recent notifications.
|
|
|
|
/*notifPager := &models.Pagination{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 10,
|
|
|
|
}
|
|
|
|
if notifs, err := models.PaginateNotifications(user, notifPager); err == nil {
|
|
|
|
m["Notifications"] = notifs
|
|
|
|
}*/
|
|
|
|
|
2022-08-22 04:24:36 +00:00
|
|
|
// Collect notification counts.
|
|
|
|
var (
|
|
|
|
// For users
|
2022-08-25 04:17:34 +00:00
|
|
|
countMessages int64
|
|
|
|
countFriendReqs int64
|
|
|
|
countNotifications int64
|
2022-08-22 04:24:36 +00:00
|
|
|
|
|
|
|
// For admins
|
|
|
|
countCertPhotos int64
|
|
|
|
countFeedback int64
|
|
|
|
)
|
|
|
|
|
2022-08-14 00:42:42 +00:00
|
|
|
// Get unread message count.
|
2023-10-22 22:02:24 +00:00
|
|
|
if count, err := models.CountUnreadMessages(user); err == nil {
|
2022-08-14 00:42:42 +00:00
|
|
|
m["NavUnreadMessages"] = count
|
2022-08-22 04:24:36 +00:00
|
|
|
countMessages = count
|
2022-08-14 00:42:42 +00:00
|
|
|
} else {
|
|
|
|
log.Error("MergeUserVars: couldn't CountUnreadMessages for %d: %s", user.ID, err)
|
|
|
|
}
|
2022-08-14 05:44:57 +00:00
|
|
|
|
|
|
|
// Get friend request count.
|
|
|
|
if count, err := models.CountFriendRequests(user.ID); err == nil {
|
|
|
|
m["NavFriendRequests"] = count
|
2022-08-22 04:24:36 +00:00
|
|
|
countFriendReqs = count
|
2022-08-14 05:44:57 +00:00
|
|
|
} else {
|
|
|
|
log.Error("MergeUserVars: couldn't CountFriendRequests for %d: %s", user.ID, err)
|
|
|
|
}
|
2022-08-14 23:27:57 +00:00
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// Count other notifications.
|
2023-10-22 22:02:24 +00:00
|
|
|
if count, err := models.CountUnreadNotifications(user); err == nil {
|
2022-08-25 04:17:34 +00:00
|
|
|
m["NavUnreadNotifications"] = count
|
|
|
|
countNotifications = count
|
|
|
|
} else {
|
|
|
|
log.Error("MergeUserVars: couldn't CountFriendRequests for %d: %s", user.ID, err)
|
|
|
|
}
|
|
|
|
|
2022-08-14 23:27:57 +00:00
|
|
|
// Are we admin?
|
|
|
|
if user.IsAdmin {
|
2022-08-21 21:05:08 +00:00
|
|
|
// Any pending certification photos or feedback?
|
2022-08-22 04:24:36 +00:00
|
|
|
countCertPhotos = models.CountCertificationPhotosNeedingApproval()
|
|
|
|
countFeedback = models.CountUnreadFeedback()
|
|
|
|
m["NavCertificationPhotos"] = countCertPhotos
|
|
|
|
m["NavAdminFeedback"] = countFeedback
|
2022-08-21 21:05:08 +00:00
|
|
|
|
|
|
|
// Total notification count for admin actions.
|
2022-08-22 04:24:36 +00:00
|
|
|
m["NavAdminNotifications"] = countCertPhotos + countFeedback
|
2022-08-14 23:27:57 +00:00
|
|
|
}
|
2022-08-22 04:24:36 +00:00
|
|
|
|
|
|
|
// Total count for user notifications.
|
2022-08-25 04:17:34 +00:00
|
|
|
m["NavTotalNotifications"] = countMessages + countFriendReqs + countNotifications + countCertPhotos + countFeedback
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
}
|