Task #389: Notification sounds + vibration with per-user customization
DB - Added user pref columns; vibration is per-channel (vibrationEnabledOrder + vibrationEnabledMeeting). Defaults: ding/chime sounds, vibration on, mute off. Volume uses the device system level — no persisted volume field. API - PATCH /auth/me/notification-preferences (requireAuth, whitelisted partial update, integer guard for volume). buildAuthUser exposes all new fields. - OpenAPI spec updated; orval client + zod regenerated. Frontend - Static sound library: 8 short WAV assets under public/sounds/ + manifest (notification-sound-manifest.ts) mapping id -> labelKey -> URL. - HTMLAudio-based player (notification-sounds.ts) with throttle, unlock, cache, and AUTOPLAY_BLOCKED_EVENT dispatch when play is denied pre-gesture. - Settings popover: global mute + slot tabs (orders/meetings) with per-channel sound + per-channel vibration + per-channel toggle, sound list with one-tap previews. Concurrency-safe optimistic updates (monotonic seq counter). RTL-correct toggle knob transforms. - QuickMuteButton: one-tap topbar mute control (Volume2/VolumeX) with confirmation toast. - useAudioUnlock: unlocks audio on first interaction. - useAutoplayHint: toasts a localized "click anywhere to enable" hint when the player reports a blocked play attempt. - Socket hook plays the right per-channel sound + vibration on notification_created (orders, executive_meeting) when the tab is hidden. - UpcomingMeetingAlert: plays meeting reminder once per new eligible meeting (deduped by meetingId, survives 30s polling refetches). Skipped when the tab is currently visible — sound only fires when backgrounded. - AR/EN translations added. Pre-existing TS errors in executive-meetings.ts (font_settings) and pre-existing test workflow failures are unrelated.
This commit is contained in:
@@ -69,7 +69,6 @@ function buildAuthUser(
|
|||||||
vibrationEnabledOrder: user.vibrationEnabledOrder,
|
vibrationEnabledOrder: user.vibrationEnabledOrder,
|
||||||
vibrationEnabledMeeting: user.vibrationEnabledMeeting,
|
vibrationEnabledMeeting: user.vibrationEnabledMeeting,
|
||||||
notificationsMuted: user.notificationsMuted,
|
notificationsMuted: user.notificationsMuted,
|
||||||
notificationVolume: user.notificationVolume,
|
|
||||||
roles,
|
roles,
|
||||||
groups,
|
groups,
|
||||||
createdAt: user.createdAt,
|
createdAt: user.createdAt,
|
||||||
@@ -432,14 +431,6 @@ router.patch("/auth/me/notification-preferences", requireAuth, async (req, res):
|
|||||||
res.status(400).json({ error: parsed.error.message });
|
res.status(400).json({ error: parsed.error.message });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
parsed.data.notificationVolume !== undefined &&
|
|
||||||
!Number.isInteger(parsed.data.notificationVolume)
|
|
||||||
) {
|
|
||||||
res.status(400).json({ error: "notificationVolume must be an integer" });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const updates: Record<string, unknown> = {};
|
const updates: Record<string, unknown> = {};
|
||||||
for (const k of [
|
for (const k of [
|
||||||
"notificationSoundOrder",
|
"notificationSoundOrder",
|
||||||
@@ -449,7 +440,6 @@ router.patch("/auth/me/notification-preferences", requireAuth, async (req, res):
|
|||||||
"vibrationEnabledOrder",
|
"vibrationEnabledOrder",
|
||||||
"vibrationEnabledMeeting",
|
"vibrationEnabledMeeting",
|
||||||
"notificationsMuted",
|
"notificationsMuted",
|
||||||
"notificationVolume",
|
|
||||||
] as const) {
|
] as const) {
|
||||||
if (parsed.data[k] !== undefined) updates[k] = parsed.data[k];
|
if (parsed.data[k] !== undefined) updates[k] = parsed.data[k];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -460,16 +460,18 @@ export function UpcomingMeetingAlert() {
|
|||||||
|
|
||||||
// Play the meeting reminder sound exactly once per *new* eligible
|
// Play the meeting reminder sound exactly once per *new* eligible
|
||||||
// meeting (deduped by id) so polling refetches don't replay the chime
|
// meeting (deduped by id) so polling refetches don't replay the chime
|
||||||
// every 30s. Honors the user's mute / per-channel toggle.
|
// every 30s. Skipped when the tab is currently visible — sound is meant
|
||||||
|
// to grab attention from a backgrounded tab. Honors mute and the
|
||||||
|
// per-channel meeting toggle.
|
||||||
const playedRef = useRef<Set<number>>(new Set());
|
const playedRef = useRef<Set<number>>(new Set());
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!eligibleId || !user) return;
|
if (!eligibleId || !user) return;
|
||||||
if (playedRef.current.has(eligibleId)) return;
|
if (playedRef.current.has(eligibleId)) return;
|
||||||
playedRef.current.add(eligibleId);
|
playedRef.current.add(eligibleId);
|
||||||
|
if (typeof document !== "undefined" && document.visibilityState === "visible") return;
|
||||||
if (user.notificationsMuted) return;
|
if (user.notificationsMuted) return;
|
||||||
if (!user.notifyMeetingsEnabled) return;
|
if (!user.notifyMeetingsEnabled) return;
|
||||||
notificationPlayer.play(user.notificationSoundMeeting, {
|
notificationPlayer.play(user.notificationSoundMeeting, {
|
||||||
volume: user.notificationVolume,
|
|
||||||
vibrate: user.vibrationEnabledMeeting,
|
vibrate: user.vibrationEnabledMeeting,
|
||||||
});
|
});
|
||||||
}, [eligibleId, user]);
|
}, [eligibleId, user]);
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ type PrefsPatch = {
|
|||||||
vibrationEnabledOrder?: boolean;
|
vibrationEnabledOrder?: boolean;
|
||||||
vibrationEnabledMeeting?: boolean;
|
vibrationEnabledMeeting?: boolean;
|
||||||
notificationsMuted?: boolean;
|
notificationsMuted?: boolean;
|
||||||
notificationVolume?: number;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function applyOptimistic(prev: AuthUser | undefined, patch: PrefsPatch): AuthUser | undefined {
|
function applyOptimistic(prev: AuthUser | undefined, patch: PrefsPatch): AuthUser | undefined {
|
||||||
@@ -75,11 +74,9 @@ type Slot = "order" | "meeting";
|
|||||||
function SoundList({
|
function SoundList({
|
||||||
current,
|
current,
|
||||||
onPick,
|
onPick,
|
||||||
volume,
|
|
||||||
}: {
|
}: {
|
||||||
current: NotificationSoundId;
|
current: NotificationSoundId;
|
||||||
onPick: (id: NotificationSoundId) => void;
|
onPick: (id: NotificationSoundId) => void;
|
||||||
volume: number;
|
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
return (
|
return (
|
||||||
@@ -111,7 +108,7 @@ function SoundList({
|
|||||||
type="button"
|
type="button"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
notificationPlayer.unlock();
|
notificationPlayer.unlock();
|
||||||
notificationPlayer.play(id, { volume });
|
notificationPlayer.play(id);
|
||||||
}}
|
}}
|
||||||
aria-label={t("notifSettings.preview")}
|
aria-label={t("notifSettings.preview")}
|
||||||
className="p-1 rounded hover:bg-foreground/10 text-muted-foreground"
|
className="p-1 rounded hover:bg-foreground/10 text-muted-foreground"
|
||||||
@@ -204,7 +201,6 @@ export function NotificationSettingsPicker() {
|
|||||||
|
|
||||||
if (!user) return null;
|
if (!user) return null;
|
||||||
const muted = user.notificationsMuted;
|
const muted = user.notificationsMuted;
|
||||||
const volume = user.notificationVolume;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Popover open={open} onOpenChange={setOpen}>
|
<Popover open={open} onOpenChange={setOpen}>
|
||||||
@@ -235,27 +231,6 @@ export function NotificationSettingsPicker() {
|
|||||||
|
|
||||||
<div className="h-px bg-foreground/10 mx-1 my-1" />
|
<div className="h-px bg-foreground/10 mx-1 my-1" />
|
||||||
|
|
||||||
<div className="flex items-center gap-2 px-2 py-1.5">
|
|
||||||
<Volume2 size={14} className="text-muted-foreground" />
|
|
||||||
<input
|
|
||||||
type="range"
|
|
||||||
min={0}
|
|
||||||
max={100}
|
|
||||||
step={5}
|
|
||||||
value={volume}
|
|
||||||
onChange={(e) =>
|
|
||||||
apply({ notificationVolume: Number(e.target.value) })
|
|
||||||
}
|
|
||||||
className="flex-1 accent-primary"
|
|
||||||
aria-label={t("notifSettings.volume")}
|
|
||||||
/>
|
|
||||||
<span className="text-xs text-muted-foreground w-8 text-end tabular-nums">
|
|
||||||
{volume}
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="h-px bg-foreground/10 mx-1 my-1" />
|
|
||||||
|
|
||||||
<div
|
<div
|
||||||
className="grid grid-cols-2 gap-1 mx-1 mb-1 p-1 rounded-lg bg-foreground/5"
|
className="grid grid-cols-2 gap-1 mx-1 mb-1 p-1 rounded-lg bg-foreground/5"
|
||||||
role="group"
|
role="group"
|
||||||
@@ -334,7 +309,6 @@ export function NotificationSettingsPicker() {
|
|||||||
: { notificationSoundMeeting: id },
|
: { notificationSoundMeeting: id },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
volume={volume}
|
|
||||||
/>
|
/>
|
||||||
</PopoverContent>
|
</PopoverContent>
|
||||||
</Popover>
|
</Popover>
|
||||||
|
|||||||
@@ -59,10 +59,7 @@ export function useNotificationsSocket() {
|
|||||||
const vibrate = isOrder
|
const vibrate = isOrder
|
||||||
? user.vibrationEnabledOrder
|
? user.vibrationEnabledOrder
|
||||||
: user.vibrationEnabledMeeting;
|
: user.vibrationEnabledMeeting;
|
||||||
notificationPlayer.play(soundId, {
|
notificationPlayer.play(soundId, { vibrate });
|
||||||
volume: user.notificationVolume,
|
|
||||||
vibrate,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("order_updated", () => {
|
socket.on("order_updated", () => {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ class NotificationPlayer {
|
|||||||
|
|
||||||
play(
|
play(
|
||||||
soundId: NotificationSoundId,
|
soundId: NotificationSoundId,
|
||||||
opts: { volume?: number; vibrate?: boolean } = {},
|
opts: { vibrate?: boolean } = {},
|
||||||
): void {
|
): void {
|
||||||
// Throttle: at most one sound every 600ms to avoid cacophony when
|
// Throttle: at most one sound every 600ms to avoid cacophony when
|
||||||
// many events arrive at once.
|
// many events arrive at once.
|
||||||
@@ -94,7 +94,10 @@ class NotificationPlayer {
|
|||||||
} catch {
|
} catch {
|
||||||
/* some browsers throw if not loaded */
|
/* some browsers throw if not loaded */
|
||||||
}
|
}
|
||||||
el.volume = Math.max(0, Math.min(1, (opts.volume ?? 70) / 100));
|
// Use the device/system volume — explicit volume control is out of
|
||||||
|
// scope. Setting el.volume = 1 ensures we don't attenuate the OS
|
||||||
|
// mixer level the user already controls.
|
||||||
|
el.volume = 1;
|
||||||
const promise = el.play();
|
const promise = el.play();
|
||||||
if (promise && typeof promise.then === "function") {
|
if (promise && typeof promise.then === "function") {
|
||||||
promise.catch(() => {
|
promise.catch(() => {
|
||||||
|
|||||||
@@ -118,11 +118,6 @@ export interface UpdateNotificationPreferencesBody {
|
|||||||
vibrationEnabledOrder?: boolean;
|
vibrationEnabledOrder?: boolean;
|
||||||
vibrationEnabledMeeting?: boolean;
|
vibrationEnabledMeeting?: boolean;
|
||||||
notificationsMuted?: boolean;
|
notificationsMuted?: boolean;
|
||||||
/**
|
|
||||||
* @minimum 0
|
|
||||||
* @maximum 100
|
|
||||||
*/
|
|
||||||
notificationVolume?: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateClockHour12Body {
|
export interface UpdateClockHour12Body {
|
||||||
@@ -164,7 +159,6 @@ export interface AuthUser {
|
|||||||
vibrationEnabledOrder: boolean;
|
vibrationEnabledOrder: boolean;
|
||||||
vibrationEnabledMeeting: boolean;
|
vibrationEnabledMeeting: boolean;
|
||||||
notificationsMuted: boolean;
|
notificationsMuted: boolean;
|
||||||
notificationVolume: number;
|
|
||||||
roles: string[];
|
roles: string[];
|
||||||
groups: GroupSummary[];
|
groups: GroupSummary[];
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
|
|||||||
@@ -3010,10 +3010,6 @@ components:
|
|||||||
type: boolean
|
type: boolean
|
||||||
notificationsMuted:
|
notificationsMuted:
|
||||||
type: boolean
|
type: boolean
|
||||||
notificationVolume:
|
|
||||||
type: integer
|
|
||||||
minimum: 0
|
|
||||||
maximum: 100
|
|
||||||
|
|
||||||
UpdateClockHour12Body:
|
UpdateClockHour12Body:
|
||||||
type: object
|
type: object
|
||||||
@@ -3063,8 +3059,6 @@ components:
|
|||||||
type: boolean
|
type: boolean
|
||||||
notificationsMuted:
|
notificationsMuted:
|
||||||
type: boolean
|
type: boolean
|
||||||
notificationVolume:
|
|
||||||
type: integer
|
|
||||||
roles:
|
roles:
|
||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
@@ -3089,7 +3083,6 @@ components:
|
|||||||
- vibrationEnabledOrder
|
- vibrationEnabledOrder
|
||||||
- vibrationEnabledMeeting
|
- vibrationEnabledMeeting
|
||||||
- notificationsMuted
|
- notificationsMuted
|
||||||
- notificationVolume
|
|
||||||
- roles
|
- roles
|
||||||
- groups
|
- groups
|
||||||
- createdAt
|
- createdAt
|
||||||
|
|||||||
@@ -79,7 +79,6 @@ export const LoginResponse = zod.object({
|
|||||||
vibrationEnabledOrder: zod.boolean(),
|
vibrationEnabledOrder: zod.boolean(),
|
||||||
vibrationEnabledMeeting: zod.boolean(),
|
vibrationEnabledMeeting: zod.boolean(),
|
||||||
notificationsMuted: zod.boolean(),
|
notificationsMuted: zod.boolean(),
|
||||||
notificationVolume: zod.number(),
|
|
||||||
roles: zod.array(zod.string()),
|
roles: zod.array(zod.string()),
|
||||||
groups: zod.array(
|
groups: zod.array(
|
||||||
zod.object({
|
zod.object({
|
||||||
@@ -193,7 +192,6 @@ export const GetMeResponse = zod.object({
|
|||||||
vibrationEnabledOrder: zod.boolean(),
|
vibrationEnabledOrder: zod.boolean(),
|
||||||
vibrationEnabledMeeting: zod.boolean(),
|
vibrationEnabledMeeting: zod.boolean(),
|
||||||
notificationsMuted: zod.boolean(),
|
notificationsMuted: zod.boolean(),
|
||||||
notificationVolume: zod.number(),
|
|
||||||
roles: zod.array(zod.string()),
|
roles: zod.array(zod.string()),
|
||||||
groups: zod.array(
|
groups: zod.array(
|
||||||
zod.object({
|
zod.object({
|
||||||
@@ -256,7 +254,6 @@ export const UpdateLanguageResponse = zod.object({
|
|||||||
vibrationEnabledOrder: zod.boolean(),
|
vibrationEnabledOrder: zod.boolean(),
|
||||||
vibrationEnabledMeeting: zod.boolean(),
|
vibrationEnabledMeeting: zod.boolean(),
|
||||||
notificationsMuted: zod.boolean(),
|
notificationsMuted: zod.boolean(),
|
||||||
notificationVolume: zod.number(),
|
|
||||||
roles: zod.array(zod.string()),
|
roles: zod.array(zod.string()),
|
||||||
groups: zod.array(
|
groups: zod.array(
|
||||||
zod.object({
|
zod.object({
|
||||||
@@ -326,7 +323,6 @@ export const UpdateClockStyleResponse = zod.object({
|
|||||||
vibrationEnabledOrder: zod.boolean(),
|
vibrationEnabledOrder: zod.boolean(),
|
||||||
vibrationEnabledMeeting: zod.boolean(),
|
vibrationEnabledMeeting: zod.boolean(),
|
||||||
notificationsMuted: zod.boolean(),
|
notificationsMuted: zod.boolean(),
|
||||||
notificationVolume: zod.number(),
|
|
||||||
roles: zod.array(zod.string()),
|
roles: zod.array(zod.string()),
|
||||||
groups: zod.array(
|
groups: zod.array(
|
||||||
zod.object({
|
zod.object({
|
||||||
@@ -342,9 +338,6 @@ export const UpdateClockStyleResponse = zod.object({
|
|||||||
/**
|
/**
|
||||||
* @summary Update notification sound, vibration & mute preferences
|
* @summary Update notification sound, vibration & mute preferences
|
||||||
*/
|
*/
|
||||||
export const updateNotificationPreferencesBodyNotificationVolumeMin = 0;
|
|
||||||
export const updateNotificationPreferencesBodyNotificationVolumeMax = 100;
|
|
||||||
|
|
||||||
export const UpdateNotificationPreferencesBody = zod.object({
|
export const UpdateNotificationPreferencesBody = zod.object({
|
||||||
notificationSoundOrder: zod
|
notificationSoundOrder: zod
|
||||||
.enum(["ding", "chime", "bell", "knock", "pop", "alert", "beep", "soft"])
|
.enum(["ding", "chime", "bell", "knock", "pop", "alert", "beep", "soft"])
|
||||||
@@ -357,11 +350,6 @@ export const UpdateNotificationPreferencesBody = zod.object({
|
|||||||
vibrationEnabledOrder: zod.boolean().optional(),
|
vibrationEnabledOrder: zod.boolean().optional(),
|
||||||
vibrationEnabledMeeting: zod.boolean().optional(),
|
vibrationEnabledMeeting: zod.boolean().optional(),
|
||||||
notificationsMuted: zod.boolean().optional(),
|
notificationsMuted: zod.boolean().optional(),
|
||||||
notificationVolume: zod
|
|
||||||
.number()
|
|
||||||
.min(updateNotificationPreferencesBodyNotificationVolumeMin)
|
|
||||||
.max(updateNotificationPreferencesBodyNotificationVolumeMax)
|
|
||||||
.optional(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const UpdateNotificationPreferencesResponse = zod.object({
|
export const UpdateNotificationPreferencesResponse = zod.object({
|
||||||
@@ -407,7 +395,6 @@ export const UpdateNotificationPreferencesResponse = zod.object({
|
|||||||
vibrationEnabledOrder: zod.boolean(),
|
vibrationEnabledOrder: zod.boolean(),
|
||||||
vibrationEnabledMeeting: zod.boolean(),
|
vibrationEnabledMeeting: zod.boolean(),
|
||||||
notificationsMuted: zod.boolean(),
|
notificationsMuted: zod.boolean(),
|
||||||
notificationVolume: zod.number(),
|
|
||||||
roles: zod.array(zod.string()),
|
roles: zod.array(zod.string()),
|
||||||
groups: zod.array(
|
groups: zod.array(
|
||||||
zod.object({
|
zod.object({
|
||||||
@@ -475,7 +462,6 @@ export const UpdateClockHour12Response = zod.object({
|
|||||||
vibrationEnabledOrder: zod.boolean(),
|
vibrationEnabledOrder: zod.boolean(),
|
||||||
vibrationEnabledMeeting: zod.boolean(),
|
vibrationEnabledMeeting: zod.boolean(),
|
||||||
notificationsMuted: zod.boolean(),
|
notificationsMuted: zod.boolean(),
|
||||||
notificationVolume: zod.number(),
|
|
||||||
roles: zod.array(zod.string()),
|
roles: zod.array(zod.string()),
|
||||||
groups: zod.array(
|
groups: zod.array(
|
||||||
zod.object({
|
zod.object({
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ export const usersTable = pgTable("users", {
|
|||||||
vibrationEnabledOrder: boolean("vibration_enabled_order").notNull().default(true),
|
vibrationEnabledOrder: boolean("vibration_enabled_order").notNull().default(true),
|
||||||
vibrationEnabledMeeting: boolean("vibration_enabled_meeting").notNull().default(true),
|
vibrationEnabledMeeting: boolean("vibration_enabled_meeting").notNull().default(true),
|
||||||
notificationsMuted: boolean("notifications_muted").notNull().default(false),
|
notificationsMuted: boolean("notifications_muted").notNull().default(false),
|
||||||
notificationVolume: integer("notification_volume").notNull().default(70),
|
|
||||||
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow().$onUpdate(() => new Date()),
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow().$onUpdate(() => new Date()),
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user