Fix alert system initialization - clean up testAlertSound function scope
This commit is contained in:
+99
@@ -333,6 +333,35 @@
|
||||
|
||||
console.log('✓ Capture tools initialized successfully');
|
||||
console.log('Available: window.captureScreenshot, window.captureSnip, window.canvasToFile, window.recordScreen');
|
||||
|
||||
// Alert sound function
|
||||
window.testAlertSound = async function() {
|
||||
try {
|
||||
console.log('🔔 Testing alert sound...');
|
||||
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||
const now = audioContext.currentTime;
|
||||
|
||||
const playBell = (startTime, volume = 1) => {
|
||||
const osc = audioContext.createOscillator();
|
||||
const gain = audioContext.createGain();
|
||||
osc.connect(gain);
|
||||
gain.connect(audioContext.destination);
|
||||
osc.frequency.setValueAtTime(1000, startTime);
|
||||
osc.frequency.exponentialRampToValueAtTime(600, startTime + 0.15);
|
||||
gain.gain.setValueAtTime(0.3 * volume, startTime);
|
||||
gain.gain.exponentialRampToValueAtTime(0.01, startTime + 0.2);
|
||||
osc.start(startTime);
|
||||
osc.stop(startTime + 0.2);
|
||||
};
|
||||
|
||||
playBell(now, 1);
|
||||
playBell(now + 0.2, 0.7);
|
||||
console.log('✓ Sound played');
|
||||
} catch (error) {
|
||||
console.error('Failed to play sound:', error);
|
||||
}
|
||||
};
|
||||
console.log('✓ testAlertSound available - call window.testAlertSound()');
|
||||
</script>
|
||||
</head>
|
||||
<body class="bg-gradient-to-br from-primary to-secondary p-4 sm:p-8 flex flex-col items-center justify-center font-sans relative h-screen overflow-hidden">
|
||||
@@ -1358,6 +1387,76 @@
|
||||
console.log(`User display name: ${displayName}`);
|
||||
console.log(`User email: ${email}`);
|
||||
console.log('PB token:', pb.authStore.token ? '(present)' : '(missing)');
|
||||
|
||||
// Initialize user alert system for shared notes
|
||||
try {
|
||||
const alertedNotes = new Set();
|
||||
|
||||
async function verifyUserMatch(firstName) {
|
||||
try {
|
||||
console.log(` Checking: firstName="${firstName}", email="${email}"`);
|
||||
const records = await pb.collection('Associations').getList(1, 50, {
|
||||
filter: `first_name = "${firstName}"`,
|
||||
});
|
||||
|
||||
console.log(` Found ${records.items.length} records`);
|
||||
|
||||
const match = records.items.find((record) => {
|
||||
console.log(` Checking: ${record.first_name} ${record.last_name} (${record.email})`);
|
||||
return record.email === email;
|
||||
});
|
||||
|
||||
return !!match;
|
||||
} catch (error) {
|
||||
console.error('Error verifying user match:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('🔔 Starting user alert monitoring...');
|
||||
await pb.collection('Notes').subscribe('*', async (event) => {
|
||||
try {
|
||||
const note = event.record;
|
||||
console.log(`📝 Note event (${event.action}):`, note.title, `shared=${note.shared}`);
|
||||
|
||||
if (!note.shared) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sharedWithNames = Array.isArray(note.shared_with)
|
||||
? note.shared_with
|
||||
: typeof note.shared_with === 'string'
|
||||
? note.shared_with.split(',').map((n) => n.trim())
|
||||
: [];
|
||||
|
||||
console.log(` → shared_with:`, sharedWithNames);
|
||||
|
||||
let userIsRecipient = false;
|
||||
for (const sharedName of sharedWithNames) {
|
||||
const firstName = sharedName.split(' ')[0];
|
||||
if (await verifyUserMatch(firstName)) {
|
||||
userIsRecipient = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!userIsRecipient || alertedNotes.has(note.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
alertedNotes.add(note.id);
|
||||
console.log(`🔔🔔 ALERT! Note "${note.title}" shared with you`);
|
||||
await window.testAlertSound();
|
||||
} catch (error) {
|
||||
console.error('Error processing note event:', error);
|
||||
}
|
||||
});
|
||||
|
||||
console.log('✓ User alert monitoring started');
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize user alert system:', error);
|
||||
}
|
||||
|
||||
updateAuthUI();
|
||||
await checkTokens();
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user