This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
121 lines (108 loc) · 3.43 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var config = require('./config');
var path = require('path');
var ipc = require('ipc');
var notifier = require('node-notifier');
var shell = require('shell');
var CronJob = require('cron').CronJob;
var moment = require('moment-timezone');
var notifiedEvents = {};
var topRepo;
function renderFeedback() {
var templateSource = document.getElementById('template-feedback').innerHTML;
var template = Handlebars.compile(templateSource);
var resultsPlaceholder = document.getElementById('result-feedback');
resultsPlaceholder.innerHTML = template({
feedback: config.feedback
});
}
function renderTemplate(type, data) {
var templateSource = document.getElementById('template-' + type).innerHTML;
var template = Handlebars.compile(templateSource);
var resultsPlaceholder = document.getElementById('result-' + type);
resultsPlaceholder.innerHTML = template(data);
}
function isWithinAnHour(startTime) {
return moment(startTime, 'DD MMM YYYY, ddd, hh:mm a').isBefore(moment().add(1, 'hour'))
}
function createNotification(type, data) {
// FIXME: this does not work
// an issue with terminal-notifier
notifier.notify({
'remove': 'ALL'
});
if (type === 'events') {
data.forEach(function(upcomingEvent, index) {
if (!notifiedEvents.hasOwnProperty(upcomingEvent.id)) {
notifiedEvents[upcomingEvent.id] = upcomingEvent.id;
ipc.send('event', 'notify');
if (isWithinAnHour(upcomingEvent.formatted_time)) {
notifier.notify({
'title': upcomingEvent.name,
'message': 'by ' + upcomingEvent.group_name + ' on ' + upcomingEvent.formatted_time,
'icon': path.join(__dirname, 'logo.png'),
'wait': true,
'open': upcomingEvent.url,
'group': index + 1
});
}
}
})
} else if (type === 'repos') {
var repo = data[0];
if (topRepo !== repo.name) {
topRepo = repo.name;
ipc.send('event', 'notify');
notifier.notify({
'title': 'New top open source project',
'message': repo.name + ' by ' + repo.owner.login,
'icon': path.join(__dirname, 'logo.png'),
'wait': true,
'open': repo.html_url,
'group': 0
});
}
}
}
function callAPI(type, willNotify){
var xhr = new XMLHttpRequest();
xhr.onload = function(){
var body = JSON.parse(this.responseText);
var data = {};
data[type] = body[type].slice(0, 3);
if (willNotify) {
createNotification(type, data[type]);
} else {
if (type === 'events') {
data[type].forEach(function(event) {
notifiedEvents[event.id] = event.id;
});
} else if (type === 'repos') {
topRepo = data[type][0].name;
}
}
data.website = config.website;
data.feedback = config.feedback;
renderTemplate(type, data);
};
xhr.open('GET', config.apiUrl + type, true);
xhr.send();
}
document.body.addEventListener('click', function(e){
var el = e.target;
if (!el) return;
if (el.tagName.toLowerCase() == 'a' && el.target == '_blank'){
e.preventDefault();
shell.openExternal(el.href);
}
});
document.getElementById('quit').addEventListener('click', function() {
ipc.sendSync('event', 'quit');
})
new CronJob('0 15 * * * *', function() {
console.log('Fetching new events and repo...')
callAPI('events', true);
callAPI('repos', true);
}, null, true, config.timezone);
renderFeedback();
callAPI('events', false);
callAPI('repos', false);