Skip to content

Commit b354cf3

Browse files
committed
Add pagination for notifications
1 parent 545ba2e commit b354cf3

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

models/notification.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,20 @@ func getIssueNotification(e Engine, userID, issueID int64) (*Notification, error
182182
}
183183

184184
// NotificationsForUser returns notifications for a given user and status
185-
func NotificationsForUser(user *User, status NotificationStatus) ([]*Notification, error) {
186-
return notificationsForUser(x, user, status)
185+
func NotificationsForUser(user *User, status NotificationStatus, page, perPage int) ([]*Notification, error) {
186+
return notificationsForUser(x, user, status, page, perPage)
187187
}
188-
func notificationsForUser(e Engine, user *User, status NotificationStatus) (notifications []*Notification, err error) {
189-
err = e.
188+
func notificationsForUser(e Engine, user *User, status NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
189+
sess := e.
190190
Where("user_id = ?", user.ID).
191191
And("status = ?", status).
192-
OrderBy("updated_unix DESC").
192+
OrderBy("updated_unix DESC")
193+
194+
if page > 0 && perPage > 0 {
195+
sess.Limit(perPage, (page-1)*perPage)
196+
}
197+
198+
err = sess.
193199
Find(&notifications)
194200
return
195201
}

routers/user/notification.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/Unknwon/paginater"
8+
79
"code.gitea.io/gitea/models"
810
"code.gitea.io/gitea/modules/base"
911
"code.gitea.io/gitea/modules/context"
@@ -34,26 +36,46 @@ func GetNotificationCount(c *context.Context) {
3436

3537
// Notifications is the notifications page
3638
func Notifications(c *context.Context) {
37-
var status models.NotificationStatus
38-
switch c.Query("status") {
39+
var (
40+
keyword = c.Query("q")
41+
status models.NotificationStatus
42+
page = c.QueryInt("page")
43+
perPage = c.QueryInt("perPage")
44+
)
45+
if page < 1 {
46+
page = 1
47+
}
48+
if perPage < 1 {
49+
perPage = 20
50+
}
51+
52+
switch keyword {
3953
case "read":
4054
status = models.NotificationStatusRead
4155
default:
4256
status = models.NotificationStatusUnread
4357
}
4458

45-
notifications, err := models.NotificationsForUser(c.User, status)
59+
notifications, err := models.NotificationsForUser(c.User, status, page, perPage)
4660
if err != nil {
4761
c.Handle(500, "ErrNotificationsForUser", err)
4862
return
4963
}
5064

65+
total, err := models.GetNotificationCount(c.User, status)
66+
if err != nil {
67+
c.Handle(500, "ErrGetNotificationCount", err)
68+
return
69+
}
70+
5171
title := "Notifications"
5272
if count := len(notifications); count > 0 {
5373
title = fmt.Sprintf("(%d) %s", count, title)
5474
}
5575
c.Data["Title"] = title
76+
c.Data["Keyword"] = keyword
5677
c.Data["Status"] = status
5778
c.Data["Notifications"] = notifications
79+
c.Data["Page"] = paginater.New(int(total), perPage, page, 5)
5880
c.HTML(200, tplNotification)
5981
}

templates/user/notification/notification.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
<h1 class="ui header">{{.i18n.Tr "notification.notifications"}}</h1>
66

77
<div class="ui top attached tabular menu">
8-
<a href="/notifications?status=unread">
8+
<a href="/notifications?q=unread">
99
<div class="{{if eq .Status 1}}active{{end}} item">
1010
{{.i18n.Tr "notification.unread"}}
1111
{{if eq .Status 1}}
1212
<div class="ui label">{{len .Notifications}}</div>
1313
{{end}}
1414
</div>
1515
</a>
16-
<a href="/notifications?status=read">
16+
<a href="/notifications?q=read">
1717
<div class="{{if eq .Status 2}}active{{end}} item">
1818
{{.i18n.Tr "notification.read"}}
1919
{{if eq .Status 2}}
@@ -62,6 +62,8 @@
6262
</div>
6363
{{end}}
6464
</div>
65+
66+
{{template "base/paginate" .}}
6567
</div>
6668
</div>
6769

0 commit comments

Comments
 (0)