2022-08-10 05:10:47 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/config"
|
2022-08-14 00:42:42 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/log"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/models"
|
2022-08-10 05:10:47 +00:00
|
|
|
"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
|
|
|
|
}
|
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 00:42:42 +00:00
|
|
|
m["NavUnreadMessages"] = 0
|
2022-08-14 05:44:57 +00:00
|
|
|
m["NavFriendRequests"] = 0
|
2022-08-14 23:27:57 +00:00
|
|
|
m["NavAdminNotifications"] = 0 // total count of admin notifications for nav
|
|
|
|
m["NavCertificationPhotos"] = 0 // admin indicator for certification photos
|
2022-08-21 21:05:08 +00:00
|
|
|
m["NavAdminFeedback"] = 0 // admin indicator for unread feedback
|
2022-08-14 23:27:57 +00:00
|
|
|
m["SessionImpersonated"] = false
|
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
|
|
|
|
|
|
|
// Get unread message count.
|
|
|
|
if count, err := models.CountUnreadMessages(user.ID); err == nil {
|
|
|
|
m["NavUnreadMessages"] = count
|
|
|
|
} 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
|
|
|
|
} 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?
|
|
|
|
var (
|
|
|
|
certPhotos = models.CountCertificationPhotosNeedingApproval()
|
|
|
|
feedback = models.CountUnreadFeedback()
|
|
|
|
)
|
|
|
|
m["NavCertificationPhotos"] = certPhotos
|
|
|
|
m["NavAdminFeedback"] = feedback
|
|
|
|
|
|
|
|
// Total notification count for admin actions.
|
|
|
|
m["NavAdminNotifications"] = certPhotos + feedback
|
2022-08-14 23:27:57 +00:00
|
|
|
}
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
}
|