-
Notifications
You must be signed in to change notification settings - Fork 5
fix: sync Telegram alarms across directories and preserve latest tool states #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -560,29 +560,13 @@ export function Session() { | |||||||||||||||||||||||||
| })(), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| const [notifyDenied, setNotifyDenied] = createSignal(false); | ||||||||||||||||||||||||||
| const [notifySourceId, setNotifySourceId] = createSignal<string | undefined>(); | ||||||||||||||||||||||||||
| const [notifySourceReady, setNotifySourceReady] = createSignal(false); | ||||||||||||||||||||||||||
| const notifyVersion = { value: 0 }; | ||||||||||||||||||||||||||
| const deniedTimer = { id: null as ReturnType<typeof setTimeout> | null }; | ||||||||||||||||||||||||||
| onCleanup(() => { if (deniedTimer.id !== null) clearTimeout(deniedTimer.id) }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| createEffect(() => { | ||||||||||||||||||||||||||
| const dir = directory || base64Decode(params.dir); | ||||||||||||||||||||||||||
| setNotifySourceReady(false); | ||||||||||||||||||||||||||
| let cancelled = false; | ||||||||||||||||||||||||||
| onCleanup(() => { cancelled = true }); | ||||||||||||||||||||||||||
| resolveTelegramSourceId(url, dir).then((sourceId) => { | ||||||||||||||||||||||||||
| if (cancelled) return; | ||||||||||||||||||||||||||
| setNotifySourceId(sourceId); | ||||||||||||||||||||||||||
| setNotifySourceReady(true); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Re-read notification state when session changes, and seed from server alarm state | ||||||||||||||||||||||||||
| createEffect(() => { | ||||||||||||||||||||||||||
| const id = params.id; | ||||||||||||||||||||||||||
| const sourceReady = notifySourceReady(); | ||||||||||||||||||||||||||
| const sourceId = notifySourceId(); | ||||||||||||||||||||||||||
| const dir = directory || base64Decode(params.dir); | ||||||||||||||||||||||||||
| setNotifyDenied(false); | ||||||||||||||||||||||||||
| if (!id) { | ||||||||||||||||||||||||||
|
|
@@ -595,40 +579,38 @@ export function Session() { | |||||||||||||||||||||||||
| if (migrated) writeNotifyMap(map); | ||||||||||||||||||||||||||
| const local = notifyEnabledForSession(map, id, dir); | ||||||||||||||||||||||||||
| setNotifyEnabled(local); | ||||||||||||||||||||||||||
| if (!sourceReady) return; | ||||||||||||||||||||||||||
| // Cancellation flag for stale responses | ||||||||||||||||||||||||||
| let cancelled = false; | ||||||||||||||||||||||||||
| const version = notifyVersion.value; | ||||||||||||||||||||||||||
| onCleanup(() => { cancelled = true }); | ||||||||||||||||||||||||||
| // Then fetch server-side alarm state asynchronously | ||||||||||||||||||||||||||
| getSessionAlarm(url, id, sourceId).then((state) => { | ||||||||||||||||||||||||||
| // Skip if effect was cleaned up, session changed, or local state was updated | ||||||||||||||||||||||||||
| if (cancelled || !state || params.id !== id) return; | ||||||||||||||||||||||||||
| if (notifyVersion.value !== version) return; | ||||||||||||||||||||||||||
| setNotifyEnabled(state.enabled); | ||||||||||||||||||||||||||
| // Sync localStorage to match server truth | ||||||||||||||||||||||||||
| const map = readNotifyMap(); | ||||||||||||||||||||||||||
| writeNotifySessionEnabled(map, id, state.enabled, dir); | ||||||||||||||||||||||||||
| writeNotifyMap(map); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| resolveTelegramSourceId(url, dir) | ||||||||||||||||||||||||||
| .then((sourceId) => getSessionAlarm(url, id, sourceId)) | ||||||||||||||||||||||||||
| .then((state) => { | ||||||||||||||||||||||||||
| // Skip if effect was cleaned up, session changed, or local state was updated | ||||||||||||||||||||||||||
| if (cancelled || !state || params.id !== id) return; | ||||||||||||||||||||||||||
| if (notifyVersion.value !== version) return; | ||||||||||||||||||||||||||
| setNotifyEnabled(state.enabled); | ||||||||||||||||||||||||||
| // Sync localStorage to match server truth | ||||||||||||||||||||||||||
| const map = readNotifyMap(); | ||||||||||||||||||||||||||
| writeNotifySessionEnabled(map, id, state.enabled, dir); | ||||||||||||||||||||||||||
| writeNotifyMap(map); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** Mirror bell toggle to server-side alarm state (fire-and-forget). */ | ||||||||||||||||||||||||||
| function syncAlarmToServer(id: string, enabled: boolean) { | ||||||||||||||||||||||||||
| const sourceId = notifySourceId(); | ||||||||||||||||||||||||||
| if (sourceId) { | ||||||||||||||||||||||||||
| setSessionAlarm(url, id, enabled, sourceId); | ||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| const dir = directory || base64Decode(params.dir); | ||||||||||||||||||||||||||
| resolveTelegramSourceId(url, dir).then((resolved) => { | ||||||||||||||||||||||||||
| if (resolved) { | ||||||||||||||||||||||||||
| setNotifySourceId(resolved); | ||||||||||||||||||||||||||
| setSessionAlarm(url, id, enabled, resolved); | ||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| console.warn("[session] skip setSessionAlarm: telegram source id unresolved", { id, dir }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| const dir = directory || base64Decode(params.dir) | ||||||||||||||||||||||||||
| console.log("[session] syncing telegram session alarm", { sessionId: id, enabled, directory: dir }) | ||||||||||||||||||||||||||
| resolveTelegramSourceId(url, dir) | ||||||||||||||||||||||||||
| .then((sourceId) => setSessionAlarm(url, id, enabled, sourceId, dir)) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| .then((sourceId) => setSessionAlarm(url, id, enabled, sourceId, dir)) | |
| .then((sourceId) => { | |
| if (sourceId === undefined) { | |
| console.warn("[session] telegram session alarm sync failed: unable to resolve telegram source", { | |
| sessionId: id, | |
| enabled, | |
| directory: dir, | |
| }) | |
| return false | |
| } | |
| return setSessionAlarm(url, id, enabled, sourceId, dir) | |
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolveTelegramSourceId(url, dir)can returnundefined(e.g. multi-source enabled but no matching source for this directory). In that case this code callsgetSessionAlarmwithout asourceId, which will query the default source and can display the wrong alarm state for the current directory. Handle theundefinedcase explicitly (skip the request and keep local state, or surface a warning/error) rather than falling back to default implicitly.