PWA improvements

This commit is contained in:
2025-09-07 17:10:50 +02:00
parent 5bed125cf6
commit 70d4db9901
10 changed files with 708 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ const express = require('express');
const { Pool } = require('pg');
const bcrypt = require('bcrypt');
const { start } = require('repl');
const pushService = require('../push-service');
const router = express.Router();
// PostgreSQL Pool mit .env Konfiguration
@@ -2475,6 +2476,17 @@ router.get('/v1/public/best-times', async (req, res) => {
router.post('/v1/public/subscribe', async (req, res) => {
try {
const { endpoint, keys } = req.body;
const userId = req.session.userId || 'anonymous';
// Generate a UUID for anonymous users or use existing UUID
let playerId;
if (userId === 'anonymous') {
// Generate a random UUID for anonymous users
const { v4: uuidv4 } = require('uuid');
playerId = uuidv4();
} else {
playerId = userId;
}
// Store subscription in database
await pool.query(`
@@ -2487,11 +2499,21 @@ router.post('/v1/public/subscribe', async (req, res) => {
auth = EXCLUDED.auth,
updated_at = NOW()
`, [
req.session.userId || 'anonymous',
playerId,
endpoint,
keys.p256dh,
keys.auth
]);
// Also store in push service for immediate use
const subscription = {
endpoint: endpoint,
keys: {
p256dh: keys.p256dh,
auth: keys.auth
}
};
pushService.subscribe(playerId, subscription);
res.json({
success: true,
@@ -2507,6 +2529,64 @@ router.post('/v1/public/subscribe', async (req, res) => {
}
});
// Test push notification endpoint
router.post('/v1/public/test-push', async (req, res) => {
try {
const { userId, message } = req.body;
const payload = {
title: '🧪 Test Notification',
body: message || 'Das ist eine Test-Push-Notification!',
icon: '/pictures/icon-192.png',
badge: '/pictures/icon-192.png',
data: {
type: 'test',
timestamp: Date.now()
}
};
const success = await pushService.sendToUser(userId || 'anonymous', payload);
res.json({
success: success,
message: success ? 'Test-Push gesendet!' : 'Keine Subscription gefunden'
});
} catch (error) {
console.error('Error sending test push:', error);
res.status(500).json({
success: false,
message: 'Fehler beim Senden der Test-Push'
});
}
});
// Get push subscription status
router.get('/v1/public/push-status', async (req, res) => {
try {
const userId = req.session.userId || 'anonymous';
// For anonymous users, we can't check specific subscription
// but we can show general stats
const hasSubscription = userId !== 'anonymous' ? pushService.subscriptions.has(userId) : false;
res.json({
success: true,
data: {
hasSubscription: hasSubscription,
totalSubscriptions: pushService.getSubscriptionCount(),
subscribedUsers: pushService.getSubscribedUsers(),
isAnonymous: userId === 'anonymous'
}
});
} catch (error) {
console.error('Error getting push status:', error);
res.status(500).json({
success: false,
message: 'Fehler beim Abrufen des Push-Status'
});
}
});
// ==================== ACHIEVEMENT ENDPOINTS ====================
/**