refactor: recipient handling
removes default user, changes recipient to optionally include device Also fixes url_title replacing message title Signed-off-by: Alix von Schirp <github@avonschirp.bootmedia.de>
This commit is contained in:
@@ -52,7 +52,7 @@ const MessageSchema = z
|
|||||||
.default(0),
|
.default(0),
|
||||||
emergencyOpts: z
|
emergencyOpts: z
|
||||||
.object({
|
.object({
|
||||||
repeat: z.number().min(30),
|
retry: z.number().min(30),
|
||||||
expire: z.number().max(10800),
|
expire: z.number().max(10800),
|
||||||
callback: z.string().url().optional(),
|
callback: z.string().url().optional(),
|
||||||
tags: z.string().array().optional(),
|
tags: z.string().array().optional(),
|
||||||
@@ -60,8 +60,8 @@ const MessageSchema = z
|
|||||||
.optional(),
|
.optional(),
|
||||||
sound: z.string().optional(),
|
sound: z.string().optional(),
|
||||||
timestamp: z.number().optional(),
|
timestamp: z.number().optional(),
|
||||||
html: z.boolean().default(false),
|
html: z.boolean().optional(),
|
||||||
monospace: z.boolean().default(false),
|
monospace: z.boolean().optional(),
|
||||||
ttl: z.number().optional(),
|
ttl: z.number().optional(),
|
||||||
})
|
})
|
||||||
.refine(
|
.refine(
|
||||||
@@ -76,8 +76,7 @@ const MessageSchema = z
|
|||||||
)
|
)
|
||||||
.refine(
|
.refine(
|
||||||
(data) => {
|
(data) => {
|
||||||
if (data.html && data.monospace) return false;
|
return !(data.html && data.monospace);
|
||||||
return true;
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: ["html", "monospace"],
|
path: ["html", "monospace"],
|
||||||
@@ -101,30 +100,27 @@ export interface PushoverValidationResponse extends PushoverResponse {
|
|||||||
licenses?: string[];
|
licenses?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PushoverConfig {
|
|
||||||
token: string;
|
|
||||||
defaultUser?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface SendOptions {
|
export interface SendOptions {
|
||||||
recipients?: string | string[];
|
recipients: PushoverUser[];
|
||||||
device?: string | string[];
|
|
||||||
verbose?: boolean;
|
verbose?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PushoverUser {
|
||||||
|
id: string;
|
||||||
|
devices?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface ValidateOptions {
|
export interface ValidateOptions {
|
||||||
user?: string;
|
user: string;
|
||||||
deviceName?: string;
|
deviceName?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Pushover {
|
export class Pushover {
|
||||||
private token: string;
|
private token: string;
|
||||||
private defaultUser?: string;
|
|
||||||
private apiUrl = "https://api.pushover.net/1/";
|
private apiUrl = "https://api.pushover.net/1/";
|
||||||
|
|
||||||
constructor(config: PushoverConfig) {
|
constructor(token: string) {
|
||||||
this.token = config.token;
|
this.token = token;
|
||||||
this.defaultUser = config.defaultUser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -132,16 +128,13 @@ export class Pushover {
|
|||||||
*/
|
*/
|
||||||
public async send(
|
public async send(
|
||||||
message: PushoverMessage,
|
message: PushoverMessage,
|
||||||
options: SendOptions = {},
|
options: SendOptions,
|
||||||
): Promise<PushoverResponse[]> {
|
): Promise<PushoverResponse[]> {
|
||||||
const recipients = this.getRecipients(options);
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
console.log(MessageSchema.parse(message));
|
console.log(MessageSchema.parse(message));
|
||||||
|
|
||||||
if (recipients.length === 0) {
|
if (options.recipients.length === 0) {
|
||||||
throw new Error(
|
reject("No recipients specified.");
|
||||||
"No recipients specified. Provide recipients in options or set a defaultUser.",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.verbose) {
|
if (options.verbose) {
|
||||||
@@ -152,47 +145,31 @@ export class Pushover {
|
|||||||
console.log("Sending message...");
|
console.log("Sending message...");
|
||||||
}
|
}
|
||||||
|
|
||||||
const promises = recipients.map((recipient) =>
|
const promises = options.recipients.map((recipient) =>
|
||||||
this.sendToSingleRecipient(message, recipient, options.device),
|
this.sendToSingleRecipient(message, recipient),
|
||||||
);
|
);
|
||||||
|
|
||||||
const results = await Promise.all(promises);
|
resolve(Promise.all(promises));
|
||||||
|
});
|
||||||
// Combine results
|
|
||||||
const combinedResponse: PushoverResponse[] = results.map(
|
|
||||||
(result) => result,
|
|
||||||
);
|
|
||||||
|
|
||||||
return combinedResponse;
|
|
||||||
}
|
|
||||||
|
|
||||||
private getRecipients(options: SendOptions): string[] {
|
|
||||||
const { recipients } = options;
|
|
||||||
|
|
||||||
if (recipients) {
|
|
||||||
return Array.isArray(recipients) ? recipients : [recipients];
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.defaultUser ? [this.defaultUser] : [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sendToSingleRecipient(
|
private async sendToSingleRecipient(
|
||||||
message: PushoverMessage,
|
message: PushoverMessage,
|
||||||
user: string,
|
user: PushoverUser,
|
||||||
device?: string | string[],
|
|
||||||
): Promise<PushoverResponse> {
|
): Promise<PushoverResponse> {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
// Add token and user
|
// Add token and user
|
||||||
params.append("token", this.token);
|
params.append("token", this.token);
|
||||||
params.append("user", user);
|
params.append("user", user.id);
|
||||||
|
params.append("device", user.devices?.join(",") ?? "");
|
||||||
|
|
||||||
// Add message properties
|
// Add message properties
|
||||||
params.append("message", message.message);
|
params.append("message", message.message);
|
||||||
if (message.title) params.append("title", message.title);
|
if (message.title) params.append("title", message.title);
|
||||||
params.append("priority", "" + message.priority);
|
params.append("priority", "" + message.priority);
|
||||||
if (message.priority === 2 && message.emergencyOpts) {
|
if (message.priority === 2 && message.emergencyOpts) {
|
||||||
params.append("repeat", String(message.emergencyOpts.repeat));
|
params.append("retry", String(message.emergencyOpts.retry));
|
||||||
params.append("expire", String(message.emergencyOpts.expire));
|
params.append("expire", String(message.emergencyOpts.expire));
|
||||||
if (message.emergencyOpts.callback)
|
if (message.emergencyOpts.callback)
|
||||||
params.append("callback", message.emergencyOpts.callback);
|
params.append("callback", message.emergencyOpts.callback);
|
||||||
@@ -204,7 +181,7 @@ export class Pushover {
|
|||||||
params.append("url", message.link);
|
params.append("url", message.link);
|
||||||
} else {
|
} else {
|
||||||
params.append("url", message.link.url);
|
params.append("url", message.link.url);
|
||||||
if (message.link.title) params.append("title", message.link.title);
|
if (message.link.title) params.append("url_title", message.link.title);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (message.html) params.append("html", "1");
|
if (message.html) params.append("html", "1");
|
||||||
@@ -214,15 +191,6 @@ export class Pushover {
|
|||||||
params.append("timestamp", String(message.timestamp));
|
params.append("timestamp", String(message.timestamp));
|
||||||
if (message.ttl) params.append("ttl", String(message.ttl));
|
if (message.ttl) params.append("ttl", String(message.ttl));
|
||||||
|
|
||||||
// Add device if specified
|
|
||||||
if (device) {
|
|
||||||
if (Array.isArray(device)) {
|
|
||||||
params.append("device", device.join(","));
|
|
||||||
} else {
|
|
||||||
params.append("device", device);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.makeRequest("messages.json", params);
|
return this.makeRequest("messages.json", params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,7 +246,7 @@ export class Pushover {
|
|||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
params.append("token", this.token);
|
params.append("token", this.token);
|
||||||
params.append("user", options.user ?? this.defaultUser ?? "");
|
params.append("user", options.user ?? "");
|
||||||
if (options.deviceName) params.append("device", options.deviceName);
|
if (options.deviceName) params.append("device", options.deviceName);
|
||||||
|
|
||||||
return this.makeRequest("users/validate.json", params);
|
return this.makeRequest("users/validate.json", params);
|
||||||
|
|||||||
Reference in New Issue
Block a user