dsh-bark-push 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/notifier.js CHANGED
@@ -70,15 +70,17 @@ export class Notifier {
70
70
  * @param {() => Record<string, unknown>} options.getConfig - current effective config.
71
71
  * @param {object} options.state - StateStore.
72
72
  * @param {object} options.bark - BarkClient.
73
+ * @param {object} [options.channels] - ChannelSender for the extra channels.
73
74
  * @param {object} options.queue - SerialQueue.
74
75
  * @param {{ info: Function, warn: Function, error: Function, debug: Function }} options.logger - logger.
75
76
  * @param {string} options.version - plugin version for diagnostics.
76
77
  */
77
- constructor({ ctx, getConfig, state, bark, queue, logger, version }) {
78
+ constructor({ ctx, getConfig, state, bark, channels, queue, logger, version }) {
78
79
  this.ctx = ctx;
79
80
  this.getConfig = getConfig;
80
81
  this.state = state;
81
82
  this.bark = bark;
83
+ this.channels = channels;
82
84
  this.queue = queue;
83
85
  this.logger = logger;
84
86
  this.version = version;
@@ -207,6 +209,10 @@ export class Notifier {
207
209
  results: Array.isArray(record.results)
208
210
  ? record.results.slice(0, 20).map((entry) => ({
209
211
  maskedKey: entry.maskedKey,
212
+ channelId: entry.channelId,
213
+ channelType: entry.channelType,
214
+ label: entry.label,
215
+ skipped: entry.skipped === true,
210
216
  ok: entry.ok === true,
211
217
  status: entry.status ?? null,
212
218
  attempts: entry.attempts ?? null,
@@ -321,7 +327,9 @@ export class Notifier {
321
327
  if (job.kind === 'reply' && config.notifyOnReply !== true) return;
322
328
  if (job.kind === 'approval' && config.notifyOnApproval !== true) return;
323
329
  if (job.kind === 'question' && config.notifyOnQuestion !== true) return;
324
- if (!Array.isArray(config.deviceKeys) || config.deviceKeys.length === 0) return;
330
+ const hasBark = Array.isArray(config.deviceKeys) && config.deviceKeys.length > 0;
331
+ const hasChannels = this.channels?.hasEnabled?.() === true;
332
+ if (!hasBark && !hasChannels) return;
325
333
 
326
334
  // Re-read shared state and re-resolve the workspace immediately before
327
335
  // sending: a mute applied while this job waited (including one written
@@ -364,11 +372,16 @@ export class Notifier {
364
372
  });
365
373
  if (url !== undefined) note.url = url;
366
374
  }
367
- const result = await this.bark.send(note);
368
- this.#record({ kind: job.kind, ...result });
369
- if (result.ok) {
375
+ const barkResult = hasBark
376
+ ? await this.bark.send(note)
377
+ : { ok: true, skipped: true, reason: 'no-device-key', results: [] };
378
+ const channelResult = hasChannels ? await this.channels.send(note) : { ok: true, results: [] };
379
+ const results = [...(barkResult.results ?? []), ...(channelResult.results ?? [])];
380
+ const ok = (hasBark ? barkResult.ok === true : true) && (hasChannels ? channelResult.ok === true : true);
381
+ this.#record({ kind: job.kind, ok, skipped: !hasBark && !hasChannels, results });
382
+ if (ok) {
370
383
  this.logger.debug?.(`bark-push: ${job.kind} 已推送`);
371
- } else if (!result.skipped) {
384
+ } else if (!(barkResult.skipped === true && channelResult.results.length === 0)) {
372
385
  this.logger.warn?.(`bark-push: ${job.kind} 推送失败`);
373
386
  }
374
387
  }, job.kind);
@@ -379,35 +392,75 @@ export class Notifier {
379
392
 
380
393
  /**
381
394
  * Send one explicit test notification with the current effective config.
395
+ * Bark (when keys exist) and every enabled extra channel are exercised.
382
396
  * @returns {Promise<object>} sanitized outcome.
383
397
  */
384
398
  async test() {
385
399
  const config = this.getConfig();
386
- const note = formatTest(config);
387
- const result = await this.bark.send(note, { force: true });
388
- this.#record({ kind: 'test', ...result });
400
+ const hasBark = Array.isArray(config.deviceKeys) && config.deviceKeys.length > 0;
401
+ // The default "test push" button exercises only the built-in Bark channel.
402
+ // Extra channels are tested individually from their own card, so one test
403
+ // never blasts every configured channel at once.
404
+ const barkResult = hasBark
405
+ ? await this.bark.send(formatTest(config), { force: true })
406
+ : { ok: false, skipped: true, reason: 'no-device-key', results: [] };
407
+ const ok = hasBark ? barkResult.ok === true : false;
408
+ const results = barkResult.results ?? [];
409
+ this.#record({ kind: 'test', ok, skipped: !hasBark, results });
389
410
  try {
390
- await this.state.recordTest({
391
- at: new Date().toISOString(),
392
- ok: result.ok === true,
393
- results: Array.isArray(result.results) ? result.results : [],
394
- });
411
+ await this.state.recordTest({ at: new Date().toISOString(), ok, results });
395
412
  } catch (error) {
396
413
  this.logger.warn?.(`bark-push: 测试结果无法写入状态文件:${error?.message ?? String(error)}`);
397
414
  }
415
+ return {
416
+ ok,
417
+ skipped: !hasBark,
418
+ reason: hasBark ? barkResult.reason : 'no-device-key',
419
+ results: results.map((entry) => this.#publicResult(entry)),
420
+ };
421
+ }
422
+
423
+ /**
424
+ * Test a single extra channel by id.
425
+ * @param {string} id - channel id.
426
+ * @returns {Promise<object>} sanitized outcome.
427
+ */
428
+ async testChannel(id) {
429
+ if (!this.channels) return { ok: false, skipped: true, reason: 'no-channels', results: [] };
430
+ const result = await this.channels.testOne(id, this.#testNote());
398
431
  return {
399
432
  ok: result.ok === true,
400
433
  skipped: result.skipped === true,
401
434
  reason: result.reason,
402
- results: Array.isArray(result.results)
403
- ? result.results.map((entry) => ({
404
- maskedKey: entry.maskedKey,
405
- ok: entry.ok === true,
406
- status: entry.status ?? null,
407
- attempts: entry.attempts ?? null,
408
- message: entry.message,
409
- }))
410
- : [],
435
+ results: Array.isArray(result.results) ? result.results.map((entry) => this.#publicResult(entry)) : [],
436
+ };
437
+ }
438
+
439
+ /**
440
+ * A channel-agnostic test note.
441
+ * @returns {{ title: string, body: string, kind: 'test' }} note.
442
+ */
443
+ #testNote() {
444
+ const stamp = new Date().toLocaleString('zh-CN', { hour12: false });
445
+ return { title: 'DSH 推送测试', body: `配置有效:这是一条来自 DSH 的测试通知。\n时间:${stamp}`, kind: 'test' };
446
+ }
447
+
448
+ /**
449
+ * Reduce one delivery result to the shape the management page consumes.
450
+ * @param {object} entry - raw result.
451
+ * @returns {object} sanitized result.
452
+ */
453
+ #publicResult(entry) {
454
+ return {
455
+ maskedKey: entry.maskedKey,
456
+ channelId: entry.channelId,
457
+ channelType: entry.channelType,
458
+ label: entry.label,
459
+ ok: entry.ok === true,
460
+ skipped: entry.skipped === true,
461
+ status: entry.status ?? null,
462
+ attempts: entry.attempts ?? null,
463
+ message: entry.message,
411
464
  };
412
465
  }
413
466
  }