feat: akce "Neobědvám" přímo z push notifikace
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/workflow Pipeline was successful
This commit is contained in:
@@ -7,12 +7,30 @@ self.addEventListener('push', (event) => {
|
|||||||
body: data.body,
|
body: data.body,
|
||||||
icon: '/favicon.ico',
|
icon: '/favicon.ico',
|
||||||
tag: 'lunch-reminder',
|
tag: 'lunch-reminder',
|
||||||
|
actions: [
|
||||||
|
{ action: 'neobedvam', title: 'Mám vlastní/neobědvám' },
|
||||||
|
],
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('notificationclick', (event) => {
|
self.addEventListener('notificationclick', (event) => {
|
||||||
event.notification.close();
|
event.notification.close();
|
||||||
|
|
||||||
|
if (event.action === 'neobedvam') {
|
||||||
|
event.waitUntil(
|
||||||
|
self.registration.pushManager.getSubscription().then((subscription) => {
|
||||||
|
if (!subscription) return;
|
||||||
|
return fetch('/api/notifications/push/quickChoice', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ endpoint: subscription.endpoint }),
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
self.clients.matchAll({ type: 'window' }).then((clientList) => {
|
self.clients.matchAll({ type: 'window' }).then((clientList) => {
|
||||||
// Pokud je již otevřené okno, zaostříme na něj
|
// Pokud je již otevřené okno, zaostříme na něj
|
||||||
|
|||||||
@@ -68,6 +68,17 @@ export function getVapidPublicKey(): string | undefined {
|
|||||||
return process.env.VAPID_PUBLIC_KEY;
|
return process.env.VAPID_PUBLIC_KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Najde login uživatele podle push subscription endpointu. */
|
||||||
|
export async function findLoginByEndpoint(endpoint: string): Promise<string | undefined> {
|
||||||
|
const registry = await getRegistry();
|
||||||
|
for (const [login, entry] of Object.entries(registry)) {
|
||||||
|
if (entry.subscription.endpoint === endpoint) {
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
/** Zkontroluje a odešle připomínky uživatelům, kteří si nezvolili oběd. */
|
/** Zkontroluje a odešle připomínky uživatelům, kteří si nezvolili oběd. */
|
||||||
async function checkAndSendReminders(): Promise<void> {
|
async function checkAndSendReminders(): Promise<void> {
|
||||||
// Přeskočit víkendy
|
// Přeskočit víkendy
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ import express, { Request } from "express";
|
|||||||
import { getLogin } from "../auth";
|
import { getLogin } from "../auth";
|
||||||
import { parseToken } from "../utils";
|
import { parseToken } from "../utils";
|
||||||
import { getNotificationSettings, saveNotificationSettings } from "../notifikace";
|
import { getNotificationSettings, saveNotificationSettings } from "../notifikace";
|
||||||
import { subscribePush, unsubscribePush, getVapidPublicKey } from "../pushReminder";
|
import { subscribePush, unsubscribePush, getVapidPublicKey, findLoginByEndpoint } from "../pushReminder";
|
||||||
|
import { addChoice } from "../service";
|
||||||
|
import { getWebsocket } from "../websocket";
|
||||||
import { UpdateNotificationSettingsData } from "../../../types";
|
import { UpdateNotificationSettingsData } from "../../../types";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@@ -64,4 +66,21 @@ router.post("/push/unsubscribe", async (req, res, next) => {
|
|||||||
} catch (e: any) { next(e) }
|
} catch (e: any) { next(e) }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/** Rychlá akce z push notifikace — nastaví volbu bez JWT (identita přes subscription endpoint). */
|
||||||
|
router.post("/push/quickChoice", async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const { endpoint } = req.body;
|
||||||
|
if (!endpoint) {
|
||||||
|
return res.status(400).json({ error: "Nebyl předán endpoint" });
|
||||||
|
}
|
||||||
|
const login = await findLoginByEndpoint(endpoint);
|
||||||
|
if (!login) {
|
||||||
|
return res.status(404).json({ error: "Subscription nenalezena" });
|
||||||
|
}
|
||||||
|
const data = await addChoice(login, false, 'NEOBEDVAM', undefined, undefined);
|
||||||
|
getWebsocket().emit("message", data);
|
||||||
|
res.status(200).json({});
|
||||||
|
} catch (e: any) { next(e) }
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user