2019-10-05 02:09:52 +02:00
|
|
|
/**
|
|
|
|
* Notifications
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { fetchJson, handleError } from './utils/requests';
|
2019-12-02 15:55:48 +01:00
|
|
|
import { $ } from './utils/dom';
|
2019-11-17 18:50:42 +01:00
|
|
|
import { delegate } from './utils/events';
|
2024-06-23 19:21:24 +02:00
|
|
|
import { assertNotNull, assertNotUndefined } from './utils/assert';
|
2019-10-05 02:09:52 +02:00
|
|
|
import store from './utils/store';
|
|
|
|
|
|
|
|
const NOTIFICATION_INTERVAL = 600000,
|
2024-07-03 22:54:14 +02:00
|
|
|
NOTIFICATION_EXPIRES = 300000;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2019-11-17 18:50:42 +01:00
|
|
|
function bindSubscriptionLinks() {
|
|
|
|
delegate(document, 'fetchcomplete', {
|
2024-07-03 23:03:46 +02:00
|
|
|
'.js-subscription-link': event => {
|
2024-06-23 19:21:24 +02:00
|
|
|
const target = assertNotNull(event.target.closest('.js-subscription-target'));
|
|
|
|
|
2024-07-03 23:03:46 +02:00
|
|
|
event.detail.text().then(text => {
|
2019-11-17 18:50:42 +01:00
|
|
|
target.outerHTML = text;
|
|
|
|
});
|
2024-07-03 22:54:14 +02:00
|
|
|
},
|
2019-11-17 18:50:42 +01:00
|
|
|
});
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getNewNotifications() {
|
|
|
|
if (document.hidden || !store.hasExpired('notificationCount')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:21:24 +02:00
|
|
|
fetchJson('GET', '/notifications/unread')
|
|
|
|
.then(handleError)
|
2024-07-03 23:03:46 +02:00
|
|
|
.then(response => response.json())
|
2024-06-23 19:21:24 +02:00
|
|
|
.then(({ notifications }) => {
|
|
|
|
updateNotificationTicker(notifications);
|
|
|
|
storeNotificationCount(notifications);
|
2020-01-13 14:14:16 +01:00
|
|
|
|
2024-06-23 19:21:24 +02:00
|
|
|
setTimeout(getNewNotifications, NOTIFICATION_INTERVAL);
|
|
|
|
});
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:21:24 +02:00
|
|
|
function updateNotificationTicker(notificationCount: string | null) {
|
|
|
|
const ticker = assertNotNull($<HTMLSpanElement>('.js-notification-ticker'));
|
2019-10-05 02:09:52 +02:00
|
|
|
const parsedNotificationCount = Number(notificationCount);
|
|
|
|
|
2024-06-23 19:21:24 +02:00
|
|
|
ticker.dataset.notificationCount = parsedNotificationCount.toString();
|
|
|
|
ticker.textContent = parsedNotificationCount.toString();
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-23 19:21:24 +02:00
|
|
|
function storeNotificationCount(notificationCount: string) {
|
2019-10-05 02:09:52 +02:00
|
|
|
// The current number of notifications are stored along with the time when the data expires
|
|
|
|
store.setWithExpireTime('notificationCount', notificationCount, NOTIFICATION_EXPIRES);
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:21:24 +02:00
|
|
|
export function setupNotifications() {
|
2019-10-05 02:09:52 +02:00
|
|
|
if (!window.booru.userIsSignedIn) return;
|
|
|
|
|
|
|
|
// Fetch notifications from the server at a regular interval
|
2020-01-13 14:14:16 +01:00
|
|
|
setTimeout(getNewNotifications, NOTIFICATION_INTERVAL);
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// Update the current number of notifications based on the latest page load
|
2024-06-23 19:21:24 +02:00
|
|
|
const ticker = assertNotNull($<HTMLSpanElement>('.js-notification-ticker'));
|
|
|
|
storeNotificationCount(assertNotUndefined(ticker.dataset.notificationCount));
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// Update ticker when the stored value changes - this will occur in all open tabs
|
|
|
|
store.watch('notificationCount', updateNotificationTicker);
|
2019-11-17 18:50:42 +01:00
|
|
|
|
|
|
|
bindSubscriptionLinks();
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|