Add support for combined notifications completion
This commit is contained in:
@@ -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<NtfyConfig> {
|
||||
: 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<void> {
|
||||
const chunks = chunkArray(tasks, 4);
|
||||
const chunks = chunkArray(tasks, 3);
|
||||
|
||||
const context = {
|
||||
mapPriority,
|
||||
@@ -57,39 +62,23 @@ export class NtfySH extends Backend<NtfyConfig> {
|
||||
|
||||
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<Response> {
|
||||
async #doNotify(backendConfig: NtfyConfig, dto: NtfyDTO, actions: object[] = []): Promise<Response> {
|
||||
const token = enhancedStringConfig(backendConfig.token);
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
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<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) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user