diff --git a/src/backend/base.ts b/src/backend/base.ts new file mode 100644 index 0000000..412faa0 --- /dev/null +++ b/src/backend/base.ts @@ -0,0 +1,9 @@ +import { BackendConfig } from "../types/config"; +import { Notification } from "../types/notification"; + +export abstract class Backend { + constructor(config: BackendConfig) { + } + + abstract notify(notification: Notification): void; +} \ No newline at end of file diff --git a/src/backend/index.ts b/src/backend/index.ts new file mode 100644 index 0000000..db03e71 --- /dev/null +++ b/src/backend/index.ts @@ -0,0 +1,3 @@ +export { Backend } from "./base"; +export { NtfySH } from "./ntfy-sh"; + diff --git a/src/backend/ntfy-sh.ts b/src/backend/ntfy-sh.ts new file mode 100644 index 0000000..364a450 --- /dev/null +++ b/src/backend/ntfy-sh.ts @@ -0,0 +1,28 @@ +import { Notification } from "../types/notification"; +import { Backend } from "./base"; + +type Config = { + url: string; + token: string; + topic: string; +} + +export class NtfySH extends Backend { + #config: Config; + + constructor(config: Config) { + super(config); + this.#config = config; + } + + notify(notification: Notification): void { + fetch(`https://${this.#config.url}/${this.#config.topic}`, { + method: 'POST', + body: notification.text, + headers: { + 'Title': notification.title, + 'Authorization': `Bearer ${this.#config.token}` + } + }) + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index b84dd88..e69de29 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +0,0 @@ -import { loadTasks } from "./loader"; - diff --git a/src/reminder/index.ts b/src/reminder/index.ts new file mode 100644 index 0000000..29db4b4 --- /dev/null +++ b/src/reminder/index.ts @@ -0,0 +1,21 @@ +import dayjs from "dayjs"; +import { NotificationDatabase } from "../types/notification"; +import { Backend } from "../backend"; + +/** + * Iterates through all the database notifications for current time + * and triggers the notification using specified backend. + */ +export async function remind(db: NotificationDatabase, backend: Backend) { + const now = dayjs().format("HH:mm"); + const notifications = db[now] ?? []; + + for (const notification of notifications) { + backend.notify(notification); + await snooze(1500); + } +} + +function snooze(ms: number) { + return new Promise(resolve => setTimeout(resolve, ms)); +} \ No newline at end of file diff --git a/src/types/config.ts b/src/types/config.ts new file mode 100644 index 0000000..358ed60 --- /dev/null +++ b/src/types/config.ts @@ -0,0 +1 @@ +export type BackendConfig = Record; \ No newline at end of file