2019-11-28 05:17:01 -09:00
|
|
|
import ms from 'ms';
|
|
|
|
|
import fetch from 'electron-fetch';
|
2020-01-02 05:44:11 -09:00
|
|
|
import {version} from './package.json';
|
|
|
|
|
import {BrowserWindow} from 'electron';
|
2016-10-07 19:28:40 -08:00
|
|
|
|
|
|
|
|
const NEWS_URL = 'https://hyper-news.now.sh';
|
|
|
|
|
|
2020-01-02 05:44:11 -09:00
|
|
|
export default function fetchNotifications(win: BrowserWindow) {
|
2016-10-07 19:28:40 -08:00
|
|
|
const {rpc} = win;
|
2020-01-02 05:44:11 -09:00
|
|
|
const retry = (err?: any) => {
|
2016-10-22 11:16:05 -08:00
|
|
|
setTimeout(() => fetchNotifications(win), ms('30m'));
|
2016-10-07 19:28:40 -08:00
|
|
|
if (err) {
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2016-10-07 19:28:40 -08:00
|
|
|
console.error('Notification messages fetch error', err.stack);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-09-10 05:35:10 -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
|
|
|
|
|
}
|
|
|
|
|
})
|
2017-09-10 05:35:10 -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
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
retry();
|
|
|
|
|
})
|
|
|
|
|
.catch(retry);
|
2019-11-28 05:17:01 -09:00
|
|
|
}
|