From 67db439f8491aa94446ad7c5dc65d102d58a7030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Fri, 17 Jan 2025 15:11:55 +0100 Subject: [PATCH] Create working scaffolding of triggering notifications --- src/backend/base.ts | 9 +++++++++ src/backend/index.ts | 3 +++ src/backend/ntfy-sh.ts | 28 ++++++++++++++++++++++++++++ src/index.ts | 2 -- src/reminder/index.ts | 21 +++++++++++++++++++++ src/types/config.ts | 1 + 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/backend/base.ts create mode 100644 src/backend/index.ts create mode 100644 src/backend/ntfy-sh.ts create mode 100644 src/reminder/index.ts create mode 100644 src/types/config.ts 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