forked from hagino3000/Cybozu-schedule-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (90 loc) · 3.06 KB
/
app.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
'use strict';
var NotificationCenter = require('node-notifier/notifiers/notificationcenter'),
keychain = require('keychain');
var Cybozu = require('./thirdparty/cybozu-connect'),
SETTINGS = require('./settings');
function notifySchedule(data) {
data.forEach(function(entry) {
var notifier = new NotificationCenter();
notifier.notify({
title: entry.title,
subtitle: entry.time,
message: entry.location,
icon: __dirname + '/image/icon.png',
open: entry.url
});
});
}
function notifyError(message) {
console.error(message);
var notifier = new NotificationCenter();
notifier.notify({
title: 'Garoon notifier Error',
message: message,
});
}
function createRunner(GaroonAPI, password) {
var url = SETTINGS.URL;
var account = SETTINGS.LOGIN_ACCOUNT;
var runner = function() {
var con = new GaroonAPI.CybozuConnect.App(url, account, password);
if (!con.user) {
notifyError('ガルーンにログインできませんでした');
return;
}
// 直近60分のスケジュールを取得する
var now = new Date();
var offset = 1000*60*60;
var end = new Date(+now + offset);
var schedule = new GaroonAPI.CybozuConnect.Schedule(con);
var events = schedule.getEvents({
start : now,
end : end,
userIdList : [con.user.id]
}, true);
if (events.users && events.users[con.user.id]) {
var nearestSchedule = [];
(function processEvents(){
var ev = events.users[con.user.id].pop();
if (ev) {
if (!ev.allDay) {
nearestSchedule.push({
title: ev.title,
time: (new Date(ev.start)).toLocaleTimeString() + ' - ' + (new Date(ev.end)).toLocaleTimeString(),
body: ev.description,
location: ev.facilities[0] ? ev.facilities[0].name : '場所の指定無し',
url: SETTINGS.URL + '/schedule/view?event=' + ev.id
});
}
processEvents();
} else {
notifySchedule(nearestSchedule);
}
})();
}
};
return runner;
}
function getRunner(next) {
keychain.getPassword({
account: SETTINGS.LOGIN_ACCOUNT,
service: SETTINGS.KEYCHAIN_ENTRY_NAME
}, function(err, password) {
if (err) {
console.error(err);
console.error('Failed to get Password from Keychain.');
return;
}
Cybozu.init(SETTINGS.JQUERY_PATH, function(API) {
if (!API) {
console.error('Cannot get Garoon API');
return;
}
next(createRunner(API, password));
});
});
}
process.on('uncaughtexception', function(e) {
console.error(e);
});
exports.getRunner = getRunner;