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:
@@ -6931,27 +6931,29 @@ JOIN libraries l ON mi.library_id = l.id
|
||||
JOIN library_types lt ON l.library_type_id = lt.id
|
||||
LEFT JOIN library_visibility lv ON l.id = lv.library_id AND lv.user_id = $1
|
||||
WHERE COALESCE(lv.is_visible, true) = true
|
||||
AND ($2 IS NULL OR mi.library_id = $2)
|
||||
AND (
|
||||
mi.title ILIKE $2 OR
|
||||
mi.author ILIKE $2 OR
|
||||
mi.series ILIKE $2 OR
|
||||
$2 = ANY(mi.tags_search) OR
|
||||
$2 = ANY(mi.contributors_search)
|
||||
mi.title ILIKE $3 OR
|
||||
mi.author ILIKE $3 OR
|
||||
mi.series ILIKE $3 OR
|
||||
$3 = ANY(mi.tags_search) OR
|
||||
$3 = ANY(mi.contributors_search)
|
||||
)
|
||||
ORDER BY
|
||||
CASE
|
||||
WHEN mi.title ILIKE $2 THEN 1
|
||||
WHEN mi.author ILIKE $2 THEN 2
|
||||
WHEN mi.series ILIKE $2 THEN 3
|
||||
WHEN $2 = ANY(mi.tags_search) THEN 4
|
||||
WHEN mi.title ILIKE $3 THEN 1
|
||||
WHEN mi.author ILIKE $3 THEN 2
|
||||
WHEN mi.series ILIKE $3 THEN 3
|
||||
WHEN $3 = ANY(mi.tags_search) THEN 4
|
||||
ELSE 5
|
||||
END,
|
||||
mi.title ASC
|
||||
LIMIT $4 OFFSET $3
|
||||
LIMIT $5 OFFSET $4
|
||||
`
|
||||
|
||||
type SearchMediaItemsParams struct {
|
||||
UserID pgtype.UUID `db:"user_id" json:"user_id"`
|
||||
LibraryID interface{} `db:"library_id" json:"library_id"`
|
||||
SearchPattern pgtype.Text `db:"search_pattern" json:"search_pattern"`
|
||||
Offset pgtype.Int4 `db:"offset" json:"offset"`
|
||||
Limit pgtype.Int4 `db:"limit" json:"limit"`
|
||||
@@ -7010,6 +7012,7 @@ type SearchMediaItemsRow struct {
|
||||
func (q *Queries) SearchMediaItems(ctx context.Context, arg SearchMediaItemsParams) ([]SearchMediaItemsRow, error) {
|
||||
rows, err := q.db.Query(ctx, SearchMediaItems,
|
||||
arg.UserID,
|
||||
arg.LibraryID,
|
||||
arg.SearchPattern,
|
||||
arg.Offset,
|
||||
arg.Limit,
|
||||
@@ -7086,43 +7089,45 @@ JOIN libraries l ON mi.library_id = l.id
|
||||
JOIN library_types lt ON l.library_type_id = lt.id
|
||||
LEFT JOIN library_visibility lv ON l.id = lv.library_id AND lv.user_id = $1
|
||||
WHERE COALESCE(lv.is_visible, true) = true
|
||||
AND ($2 IS NULL OR mi.library_id = $2)
|
||||
AND (
|
||||
word_similarity($2, mi.title) > 0.3 OR
|
||||
word_similarity($2, COALESCE(mi.author, '')) > 0.3 OR
|
||||
word_similarity($2, COALESCE(mi.series, '')) > 0.3 OR
|
||||
word_similarity($3, mi.title) > 0.3 OR
|
||||
word_similarity($3, COALESCE(mi.author, '')) > 0.3 OR
|
||||
word_similarity($3, COALESCE(mi.series, '')) > 0.3 OR
|
||||
EXISTS (
|
||||
SELECT 1 FROM unnest(mi.tags_search) AS tag
|
||||
WHERE word_similarity($2, tag) > 0.3
|
||||
WHERE word_similarity($3, tag) > 0.3
|
||||
LIMIT 1
|
||||
) OR
|
||||
EXISTS (
|
||||
SELECT 1 FROM unnest(mi.contributors_search) AS contributor
|
||||
WHERE word_similarity($2, contributor) > 0.3
|
||||
WHERE word_similarity($3, contributor) > 0.3
|
||||
LIMIT 1
|
||||
)
|
||||
)
|
||||
ORDER BY
|
||||
GREATEST(
|
||||
word_similarity($2, mi.title),
|
||||
word_similarity($2, COALESCE(mi.author, '')),
|
||||
word_similarity($2, COALESCE(mi.series, '')),
|
||||
word_similarity($3, mi.title),
|
||||
word_similarity($3, COALESCE(mi.author, '')),
|
||||
word_similarity($3, COALESCE(mi.series, '')),
|
||||
COALESCE(
|
||||
(SELECT MAX(word_similarity($2, tag))
|
||||
(SELECT MAX(word_similarity($3, tag))
|
||||
FROM unnest(mi.tags_search) AS tag),
|
||||
0
|
||||
),
|
||||
COALESCE(
|
||||
(SELECT MAX(word_similarity($2, contributor))
|
||||
(SELECT MAX(word_similarity($3, contributor))
|
||||
FROM unnest(mi.contributors_search) AS contributor),
|
||||
0
|
||||
)
|
||||
) DESC,
|
||||
mi.title ASC
|
||||
LIMIT $4 OFFSET $3
|
||||
LIMIT $5 OFFSET $4
|
||||
`
|
||||
|
||||
type SearchMediaItemsFuzzyParams struct {
|
||||
UserID pgtype.UUID `db:"user_id" json:"user_id"`
|
||||
LibraryID interface{} `db:"library_id" json:"library_id"`
|
||||
SearchQuery interface{} `db:"search_query" json:"search_query"`
|
||||
Offset pgtype.Int4 `db:"offset" json:"offset"`
|
||||
Limit pgtype.Int4 `db:"limit" json:"limit"`
|
||||
@@ -7180,6 +7185,7 @@ type SearchMediaItemsFuzzyRow struct {
|
||||
func (q *Queries) SearchMediaItemsFuzzy(ctx context.Context, arg SearchMediaItemsFuzzyParams) ([]SearchMediaItemsFuzzyRow, error) {
|
||||
rows, err := q.db.Query(ctx, SearchMediaItemsFuzzy,
|
||||
arg.UserID,
|
||||
arg.LibraryID,
|
||||
arg.SearchQuery,
|
||||
arg.Offset,
|
||||
arg.Limit,
|
||||
|
||||
@@ -397,6 +397,7 @@ JOIN libraries l ON mi.library_id = l.id
|
||||
JOIN library_types lt ON l.library_type_id = lt.id
|
||||
LEFT JOIN library_visibility lv ON l.id = lv.library_id AND lv.user_id = sqlc.narg('user_id')
|
||||
WHERE COALESCE(lv.is_visible, true) = true
|
||||
AND (sqlc.narg('library_id') IS NULL OR mi.library_id = sqlc.narg('library_id'))
|
||||
AND (
|
||||
mi.title ILIKE sqlc.narg('search_pattern') OR
|
||||
mi.author ILIKE sqlc.narg('search_pattern') OR
|
||||
@@ -422,6 +423,7 @@ JOIN libraries l ON mi.library_id = l.id
|
||||
JOIN library_types lt ON l.library_type_id = lt.id
|
||||
LEFT JOIN library_visibility lv ON l.id = lv.library_id AND lv.user_id = sqlc.narg('user_id')
|
||||
WHERE COALESCE(lv.is_visible, true) = true
|
||||
AND (sqlc.narg('library_id') IS NULL OR mi.library_id = sqlc.narg('library_id'))
|
||||
AND (
|
||||
word_similarity(sqlc.narg('search_query'), mi.title) > 0.3 OR
|
||||
word_similarity(sqlc.narg('search_query'), COALESCE(mi.author, '')) > 0.3 OR
|
||||
|
||||
@@ -330,7 +330,7 @@ func (h *CollectionHandler) AddBooks(c echo.Context) error {
|
||||
}
|
||||
|
||||
if addedCount > 0 && h.connManager != nil {
|
||||
h.connManager.Broadcast(wsync.BroadcastMessage{
|
||||
h.connManager.BroadcastToUser(userUUID.String(), wsync.BroadcastMessage{
|
||||
Type: "collection_updated",
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
Data: map[string]interface{}{
|
||||
@@ -374,6 +374,9 @@ func (h *CollectionHandler) BulkRemoveBooks(c echo.Context) error {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid collection id"})
|
||||
}
|
||||
|
||||
user := c.Get("user").(database.Users)
|
||||
userUUID := uuid.UUID(user.ID.Bytes)
|
||||
|
||||
var req BulkRemoveBooksRequest
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request"})
|
||||
@@ -398,7 +401,7 @@ func (h *CollectionHandler) BulkRemoveBooks(c echo.Context) error {
|
||||
}
|
||||
|
||||
if removedCount > 0 && h.connManager != nil {
|
||||
h.connManager.Broadcast(wsync.BroadcastMessage{
|
||||
h.connManager.BroadcastToUser(userUUID.String(), wsync.BroadcastMessage{
|
||||
Type: "collection_updated",
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
Data: map[string]interface{}{
|
||||
@@ -753,6 +756,7 @@ func (h *CollectionHandler) compareValues(itemValue, operator, ruleValue string)
|
||||
// Bulk add books to multiple collections
|
||||
func (h *CollectionHandler) HandleBulkAddBooks(c echo.Context) error {
|
||||
user := c.Get("user").(database.Users)
|
||||
userUUID := uuid.UUID(user.ID.Bytes)
|
||||
|
||||
var req struct {
|
||||
Operations []struct {
|
||||
@@ -828,7 +832,7 @@ func (h *CollectionHandler) HandleBulkAddBooks(c echo.Context) error {
|
||||
}
|
||||
|
||||
if successCount > 0 && h.connManager != nil {
|
||||
h.connManager.Broadcast(wsync.BroadcastMessage{
|
||||
h.connManager.BroadcastToUser(userUUID.String(), wsync.BroadcastMessage{
|
||||
Type: "collection_updated",
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
Data: map[string]interface{}{
|
||||
|
||||
@@ -1419,6 +1419,7 @@ func (mh *MediaHandler) DeleteMediaHighlight(c echo.Context) error {
|
||||
func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
|
||||
query := c.QueryParam("q")
|
||||
userID := c.Get("user_id").(string)
|
||||
libraryID := c.QueryParam("library_id")
|
||||
|
||||
if query == "" {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "query parameter 'q' is required"})
|
||||
@@ -1429,17 +1430,31 @@ func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user id"})
|
||||
}
|
||||
|
||||
// Validate library_id if provided
|
||||
var libUUID pgtype.UUID
|
||||
if libraryID != "" {
|
||||
lib, err := uuid.Parse(libraryID)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid library_id"})
|
||||
}
|
||||
libUUID = pgtype.UUID{Bytes: lib, Valid: true}
|
||||
}
|
||||
|
||||
limit := int32(50)
|
||||
offset := int32(0)
|
||||
|
||||
searchPattern := "%" + query + "%"
|
||||
|
||||
partialResults, err := mh.db.SearchMediaItems(c.Request().Context(), database.SearchMediaItemsParams{
|
||||
// Build params - conditionally and library_id filter
|
||||
partialParams := database.SearchMediaItemsParams{
|
||||
SearchPattern: pgtype.Text{String: searchPattern, Valid: true},
|
||||
UserID: pgtype.UUID{Bytes: userUUID, Valid: true},
|
||||
Limit: pgtype.Int4{Int32: limit, Valid: true},
|
||||
Offset: pgtype.Int4{Int32: offset, Valid: true},
|
||||
})
|
||||
LibraryID: libUUID, // May be invalid (empty)
|
||||
}
|
||||
|
||||
partialResults, err := mh.db.SearchMediaItems(c.Request().Context(), partialParams)
|
||||
|
||||
if err != nil && err != pgx.ErrNoRows {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
@@ -1449,12 +1464,15 @@ func (mh *MediaHandler) SearchMediaItems(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, partialResults)
|
||||
}
|
||||
|
||||
fuzzyResults, err := mh.db.SearchMediaItemsFuzzy(c.Request().Context(), database.SearchMediaItemsFuzzyParams{
|
||||
fuzzyParams := database.SearchMediaItemsFuzzyParams{
|
||||
SearchQuery: pgtype.Text{String: query, Valid: true},
|
||||
UserID: pgtype.UUID{Bytes: userUUID, Valid: true},
|
||||
Limit: pgtype.Int4{Int32: limit, Valid: true},
|
||||
Offset: pgtype.Int4{Int32: offset, Valid: true},
|
||||
})
|
||||
LibraryID: libUUID,
|
||||
}
|
||||
|
||||
fuzzyResults, err := mh.db.SearchMediaItemsFuzzy(c.Request().Context(), fuzzyParams)
|
||||
|
||||
if err != nil && err != pgx.ErrNoRows {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
|
||||
@@ -427,9 +427,12 @@ func registerFrontendRoutes(cfg *Config) {
|
||||
Color: collection.Color.String,
|
||||
Icon: collection.Icon.String,
|
||||
}
|
||||
|
||||
// Get library_id from query params for template
|
||||
libraryID := c.QueryParam("library_id")
|
||||
// Render the CollectionDetail template
|
||||
var buf bytes.Buffer
|
||||
err = templates.CollectionDetail(user, colData, books).Render(c.Request().Context(), &buf)
|
||||
err = templates.CollectionDetail(user, colData, books, libraryID).Render(c.Request().Context(), &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -93,6 +93,23 @@ func (m *ConnectionManager) Broadcast(msg BroadcastMessage) {
|
||||
}
|
||||
}
|
||||
|
||||
// BroadcastToUser sends a message to all connections for a specific user
|
||||
func (m *ConnectionManager) BroadcastToUser(userID string, msg BroadcastMessage) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
for _, conn := range m.connections {
|
||||
if conn.UserID == userID {
|
||||
select {
|
||||
case conn.Send <- msg:
|
||||
default:
|
||||
// Channel full, skip this connection
|
||||
log.Printf("WebSocket: Channel full for %s, skipping broadcast", conn.DeviceName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BroadcastProgressUpdate broadcasts a progress update to all connected clients
|
||||
func (m *ConnectionManager) BroadcastProgressUpdate(bookID uuid.UUID, percentage float64, source SourceDevice) {
|
||||
msg := BroadcastMessage{
|
||||
|
||||
Reference in New Issue
Block a user