Phase 2 Week 5: Device Registration & Management
Implement device registration and management system for universal sync. Database Changes: - Add device queries to queries.sql (CRUD operations, registration, auth) - Add sync queue management queries - Add conflict resolution queries - Regenerate sqlc models with new device-related types Device Handler (devices.go): - InitiateRegistration: Start device registration with auth URL and QR code - CheckRegistrationStatus: Poll for registration approval - ListDevices: Get all devices for current user - GetDevice: Get specific device details - UpdateDevice: Update device settings (name, sync settings, frequency) - DeleteDevice: Remove device from account - ApproveDevice: User approves device registration via web - RejectDevice: Reject pending device registration - ListPendingRegistrations: Show all pending registrations - generateDeviceToken: Generate secure Bearer token for devices Device Authentication Middleware (device_auth.go): - Authenticate: Validate device Bearer tokens - RequirePermission: Check device permissions by type - hasPermission: Define permissions per device type - UpdateLastSeen: Auto-update device last_seen timestamp Configuration: - Add BaseURL field to Config for device setup URLs API Endpoints: POST /api/devices/register - Initiate device registration POST /api/devices/register/status - Check registration status GET /api/devices/approve/:id - Approve device (web UI) POST /api/devices/reject/:id - Reject device GET /api/devices - List user's devices GET /api/devices/:id - Get device details PUT /api/devices/:id - Update device settings DELETE /api/devices/:id - Delete device GET /api/devices/pending - List pending registrations Bruno API Collection: - Initiate Device Registration - Check Registration Status - List Devices - Get Device - Update Device - Delete Device Dependencies: - github.com/skip2/go-qrcode for QR code generation Device Types Supported: - koreader: Calibre-compatible sync - kobo: Kobo sync protocol - web: Web interface - mobile: Mobile apps Device Permissions: - sync:progress - sync:annotations - sync:metadata - device:manage (web only)
This commit is contained in:
@@ -621,4 +621,137 @@ INSERT INTO reading_history (
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING *;
|
||||
|
||||
-- ============================================
|
||||
-- PHASE 2: DEVICE MANAGEMENT & AUTH (Weeks 5-6)
|
||||
-- ============================================
|
||||
|
||||
-- Device Registration & Management
|
||||
-- name: CreateDevice :one
|
||||
INSERT INTO devices (user_id, device_name, device_type, device_identifier, auth_token, sync_enabled, auto_sync, sync_frequency_minutes, device_metadata)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetDevice :one
|
||||
SELECT * FROM devices WHERE id = $1;
|
||||
|
||||
-- name: GetDeviceByIdentifier :one
|
||||
SELECT * FROM devices WHERE device_identifier = $1;
|
||||
|
||||
-- name: GetDeviceByAuthToken :one
|
||||
SELECT * FROM devices WHERE auth_token = $1;
|
||||
|
||||
-- name: ListDevicesByUser :many
|
||||
SELECT * FROM devices WHERE user_id = $1 ORDER BY created_at DESC;
|
||||
|
||||
-- name: ListDevicesByType :many
|
||||
SELECT * FROM devices WHERE device_type = $1 ORDER BY created_at DESC;
|
||||
|
||||
-- name: UpdateDevice :one
|
||||
UPDATE devices
|
||||
SET
|
||||
device_name = $2,
|
||||
sync_enabled = $3,
|
||||
auto_sync = $4,
|
||||
sync_frequency_minutes = $5,
|
||||
device_metadata = $6,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateDeviceLastSync :one
|
||||
UPDATE devices
|
||||
SET
|
||||
last_sync = NOW(),
|
||||
last_seen = NOW(),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateDeviceLastSeen :one
|
||||
UPDATE devices
|
||||
SET
|
||||
last_seen = NOW(),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: RevokeDevice :exec
|
||||
UPDATE devices
|
||||
SET
|
||||
auth_token = NULL,
|
||||
sync_enabled = false,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: DeleteDevice :exec
|
||||
DELETE FROM devices WHERE id = $1;
|
||||
|
||||
-- name: DeleteDeviceByToken :exec
|
||||
DELETE FROM devices WHERE auth_token = $1;
|
||||
|
||||
-- Sync Queue Management
|
||||
-- name: CreateSyncQueueItem :one
|
||||
INSERT INTO sync_queue (device_id, media_item_id, sync_type, sync_data, priority, max_attempts, status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetSyncQueueItem :one
|
||||
SELECT * FROM sync_queue WHERE id = $1;
|
||||
|
||||
-- name: ListPendingSyncQueueItems :many
|
||||
SELECT * FROM sync_queue
|
||||
WHERE device_id = $1 AND status = 'pending'
|
||||
ORDER BY priority ASC, created_at ASC
|
||||
LIMIT $2;
|
||||
|
||||
-- name: UpdateSyncQueueItemStatus :one
|
||||
UPDATE sync_queue
|
||||
SET
|
||||
status = $2,
|
||||
attempts = attempts + 1,
|
||||
error_message = $3,
|
||||
processed_at = CASE WHEN $2 = 'completed' THEN NOW() ELSE NULL END
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteSyncQueueItem :exec
|
||||
DELETE FROM sync_queue WHERE id = $1;
|
||||
|
||||
-- name: ClearDeviceSyncQueue :exec
|
||||
DELETE FROM sync_queue WHERE device_id = $1;
|
||||
|
||||
-- Conflict Resolution
|
||||
-- name: CreateSyncConflict :one
|
||||
INSERT INTO sync_conflicts (media_item_id, user_id, conflict_type, conflict_data)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetSyncConflict :one
|
||||
SELECT * FROM sync_conflicts WHERE id = $1;
|
||||
|
||||
-- name: ListSyncConflictsByUser :many
|
||||
SELECT sc.*, mi.title, mi.author
|
||||
FROM sync_conflicts sc
|
||||
JOIN media_items mi ON sc.media_item_id = mi.id
|
||||
WHERE sc.user_id = $1 AND sc.resolution_status = 'unresolved'
|
||||
ORDER BY sc.created_at DESC;
|
||||
|
||||
-- name: ListSyncConflictsByMediaItem :many
|
||||
SELECT * FROM sync_conflicts
|
||||
WHERE media_item_id = $1 AND user_id = $2
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: ResolveSyncConflict :one
|
||||
UPDATE sync_conflicts
|
||||
SET
|
||||
resolution_status = $2,
|
||||
resolution_data = $3,
|
||||
resolved_by = $4,
|
||||
resolved_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteSyncConflict :exec
|
||||
DELETE FROM sync_conflicts WHERE id = $1;
|
||||
|
||||
-- Media Items Admin Operations
|
||||
Reference in New Issue
Block a user