show user name

This commit is contained in:
Daniel Bulant 2026-05-13 11:10:52 +02:00
parent 40f6c0dffd
commit f65547870e
No known key found for this signature in database
3 changed files with 46 additions and 13 deletions

View file

@ -17,7 +17,7 @@ type DeviceSocketMessage =
type DeviceProxyEvent = type DeviceProxyEvent =
| PartySocketEvent | PartySocketEvent
| { type: "device_connect_required" } | { type: "device_connect_required" }
| { type: "device_connected" }; | { type: "device_connected"; username: string };
type DeviceQuizResponsePayload = { type DeviceQuizResponsePayload = {
QuizResponse: number; QuizResponse: number;
@ -125,12 +125,24 @@ async function syncDeviceConnectionStatus(deviceId: string) {
return; return;
} }
console.log("[device-socket] device claimed", deviceId, device.userId); const userRecord = await db.query.user.findFirst({
where: { id: device.userId },
});
const username = userRecord?.name ?? device.userId;
console.log(
"[device-socket] device claimed",
deviceId,
device.userId,
username,
);
await db await db
.update(deviceConnection) .update(deviceConnection)
.set({ lastSeen: new Date() }) .set({ lastSeen: new Date() })
.where(eq(deviceConnection.id, deviceId)); .where(eq(deviceConnection.id, deviceId));
sendDeviceEvent(deviceId, { type: "device_connected" }); sendDeviceEvent(deviceId, {
type: "device_connected",
username,
});
} }
export async function claimDeviceForUser(deviceId: string, userId: string) { export async function claimDeviceForUser(deviceId: string, userId: string) {
@ -296,7 +308,10 @@ export const deviceClaimApp = new Elysia()
"/:deviceId/connect", "/:deviceId/connect",
async ({ user, params }) => { async ({ user, params }) => {
await claimDeviceForUser(params.deviceId, user.id); await claimDeviceForUser(params.deviceId, user.id);
sendDeviceEvent(params.deviceId, { type: "device_connected" }); sendDeviceEvent(params.deviceId, {
type: "device_connected",
username: user.name,
});
return { ok: true, deviceId: params.deviceId, userId: user.id }; return { ok: true, deviceId: params.deviceId, userId: user.id };
}, },
{ auth: true }, { auth: true },

View file

@ -66,7 +66,7 @@ type ErrorEvent = {
type DeviceLifecycleEvent = type DeviceLifecycleEvent =
| { type: "device_connect_required" } | { type: "device_connect_required" }
| { type: "device_connected" }; | { type: "device_connected"; username: string };
type PartySocketEvent = type PartySocketEvent =
| PartyStatusEvent | PartyStatusEvent
@ -262,7 +262,7 @@ function handleApiMessage(e: MessageEvent) {
if (event.type === "device_connected") { if (event.type === "device_connected") {
console.log("Writing waiting-for-party", message.deviceId); console.log("Writing waiting-for-party", message.deviceId);
writeProxyOutput(socket, "WaitingForParty"); writeProxyOutput(socket, { WaitingForParty: event.username });
return; return;
} }

View file

@ -48,7 +48,7 @@ pub struct QuestionDataNet<'a> {
#[derive(Deserialize)] #[derive(Deserialize)]
pub enum ProxyOutput<'a> { pub enum ProxyOutput<'a> {
ConnectPrompt(&'a str), ConnectPrompt(&'a str),
WaitingForParty, WaitingForParty(&'a str),
Question(QuestionDataNet<'a>), Question(QuestionDataNet<'a>),
Results, Results,
Error(&'a str), Error(&'a str),
@ -87,6 +87,7 @@ impl WheelData {
pub struct DeviceState { pub struct DeviceState {
view: ViewState, view: ViewState,
device_id: Option<OwnedStr<64>>, device_id: Option<OwnedStr<64>>,
connected_user_name: Option<OwnedStr<64>>,
question: Option<QuestionData>, question: Option<QuestionData>,
wheel: WheelData, wheel: WheelData,
last_index: usize, last_index: usize,
@ -98,6 +99,7 @@ impl DeviceState {
Self { Self {
view: ViewState::Loading, view: ViewState::Loading,
device_id: None, device_id: None,
connected_user_name: None,
question: None, question: None,
wheel: WheelData::empty(), wheel: WheelData::empty(),
last_index: 0, last_index: 0,
@ -107,6 +109,7 @@ impl DeviceState {
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.view = ViewState::Loading; self.view = ViewState::Loading;
self.connected_user_name = None;
self.question = None; self.question = None;
self.wheel = WheelData::empty(); self.wheel = WheelData::empty();
self.last_index = 0; self.last_index = 0;
@ -114,6 +117,7 @@ impl DeviceState {
} }
pub fn reconnecting(&mut self) { pub fn reconnecting(&mut self) {
self.connected_user_name = None;
self.question = None; self.question = None;
self.wheel = WheelData::empty(); self.wheel = WheelData::empty();
self.view = ViewState::Reconnecting; self.view = ViewState::Reconnecting;
@ -144,7 +148,14 @@ impl DeviceState {
self.question = None; self.question = None;
self.view = ViewState::ConnectPrompt; self.view = ViewState::ConnectPrompt;
} }
ProxyOutput::WaitingForParty => { ProxyOutput::WaitingForParty(user_name) => {
let mut owned_user_name = OwnedStr::new();
for char in user_name.chars() {
if owned_user_name.try_push(char).is_err() {
break;
}
}
self.connected_user_name = Some(owned_user_name);
self.question = None; self.question = None;
self.view = ViewState::WaitingForParty; self.view = ViewState::WaitingForParty;
} }
@ -215,9 +226,16 @@ impl DeviceState {
} }
if self.view == ViewState::WaitingForParty { if self.view == ViewState::WaitingForParty {
let user_name = self.connected_user_name.as_ref()?;
let mut display_name: OwnedStr<16> = OwnedStr::new();
for char in user_name.as_str().chars() {
if display_name.try_push(char).is_err() {
break;
}
}
return Some(( return Some((
OwnedStr::from_str("Connected").unwrap(), display_name,
OwnedStr::from_str("Waiting party").unwrap(), OwnedStr::from_str("Awaiting party").unwrap(),
)); ));
} }
@ -409,15 +427,15 @@ mod tests {
#[test] #[test]
fn parses_and_renders_waiting_for_party() { fn parses_and_renders_waiting_for_party() {
let data = parse_proxy_output(r#""WaitingForParty""#).unwrap(); let data = parse_proxy_output(r#"{"WaitingForParty":"User"}"#).unwrap();
let mut state = DeviceState::new(); let mut state = DeviceState::new();
state.apply_proxy_output(data); state.apply_proxy_output(data);
assert_eq!(state.view_state(), ViewState::WaitingForParty); assert_eq!(state.view_state(), ViewState::WaitingForParty);
let (line1, line2) = state.render_lines().unwrap(); let (line1, line2) = state.render_lines().unwrap();
assert_eq!(line1.as_str(), "Connected"); assert_eq!(line1.as_str(), "User");
assert_eq!(line2.as_str(), "Waiting party"); assert_eq!(line2.as_str(), "Awaiting party");
} }
#[test] #[test]