summaryrefslogtreecommitdiff
path: root/src/sinks/IcalSink.ts
blob: efb94ac21ea791bf2f932ccb690a8fbc6b602bd6 (plain)
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
import {createHash} from 'crypto';
import {writeFile} from 'fs';

import * as ical from 'ical-generator';

import {ISink} from './ISink';
import {IEvent} from '../core/IEvent';


const sha256 = (x) => createHash('sha256').update(x, 'utf8').digest('hex');


// ref. https://www.npmjs.com/package/ical-generator
export class IcalSink implements ISink {
    private _cal = ical();
    private _targetFile: string;

    constructor(targetFile: string) {
        this._targetFile = targetFile;
    }

    createEvent(event: IEvent): Promise<void> {
        // TODO better duplicate detection and resolve conflicts lol
        const eventId = sha256(JSON.stringify(event)).substr(0, 16);

        const icalEvent = this._cal.createEvent({
            uid: eventId,
            start: event.start.toISOString(),
            end: event.end.toISOString(),
            timestamp: new Date(),
            summary: event.summary,
            description: event.description,
        });

        return Promise.resolve();
    }

    commit(): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            writeFile(this._targetFile, this._cal.toString(), err => {
                if (err) return reject(err);
                resolve();
            });
        });
    }
}