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
|
|
|
|
2019-05-05 18:16:01 +05:30
|
|
|
var notifications, delivered;
|
2022-05-06 07:16:59 +05:30
|
|
|
var notifications_substitution = { close: function () { } };
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (start_time < notification.published && !delivered.includes(notification.videoId)) {
|
|
|
|
if (Notification.permission === 'granted') {
|
|
|
|
var system_notification =
|
|
|
|
new Notification((notification.liveNow ? notification_data.live_now_text : notification_data.upload_text).replace('`x`', notification.author), {
|
|
|
|
body: notification.title,
|
|
|
|
icon: '/ggpht' + new URL(notification.authorThumbnails[2].url).pathname,
|
|
|
|
img: '/ggpht' + new URL(notification.authorThumbnails[4].url).pathname,
|
|
|
|
tag: notification.videoId
|
|
|
|
});
|
|
|
|
|
|
|
|
system_notification.onclick = function (event) {
|
2022-05-06 07:16:59 +05:30
|
|
|
open('/watch?v=' + event.currentTarget.tag, '_blank');
|
2022-04-20 14:35:19 +05:30
|
|
|
};
|
2019-05-05 18:16:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
delivered.push(notification.videoId);
|
2022-05-06 09:51:19 +05:30
|
|
|
helpers.storage.set('notification_count', (helpers.storage.get('notification_count') || 0) + 1);
|
2019-05-05 18:16:01 +05:30
|
|
|
var notification_ticker = document.getElementById('notification_ticker');
|
|
|
|
|
2022-05-06 07:16:59 +05:30
|
|
|
if (parseInt(helpers.storage.get('notification_count')) > 0) {
|
2019-05-05 18:16:01 +05:30
|
|
|
notification_ticker.innerHTML =
|
2022-05-06 07:16:59 +05:30
|
|
|
'<span id="notification_count">' + helpers.storage.get('notification_count') + '</span> <i class="icon ion-ios-notifications"></i>';
|
2019-05-05 18:16:01 +05:30
|
|
|
} else {
|
|
|
|
notification_ticker.innerHTML =
|
|
|
|
'<i class="icon ion-ios-notifications-outline"></i>';
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 14:35:19 +05:30
|
|
|
};
|
2019-05-05 18:16:01 +05:30
|
|
|
|
2019-06-17 04:41:34 +05:30
|
|
|
notifications.addEventListener('error', handle_notification_error);
|
2019-05-05 18:16:01 +05:30
|
|
|
notifications.stream();
|
|
|
|
}
|
|
|
|
|
2019-06-17 04:41:34 +05:30
|
|
|
function handle_notification_error(event) {
|
2022-04-20 19:06:03 +05:30
|
|
|
console.warn('Something went wrong with notifications, trying to reconnect...');
|
2022-05-06 07:16:59 +05:30
|
|
|
notifications = notifications_substitution;
|
|
|
|
setTimeout(get_subscriptions, 1000);
|
2019-06-17 04:41:34 +05:30
|
|
|
}
|
|
|
|
|
2022-05-06 07:16:59 +05:30
|
|
|
addEventListener('load', function (e) {
|
2022-05-06 09:51:19 +05:30
|
|
|
var notification_count = document.getElementById('notification_count');
|
|
|
|
if (notification_count) {
|
|
|
|
helpers.storage.set('notification_count', parseInt(notification_count.innerText));
|
|
|
|
} else {
|
|
|
|
helpers.storage.set('notification_count', 0);
|
|
|
|
}
|
2019-05-05 18:16:01 +05:30
|
|
|
|
2022-05-06 07:16:59 +05:30
|
|
|
if (helpers.storage.get('stream')) {
|
|
|
|
helpers.storage.remove('stream');
|
2019-05-05 18:16:01 +05:30
|
|
|
} else {
|
|
|
|
setTimeout(function () {
|
2022-05-06 07:16:59 +05:30
|
|
|
if (!helpers.storage.get('stream')) {
|
|
|
|
notifications = notifications_substitution;
|
|
|
|
helpers.storage.set('stream', true);
|
|
|
|
get_subscriptions();
|
2019-05-05 18:16:01 +05:30
|
|
|
}
|
2019-06-16 23:04:00 +05:30
|
|
|
}, Math.random() * 1000 + 50);
|
2019-05-05 18:16:01 +05:30
|
|
|
}
|
2019-06-02 04:08:49 +05:30
|
|
|
|
2022-05-06 07:16:59 +05:30
|
|
|
addEventListener('storage', function (e) {
|
2019-06-02 04:08:49 +05:30
|
|
|
if (e.key === 'stream' && !e.newValue) {
|
|
|
|
if (notifications) {
|
2022-05-06 07:16:59 +05:30
|
|
|
helpers.storage.set('stream', true);
|
2019-06-02 04:08:49 +05:30
|
|
|
} else {
|
|
|
|
setTimeout(function () {
|
2022-05-06 07:16:59 +05:30
|
|
|
if (!helpers.storage.get('stream')) {
|
|
|
|
notifications = notifications_substitution;
|
|
|
|
helpers.storage.set('stream', true);
|
|
|
|
get_subscriptions();
|
2019-06-02 04:08:49 +05:30
|
|
|
}
|
2019-06-16 23:04:00 +05:30
|
|
|
}, Math.random() * 1000 + 50);
|
2019-06-02 04:08:49 +05:30
|
|
|
}
|
|
|
|
} else if (e.key === 'notification_count') {
|
|
|
|
var notification_ticker = document.getElementById('notification_ticker');
|
|
|
|
|
|
|
|
if (parseInt(e.newValue) > 0) {
|
|
|
|
notification_ticker.innerHTML =
|
|
|
|
'<span id="notification_count">' + e.newValue + '</span> <i class="icon ion-ios-notifications"></i>';
|
|
|
|
} else {
|
|
|
|
notification_ticker.innerHTML =
|
|
|
|
'<i class="icon ion-ios-notifications-outline"></i>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-05-05 18:16:01 +05:30
|
|
|
});
|
|
|
|
|
2022-05-06 07:16:59 +05:30
|
|
|
addEventListener('unload', function (e) {
|
|
|
|
if (notifications) helpers.storage.remove('stream');
|
2019-05-05 18:16:01 +05:30
|
|
|
});
|