fix: ošetření témat ntfy a jednorázová notifikace Objednána pizza
CI / Generate TypeScript types (push) Successful in 10s
CI / Server unit tests (push) Successful in 28s
CI / Build server (push) Successful in 30s
CI / Build client (push) Successful in 42s
CI / Playwright E2E tests (push) Successful in 1m40s
CI / Build and push Docker image (push) Successful in 51s
CI / Notify (push) Successful in 3s
CI / Generate TypeScript types (push) Successful in 10s
CI / Server unit tests (push) Successful in 28s
CI / Build server (push) Successful in 30s
CI / Build client (push) Successful in 42s
CI / Playwright E2E tests (push) Successful in 1m40s
CI / Build and push Docker image (push) Successful in 51s
CI / Notify (push) Successful in 3s
Téma ntfy vzniká z loginu nebo uživatelského nastavení a šlo do URL bez ošetření – login typu „x?email=…“ by přidal parametry požadavku s údaji aplikace. Posílá se jen na platná témata ([-_A-Za-z0-9], max. 64 znaků). Notifikace „Objednána pizza“ u skupin Pizza Chefie jde jen jednou za skupinu (příznak pizzaOrderedNotified), přepínání Objednáno ⇄ Uzamčeno ji už znovu nepošle.
This commit is contained in:
5 files changed
+61
-6
No files matched your search
@@ -43,6 +43,7 @@ function isChefieGroup(group: OrderGroup, stores: Store[] = []): boolean {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Ohlásí členům skupiny z Pizza Chefie, že je pizza objednaná (dřívější notifikace Pizza day).
|
* Ohlásí členům skupiny z Pizza Chefie, že je pizza objednaná (dřívější notifikace Pizza day).
|
||||||
|
* Posílá se jednou za skupinu (viz `pizzaOrderedNotified`), aby přepínání stavů nespamovalo.
|
||||||
* Bez čekání na výsledek — selhání notifikace nesmí shodit akci.
|
* Bez čekání na výsledek — selhání notifikace nesmí shodit akci.
|
||||||
*/
|
*/
|
||||||
function notifyPizzaOrdered(group: OrderGroup): void {
|
function notifyPizzaOrdered(group: OrderGroup): void {
|
||||||
@@ -225,10 +226,11 @@ export async function setGroupState(login: string, groupId: string, newState: Gr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
group.state = newState;
|
group.state = newState;
|
||||||
|
// Příznak se ukládá spolu se stavem a návrat do „uzamčeno“ ho nemaže — notifikace jde jen jednou
|
||||||
|
const notifyOrdered = newState === GroupState.ORDERED && !group.pizzaOrderedNotified && isChefieGroup(group, data.stores);
|
||||||
|
if (notifyOrdered) group.pizzaOrderedNotified = true;
|
||||||
const saved = await saveExtraData(data, date);
|
const saved = await saveExtraData(data, date);
|
||||||
if (newState === GroupState.ORDERED && isChefieGroup(group, data.stores)) {
|
if (notifyOrdered) notifyPizzaOrdered(group);
|
||||||
notifyPizzaOrdered(group);
|
|
||||||
}
|
|
||||||
return saved;
|
return saved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,15 @@ export async function saveNotificationSettings(login: string, settings: Notifica
|
|||||||
return settings;
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Platné téma ntfy (jen písmena, číslice, „-“ a „_“, nejvýše 64 znaků). Témata vznikají z loginů
|
||||||
|
* a uživatelských nastavení — cokoli jiného by se do URL ntfy dostalo jako cesta nebo parametry
|
||||||
|
* (např. „x?email=…“), a požadavek přitom nese přihlašovací údaje aplikace.
|
||||||
|
*/
|
||||||
|
export function isValidNtfyTopic(topic: string): boolean {
|
||||||
|
return /^[-_A-Za-z0-9]{1,64}$/.test(topic);
|
||||||
|
}
|
||||||
|
|
||||||
/** Odešle ntfy notifikaci na dané téma. */
|
/** Odešle ntfy notifikaci na dané téma. */
|
||||||
async function ntfyCallToTopic(topic: string, message: string) {
|
async function ntfyCallToTopic(topic: string, message: string) {
|
||||||
const url = process.env.NTFY_HOST;
|
const url = process.env.NTFY_HOST;
|
||||||
@@ -37,10 +46,14 @@ async function ntfyCallToTopic(topic: string, message: string) {
|
|||||||
if (!url || !username || !password) {
|
if (!url || !username || !password) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!isValidNtfyTopic(topic)) {
|
||||||
|
console.warn(`Neplatné téma ntfy "${topic}" — notifikace se neposílá`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64');
|
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64');
|
||||||
try {
|
try {
|
||||||
const response = await axios({
|
const response = await axios({
|
||||||
url: `${url}/${topic}`,
|
url: `${url}/${encodeURIComponent(topic)}`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: message,
|
data: message,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -80,8 +93,13 @@ export const ntfyCall = async (data: NotifikaceInput, recipients?: string[]) =>
|
|||||||
try {
|
try {
|
||||||
// Odstraníme mezery a diakritiku a převedeme na lowercase
|
// Odstraníme mezery a diakritiku a převedeme na lowercase
|
||||||
const topic = user.normalize('NFD').replace(' ', '').replace(/[\u0300-\u036f]/g, '').toLowerCase();
|
const topic = user.normalize('NFD').replace(' ', '').replace(/[\u0300-\u036f]/g, '').toLowerCase();
|
||||||
|
// Login je volný text — téma mimo povolené znaky by se do URL dostalo jako cesta/parametry
|
||||||
|
if (!isValidNtfyTopic(topic)) {
|
||||||
|
console.warn(`Login "${user}" nedává platné téma ntfy — notifikace se neposílá`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const response = await axios({
|
const response = await axios({
|
||||||
url: `${url}/${topic}`,
|
url: `${url}/${encodeURIComponent(topic)}`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: `${data.udalost} - spustil:${data.user}`,
|
data: `${data.udalost} - spustil:${data.user}`,
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@@ -382,6 +382,18 @@ describe('notifikace skupin z Pizza Chefie', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('přepínání Objednáno ⇄ Uzamčeno notifikaci znovu neposílá', async () => {
|
||||||
|
const created = await createGroup(CREATOR, CHEFIE_STORE, TODAY);
|
||||||
|
const groupId = created.groups![0].id;
|
||||||
|
await order(groupId);
|
||||||
|
expect(mockedNotifikace).toHaveBeenCalledTimes(1);
|
||||||
|
const reverted = await setGroupState(CREATOR, groupId, GroupState.LOCKED, TODAY);
|
||||||
|
// Návrat do uzamčeno maže QR i časy, ale příznak odeslané notifikace ponechá
|
||||||
|
expect(reverted.groups![0].pizzaOrderedNotified).toBe(true);
|
||||||
|
await setGroupState(CREATOR, groupId, GroupState.ORDERED, TODAY);
|
||||||
|
expect(mockedNotifikace).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
test('Doručeno žádnou další notifikaci neposílá', async () => {
|
test('Doručeno žádnou další notifikaci neposílá', async () => {
|
||||||
const created = await createGroup(CREATOR, CHEFIE_STORE, TODAY);
|
const created = await createGroup(CREATOR, CHEFIE_STORE, TODAY);
|
||||||
const groupId = created.groups![0].id;
|
const groupId = created.groups![0].id;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { resetMemoryStorage } from '../storage/memory';
|
import { resetMemoryStorage } from '../storage/memory';
|
||||||
import { callNotifikace, saveNotificationSettings } from '../notifikace';
|
import { callNotifikace, isValidNtfyTopic, saveNotificationSettings } from '../notifikace';
|
||||||
import { addChoice } from '../service';
|
import { addChoice } from '../service';
|
||||||
import { LunchChoice, UdalostEnum } from '../../../types/gen/types.gen';
|
import { LunchChoice, UdalostEnum } from '../../../types/gen/types.gen';
|
||||||
|
|
||||||
@@ -55,3 +55,23 @@ describe('callNotifikace — příjemci', () => {
|
|||||||
expect(ntfyTopics().sort()).toEqual(['petr', 'petr-osobni', 'tomaszakladatel']);
|
expect(ntfyTopics().sort()).toEqual(['petr', 'petr-osobni', 'tomaszakladatel']);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('ochrana URL ntfy', () => {
|
||||||
|
test('platné téma ntfy', () => {
|
||||||
|
expect(isValidNtfyTopic('tomaszakladatel')).toBe(true);
|
||||||
|
expect(isValidNtfyTopic('petr-osobni_2')).toBe(true);
|
||||||
|
expect(isValidNtfyTopic('x?email=a@b.cz')).toBe(false);
|
||||||
|
expect(isValidNtfyTopic('a/../v1/account')).toBe(false);
|
||||||
|
expect(isValidNtfyTopic('')).toBe(false);
|
||||||
|
expect(isValidNtfyTopic('a'.repeat(65))).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('login, který nedává platné téma, se přeskočí — ostatní příjemci notifikaci dostanou', async () => {
|
||||||
|
const warn = jest.spyOn(console, 'warn').mockImplementation(() => { });
|
||||||
|
await saveNotificationSettings('Petr', { ntfyTopic: 'x?call=yes', enabledEvents: [UdalostEnum.OBJEDNANA_PIZZA] });
|
||||||
|
await callNotifikace({ input: INPUT }, ['Tomáš Zakladatel', 'x?email=a@b.cz', 'a/../v1/account', 'Petr']);
|
||||||
|
// Globální ntfy jen pro platná témata; Petrovo osobní téma s parametrem se zahodí
|
||||||
|
expect(ntfyTopics().sort()).toEqual(['petr', 'tomaszakladatel']);
|
||||||
|
expect(warn).toHaveBeenCalledTimes(3);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1271,6 +1271,9 @@ OrderGroup:
|
|||||||
qrGenerated:
|
qrGenerated:
|
||||||
description: Příznak, zda byly pro skupinu vygenerovány QR kódy (blokuje opakované generování)
|
description: Příznak, zda byly pro skupinu vygenerovány QR kódy (blokuje opakované generování)
|
||||||
type: boolean
|
type: boolean
|
||||||
|
pizzaOrderedNotified:
|
||||||
|
description: Členům skupiny z Pizza Chefie už byla odeslána notifikace „Objednána pizza“ (posílá se jednou za skupinu, ani návrat do stavu uzamčeno ji neobnoví)
|
||||||
|
type: boolean
|
||||||
trackingProvider:
|
trackingProvider:
|
||||||
$ref: "#/TrackingProvider"
|
$ref: "#/TrackingProvider"
|
||||||
trackingCode:
|
trackingCode:
|
||||||
|
|||||||
Reference in new issue
Block a user