From 612285d91c0c077509df7bc55464766046241173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Wed, 4 Jun 2025 19:18:04 +0200 Subject: [PATCH] Add support for combined notifications completion --- src/backend/ntfy.ts | 55 +++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/src/backend/ntfy.ts b/src/backend/ntfy.ts index 60bd481..687079c 100644 --- a/src/backend/ntfy.ts +++ b/src/backend/ntfy.ts @@ -5,6 +5,7 @@ import { Backend } from "./base"; import { Task, TaskPriority } from "../types/task"; import { enhancedStringConfig } from "../util/config"; import { jsMapper } from "../util/code"; +import { profile } from "console"; type NtfyConfig = { url: string; @@ -38,12 +39,16 @@ export class NtfySH extends Backend { : defaultMapper; const dto = mapper(task); + + const actions = task && profileConfig.completion?.enable && config.server?.baseUrl && !task.recurrenceRule + ? [buildCompleteAction(task, profileConfig.name, config.server.baseUrl, "✅")] + : []; - this.#doNotify(config, profileConfig, backendConfig, dto, task); + this.#doNotify(backendConfig, dto, actions); } protected async notifyCombined(config: Config, profileConfig: ProfileConfig, backendConfig: NtfyConfig, tasks: Task[]): Promise { - const chunks = chunkArray(tasks, 4); + const chunks = chunkArray(tasks, 3); const context = { mapPriority, @@ -57,39 +62,23 @@ export class NtfySH extends Backend { const dto = mapper(chunk); - await this.#doNotify(config, profileConfig, backendConfig, dto); + const actions = profileConfig.completion?.enable && config.server?.baseUrl + ? chunk.map((t, i) => buildCompleteAction(t, profileConfig.name, config.server!.baseUrl!, ["✅1️⃣", "✅2️⃣", "✅3️⃣"][i] ?? "✅")) + : []; + + await this.#doNotify(backendConfig, dto, actions); await snooze(2500); } } - async #doNotify(config: Config, profileConfig: ProfileConfig, backendConfig: NtfyConfig, dto: NtfyDTO, task?: Task): Promise { + async #doNotify(backendConfig: NtfyConfig, dto: NtfyDTO, actions: object[] = []): Promise { const token = enhancedStringConfig(backendConfig.token); const headers: Record = { Authorization: `Bearer ${token}`, }; - const actions: object[] = []; - - if (task && profileConfig.completion?.enable && config.server?.baseUrl && !task.recurrenceRule) { - const body: CompleteTaskDTO = { - task, - profile: profileConfig.name - }; - - actions.push({ - action: "http", - label: "Mark as completed", - url: `${config.server.baseUrl}/complete`, - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(body) - }); - } - buildHeaders(dto, headers); return fetch(`https://${backendConfig.url}`, { @@ -104,6 +93,24 @@ export class NtfySH extends Backend { } } +function buildCompleteAction(task: Task, profile: string, baseUrl: string, label: string): object { + const body: CompleteTaskDTO = { + task, + profile: profile, + }; + + return { + action: "http", + label: "✅", + url: `${baseUrl}/complete`, + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(body) + }; +} + function snooze(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); }