|
| 1 | +// Native OS notification bridge for ObjectOS One. |
| 2 | +// |
| 3 | +// Injected into every page of the main webview (alongside update-banner.js). |
| 4 | +// Activates only on the Console — where the in-app inbox lives — and only |
| 5 | +// inside Tauri. It polls the in-app inbox and, when the window is in the |
| 6 | +// background, surfaces each NEW message as a native OS notification (macOS |
| 7 | +// Notification Center / Windows toast / Linux libnotify) and badges the dock |
| 8 | +// with the count missed while away (cleared on focus). |
| 9 | +// |
| 10 | +// Source of truth is `sys_inbox_message` (ADR-0030 L5) — the same table the |
| 11 | +// Console bell reads. The dedicated `/api/v1/notifications` route isn't mounted |
| 12 | +// in this runtime, so we read the canonical inbox object via the data API. |
| 13 | +// |
| 14 | +// All native work goes through Rust commands (`notify_native`, `set_badge`, |
| 15 | +// `notif_request_permission`) via `core.invoke` — matching this app's pattern. |
| 16 | +// `withGlobalTauri` only exposes `core`/`event`, NOT the plugin/window JS APIs, |
| 17 | +// so we must not call `__TAURI__.notification`/`.window`/`.app` directly. |
| 18 | +(function () { |
| 19 | + if (window.__objectosNotifyBridge) return; |
| 20 | + // Only on the Console (its login/app routes live under /_console), and only |
| 21 | + // when the Tauri bridge is present (no-op in a plain browser). |
| 22 | + if (!/\/_console(\/|$)/.test(location.pathname)) return; |
| 23 | + if (!window.__TAURI__ || !window.__TAURI__.core) return; |
| 24 | + window.__objectosNotifyBridge = true; |
| 25 | + |
| 26 | + var invoke = window.__TAURI__.core.invoke; |
| 27 | + var INBOX_URL = '/api/v1/data/sys_inbox_message?sort=-created_at&limit=25'; |
| 28 | + var POLL_MS = 20000; |
| 29 | + var SEEN_KEY = '__objectos_notif_seen_v1'; |
| 30 | + var MAX_SEEN = 500; |
| 31 | + |
| 32 | + var baselined = false; // first successful poll adopts the backlog silently |
| 33 | + var inFlight = false; |
| 34 | + var fatal = false; // inbox object absent → stop |
| 35 | + var unseen = 0; // count surfaced while backgrounded, cleared on focus |
| 36 | + |
| 37 | + function loadSeen() { |
| 38 | + try { |
| 39 | + return new Set(JSON.parse(localStorage.getItem(SEEN_KEY) || '[]')); |
| 40 | + } catch (_) { |
| 41 | + return new Set(); |
| 42 | + } |
| 43 | + } |
| 44 | + function saveSeen(set) { |
| 45 | + try { |
| 46 | + var arr = Array.from(set); |
| 47 | + if (arr.length > MAX_SEEN) arr = arr.slice(arr.length - MAX_SEEN); |
| 48 | + localStorage.setItem(SEEN_KEY, JSON.stringify(arr)); |
| 49 | + } catch (_) {} |
| 50 | + } |
| 51 | + var seen = loadSeen(); |
| 52 | + |
| 53 | + // Surface only when the user isn't actively looking at the window. |
| 54 | + function backgrounded() { |
| 55 | + return document.visibilityState === 'hidden' || !document.hasFocus(); |
| 56 | + } |
| 57 | + |
| 58 | + function notify(title, body) { |
| 59 | + try { |
| 60 | + return invoke('notify_native', { title: title || 'Notification', body: body || '' }); |
| 61 | + } catch (_) {} |
| 62 | + } |
| 63 | + function setBadge(count) { |
| 64 | + try { |
| 65 | + return invoke('set_badge', { count: count > 0 ? count : null }); |
| 66 | + } catch (_) {} |
| 67 | + } |
| 68 | + |
| 69 | + // sys_inbox_message → the minimal shape we need for a toast. |
| 70 | + function view(row) { |
| 71 | + return { |
| 72 | + id: row && row.id, |
| 73 | + title: (row && row.title) || (row && row.topic) || 'Notification', |
| 74 | + body: (row && (row.body_md || row.body)) || '', |
| 75 | + createdAt: (row && row.created_at) || '', |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + async function poll() { |
| 80 | + if (fatal || inFlight) return; |
| 81 | + inFlight = true; |
| 82 | + try { |
| 83 | + var res = await fetch(INBOX_URL, { |
| 84 | + headers: { accept: 'application/json' }, |
| 85 | + credentials: 'same-origin', |
| 86 | + }); |
| 87 | + if (res.status === 401) return; // not signed in yet |
| 88 | + if (res.status === 404) { fatal = true; return; } // no inbox object |
| 89 | + if (!res.ok) return; |
| 90 | + |
| 91 | + var json = await res.json(); |
| 92 | + var rows = (json && (json.records || json.items || json.data)) || []; |
| 93 | + // Oldest-first so a burst toasts in chronological order. |
| 94 | + var list = rows |
| 95 | + .map(view) |
| 96 | + .filter(function (v) { return v.id; }) |
| 97 | + .sort(function (a, b) { |
| 98 | + return String(a.createdAt).localeCompare(String(b.createdAt)); |
| 99 | + }); |
| 100 | + var fresh = list.filter(function (v) { return !seen.has(v.id); }); |
| 101 | + if (!fresh.length) return; |
| 102 | + |
| 103 | + if (!baselined) { |
| 104 | + // First poll after a (re)load: adopt existing messages as baseline so |
| 105 | + // we don't replay the backlog as a burst of toasts. |
| 106 | + fresh.forEach(function (v) { seen.add(v.id); }); |
| 107 | + baselined = true; |
| 108 | + } else { |
| 109 | + var allowed = backgrounded(); |
| 110 | + fresh.forEach(function (v) { |
| 111 | + seen.add(v.id); |
| 112 | + if (allowed) { |
| 113 | + notify(v.title, String(v.body).slice(0, 240)); |
| 114 | + unseen += 1; |
| 115 | + } |
| 116 | + }); |
| 117 | + if (allowed && unseen > 0) setBadge(unseen); |
| 118 | + } |
| 119 | + saveSeen(seen); |
| 120 | + } catch (_) { |
| 121 | + // transient (offline, mid-navigation) — retry next tick |
| 122 | + } finally { |
| 123 | + inFlight = false; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + function onForeground() { |
| 128 | + // The user is looking now — clear the "missed while away" badge. |
| 129 | + unseen = 0; |
| 130 | + setBadge(0); |
| 131 | + poll(); |
| 132 | + } |
| 133 | + |
| 134 | + // Ask once up front, in the foreground, so the OS prompt isn't sprung from |
| 135 | + // the background. |
| 136 | + try { invoke('notif_request_permission'); } catch (_) {} |
| 137 | + |
| 138 | + poll(); |
| 139 | + setInterval(poll, POLL_MS); |
| 140 | + document.addEventListener('visibilitychange', function () { |
| 141 | + if (document.visibilityState === 'visible') onForeground(); |
| 142 | + }); |
| 143 | + window.addEventListener('focus', onForeground); |
| 144 | +})(); |
0 commit comments