aboutsummaryrefslogtreecommitdiff
path: root/src/background.ts
blob: 3c37865ad586bea952e0ff43f80541607a795a72 (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import * as gapi from './gapi';
import { MsgType, Msg } from './msg';
import { Duration, TrackedPeriod, TrackedPeriodFlat } from './duration';
import moment from 'moment';
import { GraphData, getGraphData } from './graph';
import { PatternEntry, PatternEntryFlat } from './pattern';

let mainPatterns: PatternEntry[] = [];
let analyzePatterns: PatternEntry[] = [];
let calendars: {[id: string]: gapi.GCalendarMeta} = {};
let calData: {[id: string]: gapi.GCalendar} = {};
let config = {
    trackedPeriods: [
        new TrackedPeriod('Today', Duration.days(1), Duration.days(0)),
        new TrackedPeriod('Yesterday', Duration.days(2), Duration.days(1)),
        new TrackedPeriod('This Week', Duration.weeks(1), Duration.weeks(0)),
        new TrackedPeriod('This Month', Duration.months(1), Duration.months(0))] as TrackedPeriod[],
    overrideNewTab: false
};
let mainGraphData: GraphData[] = [];
let dirtyMetadata = false;
let dirtyCalData = false;
let loadPromise: Promise<void> = null;
let auth = new gapi.Auth();

enum ChromeError {
    storageGetError = "storageGetError",
    storageSetError = "storageSetError"
}

const chromeStorageGet = (keys: string[]): Promise<any> => (
    new Promise(resolver => chrome.storage.local.get(keys, items => {
        if (chrome.runtime.lastError) throw ChromeError.storageGetError;
        resolver(items);
    }))
);

const chromeStorageSet = (obj: {[key: string]: any}): Promise<void> => (
    new Promise(resolver => chrome.storage.local.set(obj, () => {
        if (chrome.runtime.lastError) throw ChromeError.storageSetError;
        resolver();
    }))
);

async function loadMetadata() {
    try {
        let items = await chromeStorageGet(['calendars', 'config', 'mainPatterns', 'analyzePatterns']);
        if (!items.hasOwnProperty('config'))
            console.log("no saved metadata");
        else
        {
            config = {
                trackedPeriods: items.config.trackedPeriods.map((p: TrackedPeriodFlat) => TrackedPeriod.inflate(p)),
                overrideNewTab: items.config.overrideNewTab
            };
            calendars = items.calendars;
            mainPatterns = items.mainPatterns.map((p: PatternEntryFlat) => PatternEntry.inflate(p));
            analyzePatterns = items.analyzePatterns.map((p: PatternEntryFlat) => PatternEntry.inflate(p));
            console.log('metadata loaded');
        }
    } catch (_) {
        console.error("error while loading saved metadata");
    }
}

async function saveMetadata() {
    await chromeStorageSet({
        calendars,
        config: {
            trackedPeriods: config.trackedPeriods.map(p => p.deflate()),
            overrideNewTab: config.overrideNewTab
        },
        mainPatterns: mainPatterns.map(p => p.deflate()),
        analyzePatterns: analyzePatterns.map(p => p.deflate())
    });
    console.log('metadata saved');
}

async function loadCachedCals() {
    try {
        let items = await chromeStorageGet(['calData']);
        if (!items.hasOwnProperty('calData'))
            console.log("no cached cals");
        else
        {
            let calDataFlat: {[id: string]: gapi.GCalendarFlat} = items.calData;
            console.log(calDataFlat);
            for (let id in calDataFlat) {
                calData[id] = gapi.GCalendar.inflate(calDataFlat[id], auth);
            }
            console.log("cached cals loaded");
        }
    } catch (e) {
        console.log(e);
        console.error("error while loading cached cals");
    }
}

async function saveCachedCals() {
    let calDataFlat: {[id: string]: gapi.GCalendarFlat} = {};
    for (let id in calData) {
        if (!(calendars.hasOwnProperty(id) &&
            calendars[id].enabled)) continue;
        calDataFlat[id] = calData[id].deflate();
    }
    try {
        await chromeStorageSet({ calData: calDataFlat });
        console.log('cached data saved');
    } catch (_) {
        console.log("failed to save cached data");
    }
}

function getCalData(id: string) {
    if (!calData.hasOwnProperty(id))
        calData[id] = new gapi.GCalendar(id, calendars[id].name, auth);
    return calData[id];
}

function handleGApiError(id: string, err: gapi.GApiError) {
    if (err === gapi.GApiError.fetchError) {
        console.log(`${id}: fetch error`);
    } else if (err === gapi.GApiError.invalidAuthToken) {
        console.log(`${id}: invalid auth token`);
        calendars[id].enabled = false;
    } else if (err === gapi.GApiError.notLoggedIn) {
        console.log(`${id}: not logged in`);
    } else {
        console.log(`${id}: ${err}`);
        calendars[id].enabled = false;
    }
}

async function getCalEvents(id: string, start: Date, end: Date) {
    let gcal = getCalData(id);
    try {
        let res = await gcal.getEvents(new Date(start), new Date(end));
        dirtyCalData = dirtyCalData || res.changed;
        return res.events;
    } catch(err) {
        handleGApiError(id, err);
        console.log(`cannot load calendar ${id}`);
        return [];
    }
}

function updateMainGraphData() {
    console.log('refreshing graph data');
    console.log(mainGraphData);
    let pms = [];
    for (let i = 0; i < config.trackedPeriods.length; i++)
    {
        let p = config.trackedPeriods[i];
        let startD = p.start.toMoment();
        let endD = p.end.toMoment();
        if (!(startD && endD)) return;
        let start = moment().add(1, 'days').startOf('day');
        if (endD.valueOf() == 0) {
            switch (p.start.unit) {
                case 'days': start = moment().add(1, 'days').startOf('day'); break;
                case 'weeks': start = moment().add(1, 'weeks').startOf('isoWeek'); break;
                case 'months': start = moment().add(1, 'months').startOf('month'); break;
                default:
            }
        }
        let end = start.clone();
        start.subtract(startD);
        end.subtract(endD);
        pms.push(getGraphData(
            start.toDate(), end.toDate(), mainPatterns, calendars,
            getCalEvents
        ).then(r => {
            mainGraphData[i] = {
                    name: p.name,
                    start: start.toDate(),
                    end: end.toDate(),
                    data: r.patternGraphData
            };
        }));
    }
    return Promise.all(pms);
}

async function pollSync() {
    console.log('poll');
    /* sync all enabled calendars */
    let pms = [];
    for (let id in calendars) {
        if (!calendars[id].enabled) continue;
        pms.push(getCalData(id).sync().catch(err => {
            handleGApiError(id, err);
            console.log(`cannot sync calendar ${id}`);
        }));
    }
    (await Promise.all(pms)).forEach(b => b && (dirtyCalData = true));
    /* update the tracked graph data */
    await updateMainGraphData();
    pms = [];
    /* save the storage if state is changed */
    if (dirtyMetadata)
        pms.push(saveMetadata().then(() => dirtyMetadata = false));
    if (dirtyCalData)
        pms.push(saveCachedCals().then(() => dirtyCalData = false));
    await Promise.all(pms);
    /* setup the next loop */
    return new Promise(resolver => (
        window.setTimeout(() => { resolver(); pollSync();}, 10000)
    ));
}

function handleMsg(port: chrome.runtime.Port) {
    console.assert(port.name == 'main');
    port.onMessage.addListener(_msg => {
        let msg = Msg.inflate<any>(_msg);
        console.log(msg);
        switch (msg.opt) {
        case MsgType.updatePatterns: {
            let patterns = msg.data.patterns.map((p: PatternEntryFlat) => PatternEntry.inflate(p));
            if (msg.data.id == 'analyze')
                analyzePatterns = patterns;
            else
                mainPatterns = patterns;
            dirtyMetadata = true;
            port.postMessage(msg.genResp(null));
            break;
        }
        case MsgType.getPatterns: {
            let patterns;
            if (msg.data.id == 'analyze')
                patterns = analyzePatterns;
            else
                patterns = mainPatterns;
            port.postMessage(msg.genResp(patterns.map(p => p.deflate())));
            break;
        }
        case MsgType.updateCalendars: {
            calendars = msg.data;
            dirtyMetadata = true;
            port.postMessage(msg.genResp(null));
            break;
        }
        case MsgType.getCalendars: {
            let cals = calendars;
            if (msg.data.enabledOnly)
            {
                cals = Object.keys(calendars)
                    .filter(id => calendars[id].enabled)
                    .reduce((res, id) => (res[id] = calendars[id], res), {} as {[id: string]: gapi.GCalendarMeta});
            }
            port.postMessage(msg.genResp(cals));
            break;
        }
        case MsgType.getCalEvents: {
            getCalEvents(msg.data.id, new Date(msg.data.start), new Date(msg.data.end)).then(data => {
                console.log(data);
                let resp = msg.genResp(data.map(e => e.deflate()));
                console.log(resp);
                port.postMessage(resp);
            });
            break;
        }
        case MsgType.updateConfig: {
            for (let prop in msg.data) {
                if (prop === 'trackedPeriods') {
                    config.trackedPeriods = msg.data.trackedPeriods.map((p: TrackedPeriodFlat) => TrackedPeriod.inflate(p));
                } else if (prop == 'overrideNewTab') {
                    config.overrideNewTab = msg.data.overrideNewTab as boolean;
                }
            }
            dirtyMetadata = true;
            port.postMessage(msg.genResp(null));
            break;
        }
        case MsgType.getConfig: {
            let res: {[prop: string]: any} = {};
            msg.data.forEach((prop: string) => {
                if (prop === 'trackedPeriods')
                    res.trackedPeriods = config.trackedPeriods.map(p => p.deflate());
                else if (prop === 'overrideNewTab')
                    res.overrideNewTab = config.overrideNewTab;
            });
            port.postMessage(msg.genResp(res));
            break;
        }
        case MsgType.getGraphData: {
            (async () => {
                await (msg.data.sync ? updateMainGraphData().then(() => {}) : Promise.resolve());
                if (mainGraphData.length === 0)
                {
                    await loadPromise;
                    await updateMainGraphData();
                }
                port.postMessage(msg.genResp(mainGraphData.map(d => ({
                    name: d.name,
                    start: d.start.toISOString(),
                    end: d.end.toISOString(),
                    data: d.data
                }))));
            })();
            break;
        }
        case MsgType.clearCache: {
            calData = {};
            port.postMessage(msg.genResp(null));
            break;
        }
        case MsgType.fetchCalendars: {
            (async () => {
                let token = await auth.getAuthToken();
                let results = await gapi.getCalendars(token);
                port.postMessage(msg.genResp(results));
            })();
            break;
        }
        case MsgType.fetchColors: {
            (async () => {
                let token = await auth.getAuthToken();
                let results = await gapi.getColors(token);
                port.postMessage(msg.genResp(results));
            })();
            break;
        }
        case MsgType.login: {
            (async () => {
                let succ = true;
                try {
                    await auth.login();
                } catch (_) {
                    succ = false;
                }
                port.postMessage(msg.genResp(succ));
            })();
            break;
        }
        case MsgType.logout: {
            (async () => {
                let succ = true;
                try {
                    await auth.logout();
                } catch (_) {
                    succ = false;
                }
                port.postMessage(msg.genResp(succ));
            })();
            break;
        }
        case MsgType.getLoggedIn: {
            auth.loggedIn().then(b => port.postMessage(msg.genResp(b)));
            break;
        }
        default: console.error("unknown msg opt");
        }
    });
}

loadPromise = (async () => {
    await Promise.all([loadMetadata(), loadCachedCals()]);
    pollSync();
})();

chrome.runtime.onConnect.addListener(handleMsg);

chrome.tabs.onCreated.addListener(function(tab) {
    if (tab.url === "chrome://newtab/") {
        if (config.overrideNewTab) {
            chrome.tabs.update(tab.id, {
                url: chrome.extension.getURL("tab.html")
            });
        }
    }
});

chrome.runtime.onInstalled.addListener(() => {
    chrome.tabs.create({ url: "index.html" });
});