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
|
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
|
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.
|
|
|
|
if count, err := models.CountUnreadMessages(user.ID); err == nil {
|
|
|
|
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.
|
|
|
|
if count, err := models.CountUnreadNotifications(user.ID); err == nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|