Use AJAX for notifications table (#10961)

* Use AJAX for notifications table

Signed-off-by: Andrew Thornton <art27@cantab.net>

* move to separate js

Signed-off-by: Andrew Thornton <art27@cantab.net>

* placate golangci-lint

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Add autoupdating notification count

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Fix wipeall

Signed-off-by: Andrew Thornton <art27@cantab.net>

* placate tests

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Try hidden

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Try hide and hidden

Signed-off-by: Andrew Thornton <art27@cantab.net>

* More auto-update improvements

Only run checker on pages that have a count
Change starting checker to 10s with a back-off to 60s if there is no change

Signed-off-by: Andrew Thornton <art27@cantab.net>

* string comparison!

Signed-off-by: Andrew Thornton <art27@cantab.net>

* as per @silverwind

Signed-off-by: Andrew Thornton <art27@cantab.net>

* add configurability as per @6543

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Add documentation as per @6543

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Use CSRF header not query

Signed-off-by: Andrew Thornton <art27@cantab.net>

* Further JS improvements

Fix @etzelia update notification table request
Fix @silverwind comments

Co-Authored-By: silverwind <me@silverwind.io>
Signed-off-by: Andrew Thornton <art27@cantab.net>

* Simplify the notification count fns

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
zeripath 2020-04-24 04:57:38 +01:00 committed by GitHub
parent e74c4e1be9
commit b10c416f9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 331 additions and 140 deletions

View file

@ -7,6 +7,7 @@ package user
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
@ -17,7 +18,8 @@ import (
)
const (
tplNotification base.TplName = "user/notification/notification"
tplNotification base.TplName = "user/notification/notification"
tplNotificationDiv base.TplName = "user/notification/notification_div"
)
// GetNotificationCount is the middleware that sets the notification count in the context
@ -30,17 +32,31 @@ func GetNotificationCount(c *context.Context) {
return
}
count, err := models.GetNotificationCount(c.User, models.NotificationStatusUnread)
if err != nil {
c.ServerError("GetNotificationCount", err)
return
}
c.Data["NotificationUnreadCount"] = func() int64 {
count, err := models.GetNotificationCount(c.User, models.NotificationStatusUnread)
if err != nil {
c.ServerError("GetNotificationCount", err)
return -1
}
c.Data["NotificationUnreadCount"] = count
return count
}
}
// Notifications is the notifications page
func Notifications(c *context.Context) {
getNotifications(c)
if c.Written() {
return
}
if c.QueryBool("div-only") {
c.HTML(http.StatusOK, tplNotificationDiv)
return
}
c.HTML(http.StatusOK, tplNotification)
}
func getNotifications(c *context.Context) {
var (
keyword = strings.Trim(c.Query("q"), " ")
status models.NotificationStatus
@ -115,19 +131,13 @@ func Notifications(c *context.Context) {
c.Flash.Error(fmt.Sprintf("ERROR: %d notifications were removed due to missing parts - check the logs", failCount))
}
title := c.Tr("notifications")
if status == models.NotificationStatusUnread && total > 0 {
title = fmt.Sprintf("(%d) %s", total, title)
}
c.Data["Title"] = title
c.Data["Title"] = c.Tr("notifications")
c.Data["Keyword"] = keyword
c.Data["Status"] = status
c.Data["Notifications"] = notifications
pager.SetDefaultParams(c)
c.Data["Page"] = pager
c.HTML(200, tplNotification)
}
// NotificationStatusPost is a route for changing the status of a notification
@ -155,8 +165,17 @@ func NotificationStatusPost(c *context.Context) {
return
}
url := fmt.Sprintf("%s/notifications?page=%s", setting.AppSubURL, c.Query("page"))
c.Redirect(url, 303)
if !c.QueryBool("noredirect") {
url := fmt.Sprintf("%s/notifications?page=%s", setting.AppSubURL, c.Query("page"))
c.Redirect(url, http.StatusSeeOther)
}
getNotifications(c)
if c.Written() {
return
}
c.HTML(http.StatusOK, tplNotificationDiv)
}
// NotificationPurgePost is a route for 'purging' the list of notifications - marking all unread as read
@ -168,5 +187,5 @@ func NotificationPurgePost(c *context.Context) {
}
url := fmt.Sprintf("%s/notifications", setting.AppSubURL)
c.Redirect(url, 303)
c.Redirect(url, http.StatusSeeOther)
}