Create working scaffolding of triggering notifications
This commit is contained in:
9
src/backend/base.ts
Normal file
9
src/backend/base.ts
Normal 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
3
src/backend/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { Backend } from "./base";
|
||||
export { NtfySH } from "./ntfy-sh";
|
||||
|
||||
28
src/backend/ntfy-sh.ts
Normal file
28
src/backend/ntfy-sh.ts
Normal 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}`
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
import { loadTasks } from "./loader";
|
||||
|
||||
|
||||
21
src/reminder/index.ts
Normal file
21
src/reminder/index.ts
Normal 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
1
src/types/config.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type BackendConfig = Record<string, unknown>;
|
||||
Reference in New Issue
Block a user