feat: knock notifications for admins + AFK auto-mute in calls
CI / Build & Quality Checks (push) Successful in 10m26s
Trigger Desktop Build / trigger (push) Successful in 14s

P4-3 — Knock-to-join Notifications for Admins:
- usePendingKnocks() hook reactively tracks knocking members via
  RoomMemberEvent.Membership; returns empty array if user lacks invite power
- Members icon in RoomViewHeader shows a Warning badge with the knock count
  when there are pending requests; badge updates in real time without
  needing to open the drawer; aria-label updated to describe pending count

P5-11 — AFK Auto-Mute in Voice:
- useAfkAutoMute() hook opens a monitoring-only getUserMedia stream,
  connects it to an AnalyserNode, and polls RMS every 500ms
- If mic is on and RMS stays below threshold for the configured timeout,
  calls callEmbed.control.setMicrophone(false) and shows an in-app toast
- Hook is called inside CallControls so monitoring is only active during calls
- Settings: afkAutoMute toggle + afkTimeoutMinutes select (1/5/10/20/30 min,
  default 10) added to Settings → Calls

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-12 19:53:19 -04:00
parent f15c4caf97
commit 362f4943d4
6 changed files with 206 additions and 8 deletions
+83
View File
@@ -0,0 +1,83 @@
import { useEffect } from 'react';
import { useSetAtom } from 'jotai';
import { CallEmbed } from '../plugins/call';
import { useSetting } from '../state/hooks/settings';
import { settingsAtom } from '../state/settings';
import { toastQueueAtom } from '../state/toast';
const SILENCE_RMS_THRESHOLD = 0.008;
const CHECK_INTERVAL_MS = 500;
/**
* Monitors microphone audio while in a call. If the mic stays active but
* silent for longer than the configured timeout, the mic is muted and a
* toast is shown. Cleans up its own AudioContext and stream on unmount.
*/
export function useAfkAutoMute(callEmbed: CallEmbed | undefined): void {
const [enabled] = useSetting(settingsAtom, 'afkAutoMute');
const [timeoutMinutes] = useSetting(settingsAtom, 'afkTimeoutMinutes');
const setToast = useSetAtom(toastQueueAtom);
useEffect(() => {
if (!callEmbed || !enabled) return;
let stream: MediaStream | undefined;
let audioCtx: AudioContext | undefined;
let intervalId: ReturnType<typeof setInterval> | undefined;
let silenceStart: number | null = null;
let active = true;
const timeoutMs = timeoutMinutes * 60 * 1000;
navigator.mediaDevices
.getUserMedia({ audio: true, video: false })
.then((s) => {
if (!active) {
s.getTracks().forEach((t) => t.stop());
return;
}
stream = s;
audioCtx = new AudioContext();
const source = audioCtx.createMediaStreamSource(stream);
const analyser = audioCtx.createAnalyser();
analyser.fftSize = 256;
source.connect(analyser);
const buffer = new Float32Array(analyser.fftSize);
intervalId = setInterval(() => {
if (!active) return;
analyser.getFloatTimeDomainData(buffer);
const rms = Math.sqrt(buffer.reduce((sum, v) => sum + v * v, 0) / buffer.length);
if (rms > SILENCE_RMS_THRESHOLD) {
// Audio detected — reset the silence timer
silenceStart = null;
} else if (callEmbed.control.microphone) {
// Mic is on but silent — start or advance the timer
if (silenceStart === null) silenceStart = Date.now();
else if (Date.now() - silenceStart >= timeoutMs) {
callEmbed.control.setMicrophone(false);
setToast({
id: `afk-mute-${Date.now()}`,
displayName: 'Lotus Chat',
body: 'Your microphone was muted after inactivity.',
roomName: 'Voice call',
roomId: callEmbed.roomId,
});
silenceStart = null;
}
} else {
// Mic is already muted — don't count silence
silenceStart = null;
}
}, CHECK_INTERVAL_MS);
})
.catch(() => undefined);
return () => {
active = false;
if (intervalId !== undefined) clearInterval(intervalId);
stream?.getTracks().forEach((t) => t.stop());
audioCtx?.close().catch(() => undefined);
};
}, [callEmbed, enabled, timeoutMinutes, setToast]);
}
+32
View File
@@ -0,0 +1,32 @@
import { useEffect, useReducer } from 'react';
import { MatrixEvent, Room, RoomMember, RoomMemberEvent } from 'matrix-js-sdk';
import { Membership } from '../../types/matrix/room';
import { useMatrixClient } from './useMatrixClient';
import { readPowerLevel, usePowerLevelsContext } from './usePowerLevels';
/**
* Returns the list of members currently knocking on the room, reactively.
* Returns an empty array if the current user lacks invite power level.
*/
export function usePendingKnocks(room: Room): RoomMember[] {
const mx = useMatrixClient();
const powerLevels = usePowerLevelsContext();
const [, forceUpdate] = useReducer((n: number) => n + 1, 0);
useEffect(() => {
const handler = (_evt: MatrixEvent, member: RoomMember) => {
if (member.roomId === room.roomId) forceUpdate();
};
mx.on(RoomMemberEvent.Membership, handler);
return () => {
mx.removeListener(RoomMemberEvent.Membership, handler);
};
}, [mx, room.roomId]);
const myUserId = mx.getUserId();
const myPowerLevel = readPowerLevel.user(powerLevels, myUserId ?? undefined);
const invitePowerLevel = readPowerLevel.action(powerLevels, 'invite');
const canApprove = myPowerLevel >= invitePowerLevel;
return canApprove ? room.getMembersWithMembership(Membership.Knock) : [];
}