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';
|
2023-06-26 01:29:50 -08:00
|
|
|
import type {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-06-19 04:51:34 -08:00
|
|
|
const retry = (err?: Error) => {
|
2016-10-22 11:16:05 -08:00
|
|
|
setTimeout(() => fetchNotifications(win), ms('30m'));
|
2016-10-07 19:28:40 -08:00
|
|
|
if (err) {
|
|
|
|
|
console.error('Notification messages fetch error', err.stack);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-03-25 02:15:08 -08:00
|
|
|
.then((res) => res.json())
|
|
|
|
|
.then((data) => {
|
2023-06-26 01:23:37 -08:00
|
|
|
const message: {text: string; url: string; dismissable: boolean} | '' = data.message || '';
|
2017-09-10 05:35:10 -08:00
|
|
|
if (typeof message !== 'object' && message !== '') {
|
|
|
|
|
throw new Error('Bad response');
|
|
|
|
|
}
|
|
|
|
|
if (message === '') {
|
|
|
|
|
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
|
|
|
}
|