feat: implement collection library filter with WebSocket improvements and test coverage

This commit adds comprehensive functionality for filtering collections by library,
improves WebSocket real-time updates with user activity detection, and adds
extensive test coverage.

## Core Features

### Collection Library Filter
- Added library_id parameter to media-items search API
- Collections can now be filtered by specific library
- Toggle UI component for enabling/disabling library filter
- Default state is "checked" when library_id is present
- Consistent behavior across partial and fuzzy search modes

### WebSocket Auto-Reload Mitigation
- Added user activity detection to prevent disruptive page reloads
- Checks if user is actively typing in INPUT/TEXTAREA/SELECT elements
- Skips auto-reload when user is interacting with form elements
- Toast notifications still show for awareness
- Prevents data loss during editing operations

## Implementation Changes

### Backend
- internal/database/queries.sql.go: Added library filter support to search queries
- internal/handlers/media.go: Enhanced search with library_id parameter validation
- internal/handlers/collections.go: Updated collection handlers with library filtering
- internal/sync/websocket.go: Improved broadcast mechanism with user-scoped updates
- internal/router/frontend.go: Pass libraryID to collection templates

### Frontend
- templates/collections.templ: Added library filter toggle UI component
- web/src/collections.ts: TypeScript implementation with WebSocket integration
- templates/collections_templ.go: Generated template code

### Testing
- cmd/server/tests/search_test.go: Added TestCollectionSearchLibraryFilter
- cmd/server/tests/websocket_test.go: Added TestWebSocketUserScopedBroadcast
- New helper functions for creating libraries and media items via API
- Comprehensive test coverage for library filtering and user-scoped broadcasts

## API Documentation Updates

### Bruno Tests (Comprehensive Documentation)
- bruno/collections/*: Added detailed API documentation for all collection endpoints
- bruno/devices/*: Added device management and sync API documentation
- bruno/devices/kobo/api.yml: Kobo-specific sync protocol docs
- bruno/devices/koreader/api.yml: KOReader-specific sync protocol docs
- bruno/opds/*: Added OPDS feed and download endpoint documentation
- bruno/library/browse-folders.yml: Library folder browsing API docs

### New Bruno Tests
- bruno/media-items/Search All Libraries.yml: Test search without library filter
- bruno/media-items/Search Specific Library.yml: Test search with library filter
- bruno/media-items/Search Invalid Library ID.yml: Test error handling

## Documentation

- docs/developer/api/media-items/search_media_items.md: Updated with library_id parameter
- IMPLEMENTATION_COLLECTION_FIX.md: Comprehensive implementation guide with test scenarios

## Testing

### Integration Tests
- Library filter tests verify correct filtering across multiple libraries
- Invalid library_id tests ensure proper error handling
- WebSocket tests verify user-scoped broadcast behavior
- User A no longer receives User B's collection updates

### Manual Testing Scenarios
- Open collection in multiple tabs - updates propagate correctly
- Type in search box while another tab adds books - no disruptive reload
- Add/remove books from collection - toast notifications appear
- Toggle library filter - results update dynamically

## Technical Details

- WebSocket broadcasts are now user-scoped for privacy
- Active element detection uses tagName and contenteditable attributes
- Library ID validation uses UUID format checking
- Progressive enhancement maintained - page works without JavaScript
- All changes follow PROJECT_GUIDELINES.md conventions
- TypeScript only for frontend logic
- TailwindCSS only for styling
- Procedural programming style throughout

## Breaking Changes

None - all changes are additive and backward compatible.
This commit is contained in:
2026-03-04 22:37:47 -05:00
parent 72f053d179
commit 9b3d8cc949
43 changed files with 2067 additions and 438 deletions
+15
View File
@@ -5,3 +5,18 @@ info:
http:
method: POST
url: '"http://localhost:8765/api"'
docs: |-
## Bookhoard Device Management API
Collection of endpoints for managing e-reader devices and their synchronization settings.
**Base URL:** http://localhost:8765/api
**Authentication:** Most endpoints require Bearer token authentication
**Endpoints:**
- Device registration and management
- Device authentication tokens
- Sync configuration
- Device-to-collection mappings
+15
View File
@@ -5,3 +5,18 @@ info:
http:
method: POST
url: '"http://localhost:8765/api"'
docs: |-
## Bookhoard Kobo Sync API
Collection of endpoints specifically for Kobo e-reader device synchronization.
**Base URL:** http://localhost:8765/api
**Authentication:** Device token or Bearer token required
**Endpoints:**
- Kobo-specific sync protocols
- Kobo store integration
- Metadata synchronization
- Reading progress sync
+15
View File
@@ -5,3 +5,18 @@ info:
http:
method: POST
url: '"http://localhost:8765/api"'
docs: |-
## Bookhoard KOReader Sync API
Collection of endpoints specifically for KOReader e-reader device synchronization.
**Base URL:** http://localhost:8765/api
**Authentication:** Device token or Bearer token required
**Endpoints:**
- KOReader-specific sync protocols
- Metadata synchronization
- Reading progress sync
- Highlights and notes sync
@@ -8,4 +8,30 @@ http:
auth: none
body:
type: json
jsonBody: "{\n \"registration_id\": \"{{registrationId"
jsonBody: "{\n \"registration_id\": \"{{registrationId}}"
docs: |-
## Check Registration Status
Checks the current status of a device registration request.
**Method:** POST
**Endpoint:** /api/devices/register/status
**Authentication:** None
**Request Body:**
- `registration_id` (string, required): Registration request ID
**Response:**
- `status` (string): Registration status (pending, approved, rejected, expired)
- `device_name` (string): Device name
- `device_type` (string): Device type
- `created_at` (string): Request timestamp
- `expires_at` (string): Expiration timestamp
**Status Codes:**
- 200: Success
- 404: Registration ID not found
- 410: Registration expired
@@ -10,3 +10,30 @@ http:
type: json
jsonBody: "{\n \"device_name\": \"My Kindle Paperwhite\",\n \"device_type\"\
: \"koreader\",\n \"device_identifier\": \"kindle-pw5-hardware-id-12345\""
docs: |-
## Initiate Device Registration
Initiates a new device registration request that requires admin approval.
**Method:** POST
**Endpoint:** /api/devices/register
**Authentication:** None (open endpoint)
**Request Body:**
- `device_name` (string, required): Human-readable device name (max 100 chars)
- `device_type` (string, required): Device type (kobo, koreader, kindle, etc.)
- `device_identifier` (string, required): Unique hardware ID (max 200 chars)
**Response:**
- `registration_id` (string): Unique registration request ID
- `status` (string): Initial status (pending)
- `expires_at` (string): Expiration timestamp (usually 24 hours)
- `message` (string): Informational message
**Status Codes:**
- 201: Registration initiated
- 400: Invalid request
- 409: Device already registered
+30
View File
@@ -10,3 +10,33 @@ http:
type: json
jsonBody: "{\n \"device_name\": \"My Updated Kindle\",\n \"sync_enabled\"\
: true,\n \"auto_sync\": true,\n \"sync_frequency_minutes\": 10"
docs: |-
## Update Device
Updates device settings and synchronization preferences.
**Method:** PUT
**Endpoint:** /api/devices/{device_id}
**Authentication:** Required (Bearer token, admin or device owner)
**Path Parameters:**
- `device_id` (string): Device ID
**Request Body:**
- `device_name` (string, optional): New device name (max 100 chars)
- `sync_enabled` (boolean, optional): Enable/disable synchronization
- `auto_sync` (boolean, optional): Enable automatic synchronization
- `sync_frequency_minutes` (integer, optional): Sync frequency in minutes (5-1440)
**Response:**
- Updated device object
**Status Codes:**
- 200: Success
- 400: Invalid request
- 401: Unauthorized
- 403: Forbidden (not device owner)
- 404: Device not found