|
| 1 | +import notifier from 'node-notifier'; |
| 2 | +import type Database from 'better-sqlite3'; |
| 3 | +import type { ConflictWithDetails } from '../types/index.js'; |
| 4 | + |
| 5 | +// Track conflict IDs that have already been notified (in-memory). |
| 6 | +// When a conflict is resolved, remove its tracked file from the set |
| 7 | +// so a new conflict on the same file will trigger a fresh notification. |
| 8 | +const notifiedTrackedFiles = new Set<string>(); |
| 9 | + |
| 10 | +function isEnabled(db: Database.Database): boolean { |
| 11 | + const row = db.prepare("SELECT value FROM settings WHERE key = 'desktop_notifications'").get() as |
| 12 | + | { value: string } |
| 13 | + | undefined; |
| 14 | + // Default to enabled if not set |
| 15 | + return row ? row.value === 'true' : true; |
| 16 | +} |
| 17 | + |
| 18 | +export function sendConflictNotification( |
| 19 | + db: Database.Database, |
| 20 | + conflict: ConflictWithDetails, |
| 21 | +): void { |
| 22 | + try { |
| 23 | + if (!isEnabled(db)) return; |
| 24 | + |
| 25 | + // Only notify once per tracked file until it's resolved |
| 26 | + if (notifiedTrackedFiles.has(conflict.trackedFileId)) return; |
| 27 | + notifiedTrackedFiles.add(conflict.trackedFileId); |
| 28 | + |
| 29 | + const target = conflict.repoName || conflict.serviceName || 'Unknown'; |
| 30 | + notifier.notify({ |
| 31 | + title: 'AI Sync — New Conflict', |
| 32 | + message: `${target}: ${conflict.relativePath}`, |
| 33 | + }); |
| 34 | + } catch { |
| 35 | + // Never let notification errors break sync |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export function clearNotifiedConflict(trackedFileId: string): void { |
| 40 | + notifiedTrackedFiles.delete(trackedFileId); |
| 41 | +} |
0 commit comments