aboutsummaryrefslogtreecommitdiff
path: root/src/gapi.js
blob: 34fc9e9e1b48ac626d8bcff6c2ba1c680d6dafd1 (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
/* global chrome */
const gapi_base = 'https://www.googleapis.com/calendar/v3';

const GApiError = {
    invalidSyncToken: 1,
    otherError: 2,
};

function to_params(dict) {
    return Object.entries(dict).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&');
}

export function getAuthToken() {
    return new Promise(resolver =>
        chrome.identity.getAuthToken(
            {interactive: true}, token => resolver(token)));
}

export function getCalendars(token) {
    return fetch(`${gapi_base}/users/me/calendarList?${to_params({access_token: token})}`,
            { method: 'GET', async: true })
        .then(response => response.json())
        .then(data => data.items);
}

export function getColors(token) {
    return fetch(`${gapi_base}/colors?${to_params({access_token: token})}`,
        { method: 'GET', async: true })
        .then(response => response.json());
}

function getEvent(calId, eventId, token) {
    return fetch(`${gapi_base}/calendars/${calId}/events/${eventId}?${to_params({access_token: token})}`,
        { method: 'GET', async: true })
        .then(response => response.json());
}

function getEvents(calId, token, syncToken, resultsPerRequest=100) {
    let results = [];
    const singleFetch = (pageToken, syncToken) => fetch(`${gapi_base}/calendars/${calId}/events?${to_params({
            access_token: token,
            pageToken,
            syncToken,
            maxResults: resultsPerRequest
        })}`, { method: 'GET', async: true })
            .then(response => {
                if (response.status === 200)
                    return response.json();
                else if (response.status == 410)
                    throw GApiError.invalidSyncToken;
                else throw GApiError.otherErrors;
            })
            .then(data => {
                results.push(...data.items);
                if (data.nextPageToken) {
                    return singleFetch(data.nextPageToken, '');
                } else {
                    return ({
                        nextSyncToken: data.nextSyncToken,
                        results
                    });
                }
            })

    return singleFetch('', syncToken);
}

export class GCalendar {
    constructor(calId, name) {
        this.calId = calId;
        this.name = name;
        this.token = getAuthToken();
        this.syncToken = '';
        this.cache = {};
    }

    static dateToCacheKey(date) {
        return Math.floor(date / 8.64e7);
    }

    getSlot(k) {
        if (!this.cache[k])
            this.cache[k] = {};
        return this.cache[k];
    }

    static slotStartDate(k) { return new Date(k * 8.64e7); }
    static slotEndDate(k) { return new Date((k + 1) * 8.64e7); }

    addEvent(e) {
        let ks = GCalendar.dateToCacheKey(e.start);
        let ke = GCalendar.dateToCacheKey(new Date(e.end.getTime() - 1));
        if (ks === ke)
            this.getSlot(ks)[e.id] = {
                start: e.start,
                end: e.end,
                id: e.id };
        else
        {
            this.getSlot(ks)[e.id] = {
                start: e.start,
                end: GCalendar.slotEndDate(ks),
                id: e.id };
            this.getSlot(ke)[e.id] = {
                start: GCalendar.slotStartDate(ke),
                end: e.end,
                id: e.id };
            for (let k = ks + 1; k < ke; k++)
                this.getSlot(k)[e.id] = {
                    start: GCalendar.slotStartDate(k),
                    end: GCalendar.slotEndDate(k),
                    id: e.id };
        }
    }

    removeEvent(e) {
        let ks = GCalendar.dateToCacheKey(e.start);
        let ke = GCalendar.dateToCacheKey(new Date(e.end.getTime() - 1));
        for (let k = ks; k <= ke; k++)
            delete this.getSlot(k)[e.id];
    }

    getSlotEvents(k, start, end) {
        let s = this.getSlot(k);
        let results = [];
        for (let id in s) {
            if (!(s[id].start >= end || s[id].end <= start))
            {
                let nstart = s[id].start < start ? start: s[id].start;
                let nend = s[id].end > end ? end: s[id].end;
                if (nstart > nend) console.log(s[id], start, end);
                results.push({
                    id,
                    start: s[id].start < start ? start: s[id].start,
                    end: s[id].end > end ? end: s[id].end
                });
            }
        }
        return results;
    }

    getCachedEvents(start, end) {
        let ks = GCalendar.dateToCacheKey(start);
        let ke = GCalendar.dateToCacheKey(new Date(end.getTime() - 1));
        let results = this.getSlotEvents(ks, start, end);
        for (let k = ks + 1; k < ke; k++)
        {
            let s = this.getSlot(k);
            for (let id in s)
                results.push(s[id]);
        }
        if (ke > ks)
            results.push(...this.getSlotEvents(ke, start, end));
        return results;
    }

    sync() {
        return this.token.then(token => getEvents(this.calId, token, this.syncToken).then(r => {
            this.syncToken = r.nextSyncToken;
            let pm_results = r.results.map(e => e.start ? Promise.resolve(e) : getEvent(this.calId, e.id, token));
            return Promise.all(pm_results).then(results => results.forEach(e => {
                e.start = new Date(e.start.dateTime);
                e.end = new Date(e.end.dateTime);
                if (e.status === 'confirmed')
                    this.addEvent(e);
                else if (e.status === 'cancelled')
                    this.removeEvent(e);
            }));
        })).catch(e => {
            if (e == GApiError.invalidSyncToken) {
                this.syncToken = '';
                this.sync();
            } else throw e;
        });
    }

    getEvents(start, end) {
        return this.sync().then(() => this.getCachedEvents(start, end));
    }
}