2020-04-24 09:27:38 +05:30
|
|
|
const {AppSubUrl, csrf, NotificationSettings} = window.config;
|
|
|
|
|
|
|
|
export function initNotificationsTable() {
|
|
|
|
$('#notification_table .button').on('click', async function () {
|
|
|
|
const data = await updateNotification(
|
|
|
|
$(this).data('url'),
|
|
|
|
$(this).data('status'),
|
|
|
|
$(this).data('page'),
|
|
|
|
$(this).data('q'),
|
|
|
|
$(this).data('notification-id'),
|
|
|
|
);
|
|
|
|
|
|
|
|
$('#notification_div').replaceWith(data);
|
|
|
|
initNotificationsTable();
|
|
|
|
await updateNotificationCount();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-03 15:25:36 +05:30
|
|
|
async function receiveUpdateCount(event) {
|
|
|
|
try {
|
|
|
|
const data = JSON.parse(event.data);
|
|
|
|
|
|
|
|
const notificationCount = document.querySelector('.notification_count');
|
|
|
|
if (data.Count > 0) {
|
|
|
|
notificationCount.classList.remove('hidden');
|
|
|
|
} else {
|
|
|
|
notificationCount.classList.add('hidden');
|
|
|
|
}
|
|
|
|
|
2020-07-05 03:34:00 +05:30
|
|
|
notificationCount.textContent = `${data.Count}`;
|
2020-07-03 15:25:36 +05:30
|
|
|
await updateNotificationTable();
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function initNotificationCount() {
|
2020-05-08 03:19:00 +05:30
|
|
|
const notificationCount = $('.notification_count');
|
|
|
|
|
|
|
|
if (!notificationCount.length) {
|
2020-04-24 09:27:38 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-08 03:19:00 +05:30
|
|
|
if (NotificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource) {
|
2020-07-03 15:25:36 +05:30
|
|
|
// Try to connect to the event source via the shared worker first
|
|
|
|
if (window.SharedWorker) {
|
|
|
|
const worker = new SharedWorker(`${__webpack_public_path__}js/eventsource.sharedworker.js`, 'notification-worker');
|
|
|
|
worker.addEventListener('error', (event) => {
|
|
|
|
console.error(event);
|
|
|
|
});
|
|
|
|
worker.port.onmessageerror = () => {
|
|
|
|
console.error('Unable to deserialize message');
|
|
|
|
};
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'start',
|
|
|
|
url: `${window.location.origin}${AppSubUrl}/user/events`,
|
|
|
|
});
|
2020-07-04 19:31:25 +05:30
|
|
|
worker.port.addEventListener('message', (event) => {
|
|
|
|
if (!event.data || !event.data.type) {
|
|
|
|
console.error(event);
|
2020-07-03 15:25:36 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.data.type === 'notification-count') {
|
2020-07-04 19:31:25 +05:30
|
|
|
receiveUpdateCount(event.data);
|
2020-07-03 15:25:36 +05:30
|
|
|
} else if (event.data.type === 'error') {
|
2020-07-04 19:31:25 +05:30
|
|
|
console.error(event.data);
|
2020-07-03 15:25:36 +05:30
|
|
|
} else if (event.data.type === 'logout') {
|
2020-07-04 19:31:25 +05:30
|
|
|
if (event.data !== 'here') {
|
2020-07-03 15:25:36 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
|
|
|
window.location.href = AppSubUrl;
|
2020-05-08 03:19:00 +05:30
|
|
|
}
|
2020-07-03 15:25:36 +05:30
|
|
|
});
|
|
|
|
worker.port.addEventListener('error', (e) => {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
worker.port.start();
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
|
|
worker.port.postMessage({
|
|
|
|
type: 'close',
|
|
|
|
});
|
|
|
|
worker.port.close();
|
|
|
|
});
|
2020-04-24 09:27:38 +05:30
|
|
|
|
2020-07-03 15:25:36 +05:30
|
|
|
return;
|
|
|
|
}
|
2020-04-24 09:27:38 +05:30
|
|
|
}
|
2020-05-08 03:19:00 +05:30
|
|
|
|
|
|
|
if (NotificationSettings.MinTimeout <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn = (timeout, lastCount) => {
|
|
|
|
setTimeout(async () => {
|
|
|
|
await updateNotificationCountWithCallback(fn, timeout, lastCount);
|
|
|
|
}, timeout);
|
|
|
|
};
|
|
|
|
|
|
|
|
fn(NotificationSettings.MinTimeout, notificationCount.text());
|
2020-04-24 09:27:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNotificationCountWithCallback(callback, timeout, lastCount) {
|
|
|
|
const currentCount = $('.notification_count').text();
|
|
|
|
if (lastCount !== currentCount) {
|
|
|
|
callback(NotificationSettings.MinTimeout, currentCount);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newCount = await updateNotificationCount();
|
|
|
|
let needsUpdate = false;
|
|
|
|
|
|
|
|
if (lastCount !== newCount) {
|
|
|
|
needsUpdate = true;
|
|
|
|
timeout = NotificationSettings.MinTimeout;
|
|
|
|
} else if (timeout < NotificationSettings.MaxTimeout) {
|
|
|
|
timeout += NotificationSettings.TimeoutStep;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(timeout, newCount);
|
2020-05-08 03:19:00 +05:30
|
|
|
if (needsUpdate) {
|
|
|
|
await updateNotificationTable();
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 09:27:38 +05:30
|
|
|
|
2020-05-08 03:19:00 +05:30
|
|
|
async function updateNotificationTable() {
|
2020-04-24 09:27:38 +05:30
|
|
|
const notificationDiv = $('#notification_div');
|
2020-05-08 03:19:00 +05:30
|
|
|
if (notificationDiv.length > 0) {
|
2020-04-24 09:27:38 +05:30
|
|
|
const data = await $.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url: `${AppSubUrl}/notifications?${notificationDiv.data('params')}`,
|
|
|
|
data: {
|
|
|
|
'div-only': true,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
notificationDiv.replaceWith(data);
|
|
|
|
initNotificationsTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNotificationCount() {
|
|
|
|
const data = await $.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url: `${AppSubUrl}/api/v1/notifications/new`,
|
|
|
|
headers: {
|
|
|
|
'X-Csrf-Token': csrf,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const notificationCount = $('.notification_count');
|
|
|
|
if (data.new === 0) {
|
|
|
|
notificationCount.addClass('hidden');
|
|
|
|
} else {
|
|
|
|
notificationCount.removeClass('hidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationCount.text(`${data.new}`);
|
|
|
|
|
|
|
|
return `${data.new}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNotification(url, status, page, q, notificationID) {
|
|
|
|
if (status !== 'pinned') {
|
|
|
|
$(`#notification_${notificationID}`).remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url,
|
|
|
|
data: {
|
|
|
|
_csrf: csrf,
|
|
|
|
notification_id: notificationID,
|
|
|
|
status,
|
|
|
|
page,
|
|
|
|
q,
|
|
|
|
noredirect: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|