Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

2.0 KB
Raw Blame
import { deviceService } from '../services/index.js'
import { pushDeviceRevoked } from './hub.js'
import { publishDeviceControl, publishDeviceStatus } from './bus.js'
import { logger } from '../utils/logger.js'

/**
 * Revoke one of a user's devices and tell it, on every channel that can reach it.
 *
 * Extracted because there are now TWO callers (the web's `/api/devices/:id` and the mobile group's
 * own DELETE) and the three steps are not optional:
 *
 *  1. delete the binding, which is what makes the next connect fail;
 *  2. push `device_revoked` on EVERY machine of the owner — a per-user device is attached under
 *     whichever machine it currently has selected, and only the socket holding it delivers;
 *  3. push on the devctl channel too, which covers a device parked on the machine PICKER (no hub
 *     attach at all) and closes the socket, so even firmware that ignores the frame falls into its
 *     reconnect → 401 → re-pair path.
 *
 * A second copy of this that forgot step 2 or 3 would leave a revoked device holding a live socket,
 * which is exactly the bug this function exists to make impossible.
 *
 * Returns false when the device is not this user's (or does not exist) — callers answer 404.
 */
export async function revokeDeviceForUser(deviceId: string, userId: string): Promise<boolean> {
  const revoked = await deviceService.revoke(deviceId, userId)
  if (!revoked) return false
  for (const machineId of revoked.machineIds) pushDeviceRevoked(machineId, revoked.deviceId)
  // Duplicate delivery is harmless — the first frame reboots the device.
  void publishDeviceControl(revoked.deviceId, { action: 'revoked' })
  // The owner's open web sockets keep their own copy of the account's devices (lib/webWs.ts) and drop it on this.
  void publishDeviceStatus(userId, { deviceId: revoked.deviceId, online: false, removed: true })
  logger.info('device revoked', { deviceId: revoked.deviceId, machines: revoked.machineIds.length, by: userId })
  return true
}