Skip to content

Commit 51e86e5

Browse files
zeripath6543
authored andcommitted
Update notification table with only latest data (go-gitea#16445)
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 <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
1 parent bc48b96 commit 51e86e5

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

Diff for: routers/web/user/notification.go

+2
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
}

Diff for: templates/user/notification/notification_div.tmpl

+1-1
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">

Diff for: web_src/js/features/notification.js

+12-4
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)