dsh-reminder-notify 0.8.1 → 0.9.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/client.js +164 -20
- package/host/ReminderHost.ps1 +20 -0
- package/index.js +159 -0
- package/package.json +1 -1
- package/publish/index.json +2 -2
package/client.js
CHANGED
|
@@ -16,12 +16,30 @@ window.__ModuleLoader__.load({
|
|
|
16
16
|
const NS = 'dsh-reminder-notify'
|
|
17
17
|
// 与 index.js 里 Host 注册的路由保持一致(同源 GET,返回 { dir, files, error? })。
|
|
18
18
|
const AUDIO_ROUTE = '/dsh-reminder-notify/audio'
|
|
19
|
+
const CONFIG_ROUTE = '/dsh-reminder-notify/config'
|
|
19
20
|
const AUDIO_KEYS = ['doneAudio', 'decideAudio', 'rawAudio']
|
|
21
|
+
const PREVIEW_ROUTE = '/dsh-reminder-notify/preview'
|
|
22
|
+
// 下拉按扩展名分组:wav 一组、mp3 一组……组内按文件名排序,一眼能看出格式。
|
|
23
|
+
function groupAudioFiles(files) {
|
|
24
|
+
const byExt = new Map()
|
|
25
|
+
for (const name of files) {
|
|
26
|
+
const dot = name.lastIndexOf('.')
|
|
27
|
+
const ext = dot >= 0 ? name.slice(dot + 1).toLowerCase() : 'other'
|
|
28
|
+
if (!byExt.has(ext)) byExt.set(ext, [])
|
|
29
|
+
byExt.get(ext).push(name)
|
|
30
|
+
}
|
|
31
|
+
return [...byExt.entries()]
|
|
32
|
+
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
33
|
+
.map(([ext, names]) => ({ ext, names: names.sort((a, b) => a.localeCompare(b, 'zh-Hans-CN')) }))
|
|
34
|
+
}
|
|
20
35
|
|
|
21
36
|
const zh = {
|
|
22
37
|
title: '桌面提醒设置',
|
|
23
|
-
intro: '
|
|
38
|
+
intro: '调整弹窗停留时间、位置、提示音和卡片配色。改完点保存,立刻生效,不用重启。',
|
|
24
39
|
unavailable: '这个页面拿不到插件配置(可能是宿主版本较旧),请改用 cordis.patch.yml 修改。',
|
|
40
|
+
loading: '正在读取插件配置…',
|
|
41
|
+
loadFailed: '读取配置失败:',
|
|
42
|
+
retry: '重试',
|
|
25
43
|
readonly: '当前配置由更高优先级的层覆盖,这里只能查看。',
|
|
26
44
|
groupBasic: '基本',
|
|
27
45
|
groupAudio: '提示音',
|
|
@@ -71,6 +89,10 @@ window.__ModuleLoader__.load({
|
|
|
71
89
|
audioScanning: '正在读取音频目录…',
|
|
72
90
|
audioEmpty: '音频目录里没有可用的音频文件',
|
|
73
91
|
audioFailed: '读取音频目录失败:',
|
|
92
|
+
preview: '试听',
|
|
93
|
+
previewing: '播放中…',
|
|
94
|
+
previewOk: '已播放:',
|
|
95
|
+
previewFailed: '试听失败:',
|
|
74
96
|
hostMode: '宿主模式',
|
|
75
97
|
hostModeScript: '包内脚本(推荐)',
|
|
76
98
|
hostModeExe: '外部 exe 客户端',
|
|
@@ -95,8 +117,11 @@ window.__ModuleLoader__.load({
|
|
|
95
117
|
|
|
96
118
|
const en = {
|
|
97
119
|
title: 'Desktop reminder settings',
|
|
98
|
-
intro: 'Tune the popup duration, position, sounds and card colours. Saving
|
|
120
|
+
intro: 'Tune the popup duration, position, sounds and card colours. Saving applies immediately, no restart.',
|
|
99
121
|
unavailable: 'This page cannot read the plugin config (older host). Edit cordis.patch.yml instead.',
|
|
122
|
+
loading: 'Reading the plugin config…',
|
|
123
|
+
loadFailed: 'Could not read the config: ',
|
|
124
|
+
retry: 'Retry',
|
|
100
125
|
readonly: 'A higher-priority layer overrides this config; it is read-only here.',
|
|
101
126
|
groupBasic: 'Basics',
|
|
102
127
|
groupAudio: 'Sounds',
|
|
@@ -146,6 +171,10 @@ window.__ModuleLoader__.load({
|
|
|
146
171
|
audioScanning: 'Reading the sound directory…',
|
|
147
172
|
audioEmpty: 'No audio files in the sound directory',
|
|
148
173
|
audioFailed: 'Could not read the sound directory: ',
|
|
174
|
+
preview: 'Play',
|
|
175
|
+
previewing: 'Playing…',
|
|
176
|
+
previewOk: 'Played: ',
|
|
177
|
+
previewFailed: 'Preview failed: ',
|
|
149
178
|
hostMode: 'Host mode',
|
|
150
179
|
hostModeScript: 'Bundled script (recommended)',
|
|
151
180
|
hostModeExe: 'External exe client',
|
|
@@ -265,17 +294,51 @@ window.__ModuleLoader__.load({
|
|
|
265
294
|
|
|
266
295
|
function ReminderConfigPanel(props) {
|
|
267
296
|
const t = props.t
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
const
|
|
272
|
-
const
|
|
297
|
+
// 配置读写走插件自己注册的同源 HTTP 路由(Host 端用 configEditor 落盘),
|
|
298
|
+
// 不依赖宿主是否把 ConfigPageForm 传进槽位——那条链路在部分宿主版本里拿不到。
|
|
299
|
+
const [state, setState] = React.useState({ status: 'loading', values: {}, writable: true, error: null })
|
|
300
|
+
const [reload, setReload] = React.useState(0)
|
|
301
|
+
const values = state.values
|
|
302
|
+
const writable = state.writable !== false
|
|
273
303
|
const [draft, setDraft] = React.useState(null)
|
|
274
304
|
const [busy, setBusy] = React.useState(false)
|
|
275
305
|
const [picking, setPicking] = React.useState(false)
|
|
306
|
+
const [previewing, setPreviewing] = React.useState(null)
|
|
276
307
|
const [message, setMessage] = React.useState(null)
|
|
277
308
|
const [audio, setAudio] = React.useState({ dir: '', files: [], error: null })
|
|
278
309
|
|
|
310
|
+
React.useEffect(() => {
|
|
311
|
+
if (typeof fetch !== 'function') {
|
|
312
|
+
setState({ status: 'error', values: {}, writable: false, error: 'fetch is unavailable' })
|
|
313
|
+
return undefined
|
|
314
|
+
}
|
|
315
|
+
let cancelled = false
|
|
316
|
+
Promise.resolve()
|
|
317
|
+
.then(() => fetch(CONFIG_ROUTE, { headers: { accept: 'application/json' } }))
|
|
318
|
+
.then((response) => {
|
|
319
|
+
const status = response ? response.status : 0
|
|
320
|
+
const body = response && typeof response.json === 'function' ? response.json() : Promise.resolve(null)
|
|
321
|
+
return Promise.resolve(body)
|
|
322
|
+
.catch(() => null)
|
|
323
|
+
.then((data) => {
|
|
324
|
+
if (response && response.ok && data && data.ok !== false) return data
|
|
325
|
+
const detail = data && data.error ? data.error : 'HTTP ' + status
|
|
326
|
+
throw new Error(detail)
|
|
327
|
+
})
|
|
328
|
+
})
|
|
329
|
+
.then((data) => {
|
|
330
|
+
if (cancelled) return
|
|
331
|
+
setState({ status: 'ready', values: (data && data.values) || {}, writable: (data && data.writable) !== false, error: null })
|
|
332
|
+
})
|
|
333
|
+
.catch((error) => {
|
|
334
|
+
if (cancelled) return
|
|
335
|
+
setState({ status: 'error', values: {}, writable: false, error: String((error && error.message) || error) })
|
|
336
|
+
})
|
|
337
|
+
return () => {
|
|
338
|
+
cancelled = true
|
|
339
|
+
}
|
|
340
|
+
}, [reload])
|
|
341
|
+
|
|
279
342
|
// 音频候选:Host 端注册了只读路由 /dsh-reminder-notify/audio,页面同源 fetch 拿目录清单。
|
|
280
343
|
// 拿不到(旧宿主 / 路由被拦)就静默降级成手填文件名,不打扰用户。
|
|
281
344
|
const scanDir = (draft && typeof draft.audioDir === 'string' && draft.audioDir !== '' ? draft.audioDir : values.audioDir) || ''
|
|
@@ -306,8 +369,21 @@ window.__ModuleLoader__.load({
|
|
|
306
369
|
return h('span', null, `${tr('summaryPrefix')}${tr(posKey)} · ${values.seconds === undefined ? 60 : values.seconds}${tr('secondsUnit')} · ${sound}`)
|
|
307
370
|
}
|
|
308
371
|
|
|
309
|
-
if (
|
|
310
|
-
return h('div', { className: 'drn-panel' }, h('div', { className: 'drn-hint' }, typeof t === 'function' ? t('
|
|
372
|
+
if (state.status === 'loading') {
|
|
373
|
+
return h('div', { className: 'drn-panel' }, h('div', { className: 'drn-hint' }, typeof t === 'function' ? t('loading') : 'loading…'))
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (state.status === 'error') {
|
|
377
|
+
return h(
|
|
378
|
+
'div',
|
|
379
|
+
{ className: 'drn-panel' },
|
|
380
|
+
h('div', { className: 'drn-hint error' }, (typeof t === 'function' ? t('loadFailed') : 'cannot read config: ') + (state.error || '')),
|
|
381
|
+
h(
|
|
382
|
+
'div',
|
|
383
|
+
{ className: 'drn-actions' },
|
|
384
|
+
h('button', { type: 'button', className: 'drn-btn', onClick: () => setReload(reload + 1) }, typeof t === 'function' ? t('retry') : 'Retry'),
|
|
385
|
+
),
|
|
386
|
+
)
|
|
311
387
|
}
|
|
312
388
|
|
|
313
389
|
const effective = draft === null ? values : Object.assign({}, values, draft)
|
|
@@ -322,27 +398,41 @@ window.__ModuleLoader__.load({
|
|
|
322
398
|
setDraft(Object.assign({}, draft || {}, { [key]: value }))
|
|
323
399
|
}
|
|
324
400
|
|
|
325
|
-
function
|
|
326
|
-
const
|
|
401
|
+
function changedPatch() {
|
|
402
|
+
const patch = {}
|
|
327
403
|
for (const field of FIELDS) {
|
|
328
404
|
const current = effective[field.key]
|
|
329
405
|
const saved = values[field.key]
|
|
330
406
|
if (sameValue(current, saved)) continue
|
|
331
|
-
|
|
407
|
+
patch[field.key] = current === undefined ? '' : current
|
|
332
408
|
}
|
|
333
|
-
return
|
|
409
|
+
return patch
|
|
334
410
|
}
|
|
335
411
|
|
|
336
|
-
const
|
|
337
|
-
const dirty =
|
|
412
|
+
const patch = changedPatch()
|
|
413
|
+
const dirty = Object.keys(patch).length > 0
|
|
338
414
|
|
|
339
415
|
function save() {
|
|
340
416
|
if (!dirty || busy) return
|
|
341
417
|
setBusy(true)
|
|
342
418
|
setMessage(null)
|
|
343
|
-
Promise.resolve(
|
|
344
|
-
.then(() =>
|
|
419
|
+
Promise.resolve()
|
|
420
|
+
.then(() =>
|
|
421
|
+
fetch(CONFIG_ROUTE, {
|
|
422
|
+
method: 'POST',
|
|
423
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
424
|
+
body: JSON.stringify({ values: patch }),
|
|
425
|
+
}),
|
|
426
|
+
)
|
|
427
|
+
.then((response) =>
|
|
428
|
+
response && typeof response.json === 'function'
|
|
429
|
+
? response.json().then((data) => ({ ok: !!(response.ok && data && data.ok !== false), data }))
|
|
430
|
+
: Promise.reject(new Error('HTTP ' + (response ? response.status : '?'))),
|
|
431
|
+
)
|
|
432
|
+
.then(({ ok, data }) => {
|
|
345
433
|
setBusy(false)
|
|
434
|
+
if (!ok) throw new Error((data && data.error) || 'save rejected')
|
|
435
|
+
setState({ status: 'ready', values: (data && data.values) || effective, writable: (data && data.writable) !== false, error: null })
|
|
346
436
|
setDraft(null)
|
|
347
437
|
setMessage({ ok: true, text: typeof t === 'function' ? t('saved') : 'saved' })
|
|
348
438
|
})
|
|
@@ -373,6 +463,44 @@ window.__ModuleLoader__.load({
|
|
|
373
463
|
})
|
|
374
464
|
}
|
|
375
465
|
|
|
466
|
+
// 试听:面板没有本地文件访问能力,交给宿主按档位代播(POST /preview)。
|
|
467
|
+
// 先保存再用当前编辑值试听会更准,但这里刻意不落盘 —— 试听不该改动配置。
|
|
468
|
+
function previewAudio(key) {
|
|
469
|
+
if (previewing !== null) return
|
|
470
|
+
const kind = key === 'decideAudio' ? 'decide' : key === 'rawAudio' ? 'raw' : 'done'
|
|
471
|
+
setPreviewing(key)
|
|
472
|
+
setMessage(null)
|
|
473
|
+
Promise.resolve()
|
|
474
|
+
.then(() =>
|
|
475
|
+
fetch(PREVIEW_ROUTE, {
|
|
476
|
+
method: 'POST',
|
|
477
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
478
|
+
body: JSON.stringify({ kind }),
|
|
479
|
+
}),
|
|
480
|
+
)
|
|
481
|
+
.then((response) => {
|
|
482
|
+
const status = response ? response.status : 0
|
|
483
|
+
const body = response && typeof response.json === 'function' ? response.json() : Promise.resolve(null)
|
|
484
|
+
return Promise.resolve(body)
|
|
485
|
+
.catch(() => null)
|
|
486
|
+
.then((data) => {
|
|
487
|
+
if (response && response.ok && data && data.ok !== false) return data
|
|
488
|
+
const detail = data && (data.detail || data.error) ? (data.detail || data.error) : 'HTTP ' + status
|
|
489
|
+
throw new Error(detail)
|
|
490
|
+
})
|
|
491
|
+
})
|
|
492
|
+
.then((data) => {
|
|
493
|
+
setPreviewing(null)
|
|
494
|
+
const label = typeof t === 'function' ? t('previewOk') : 'Playing: '
|
|
495
|
+
setMessage({ ok: true, text: label + String((data && data.audio) || '') })
|
|
496
|
+
})
|
|
497
|
+
.catch((error) => {
|
|
498
|
+
setPreviewing(null)
|
|
499
|
+
const prefix = typeof t === 'function' ? t('previewFailed') : 'preview failed: '
|
|
500
|
+
setMessage({ ok: false, text: prefix + String((error && error.message) || error) })
|
|
501
|
+
})
|
|
502
|
+
}
|
|
503
|
+
|
|
376
504
|
function control(field) {
|
|
377
505
|
const value = effective[field.key]
|
|
378
506
|
const disabled = !writable || busy
|
|
@@ -411,6 +539,12 @@ window.__ModuleLoader__.load({
|
|
|
411
539
|
onChange: (e) => edit(field.key, e.target.value, field.kind),
|
|
412
540
|
})
|
|
413
541
|
if (AUDIO_KEYS.includes(field.key) && audio.files.length > 0) {
|
|
542
|
+
const groups = groupAudioFiles(audio.files)
|
|
543
|
+
const options = [h('option', { value: '', key: '__pick' }, typeof t === 'function' ? t('audioPick') : 'pick')]
|
|
544
|
+
for (const group of groups) {
|
|
545
|
+
const children = group.names.map((name) => h('option', { key: name, value: name }, name))
|
|
546
|
+
options.push(h('optgroup', { key: 'grp-' + group.ext, label: group.ext }, children))
|
|
547
|
+
}
|
|
414
548
|
return h(
|
|
415
549
|
'div',
|
|
416
550
|
{ className: 'drn-pick' },
|
|
@@ -426,9 +560,19 @@ window.__ModuleLoader__.load({
|
|
|
426
560
|
if (picked !== '') edit(field.key, picked, 'text')
|
|
427
561
|
},
|
|
428
562
|
},
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
563
|
+
options,
|
|
564
|
+
),
|
|
565
|
+
h(
|
|
566
|
+
'button',
|
|
567
|
+
{
|
|
568
|
+
type: 'button',
|
|
569
|
+
className: 'drn-btn small drn-preview',
|
|
570
|
+
disabled: disabled || previewing !== null,
|
|
571
|
+
onClick: () => previewAudio(field.key),
|
|
572
|
+
},
|
|
573
|
+
previewing === field.key
|
|
574
|
+
? (typeof t === 'function' ? t('previewing') : 'Playing…')
|
|
575
|
+
: (typeof t === 'function' ? t('preview') : 'Play'),
|
|
432
576
|
),
|
|
433
577
|
)
|
|
434
578
|
}
|
package/host/ReminderHost.ps1
CHANGED
|
@@ -25,6 +25,7 @@ param(
|
|
|
25
25
|
[string]$Background = '',
|
|
26
26
|
[string]$Icon = '',
|
|
27
27
|
[switch]$NoSound,
|
|
28
|
+
[switch]$Preview,
|
|
28
29
|
[string]$Log = ''
|
|
29
30
|
)
|
|
30
31
|
|
|
@@ -316,6 +317,25 @@ if (-not $NoSound -and -not [string]::IsNullOrEmpty($Audio) -and (Test-Path -Lit
|
|
|
316
317
|
}
|
|
317
318
|
}
|
|
318
319
|
|
|
320
|
+
# ---- 试听:只放声音,不弹卡 ----------------------------------------------------
|
|
321
|
+
# 面板里的「试听」按钮走这条分支:播完(或最多 8 秒)就退出,不留窗口。
|
|
322
|
+
if ($Preview) {
|
|
323
|
+
if ($script:player -ne $null) {
|
|
324
|
+
$deadline = (Get-Date).AddSeconds(8)
|
|
325
|
+
while ((Get-Date) -lt $deadline) { Start-Sleep -Milliseconds 200 }
|
|
326
|
+
try { $script:player.Stop() } catch { }
|
|
327
|
+
$script:player.Dispose()
|
|
328
|
+
}
|
|
329
|
+
if ($script:wmp -ne $null) {
|
|
330
|
+
$deadline = (Get-Date).AddSeconds(8)
|
|
331
|
+
while ((Get-Date) -lt $deadline -and $script:wmp.playState -ne 1) { Start-Sleep -Milliseconds 200 }
|
|
332
|
+
try { $script:wmp.controls.stop(); $script:wmp.close() } catch { }
|
|
333
|
+
try { [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($script:wmp) } catch { }
|
|
334
|
+
}
|
|
335
|
+
Write-Log 'preview exit'
|
|
336
|
+
exit 0
|
|
337
|
+
}
|
|
338
|
+
|
|
319
339
|
# ---- 显示并进入消息循环 -----------------------------------------------------
|
|
320
340
|
$form.Show()
|
|
321
341
|
$form.RaiseWithoutActivating()
|
package/index.js
CHANGED
|
@@ -434,6 +434,8 @@ function toolDefinition(getSettings) {
|
|
|
434
434
|
|
|
435
435
|
const AUDIO_EXTENSIONS = ['.wav', '.mp3', '.ogg', '.m4a', '.flac', '.aac', '.wma']
|
|
436
436
|
const AUDIO_ROUTE = '/dsh-reminder-notify/audio'
|
|
437
|
+
const CONFIG_ROUTE = '/dsh-reminder-notify/config'
|
|
438
|
+
const PREVIEW_ROUTE = '/dsh-reminder-notify/preview'
|
|
437
439
|
|
|
438
440
|
// 面板要「把音频目录里的文件列成下拉」,但 Client 侧没有任何读文件能力,
|
|
439
441
|
// 所以由 Host 注册一条只读的 GET 路由,页面用同源 fetch 取这个 JSON。
|
|
@@ -487,8 +489,165 @@ function registerAudioRoute(ctx, getSettings) {
|
|
|
487
489
|
}, 'dsh-reminder-notify: audio list route')
|
|
488
490
|
}
|
|
489
491
|
|
|
492
|
+
// 试听:只按档位放一遍提示音,不弹卡。面板拿不到本地音频文件,所以由宿主代播。
|
|
493
|
+
export async function previewAudio(kind, settings) {
|
|
494
|
+
if (process.platform !== 'win32') {
|
|
495
|
+
return { ok: false, error: 'only-windows', detail: '试听只能在 Windows 上播放' }
|
|
496
|
+
}
|
|
497
|
+
const audio = audioNameOf(kind, settings)
|
|
498
|
+
if (audio === '') {
|
|
499
|
+
return { ok: false, error: 'no-audio', detail: '这个档位没有配置提示音(留空 = 静音)' }
|
|
500
|
+
}
|
|
501
|
+
const audioPath = path.join(path.resolve(settings.audioDir), audio)
|
|
502
|
+
if (!fs.existsSync(audioPath)) {
|
|
503
|
+
return { ok: false, error: 'audio-not-found', detail: `找不到音频文件:${audioPath}` }
|
|
504
|
+
}
|
|
505
|
+
const scriptPath = path.resolve(settings.scriptPath)
|
|
506
|
+
if (!fs.existsSync(scriptPath)) {
|
|
507
|
+
return { ok: false, error: 'host-script-not-found', detail: `找不到提醒脚本宿主:${scriptPath}` }
|
|
508
|
+
}
|
|
509
|
+
const argv = [
|
|
510
|
+
'-NoProfile',
|
|
511
|
+
'-NonInteractive',
|
|
512
|
+
'-ExecutionPolicy',
|
|
513
|
+
'Bypass',
|
|
514
|
+
'-WindowStyle',
|
|
515
|
+
'Hidden',
|
|
516
|
+
'-File',
|
|
517
|
+
scriptPath,
|
|
518
|
+
'-Preview',
|
|
519
|
+
'-Audio',
|
|
520
|
+
audioPath,
|
|
521
|
+
]
|
|
522
|
+
try {
|
|
523
|
+
const { pid, exited } = await launch(settings.powershellPath, argv, PACKAGE_ROOT)
|
|
524
|
+
const early = await Promise.race([exited, new Promise((r) => setTimeout(() => r('running'), 900))])
|
|
525
|
+
return { ok: true, kind, audio: audioPath, pid, earlyExit: early === 'running' ? null : early }
|
|
526
|
+
} catch (error) {
|
|
527
|
+
return { ok: false, error: 'spawn-failed', detail: String((error && error.message) || error) }
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function readJsonBody(req, limit = 256 * 1024) {
|
|
532
|
+
return new Promise((resolve, reject) => {
|
|
533
|
+
let size = 0
|
|
534
|
+
const chunks = []
|
|
535
|
+
req.on('data', (chunk) => {
|
|
536
|
+
size += chunk.length
|
|
537
|
+
if (size > limit) {
|
|
538
|
+
reject(new Error('request body is too large'))
|
|
539
|
+
req.destroy()
|
|
540
|
+
return
|
|
541
|
+
}
|
|
542
|
+
chunks.push(chunk)
|
|
543
|
+
})
|
|
544
|
+
req.on('end', () => {
|
|
545
|
+
const text = Buffer.concat(chunks).toString('utf8').trim()
|
|
546
|
+
if (text === '') return resolve({})
|
|
547
|
+
try {
|
|
548
|
+
resolve(JSON.parse(text))
|
|
549
|
+
} catch (error) {
|
|
550
|
+
reject(error)
|
|
551
|
+
}
|
|
552
|
+
})
|
|
553
|
+
req.on('error', reject)
|
|
554
|
+
})
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function registerConfigRoute(ctx, getSettings) {
|
|
558
|
+
const web = typeof ctx.get === 'function' ? ctx.get('webServer') : undefined
|
|
559
|
+
if (web === undefined || web === null || typeof web.register !== 'function') {
|
|
560
|
+
ctx.logger?.warn?.('dsh-reminder-notify: webServer is unavailable, the settings panel stays read-only')
|
|
561
|
+
return
|
|
562
|
+
}
|
|
563
|
+
const editable = new Set(Object.keys(settingsOf({})))
|
|
564
|
+
const reply = (res, status, payload) => {
|
|
565
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
566
|
+
res.end(JSON.stringify(payload))
|
|
567
|
+
}
|
|
568
|
+
const route = {
|
|
569
|
+
kind: 'exact',
|
|
570
|
+
path: CONFIG_ROUTE,
|
|
571
|
+
handler: async (req, res) => {
|
|
572
|
+
try {
|
|
573
|
+
if ((req.method ?? 'GET').toUpperCase() === 'GET') {
|
|
574
|
+
return reply(res, 200, { ok: true, writable: true, values: getSettings() })
|
|
575
|
+
}
|
|
576
|
+
const body = await readJsonBody(req)
|
|
577
|
+
const patch = body !== null && typeof body === 'object' && body.values !== null && typeof body.values === 'object'
|
|
578
|
+
? body.values
|
|
579
|
+
: body
|
|
580
|
+
const entry = ctx.fiber?.entry
|
|
581
|
+
const editor = typeof ctx.get === 'function' ? ctx.get('configEditor') : undefined
|
|
582
|
+
if (entry === undefined || editor === undefined || typeof editor.edit !== 'function') {
|
|
583
|
+
return reply(res, 409, { ok: false, error: 'configuration cannot be saved in this profile' })
|
|
584
|
+
}
|
|
585
|
+
const changes = {}
|
|
586
|
+
for (const [key, value] of Object.entries(patch ?? {})) {
|
|
587
|
+
if (editable.has(key)) changes[key] = value
|
|
588
|
+
}
|
|
589
|
+
if (Object.keys(changes).length === 0) {
|
|
590
|
+
return reply(res, 400, { ok: false, error: 'no editable field in the request' })
|
|
591
|
+
}
|
|
592
|
+
await editor.edit(entry, (current) => ({ ...current, ...changes }))
|
|
593
|
+
return reply(res, 200, { ok: true, writable: true, values: getSettings() })
|
|
594
|
+
} catch (error) {
|
|
595
|
+
return reply(res, 500, { ok: false, error: String(error?.message ?? error) })
|
|
596
|
+
}
|
|
597
|
+
},
|
|
598
|
+
}
|
|
599
|
+
ctx.effect(() => {
|
|
600
|
+
const dispose = web.register(route)
|
|
601
|
+
return () => {
|
|
602
|
+
if (typeof dispose === 'function') dispose()
|
|
603
|
+
}
|
|
604
|
+
}, 'dsh-reminder-notify: settings route')
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function registerPreviewRoute(ctx, getSettings) {
|
|
608
|
+
const web = typeof ctx.get === 'function' ? ctx.get('webServer') : undefined
|
|
609
|
+
if (web === undefined || web === null || typeof web.register !== 'function') {
|
|
610
|
+
ctx.logger?.warn?.('dsh-reminder-notify: webServer is unavailable, the preview buttons stay hidden')
|
|
611
|
+
return
|
|
612
|
+
}
|
|
613
|
+
const reply = (res, status, payload) => {
|
|
614
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
615
|
+
res.end(JSON.stringify(payload))
|
|
616
|
+
}
|
|
617
|
+
const route = {
|
|
618
|
+
kind: 'exact',
|
|
619
|
+
path: PREVIEW_ROUTE,
|
|
620
|
+
handler: async (req, res) => {
|
|
621
|
+
try {
|
|
622
|
+
if ((req.method ?? 'GET').toUpperCase() !== 'POST') {
|
|
623
|
+
return reply(res, 405, { ok: false, error: 'use POST' })
|
|
624
|
+
}
|
|
625
|
+
const body = await readJsonBody(req)
|
|
626
|
+
const kind = str(body?.kind, 'done')
|
|
627
|
+
if (!['done', 'decide', 'raw'].includes(kind)) {
|
|
628
|
+
return reply(res, 400, { ok: false, error: `unknown kind: ${kind}` })
|
|
629
|
+
}
|
|
630
|
+
const result = await previewAudio(kind, getSettings())
|
|
631
|
+
if (result.ok) return reply(res, 200, result)
|
|
632
|
+
const status = result.error === 'no-audio' || result.error === 'audio-not-found' ? 400 : 500
|
|
633
|
+
return reply(res, status, result)
|
|
634
|
+
} catch (error) {
|
|
635
|
+
return reply(res, 500, { ok: false, error: String(error?.message ?? error) })
|
|
636
|
+
}
|
|
637
|
+
},
|
|
638
|
+
}
|
|
639
|
+
ctx.effect(() => {
|
|
640
|
+
const dispose = web.register(route)
|
|
641
|
+
return () => {
|
|
642
|
+
if (typeof dispose === 'function') dispose()
|
|
643
|
+
}
|
|
644
|
+
}, 'dsh-reminder-notify: preview route')
|
|
645
|
+
}
|
|
646
|
+
|
|
490
647
|
export function apply(ctx, config) {
|
|
491
648
|
registerAudioRoute(ctx, () => settingsOf(config))
|
|
649
|
+
registerConfigRoute(ctx, () => settingsOf(config))
|
|
650
|
+
registerPreviewRoute(ctx, () => settingsOf(config))
|
|
492
651
|
const tools = typeof ctx.get === 'function' ? ctx.get('tools') : ctx.tools
|
|
493
652
|
if (tools === undefined || tools === null || typeof tools.register !== 'function') {
|
|
494
653
|
ctx.logger?.warn?.('dsh-reminder-notify: tools service is unavailable, notify_user was not registered')
|
package/package.json
CHANGED