mirror of
https://github.com/tinyhumansai/openhuman.git
synced 2026-07-30 06:32:24 +00:00
- Replaced the `selectCurrentUserId` function with `selectSocketUserId` to derive the socket user ID from the JWT token, improving clarity and functionality. - Updated the `register` function signatures across multiple QuickJS operation files to use a generic lifetime parameter, enhancing type safety and consistency.
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import type { RootState } from './index';
|
|
|
|
const PENDING_USER = '__pending__';
|
|
|
|
/**
|
|
* Derive the socket user ID from the JWT token — must match the key used
|
|
* by tauriSocket.ts and socketService.ts when writing to byUser[].
|
|
*/
|
|
function selectSocketUserId(state: RootState): string {
|
|
const token = state.auth.token;
|
|
if (!token) return PENDING_USER;
|
|
|
|
try {
|
|
const parts = token.split('.');
|
|
if (parts.length !== 3) return PENDING_USER;
|
|
const payloadBase64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
|
|
const payloadJson = atob(payloadBase64);
|
|
const payload = JSON.parse(payloadJson);
|
|
return payload.tgUserId || payload.userId || payload.sub || PENDING_USER;
|
|
} catch {
|
|
return PENDING_USER;
|
|
}
|
|
}
|
|
|
|
export const selectSocketStatus = (state: RootState) => {
|
|
const userId = selectSocketUserId(state);
|
|
const userState = state.socket.byUser[userId];
|
|
return userState?.status ?? 'disconnected';
|
|
};
|
|
|
|
export const selectSocketId = (state: RootState): string | null => {
|
|
const userId = selectSocketUserId(state);
|
|
const userState = state.socket.byUser[userId];
|
|
return userState?.socketId ?? null;
|
|
};
|