Skip to content

Commit ea75f6f

Browse files
committed
Update notification table with only latest data
When marking notifications read the results may be returned out of order or be delayed. This PR sends a sequence number to gitea so that the browser can ensure that only the results of the latest notification change are shown. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 251d7f5 commit ea75f6f

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

routers/web/user/notification.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func Notifications(c *context.Context) {
5050
return
5151
}
5252
if c.QueryBool("div-only") {
53+
c.Data["SequenceNumber"] = c.Query("sequence-number")
5354
c.HTML(http.StatusOK, tplNotificationDiv)
5455
return
5556
}
@@ -175,6 +176,7 @@ func NotificationStatusPost(c *context.Context) {
175176
return
176177
}
177178
c.Data["Link"] = setting.AppURL + "notifications"
179+
c.Data["SequenceNumber"] = c.Req.PostFormValue("sequence-number")
178180

179181
c.HTML(http.StatusOK, tplNotificationDiv)
180182
}

templates/user/notification/notification_div.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="page-content user notification" id="notification_div" data-params="{{.Page.GetParams}}">
1+
<div class="page-content user notification" id="notification_div" data-params="{{.Page.GetParams}}" data-sequence-number="{{.SequenceNumber}}">
22
<div class="ui container">
33
<h1 class="ui dividing header">{{.i18n.Tr "notification.notifications"}}</h1>
44
<div class="ui top attached tabular menu">

web_src/js/features/notification.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const {AppSubUrl, csrf, NotificationSettings} = window.config;
22

3+
let notificationSequenceNumber = 0;
4+
35
export function initNotificationsTable() {
46
$('#notification_table .button').on('click', async function () {
57
const data = await updateNotification(
@@ -10,8 +12,10 @@ export function initNotificationsTable() {
1012
$(this).data('notification-id'),
1113
);
1214

13-
$('#notification_div').replaceWith(data);
14-
initNotificationsTable();
15+
if ($(data).data('sequence-number') === notificationSequenceNumber) {
16+
$('#notification_div').replaceWith(data);
17+
initNotificationsTable();
18+
}
1519
await updateNotificationCount();
1620

1721
return false;
@@ -139,10 +143,13 @@ async function updateNotificationTable() {
139143
url: `${AppSubUrl}/notifications?${notificationDiv.data('params')}`,
140144
data: {
141145
'div-only': true,
146+
'sequence-number': ++notificationSequenceNumber,
142147
}
143148
});
144-
notificationDiv.replaceWith(data);
145-
initNotificationsTable();
149+
if ($(data).data('sequence-number') === notificationSequenceNumber) {
150+
notificationDiv.replaceWith(data);
151+
initNotificationsTable();
152+
}
146153
}
147154
}
148155

@@ -182,6 +189,7 @@ async function updateNotification(url, status, page, q, notificationID) {
182189
page,
183190
q,
184191
noredirect: true,
192+
'sequence-number': ++notificationSequenceNumber,
185193
},
186194
});
187195
}

0 commit comments

Comments
 (0)