2022-01-28 21:00:11 +00:00
|
|
|
import $ from 'jquery';
|
2024-07-07 15:32:30 +00:00
|
|
|
import {GET} from '../modules/fetch.ts';
|
|
|
|
import {toggleElem} from '../utils/dom.ts';
|
|
|
|
import {logoutFromWorker} from '../modules/worker.ts';
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2024-03-16 01:56:17 +00:00
|
|
|
const {appSubUrl, notificationSettings, assetVersionEncoded} = window.config;
|
2021-07-17 16:18:10 +00:00
|
|
|
let notificationSequenceNumber = 0;
|
|
|
|
|
2020-04-24 03:57:38 +00:00
|
|
|
export function initNotificationsTable() {
|
2024-06-10 20:49:33 +00:00
|
|
|
const table = document.querySelector('#notification_table');
|
2023-05-25 02:31:26 +00:00
|
|
|
if (!table) return;
|
|
|
|
|
|
|
|
// when page restores from bfcache, delete previously clicked items
|
|
|
|
window.addEventListener('pageshow', (e) => {
|
|
|
|
if (e.persisted) { // page was restored from bfcache
|
2024-06-10 20:49:33 +00:00
|
|
|
const table = document.querySelector('#notification_table');
|
2024-12-15 21:02:32 +00:00
|
|
|
const unreadCountEl = document.querySelector<HTMLElement>('.notifications-unread-count');
|
2023-05-25 02:31:26 +00:00
|
|
|
let unreadCount = parseInt(unreadCountEl.textContent);
|
|
|
|
for (const item of table.querySelectorAll('.notifications-item[data-remove="true"]')) {
|
|
|
|
item.remove();
|
|
|
|
unreadCount -= 1;
|
|
|
|
}
|
2024-12-15 21:02:32 +00:00
|
|
|
unreadCountEl.textContent = String(unreadCount);
|
2023-05-25 02:31:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// mark clicked unread links for deletion on bfcache restore
|
|
|
|
for (const link of table.querySelectorAll('.notifications-item[data-status="1"] .notifications-link')) {
|
2024-12-15 21:02:32 +00:00
|
|
|
link.addEventListener('click', (e : MouseEvent & {target: HTMLElement}) => {
|
2023-05-25 02:31:26 +00:00
|
|
|
e.target.closest('.notifications-item').setAttribute('data-remove', 'true');
|
|
|
|
});
|
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 21:02:32 +00:00
|
|
|
async function receiveUpdateCount(event: MessageEvent) {
|
2020-07-03 09:55:36 +00:00
|
|
|
try {
|
|
|
|
const data = JSON.parse(event.data);
|
|
|
|
|
2022-08-03 19:58:27 +00:00
|
|
|
for (const count of document.querySelectorAll('.notification_count')) {
|
2024-03-24 18:23:38 +00:00
|
|
|
count.classList.toggle('tw-hidden', data.Count === 0);
|
2022-08-03 19:58:27 +00:00
|
|
|
count.textContent = `${data.Count}`;
|
2020-07-03 09:55:36 +00:00
|
|
|
}
|
|
|
|
await updateNotificationTable();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 09:27:25 +00:00
|
|
|
export function initNotificationCount() {
|
2024-06-10 10:12:31 +00:00
|
|
|
if (!document.querySelector('.notification_count')) return;
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2022-08-03 19:58:27 +00:00
|
|
|
let usingPeriodicPoller = false;
|
2024-12-15 21:02:32 +00:00
|
|
|
const startPeriodicPoller = (timeout: number, lastCount?: number) => {
|
2022-08-03 19:58:27 +00:00
|
|
|
if (timeout <= 0 || !Number.isFinite(timeout)) return;
|
|
|
|
usingPeriodicPoller = true;
|
2024-06-10 10:12:31 +00:00
|
|
|
lastCount = lastCount ?? getCurrentCount();
|
2022-08-03 19:58:27 +00:00
|
|
|
setTimeout(async () => {
|
|
|
|
await updateNotificationCountWithCallback(startPeriodicPoller, timeout, lastCount);
|
|
|
|
}, timeout);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (notificationSettings.EventSourceUpdateTime > 0 && window.EventSource && window.SharedWorker) {
|
2020-07-03 09:55:36 +00:00
|
|
|
// Try to connect to the event source via the shared worker first
|
2022-08-23 12:58:04 +00:00
|
|
|
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js?v=${assetVersionEncoded}`, 'notification-worker');
|
2020-12-27 14:24:27 +00:00
|
|
|
worker.addEventListener('error', (event) => {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('worker error', event);
|
2020-12-27 14:24:27 +00:00
|
|
|
});
|
2021-08-17 05:32:48 +00:00
|
|
|
worker.port.addEventListener('messageerror', () => {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('unable to deserialize message');
|
2021-08-17 05:32:48 +00:00
|
|
|
});
|
2020-12-27 14:24:27 +00:00
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'start',
|
2021-10-21 07:37:43 +00:00
|
|
|
url: `${window.location.origin}${appSubUrl}/user/events`,
|
2020-12-27 14:24:27 +00:00
|
|
|
});
|
2024-12-15 21:02:32 +00:00
|
|
|
worker.port.addEventListener('message', (event: MessageEvent) => {
|
2020-12-27 14:24:27 +00:00
|
|
|
if (!event.data || !event.data.type) {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('unknown worker message event', event);
|
2020-12-27 14:24:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.data.type === 'notification-count') {
|
2024-12-15 21:02:32 +00:00
|
|
|
receiveUpdateCount(event); // no await
|
2022-08-03 19:58:27 +00:00
|
|
|
} else if (event.data.type === 'no-event-source') {
|
|
|
|
// browser doesn't support EventSource, falling back to periodic poller
|
|
|
|
if (!usingPeriodicPoller) startPeriodicPoller(notificationSettings.MinTimeout);
|
2020-12-27 14:24:27 +00:00
|
|
|
} else if (event.data.type === 'error') {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('worker port event error', event.data);
|
2020-12-27 14:24:27 +00:00
|
|
|
} else if (event.data.type === 'logout') {
|
2021-04-07 23:48:13 +00:00
|
|
|
if (event.data.data !== 'here') {
|
2020-07-03 09:55:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
2024-04-30 15:35:42 +00:00
|
|
|
logoutFromWorker();
|
2021-04-04 21:37:50 +00:00
|
|
|
} else if (event.data.type === 'close') {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
2020-12-27 14:24:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
worker.port.addEventListener('error', (e) => {
|
2022-08-03 19:58:27 +00:00
|
|
|
console.error('worker port error', e);
|
2020-12-27 14:24:27 +00:00
|
|
|
});
|
|
|
|
worker.port.start();
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
2020-07-03 09:55:36 +00:00
|
|
|
});
|
2020-12-27 14:24:27 +00:00
|
|
|
worker.port.close();
|
|
|
|
});
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2020-12-27 14:24:27 +00:00
|
|
|
return;
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
2020-05-07 21:49:00 +00:00
|
|
|
|
2022-08-03 19:58:27 +00:00
|
|
|
startPeriodicPoller(notificationSettings.MinTimeout);
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 10:12:31 +00:00
|
|
|
function getCurrentCount() {
|
2024-12-15 21:02:32 +00:00
|
|
|
return Number(document.querySelector('.notification_count').textContent ?? '0');
|
2024-06-10 10:12:31 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 21:02:32 +00:00
|
|
|
async function updateNotificationCountWithCallback(callback: (timeout: number, newCount: number) => void, timeout: number, lastCount: number) {
|
2024-06-10 10:12:31 +00:00
|
|
|
const currentCount = getCurrentCount();
|
2020-04-24 03:57:38 +00:00
|
|
|
if (lastCount !== currentCount) {
|
2021-10-21 07:37:43 +00:00
|
|
|
callback(notificationSettings.MinTimeout, currentCount);
|
2020-04-24 03:57:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newCount = await updateNotificationCount();
|
|
|
|
let needsUpdate = false;
|
|
|
|
|
|
|
|
if (lastCount !== newCount) {
|
|
|
|
needsUpdate = true;
|
2021-10-21 07:37:43 +00:00
|
|
|
timeout = notificationSettings.MinTimeout;
|
|
|
|
} else if (timeout < notificationSettings.MaxTimeout) {
|
|
|
|
timeout += notificationSettings.TimeoutStep;
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(timeout, newCount);
|
2020-05-07 21:49:00 +00:00
|
|
|
if (needsUpdate) {
|
|
|
|
await updateNotificationTable();
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2020-05-07 21:49:00 +00:00
|
|
|
async function updateNotificationTable() {
|
2024-06-10 20:49:33 +00:00
|
|
|
const notificationDiv = document.querySelector('#notification_div');
|
2024-03-16 13:25:27 +00:00
|
|
|
if (notificationDiv) {
|
2024-03-16 01:56:17 +00:00
|
|
|
try {
|
|
|
|
const params = new URLSearchParams(window.location.search);
|
2024-12-15 21:02:32 +00:00
|
|
|
params.set('div-only', String(true));
|
|
|
|
params.set('sequence-number', String(++notificationSequenceNumber));
|
|
|
|
const response = await GET(`${appSubUrl}/notifications?${params.toString()}`);
|
2024-03-16 01:56:17 +00:00
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failed to fetch notification table');
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
2024-03-16 01:56:17 +00:00
|
|
|
|
|
|
|
const data = await response.text();
|
|
|
|
if ($(data).data('sequence-number') === notificationSequenceNumber) {
|
2024-03-16 13:25:27 +00:00
|
|
|
notificationDiv.outerHTML = data;
|
2024-03-16 01:56:17 +00:00
|
|
|
initNotificationsTable();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2021-07-17 16:18:10 +00:00
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-15 21:02:32 +00:00
|
|
|
async function updateNotificationCount(): Promise<number> {
|
2024-03-16 01:56:17 +00:00
|
|
|
try {
|
|
|
|
const response = await GET(`${appSubUrl}/notifications/new`);
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2024-03-16 01:56:17 +00:00
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failed to fetch notification count');
|
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2024-03-16 01:56:17 +00:00
|
|
|
const data = await response.json();
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2024-03-29 17:17:21 +00:00
|
|
|
toggleElem('.notification_count', data.new !== 0);
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2024-06-10 20:49:33 +00:00
|
|
|
for (const el of document.querySelectorAll('.notification_count')) {
|
2024-03-29 17:17:21 +00:00
|
|
|
el.textContent = `${data.new}`;
|
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
|
2024-12-15 21:02:32 +00:00
|
|
|
return data.new as number;
|
2024-03-16 01:56:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2024-12-15 21:02:32 +00:00
|
|
|
return 0;
|
2024-03-16 01:56:17 +00:00
|
|
|
}
|
2020-04-24 03:57:38 +00:00
|
|
|
}
|