2016-10-07 19:28:40 -08:00
|
|
|
const ms = require('ms');
|
2018-09-23 07:51:56 -08:00
|
|
|
const fetch = require('electron-fetch');
|
2016-10-07 19:28:40 -08:00
|
|
|
|
2016-12-12 06:04:32 -09:00
|
|
|
const {version} = require('./package');
|
2016-10-07 19:28:40 -08:00
|
|
|
|
|
|
|
|
const NEWS_URL = 'https://hyper-news.now.sh';
|
|
|
|
|
|
|
|
|
|
module.exports = function fetchNotifications(win) {
|
|
|
|
|
const {rpc} = win;
|
|
|
|
|
const retry = err => {
|
2016-10-22 11:16:05 -08:00
|
|
|
setTimeout(() => fetchNotifications(win), ms('30m'));
|
2016-10-07 19:28:40 -08:00
|
|
|
if (err) {
|
2018-04-16 06:17:17 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2016-10-07 19:28:40 -08:00
|
|
|
console.error('Notification messages fetch error', err.stack);
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-04-16 06:17:17 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2016-10-07 19:28:40 -08:00
|
|
|
console.log('Checking for notification messages');
|
2016-12-12 06:04:32 -09:00
|
|
|
fetch(NEWS_URL, {
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Hyper-Version': version,
|
|
|
|
|
'X-Hyper-Platform': process.platform
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-04-16 06:17:17 -08:00
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(data => {
|
|
|
|
|
const {message} = data || {};
|
|
|
|
|
if (typeof message !== 'object' && message !== '') {
|
|
|
|
|
throw new Error('Bad response');
|
|
|
|
|
}
|
|
|
|
|
if (message === '') {
|
|
|
|
|
//eslint-disable-next-line no-console
|
|
|
|
|
console.log('No matching notification messages');
|
|
|
|
|
} else {
|
|
|
|
|
rpc.emit('add notification', message);
|
|
|
|
|
}
|
2016-10-07 19:28:40 -08:00
|
|
|
|
2018-04-16 06:17:17 -08:00
|
|
|
retry();
|
|
|
|
|
})
|
|
|
|
|
.catch(retry);
|
2016-10-07 19:28:40 -08:00
|
|
|
};
|