Add support for combined notifications completion

This commit is contained in:
2025-06-04 19:18:04 +02:00
parent b266cb0b83
commit 612285d91c

View File

@@ -5,6 +5,7 @@ import { Backend } from "./base";
import { Task, TaskPriority } from "../types/task"; import { Task, TaskPriority } from "../types/task";
import { enhancedStringConfig } from "../util/config"; import { enhancedStringConfig } from "../util/config";
import { jsMapper } from "../util/code"; import { jsMapper } from "../util/code";
import { profile } from "console";
type NtfyConfig = { type NtfyConfig = {
url: string; url: string;
@@ -38,12 +39,16 @@ export class NtfySH extends Backend<NtfyConfig> {
: defaultMapper; : defaultMapper;
const dto = mapper(task); 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<void> { protected async notifyCombined(config: Config, profileConfig: ProfileConfig, backendConfig: NtfyConfig, tasks: Task[]): Promise<void> {
const chunks = chunkArray(tasks, 4); const chunks = chunkArray(tasks, 3);
const context = { const context = {
mapPriority, mapPriority,
@@ -57,39 +62,23 @@ export class NtfySH extends Backend<NtfyConfig> {
const dto = mapper(chunk); 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); await snooze(2500);
} }
} }
async #doNotify(config: Config, profileConfig: ProfileConfig, backendConfig: NtfyConfig, dto: NtfyDTO, task?: Task): Promise<Response> { async #doNotify(backendConfig: NtfyConfig, dto: NtfyDTO, actions: object[] = []): Promise<Response> {
const token = enhancedStringConfig(backendConfig.token); const token = enhancedStringConfig(backendConfig.token);
const headers: Record<string, string> = { const headers: Record<string, string> = {
Authorization: `Bearer ${token}`, 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); buildHeaders(dto, headers);
return fetch(`https://${backendConfig.url}`, { return fetch(`https://${backendConfig.url}`, {
@@ -104,6 +93,24 @@ export class NtfySH extends Backend<NtfyConfig> {
} }
} }
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) { function snooze(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }