feat(realtime): add voice/video recording activity events
Some checks are pending
CI / test (push) Has started running

This commit is contained in:
2026-03-08 19:53:48 +03:00
parent 1ef0cdf29d
commit ac82e25d16
8 changed files with 135 additions and 9 deletions

View File

@@ -75,7 +75,14 @@ async def _dispatch_event(db, user_id: int, event: IncomingRealtimeEvent) -> Non
payload = SendMessagePayload.model_validate(event.payload) payload = SendMessagePayload.model_validate(event.payload)
await realtime_gateway.handle_send_message(db, user_id, payload) await realtime_gateway.handle_send_message(db, user_id, payload)
return return
if event.event in {"typing_start", "typing_stop"}: if event.event in {
"typing_start",
"typing_stop",
"recording_voice_start",
"recording_voice_stop",
"recording_video_start",
"recording_video_stop",
}:
payload = ChatEventPayload.model_validate(event.payload) payload = ChatEventPayload.model_validate(event.payload)
await realtime_gateway.handle_typing_event(db, user_id, payload, event.event) await realtime_gateway.handle_typing_event(db, user_id, payload, event.event)
return return

View File

@@ -15,6 +15,10 @@ RealtimeEventName = Literal[
"message_deleted", "message_deleted",
"typing_start", "typing_start",
"typing_stop", "typing_stop",
"recording_voice_start",
"recording_voice_stop",
"recording_video_start",
"recording_video_stop",
"message_read", "message_read",
"message_delivered", "message_delivered",
"user_online", "user_online",
@@ -45,7 +49,18 @@ class MessageStatusPayload(BaseModel):
class IncomingRealtimeEvent(BaseModel): class IncomingRealtimeEvent(BaseModel):
event: Literal["send_message", "typing_start", "typing_stop", "message_read", "message_delivered", "ping"] event: Literal[
"send_message",
"typing_start",
"typing_stop",
"recording_voice_start",
"recording_voice_stop",
"recording_video_start",
"recording_video_stop",
"message_read",
"message_delivered",
"ping",
]
payload: dict[str, Any] payload: dict[str, Any]

View File

@@ -72,6 +72,15 @@ For `/health/ready` failure:
- `message_delivered` - `message_delivered`
- `message_read` - `message_read`
### Realtime chat activity events
- `typing_start`
- `typing_stop`
- `recording_voice_start`
- `recording_voice_stop`
- `recording_video_start`
- `recording_video_stop`
## 3. Models (request/response) ## 3. Models (request/response)
## 3.1 Auth ## 3.1 Auth

View File

@@ -21,7 +21,7 @@ Legend:
12. Pinning - `DONE` (message/chat pin-unpin) 12. Pinning - `DONE` (message/chat pin-unpin)
13. Reactions - `DONE` 13. Reactions - `DONE`
14. Delivery Status - `DONE` (sent/delivered/read + reconnect reconciliation after backend restarts) 14. Delivery Status - `DONE` (sent/delivered/read + reconnect reconciliation after backend restarts)
15. Typing Realtime - `PARTIAL` (typing start/stop done; voice/video typing signals limited) 15. Typing Realtime - `DONE` (typing start/stop + recording voice/video start/stop via realtime events)
16. Media & Attachments - `DONE` (upload/preview/download/gallery) 16. Media & Attachments - `DONE` (upload/preview/download/gallery)
17. Voice Messages - `PARTIAL` (record/send/play/seek + global speed 1x/1.5x/2x; recorder now uses mime fallback + chunked capture; UX still being polished) 17. Voice Messages - `PARTIAL` (record/send/play/seek + global speed 1x/1.5x/2x; recorder now uses mime fallback + chunked capture; UX still being polished)
18. Circle Video Messages - `PARTIAL` (send/play present, recording UX basic) 18. Circle Video Messages - `PARTIAL` (send/play present, recording UX basic)

View File

@@ -307,6 +307,22 @@ export function MessageComposer() {
return wsRef.current; return wsRef.current;
} }
function sendRealtimeChatEvent(
eventName:
| "typing_start"
| "typing_stop"
| "recording_voice_start"
| "recording_voice_stop"
| "recording_video_start"
| "recording_video_stop"
) {
if (!activeChatId) {
return;
}
const ws = getWs();
ws?.send(JSON.stringify({ event: eventName, payload: { chat_id: activeChatId } }));
}
async function handleSend() { async function handleSend() {
if (!activeChatId || !text.trim() || !me || !canSendInActiveChat) { if (!activeChatId || !text.trim() || !me || !canSendInActiveChat) {
return; return;
@@ -347,8 +363,7 @@ export function MessageComposer() {
setText(""); setText("");
clearDraft(activeChatId); clearDraft(activeChatId);
setReplyToMessage(activeChatId, null); setReplyToMessage(activeChatId, null);
const ws = getWs(); sendRealtimeChatEvent("typing_stop");
ws?.send(JSON.stringify({ event: "typing_stop", payload: { chat_id: activeChatId } }));
} catch { } catch {
removeOptimisticMessage(activeChatId, clientMessageId); removeOptimisticMessage(activeChatId, clientMessageId);
setUploadError("Message send failed. Please try again."); setUploadError("Message send failed. Please try again.");
@@ -619,6 +634,8 @@ export function MessageComposer() {
recordingStartedAtRef.current = Date.now(); recordingStartedAtRef.current = Date.now();
setRecordSeconds(0); setRecordSeconds(0);
setRecordingState("recording"); setRecordingState("recording");
sendRealtimeChatEvent("typing_stop");
sendRealtimeChatEvent("recording_voice_start");
return true; return true;
} catch { } catch {
setUploadError("Microphone access denied. Please allow microphone and retry."); setUploadError("Microphone access denied. Please allow microphone and retry.");
@@ -627,6 +644,9 @@ export function MessageComposer() {
} }
function stopRecord(send: boolean) { function stopRecord(send: boolean) {
if (recordingStateRef.current !== "idle") {
sendRealtimeChatEvent("recording_voice_stop");
}
sendVoiceOnStopRef.current = send; sendVoiceOnStopRef.current = send;
pointerCancelArmedRef.current = false; pointerCancelArmedRef.current = false;
setDragHint("idle"); setDragHint("idle");
@@ -1274,8 +1294,7 @@ export function MessageComposer() {
setText(next); setText(next);
if (activeChatId) { if (activeChatId) {
setDraft(activeChatId, next); setDraft(activeChatId, next);
const ws = getWs(); sendRealtimeChatEvent(next.trim().length > 0 ? "typing_start" : "typing_stop");
ws?.send(JSON.stringify({ event: "typing_start", payload: { chat_id: activeChatId } }));
} }
}} }}
/> />

View File

@@ -46,6 +46,8 @@ export function MessageList() {
const activeChatId = useChatStore((s) => s.activeChatId); const activeChatId = useChatStore((s) => s.activeChatId);
const messagesByChat = useChatStore((s) => s.messagesByChat); const messagesByChat = useChatStore((s) => s.messagesByChat);
const typingByChat = useChatStore((s) => s.typingByChat); const typingByChat = useChatStore((s) => s.typingByChat);
const recordingVoiceByChat = useChatStore((s) => s.recordingVoiceByChat);
const recordingVideoByChat = useChatStore((s) => s.recordingVideoByChat);
const hasMoreByChat = useChatStore((s) => s.hasMoreByChat); const hasMoreByChat = useChatStore((s) => s.hasMoreByChat);
const loadingMoreByChat = useChatStore((s) => s.loadingMoreByChat); const loadingMoreByChat = useChatStore((s) => s.loadingMoreByChat);
const loadMoreMessages = useChatStore((s) => s.loadMoreMessages); const loadMoreMessages = useChatStore((s) => s.loadMoreMessages);
@@ -130,6 +132,17 @@ export function MessageList() {
const focusedMessageId = activeChatId ? (focusedMessageIdByChat[activeChatId] ?? null) : null; const focusedMessageId = activeChatId ? (focusedMessageIdByChat[activeChatId] ?? null) : null;
const hasMore = Boolean(activeChatId && hasMoreByChat[activeChatId]); const hasMore = Boolean(activeChatId && hasMoreByChat[activeChatId]);
const isLoadingMore = Boolean(activeChatId && loadingMoreByChat[activeChatId]); const isLoadingMore = Boolean(activeChatId && loadingMoreByChat[activeChatId]);
const typingCount = activeChatId ? (typingByChat[activeChatId] ?? []).length : 0;
const recordingVoiceCount = activeChatId ? (recordingVoiceByChat[activeChatId] ?? []).length : 0;
const recordingVideoCount = activeChatId ? (recordingVideoByChat[activeChatId] ?? []).length : 0;
const activityLabel =
recordingVideoCount > 0
? "recording video..."
: recordingVoiceCount > 0
? "recording voice..."
: typingCount > 0
? "typing..."
: "";
const selectedMessages = useMemo(() => messages.filter((m) => selectedIds.has(m.id)), [messages, selectedIds]); const selectedMessages = useMemo(() => messages.filter((m) => selectedIds.has(m.id)), [messages, selectedIds]);
const channelOnlyDeleteForAll = activeChat?.type === "channel" && !activeChat?.is_saved; const channelOnlyDeleteForAll = activeChat?.type === "channel" && !activeChat?.is_saved;
const canDeleteAllForSelection = useMemo( const canDeleteAllForSelection = useMemo(
@@ -683,7 +696,7 @@ export function MessageList() {
})} })}
</div> </div>
<div className="px-5 pb-2 text-xs text-slate-300/80">{(typingByChat[activeChatId] ?? []).length > 0 ? "typing..." : ""}</div> <div className="px-5 pb-2 text-xs text-slate-300/80">{activityLabel}</div>
{showScrollToBottom ? ( {showScrollToBottom ? (
<div className="pointer-events-none absolute bottom-20 right-4 z-40 md:bottom-24"> <div className="pointer-events-none absolute bottom-20 right-4 z-40 md:bottom-24">
<button <button

View File

@@ -16,6 +16,8 @@ export function useRealtime() {
const accessToken = useAuthStore((s) => s.accessToken); const accessToken = useAuthStore((s) => s.accessToken);
const meId = useAuthStore((s) => s.me?.id ?? null); const meId = useAuthStore((s) => s.me?.id ?? null);
const typingByChat = useRef<Record<number, Set<number>>>({}); const typingByChat = useRef<Record<number, Set<number>>>({});
const recordingVoiceByChat = useRef<Record<number, Set<number>>>({});
const recordingVideoByChat = useRef<Record<number, Set<number>>>({});
const wsRef = useRef<WebSocket | null>(null); const wsRef = useRef<WebSocket | null>(null);
const reconnectTimeoutRef = useRef<number | null>(null); const reconnectTimeoutRef = useRef<number | null>(null);
const heartbeatIntervalRef = useRef<number | null>(null); const heartbeatIntervalRef = useRef<number | null>(null);
@@ -171,6 +173,48 @@ export function useRealtime() {
typingByChat.current[chatId]?.delete(userId); typingByChat.current[chatId]?.delete(userId);
chatStore.setTypingUsers(chatId, [...(typingByChat.current[chatId] ?? [])]); chatStore.setTypingUsers(chatId, [...(typingByChat.current[chatId] ?? [])]);
} }
if (event.event === "recording_voice_start") {
const chatId = Number(event.payload.chat_id);
const userId = Number(event.payload.user_id);
if (!Number.isFinite(chatId) || !Number.isFinite(userId) || userId === authStore.me?.id) {
return;
}
if (!recordingVoiceByChat.current[chatId]) {
recordingVoiceByChat.current[chatId] = new Set<number>();
}
recordingVoiceByChat.current[chatId].add(userId);
chatStore.setRecordingUsers(chatId, "voice", [...recordingVoiceByChat.current[chatId]]);
}
if (event.event === "recording_voice_stop") {
const chatId = Number(event.payload.chat_id);
const userId = Number(event.payload.user_id);
if (!Number.isFinite(chatId) || !Number.isFinite(userId)) {
return;
}
recordingVoiceByChat.current[chatId]?.delete(userId);
chatStore.setRecordingUsers(chatId, "voice", [...(recordingVoiceByChat.current[chatId] ?? [])]);
}
if (event.event === "recording_video_start") {
const chatId = Number(event.payload.chat_id);
const userId = Number(event.payload.user_id);
if (!Number.isFinite(chatId) || !Number.isFinite(userId) || userId === authStore.me?.id) {
return;
}
if (!recordingVideoByChat.current[chatId]) {
recordingVideoByChat.current[chatId] = new Set<number>();
}
recordingVideoByChat.current[chatId].add(userId);
chatStore.setRecordingUsers(chatId, "video", [...recordingVideoByChat.current[chatId]]);
}
if (event.event === "recording_video_stop") {
const chatId = Number(event.payload.chat_id);
const userId = Number(event.payload.user_id);
if (!Number.isFinite(chatId) || !Number.isFinite(userId)) {
return;
}
recordingVideoByChat.current[chatId]?.delete(userId);
chatStore.setRecordingUsers(chatId, "video", [...(recordingVideoByChat.current[chatId] ?? [])]);
}
if (event.event === "message_delivered") { if (event.event === "message_delivered") {
const chatId = Number(event.payload.chat_id); const chatId = Number(event.payload.chat_id);
const messageId = Number(event.payload.message_id); const messageId = Number(event.payload.message_id);
@@ -280,7 +324,9 @@ export function useRealtime() {
wsRef.current?.close(); wsRef.current?.close();
wsRef.current = null; wsRef.current = null;
typingByChat.current = {}; typingByChat.current = {};
useChatStore.setState({ typingByChat: {} }); recordingVoiceByChat.current = {};
recordingVideoByChat.current = {};
useChatStore.setState({ typingByChat: {}, recordingVoiceByChat: {}, recordingVideoByChat: {} });
window.removeEventListener("focus", onFocusOrVisible); window.removeEventListener("focus", onFocusOrVisible);
document.removeEventListener("visibilitychange", onFocusOrVisible); document.removeEventListener("visibilitychange", onFocusOrVisible);
}; };

View File

@@ -61,6 +61,8 @@ interface ChatState {
hasMoreByChat: Record<number, boolean>; hasMoreByChat: Record<number, boolean>;
loadingMoreByChat: Record<number, boolean>; loadingMoreByChat: Record<number, boolean>;
typingByChat: Record<number, number[]>; typingByChat: Record<number, number[]>;
recordingVoiceByChat: Record<number, number[]>;
recordingVideoByChat: Record<number, number[]>;
replyToByChat: Record<number, Message | null>; replyToByChat: Record<number, Message | null>;
editingByChat: Record<number, Message | null>; editingByChat: Record<number, Message | null>;
unreadBoundaryByChat: Record<number, number>; unreadBoundaryByChat: Record<number, number>;
@@ -93,6 +95,7 @@ interface ChatState {
incrementUnread: (chatId: number, hasMention?: boolean) => void; incrementUnread: (chatId: number, hasMention?: boolean) => void;
clearUnread: (chatId: number) => void; clearUnread: (chatId: number) => void;
setTypingUsers: (chatId: number, userIds: number[]) => void; setTypingUsers: (chatId: number, userIds: number[]) => void;
setRecordingUsers: (chatId: number, kind: "voice" | "video", userIds: number[]) => void;
setReplyToMessage: (chatId: number, message: Message | null) => void; setReplyToMessage: (chatId: number, message: Message | null) => void;
setEditingMessage: (chatId: number, message: Message | null) => void; setEditingMessage: (chatId: number, message: Message | null) => void;
updateChatPinnedMessage: (chatId: number, pinnedMessageId: number | null) => void; updateChatPinnedMessage: (chatId: number, pinnedMessageId: number | null) => void;
@@ -111,6 +114,8 @@ export const useChatStore = create<ChatState>((set, get) => ({
hasMoreByChat: {}, hasMoreByChat: {},
loadingMoreByChat: {}, loadingMoreByChat: {},
typingByChat: {}, typingByChat: {},
recordingVoiceByChat: {},
recordingVideoByChat: {},
replyToByChat: {}, replyToByChat: {},
editingByChat: {}, editingByChat: {},
unreadBoundaryByChat: {}, unreadBoundaryByChat: {},
@@ -397,6 +402,12 @@ export const useChatStore = create<ChatState>((set, get) => ({
})), })),
setTypingUsers: (chatId, userIds) => setTypingUsers: (chatId, userIds) =>
set((state) => ({ typingByChat: { ...state.typingByChat, [chatId]: userIds } })), set((state) => ({ typingByChat: { ...state.typingByChat, [chatId]: userIds } })),
setRecordingUsers: (chatId, kind, userIds) =>
set((state) =>
kind === "voice"
? { recordingVoiceByChat: { ...state.recordingVoiceByChat, [chatId]: userIds } }
: { recordingVideoByChat: { ...state.recordingVideoByChat, [chatId]: userIds } }
),
setReplyToMessage: (chatId, message) => setReplyToMessage: (chatId, message) =>
set((state) => ({ replyToByChat: { ...state.replyToByChat, [chatId]: message } })), set((state) => ({ replyToByChat: { ...state.replyToByChat, [chatId]: message } })),
setEditingMessage: (chatId, message) => setEditingMessage: (chatId, message) =>
@@ -438,6 +449,8 @@ export const useChatStore = create<ChatState>((set, get) => ({
const nextHasMoreByChat = { ...state.hasMoreByChat }; const nextHasMoreByChat = { ...state.hasMoreByChat };
const nextLoadingMoreByChat = { ...state.loadingMoreByChat }; const nextLoadingMoreByChat = { ...state.loadingMoreByChat };
const nextTypingByChat = { ...state.typingByChat }; const nextTypingByChat = { ...state.typingByChat };
const nextRecordingVoiceByChat = { ...state.recordingVoiceByChat };
const nextRecordingVideoByChat = { ...state.recordingVideoByChat };
const nextReplyToByChat = { ...state.replyToByChat }; const nextReplyToByChat = { ...state.replyToByChat };
const nextEditingByChat = { ...state.editingByChat }; const nextEditingByChat = { ...state.editingByChat };
const nextUnreadBoundaryByChat = { ...state.unreadBoundaryByChat }; const nextUnreadBoundaryByChat = { ...state.unreadBoundaryByChat };
@@ -447,6 +460,8 @@ export const useChatStore = create<ChatState>((set, get) => ({
delete nextHasMoreByChat[chatId]; delete nextHasMoreByChat[chatId];
delete nextLoadingMoreByChat[chatId]; delete nextLoadingMoreByChat[chatId];
delete nextTypingByChat[chatId]; delete nextTypingByChat[chatId];
delete nextRecordingVoiceByChat[chatId];
delete nextRecordingVideoByChat[chatId];
delete nextReplyToByChat[chatId]; delete nextReplyToByChat[chatId];
delete nextEditingByChat[chatId]; delete nextEditingByChat[chatId];
delete nextUnreadBoundaryByChat[chatId]; delete nextUnreadBoundaryByChat[chatId];
@@ -461,6 +476,8 @@ export const useChatStore = create<ChatState>((set, get) => ({
hasMoreByChat: nextHasMoreByChat, hasMoreByChat: nextHasMoreByChat,
loadingMoreByChat: nextLoadingMoreByChat, loadingMoreByChat: nextLoadingMoreByChat,
typingByChat: nextTypingByChat, typingByChat: nextTypingByChat,
recordingVoiceByChat: nextRecordingVoiceByChat,
recordingVideoByChat: nextRecordingVideoByChat,
replyToByChat: nextReplyToByChat, replyToByChat: nextReplyToByChat,
editingByChat: nextEditingByChat, editingByChat: nextEditingByChat,
unreadBoundaryByChat: nextUnreadBoundaryByChat, unreadBoundaryByChat: nextUnreadBoundaryByChat,