Create working scaffolding of triggering notifications

This commit is contained in:
2025-01-17 15:11:55 +01:00
parent d331866f46
commit 67db439f84
6 changed files with 62 additions and 2 deletions

9
src/backend/base.ts Normal file
View File

@@ -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;
}

3
src/backend/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export { Backend } from "./base";
export { NtfySH } from "./ntfy-sh";

28
src/backend/ntfy-sh.ts Normal file
View File

@@ -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}`
}
})
}
}

View File

@@ -1,2 +0,0 @@
import { loadTasks } from "./loader";

21
src/reminder/index.ts Normal file
View File

@@ -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));
}

1
src/types/config.ts Normal file
View File

@@ -0,0 +1 @@
export type BackendConfig = Record<string, unknown>;