2022-04-20 14:08:24 +05:30
|
|
|
'use strict';
|
2022-04-20 16:10:30 +05:30
|
|
|
var notification_data = JSON.parse(document.getElementById('notification_data').textContent);
|
2020-03-16 03:16:08 +05:30
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
/** Boolean meaning 'some tab have stream' */
|
|
|
|
const STORAGE_KEY_STREAM = 'stream';
|
|
|
|
/** Number of notifications. May be increased or reset */
|
|
|
|
const STORAGE_KEY_NOTIF_COUNT = 'notification_count';
|
|
|
|
|
2019-05-05 18:16:01 +05:30
|
|
|
var notifications, delivered;
|
2022-05-21 16:05:41 +05:30
|
|
|
var notifications_mock = { close: function () { } };
|
2022-05-06 07:16:59 +05:30
|
|
|
|
|
|
|
function get_subscriptions() {
|
|
|
|
helpers.xhr('GET', '/api/v1/auth/subscriptions?fields=authorId', {
|
|
|
|
retries: 5,
|
|
|
|
entity_name: 'subscriptions'
|
|
|
|
}, {
|
|
|
|
on200: create_notification_stream
|
|
|
|
});
|
2019-05-05 18:16:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function create_notification_stream(subscriptions) {
|
2022-05-06 07:16:59 +05:30
|
|
|
// sse.js can't be replaced to EventSource in place as it lack support of payload and headers
|
|
|
|
// see https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource
|
2019-05-05 18:16:01 +05:30
|
|
|
notifications = new SSE(
|
|
|
|
'/api/v1/auth/notifications?fields=videoId,title,author,authorId,publishedText,published,authorThumbnails,liveNow', {
|
|
|
|
withCredentials: true,
|
2022-04-20 14:35:19 +05:30
|
|
|
payload: 'topics=' + subscriptions.map(function (subscription) { return subscription.authorId; }).join(','),
|
2019-05-05 18:16:01 +05:30
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
|
|
|
|
});
|
|
|
|
delivered = [];
|
|
|
|
|
|
|
|
var start_time = Math.round(new Date() / 1000);
|
|
|
|
|
|
|
|
notifications.onmessage = function (event) {
|
2022-05-06 07:16:59 +05:30
|
|
|
if (!event.id) return;
|
2019-05-05 18:16:01 +05:30
|
|
|
|
|
|
|
var notification = JSON.parse(event.data);
|
2022-04-20 19:06:03 +05:30
|
|
|
console.info('Got notification:', notification);
|
2019-05-05 18:16:01 +05:30
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
// Ignore not actual and delivered notifications
|
|
|
|
if (start_time > notification.published || delivered.includes(notification.videoId)) return;
|
|
|
|
|
|
|
|
delivered.push(notification.videoId);
|
|
|
|
|
|
|
|
let notification_count = helpers.storage.get(STORAGE_KEY_NOTIF_COUNT) || 0;
|
|
|
|
notification_count++;
|
|
|
|
helpers.storage.set(STORAGE_KEY_NOTIF_COUNT, notification_count);
|
|
|
|
|
|
|
|
update_ticker_count();
|
|
|
|
|
2022-05-21 22:00:51 +05:30
|
|
|
// permission for notifications handled on settings page. JS handler is in handlers.js
|
2022-05-21 16:05:41 +05:30
|
|
|
if (window.Notification && Notification.permission === 'granted') {
|
|
|
|
var notification_text = notification.liveNow ? notification_data.live_now_text : notification_data.upload_text;
|
|
|
|
notification_text = notification_text.replace('`x`', notification.author);
|
2022-06-05 23:24:48 +05:30
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
var system_notification = new Notification(notification_text, {
|
|
|
|
body: notification.title,
|
|
|
|
icon: '/ggpht' + new URL(notification.authorThumbnails[2].url).pathname,
|
|
|
|
img: '/ggpht' + new URL(notification.authorThumbnails[4].url).pathname
|
|
|
|
});
|
2022-06-05 23:24:48 +05:30
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
system_notification.onclick = function (e) {
|
|
|
|
open('/watch?v=' + notification.videoId, '_blank');
|
2022-05-21 22:00:51 +05:30
|
|
|
};
|
2019-05-05 18:16:01 +05:30
|
|
|
}
|
2022-04-20 14:35:19 +05:30
|
|
|
};
|
2019-05-05 18:16:01 +05:30
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
notifications.addEventListener('error', function (e) {
|
|
|
|
console.warn('Something went wrong with notifications, trying to reconnect...');
|
|
|
|
notifications = notifications_mock;
|
|
|
|
setTimeout(get_subscriptions, 1000);
|
|
|
|
});
|
|
|
|
|
2019-05-05 18:16:01 +05:30
|
|
|
notifications.stream();
|
|
|
|
}
|
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
function update_ticker_count() {
|
|
|
|
var notification_ticker = document.getElementById('notification_ticker');
|
2019-06-17 04:41:34 +05:30
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
const notification_count = helpers.storage.get(STORAGE_KEY_STREAM);
|
|
|
|
if (notification_count > 0) {
|
|
|
|
notification_ticker.innerHTML =
|
|
|
|
'<span id="notification_count">' + notification_count + '</span> <i class="icon ion-ios-notifications"></i>';
|
2022-05-06 09:51:19 +05:30
|
|
|
} else {
|
2022-05-21 16:05:41 +05:30
|
|
|
notification_ticker.innerHTML =
|
|
|
|
'<i class="icon ion-ios-notifications-outline"></i>';
|
2022-05-06 09:51:19 +05:30
|
|
|
}
|
2022-05-21 16:05:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function start_stream_if_needed() {
|
|
|
|
// random wait for other tabs set 'stream' flag
|
|
|
|
setTimeout(function () {
|
|
|
|
if (!helpers.storage.get(STORAGE_KEY_STREAM)) {
|
|
|
|
// if no one set 'stream', set it by yourself and start stream
|
|
|
|
helpers.storage.set(STORAGE_KEY_STREAM, true);
|
|
|
|
notifications = notifications_mock;
|
|
|
|
get_subscriptions();
|
|
|
|
}
|
|
|
|
}, Math.random() * 1000 + 50); // [0.050 .. 1.050) second
|
|
|
|
}
|
2019-05-05 18:16:01 +05:30
|
|
|
|
2019-06-02 04:08:49 +05:30
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
addEventListener('storage', function (e) {
|
|
|
|
if (e.key === STORAGE_KEY_NOTIF_COUNT)
|
|
|
|
update_ticker_count();
|
|
|
|
|
|
|
|
// if 'stream' key was removed
|
|
|
|
if (e.key === STORAGE_KEY_STREAM && !helpers.storage.get(STORAGE_KEY_STREAM)) {
|
|
|
|
if (notifications) {
|
|
|
|
// restore it if we have active stream
|
|
|
|
helpers.storage.set(STORAGE_KEY_STREAM, true);
|
|
|
|
} else {
|
|
|
|
start_stream_if_needed();
|
2019-06-02 04:08:49 +05:30
|
|
|
}
|
2022-05-21 16:05:41 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addEventListener('load', function () {
|
|
|
|
var notification_count_el = document.getElementById('notification_count');
|
|
|
|
var notification_count = notification_count_el ? parseInt(notification_count_el.textContent) : 0;
|
|
|
|
helpers.storage.set(STORAGE_KEY_NOTIF_COUNT, notification_count);
|
|
|
|
|
|
|
|
if (helpers.storage.get(STORAGE_KEY_STREAM))
|
|
|
|
helpers.storage.remove(STORAGE_KEY_STREAM);
|
|
|
|
start_stream_if_needed();
|
2019-05-05 18:16:01 +05:30
|
|
|
});
|
|
|
|
|
2022-05-21 16:05:41 +05:30
|
|
|
addEventListener('unload', function () {
|
|
|
|
// let chance to other tabs to be a streamer via firing 'storage' event
|
|
|
|
if (notifications) helpers.storage.remove(STORAGE_KEY_STREAM);
|
2019-05-05 18:16:01 +05:30
|
|
|
});
|