refactor(collections): retire the legacy rules surface — tree-only storage
The pre-tree rules machinery is deleted: no row ever carried a rules array or a non-default match mode (verified in the dev DB), the web never read the envelope keys, and the Android app consumes none of it. Dead code gets no documentation. Removed: - PUT /api/collections/:id/match-mode — endpoint, route, and the UpdateCollectionMatchMode query. The tree's root mode owns any|all. - auto_assign_rules / rule_match_mode from every envelope (create/list/detail/update), from the create/update request shapes, and from the schema: both columns DROPPED (schema.sql carries idempotent DROP COLUMN IF EXISTS heals for existing DBs; sqlc regenerated). - The rules CRUD service layer (AddRule/SetRuleEnabled/RemoveRule + validateRule + UpdateCollectionRulesJSON), ParseRules / EnabledRules / IsSmartCollection, RulesToFilterTree (+ the SSR preload branch), the legacy branch of EvaluateSmartMembership, and SmartEvalOptions.MatchMode. IsCollectionSmart = tree presence; create-path isSmart = collection_type=smart OR a filter_tree. Kept (live consumers): POST /api/collections/preview still evaluates rules payloads in memory (the custom-section builder's preview), and include_auto stays on the list (the Android app sends it). The custom-section builder's SAVE was already broken — its stringified auto_assign_rules payload failed request binding (probed live: 400 invalid request). Create now rejects that payload loudly with a 400 naming the retirement, instead of silently creating a hollow static section; custom-section-builder.md carries the flag. The builder's tree migration remains the flagged follow-up. Test-side (asymmetry rule — deleted behavior, deleted tests): TestPodcastSmartCollections rewritten onto trees (mode lives in the tree root), match-mode subtests and tree_wins_over_legacy_rules deleted, TestRulesToFilterTree deleted, contract-pin envelope keys retracted per the contract_pins 2026-10-31 retirement entry. Mirrors hand-synced: openapi.yaml, bruno create/update, docs/api/*.md, docs/schema.go, api.d.ts. Full container suite green (155s).
This commit is contained in:
1 parent
ab98015190
commit
a86bdac87e
27 files changed
+239
-1093
No files matched your search
@@ -34,8 +34,6 @@ type Collections struct {
|
||||
Description pgtype.Text `db:"description" json:"description"`
|
||||
Color pgtype.Text `db:"color" json:"color"`
|
||||
Icon pgtype.Text `db:"icon" json:"icon"`
|
||||
AutoAssignRules []byte `db:"auto_assign_rules" json:"auto_assign_rules"`
|
||||
RuleMatchMode string `db:"rule_match_mode" json:"rule_match_mode"`
|
||||
FilterTree []byte `db:"filter_tree" json:"filter_tree"`
|
||||
ViewSettings []byte `db:"view_settings" json:"view_settings"`
|
||||
ShowOnDashboard pgtype.Bool `db:"show_on_dashboard" json:"show_on_dashboard"`
|
||||
|
||||
@@ -599,14 +599,6 @@ type Querier interface {
|
||||
GetUserSettingByKey(ctx context.Context, arg GetUserSettingByKeyParams) ([]byte, error)
|
||||
GetUserVisibleLibraries(ctx context.Context, userID pgtype.UUID) ([]GetUserVisibleLibrariesRow, error)
|
||||
GetVisibleLibraryMediaCounts(ctx context.Context, userID pgtype.UUID) ([]GetVisibleLibraryMediaCountsRow, error)
|
||||
// Smart ("dynamic") collections: rules are evaluated LIVE at render, so
|
||||
// membership comes from the candidate book set instead of collection_items.
|
||||
// The set is scoped to the requesting user's visible libraries (family
|
||||
// lens enforced at the source) and live items only — the same filters the
|
||||
// static collection paths apply. The viewer's progress rides along (plan
|
||||
// item 2.4): the played/unplayed episode rule evaluates per-user, exactly
|
||||
// like the hide_finished predicate (percentage >= 0.999 = finished).
|
||||
GetVisibleLiveMediaForRules(ctx context.Context, arg GetVisibleLiveMediaForRulesParams) ([]GetVisibleLiveMediaForRulesRow, error)
|
||||
HasRecentConflictResolution(ctx context.Context, arg HasRecentConflictResolutionParams) (bool, error)
|
||||
// Soft-dismiss one row (ownership is checked by the caller against the
|
||||
// device's user_id before calling this).
|
||||
@@ -828,12 +820,6 @@ type Querier interface {
|
||||
UnignoreUnlinkedBook(ctx context.Context, id pgtype.UUID) error
|
||||
// Update collection
|
||||
UpdateCollection(ctx context.Context, arg UpdateCollectionParams) (Collections, error)
|
||||
// Smart-collection match semantics (plan item 2.4): flip any|all on its
|
||||
// own channel — the rules page toggle writes exactly this column.
|
||||
UpdateCollectionMatchMode(ctx context.Context, arg UpdateCollectionMatchModeParams) error
|
||||
// Replace ONLY the auto_assign_rules document (rule CRUD: read-modify-write
|
||||
// of the JSONB without touching metadata stale clients did not send).
|
||||
UpdateCollectionRulesJSON(ctx context.Context, arg UpdateCollectionRulesJSONParams) (Collections, error)
|
||||
// A2c §2.4: provenance + playlist flags on an existing collection without
|
||||
// touching the rest of the row.
|
||||
// (Historical bug, fixed 2026-10-30: this wrote `updated_at = NOW()` but
|
||||
|
||||
@@ -671,9 +671,9 @@ func (q *Queries) CreateAutoResolvedSyncConflict(ctx context.Context, arg Create
|
||||
|
||||
const CreateCollection = `-- name: CreateCollection :one
|
||||
|
||||
INSERT INTO collections (user_id, name, description, color, icon, auto_assign_rules, view_settings, show_on_dashboard, filter_tree)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
INSERT INTO collections (user_id, name, description, color, icon, view_settings, show_on_dashboard, filter_tree)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
`
|
||||
|
||||
type CreateCollectionParams struct {
|
||||
@@ -682,7 +682,6 @@ type CreateCollectionParams struct {
|
||||
Description pgtype.Text `db:"description" json:"description"`
|
||||
Color pgtype.Text `db:"color" json:"color"`
|
||||
Icon pgtype.Text `db:"icon" json:"icon"`
|
||||
AutoAssignRules []byte `db:"auto_assign_rules" json:"auto_assign_rules"`
|
||||
ViewSettings []byte `db:"view_settings" json:"view_settings"`
|
||||
ShowOnDashboard pgtype.Bool `db:"show_on_dashboard" json:"show_on_dashboard"`
|
||||
FilterTree []byte `db:"filter_tree" json:"filter_tree"`
|
||||
@@ -697,7 +696,6 @@ func (q *Queries) CreateCollection(ctx context.Context, arg CreateCollectionPara
|
||||
arg.Description,
|
||||
arg.Color,
|
||||
arg.Icon,
|
||||
arg.AutoAssignRules,
|
||||
arg.ViewSettings,
|
||||
arg.ShowOnDashboard,
|
||||
arg.FilterTree,
|
||||
@@ -710,8 +708,6 @@ func (q *Queries) CreateCollection(ctx context.Context, arg CreateCollectionPara
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -2254,9 +2250,9 @@ func (q *Queries) CreateSyncQueueItem(ctx context.Context, arg CreateSyncQueueIt
|
||||
}
|
||||
|
||||
const CreateSystemCollection = `-- name: CreateSystemCollection :one
|
||||
INSERT INTO collections (user_id, name, description, icon, color, show_on_dashboard, query_type, priority, is_system_collection, auto_assign_rules)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, true, 'null'::jsonb)
|
||||
RETURNING id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
INSERT INTO collections (user_id, name, description, icon, color, show_on_dashboard, query_type, priority, is_system_collection)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, true)
|
||||
RETURNING id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
`
|
||||
|
||||
type CreateSystemCollectionParams struct {
|
||||
@@ -2289,8 +2285,6 @@ func (q *Queries) CreateSystemCollection(ctx context.Context, arg CreateSystemCo
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -4389,7 +4383,7 @@ func (q *Queries) GetBooksByTag(ctx context.Context, arg GetBooksByTagParams) ([
|
||||
}
|
||||
|
||||
const GetCollection = `-- name: GetCollection :one
|
||||
SELECT id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections WHERE id = $1
|
||||
SELECT id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections WHERE id = $1
|
||||
`
|
||||
|
||||
// Get collection
|
||||
@@ -4403,8 +4397,6 @@ func (q *Queries) GetCollection(ctx context.Context, id pgtype.UUID) (Collection
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -4418,7 +4410,7 @@ func (q *Queries) GetCollection(ctx context.Context, id pgtype.UUID) (Collection
|
||||
}
|
||||
|
||||
const GetCollectionByImportedSeries = `-- name: GetCollectionByImportedSeries :one
|
||||
SELECT id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections
|
||||
SELECT id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections
|
||||
WHERE user_id = $1
|
||||
AND view_settings->>'imported_from_series' = $2::text
|
||||
AND deleted_at IS NULL
|
||||
@@ -4443,8 +4435,6 @@ func (q *Queries) GetCollectionByImportedSeries(ctx context.Context, arg GetColl
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -4458,7 +4448,7 @@ func (q *Queries) GetCollectionByImportedSeries(ctx context.Context, arg GetColl
|
||||
}
|
||||
|
||||
const GetCollectionByUserAndName = `-- name: GetCollectionByUserAndName :one
|
||||
SELECT id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections
|
||||
SELECT id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections
|
||||
WHERE user_id = $1 AND name = $2 AND deleted_at IS NULL
|
||||
LIMIT 1
|
||||
`
|
||||
@@ -4478,8 +4468,6 @@ func (q *Queries) GetCollectionByUserAndName(ctx context.Context, arg GetCollect
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -4973,7 +4961,7 @@ func (q *Queries) GetCollectionUpNext(ctx context.Context, arg GetCollectionUpNe
|
||||
|
||||
const GetCollectionWithBookCount = `-- name: GetCollectionWithBookCount :one
|
||||
SELECT
|
||||
c.id, c.user_id, c.name, c.description, c.color, c.icon, c.auto_assign_rules, c.rule_match_mode, c.filter_tree, c.view_settings, c.show_on_dashboard, c.query_type, c.priority, c.is_system_collection, c.created_at, c.deleted_at,
|
||||
c.id, c.user_id, c.name, c.description, c.color, c.icon, c.filter_tree, c.view_settings, c.show_on_dashboard, c.query_type, c.priority, c.is_system_collection, c.created_at, c.deleted_at,
|
||||
COUNT(ci.id) as book_count
|
||||
FROM collections c
|
||||
LEFT JOIN collection_items ci ON c.id = ci.collection_id
|
||||
@@ -4988,8 +4976,6 @@ type GetCollectionWithBookCountRow struct {
|
||||
Description pgtype.Text `db:"description" json:"description"`
|
||||
Color pgtype.Text `db:"color" json:"color"`
|
||||
Icon pgtype.Text `db:"icon" json:"icon"`
|
||||
AutoAssignRules []byte `db:"auto_assign_rules" json:"auto_assign_rules"`
|
||||
RuleMatchMode string `db:"rule_match_mode" json:"rule_match_mode"`
|
||||
FilterTree []byte `db:"filter_tree" json:"filter_tree"`
|
||||
ViewSettings []byte `db:"view_settings" json:"view_settings"`
|
||||
ShowOnDashboard pgtype.Bool `db:"show_on_dashboard" json:"show_on_dashboard"`
|
||||
@@ -5012,8 +4998,6 @@ func (q *Queries) GetCollectionWithBookCount(ctx context.Context, id pgtype.UUID
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -5028,7 +5012,7 @@ func (q *Queries) GetCollectionWithBookCount(ctx context.Context, id pgtype.UUID
|
||||
}
|
||||
|
||||
const GetCollectionsByUser = `-- name: GetCollectionsByUser :many
|
||||
SELECT id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections WHERE user_id = $1 AND deleted_at IS NULL ORDER BY created_at DESC
|
||||
SELECT id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections WHERE user_id = $1 AND deleted_at IS NULL ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
// Get collections by user
|
||||
@@ -5048,8 +5032,6 @@ func (q *Queries) GetCollectionsByUser(ctx context.Context, userID pgtype.UUID)
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -5070,7 +5052,7 @@ func (q *Queries) GetCollectionsByUser(ctx context.Context, userID pgtype.UUID)
|
||||
}
|
||||
|
||||
const GetCollectionsForBook = `-- name: GetCollectionsForBook :many
|
||||
SELECT c.id, c.user_id, c.name, c.description, c.color, c.icon, c.auto_assign_rules, c.rule_match_mode, c.filter_tree, c.view_settings, c.show_on_dashboard, c.query_type, c.priority, c.is_system_collection, c.created_at, c.deleted_at
|
||||
SELECT c.id, c.user_id, c.name, c.description, c.color, c.icon, c.filter_tree, c.view_settings, c.show_on_dashboard, c.query_type, c.priority, c.is_system_collection, c.created_at, c.deleted_at
|
||||
FROM collections c
|
||||
JOIN collection_items ci ON c.id = ci.collection_id
|
||||
WHERE ci.media_item_id = $1
|
||||
@@ -5094,8 +5076,6 @@ func (q *Queries) GetCollectionsForBook(ctx context.Context, mediaItemID pgtype.
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -10801,7 +10781,7 @@ func (q *Queries) GetSyncQueueStats(ctx context.Context, deviceID pgtype.UUID) (
|
||||
|
||||
const GetSystemCollectionByQueryType = `-- name: GetSystemCollectionByQueryType :one
|
||||
|
||||
SELECT id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections
|
||||
SELECT id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections
|
||||
WHERE user_id = $1
|
||||
AND query_type = $2
|
||||
AND is_system_collection = true
|
||||
@@ -10827,8 +10807,6 @@ func (q *Queries) GetSystemCollectionByQueryType(ctx context.Context, arg GetSys
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -10842,7 +10820,7 @@ func (q *Queries) GetSystemCollectionByQueryType(ctx context.Context, arg GetSys
|
||||
}
|
||||
|
||||
const GetSystemCollectionsForDashboard = `-- name: GetSystemCollectionsForDashboard :many
|
||||
SELECT id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections
|
||||
SELECT id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at FROM collections
|
||||
WHERE user_id = $1
|
||||
AND is_system_collection = true
|
||||
AND show_on_dashboard = true
|
||||
@@ -10867,8 +10845,6 @@ func (q *Queries) GetSystemCollectionsForDashboard(ctx context.Context, userID p
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -11575,7 +11551,7 @@ func (q *Queries) GetUserByUsername(ctx context.Context, username string) (GetUs
|
||||
}
|
||||
|
||||
const GetUserCollectionsForDashboard = `-- name: GetUserCollectionsForDashboard :many
|
||||
SELECT c.id, c.user_id, c.name, c.description, c.color, c.icon, c.auto_assign_rules, c.rule_match_mode, c.filter_tree, c.view_settings, c.show_on_dashboard, c.query_type, c.priority, c.is_system_collection, c.created_at, c.deleted_at FROM collections c
|
||||
SELECT c.id, c.user_id, c.name, c.description, c.color, c.icon, c.filter_tree, c.view_settings, c.show_on_dashboard, c.query_type, c.priority, c.is_system_collection, c.created_at, c.deleted_at FROM collections c
|
||||
WHERE c.user_id = $1
|
||||
AND c.is_system_collection = false
|
||||
AND c.deleted_at IS NULL
|
||||
@@ -11602,8 +11578,6 @@ func (q *Queries) GetUserCollectionsForDashboard(ctx context.Context, userID pgt
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -12364,146 +12338,6 @@ func (q *Queries) GetVisibleLibraryMediaCounts(ctx context.Context, userID pgtyp
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const GetVisibleLiveMediaForRules = `-- name: GetVisibleLiveMediaForRules :many
|
||||
SELECT mi.id, mi.library_id, mi.title, mi.subtitle, mi.author, mi.isbn, mi.description, mi.file_path, mi.file_size, mi.mime_type, mi.cover_image_path, mi.series, mi.series_number, mi.tags, mi.narrators, mi.authors, mi.authors_search, mi.publishers, mi.publishers_search, mi.abridged, mi.explicit, mi.asin, mi.date_published, mi.publisher, mi.contributors, mi.language, mi.edition, mi.page_count, mi.genre, mi.copyright_year, mi.goodreads_id, mi.openlibrary_id, mi.google_books_id, mi.added_by_admin_id, mi.created_at, mi.imported_at, mi.updated_at, mi.format_group, mi.format_mimetype, mi.is_reflowable, mi.has_fixed_layout, mi.total_characters, mi.chapter_count, mi.entitlement_id, mi.revision_number, mi.kobo_content_id, mi.kobo_metadata, mi.manga_type, mi.reading_direction, mi.series_count, mi.volume, mi.imprint, mi.age_rating, mi.web_url, mi.story_arc, mi.is_black_and_white, mi.metadata_notes, mi.community_rating, mi.alternate_info, mi.scan_information, mi.summary, mi.metadata_overrides, mi.missing_scan_count, mi.archived_at, mi.chapter_metadata, mi.library_type_name, mi.audio_metadata, mi.tags_search, mi.contributors_search, mi.narrators_search, mi.file_sha256, mi.opf_identifier, mi.opf_uuid, mi.hash_confidence, mi.source, mi.source_feed_id, mi.episode_guid, mi.feed_position, mi.season_number, mi.episode_number, mi.enclosure_url, mi.declared_duration_seconds, mi.transcript_url, mi.chapters_url, mi.hosts, mi.guests, mi.download_state, mi.download_error, mi.download_attempts, mi.download_queue_pos, COALESCE(rp.percentage, 0) AS viewer_percentage FROM media_items mi
|
||||
LEFT JOIN reading_progress rp ON rp.media_item_id = mi.id AND rp.user_id = $1::uuid
|
||||
WHERE ($2::uuid IS NULL OR mi.library_id = $2::uuid)
|
||||
AND ($3::uuid[] IS NULL OR mi.library_id = ANY($3::uuid[]))
|
||||
AND mi.archived_at IS NULL
|
||||
AND mi.missing_scan_count = 0
|
||||
ORDER BY mi.title ASC
|
||||
`
|
||||
|
||||
type GetVisibleLiveMediaForRulesParams struct {
|
||||
ViewerUserID pgtype.UUID `db:"viewer_user_id" json:"viewer_user_id"`
|
||||
LibraryID pgtype.UUID `db:"library_id" json:"library_id"`
|
||||
VisibleLibraryIds []pgtype.UUID `db:"visible_library_ids" json:"visible_library_ids"`
|
||||
}
|
||||
|
||||
type GetVisibleLiveMediaForRulesRow struct {
|
||||
MediaItems MediaItems `db:"media_items" json:"media_items"`
|
||||
ViewerPercentage float64 `db:"viewer_percentage" json:"viewer_percentage"`
|
||||
}
|
||||
|
||||
// Smart ("dynamic") collections: rules are evaluated LIVE at render, so
|
||||
// membership comes from the candidate book set instead of collection_items.
|
||||
// The set is scoped to the requesting user's visible libraries (family
|
||||
// lens enforced at the source) and live items only — the same filters the
|
||||
// static collection paths apply. The viewer's progress rides along (plan
|
||||
// item 2.4): the played/unplayed episode rule evaluates per-user, exactly
|
||||
// like the hide_finished predicate (percentage >= 0.999 = finished).
|
||||
func (q *Queries) GetVisibleLiveMediaForRules(ctx context.Context, arg GetVisibleLiveMediaForRulesParams) ([]GetVisibleLiveMediaForRulesRow, error) {
|
||||
rows, err := q.db.Query(ctx, GetVisibleLiveMediaForRules, arg.ViewerUserID, arg.LibraryID, arg.VisibleLibraryIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []GetVisibleLiveMediaForRulesRow{}
|
||||
for rows.Next() {
|
||||
var i GetVisibleLiveMediaForRulesRow
|
||||
if err := rows.Scan(
|
||||
&i.MediaItems.ID,
|
||||
&i.MediaItems.LibraryID,
|
||||
&i.MediaItems.Title,
|
||||
&i.MediaItems.Subtitle,
|
||||
&i.MediaItems.Author,
|
||||
&i.MediaItems.Isbn,
|
||||
&i.MediaItems.Description,
|
||||
&i.MediaItems.FilePath,
|
||||
&i.MediaItems.FileSize,
|
||||
&i.MediaItems.MimeType,
|
||||
&i.MediaItems.CoverImagePath,
|
||||
&i.MediaItems.Series,
|
||||
&i.MediaItems.SeriesNumber,
|
||||
&i.MediaItems.Tags,
|
||||
&i.MediaItems.Narrators,
|
||||
&i.MediaItems.Authors,
|
||||
&i.MediaItems.AuthorsSearch,
|
||||
&i.MediaItems.Publishers,
|
||||
&i.MediaItems.PublishersSearch,
|
||||
&i.MediaItems.Abridged,
|
||||
&i.MediaItems.Explicit,
|
||||
&i.MediaItems.Asin,
|
||||
&i.MediaItems.DatePublished,
|
||||
&i.MediaItems.Publisher,
|
||||
&i.MediaItems.Contributors,
|
||||
&i.MediaItems.Language,
|
||||
&i.MediaItems.Edition,
|
||||
&i.MediaItems.PageCount,
|
||||
&i.MediaItems.Genre,
|
||||
&i.MediaItems.CopyrightYear,
|
||||
&i.MediaItems.GoodreadsID,
|
||||
&i.MediaItems.OpenlibraryID,
|
||||
&i.MediaItems.GoogleBooksID,
|
||||
&i.MediaItems.AddedByAdminID,
|
||||
&i.MediaItems.CreatedAt,
|
||||
&i.MediaItems.ImportedAt,
|
||||
&i.MediaItems.UpdatedAt,
|
||||
&i.MediaItems.FormatGroup,
|
||||
&i.MediaItems.FormatMimetype,
|
||||
&i.MediaItems.IsReflowable,
|
||||
&i.MediaItems.HasFixedLayout,
|
||||
&i.MediaItems.TotalCharacters,
|
||||
&i.MediaItems.ChapterCount,
|
||||
&i.MediaItems.EntitlementID,
|
||||
&i.MediaItems.RevisionNumber,
|
||||
&i.MediaItems.KoboContentID,
|
||||
&i.MediaItems.KoboMetadata,
|
||||
&i.MediaItems.MangaType,
|
||||
&i.MediaItems.ReadingDirection,
|
||||
&i.MediaItems.SeriesCount,
|
||||
&i.MediaItems.Volume,
|
||||
&i.MediaItems.Imprint,
|
||||
&i.MediaItems.AgeRating,
|
||||
&i.MediaItems.WebUrl,
|
||||
&i.MediaItems.StoryArc,
|
||||
&i.MediaItems.IsBlackAndWhite,
|
||||
&i.MediaItems.MetadataNotes,
|
||||
&i.MediaItems.CommunityRating,
|
||||
&i.MediaItems.AlternateInfo,
|
||||
&i.MediaItems.ScanInformation,
|
||||
&i.MediaItems.Summary,
|
||||
&i.MediaItems.MetadataOverrides,
|
||||
&i.MediaItems.MissingScanCount,
|
||||
&i.MediaItems.ArchivedAt,
|
||||
&i.MediaItems.ChapterMetadata,
|
||||
&i.MediaItems.LibraryTypeName,
|
||||
&i.MediaItems.AudioMetadata,
|
||||
&i.MediaItems.TagsSearch,
|
||||
&i.MediaItems.ContributorsSearch,
|
||||
&i.MediaItems.NarratorsSearch,
|
||||
&i.MediaItems.FileSha256,
|
||||
&i.MediaItems.OpfIdentifier,
|
||||
&i.MediaItems.OpfUuid,
|
||||
&i.MediaItems.HashConfidence,
|
||||
&i.MediaItems.Source,
|
||||
&i.MediaItems.SourceFeedID,
|
||||
&i.MediaItems.EpisodeGuid,
|
||||
&i.MediaItems.FeedPosition,
|
||||
&i.MediaItems.SeasonNumber,
|
||||
&i.MediaItems.EpisodeNumber,
|
||||
&i.MediaItems.EnclosureUrl,
|
||||
&i.MediaItems.DeclaredDurationSeconds,
|
||||
&i.MediaItems.TranscriptUrl,
|
||||
&i.MediaItems.ChaptersUrl,
|
||||
&i.MediaItems.Hosts,
|
||||
&i.MediaItems.Guests,
|
||||
&i.MediaItems.DownloadState,
|
||||
&i.MediaItems.DownloadError,
|
||||
&i.MediaItems.DownloadAttempts,
|
||||
&i.MediaItems.DownloadQueuePos,
|
||||
&i.ViewerPercentage,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const HasRecentConflictResolution = `-- name: HasRecentConflictResolution :one
|
||||
SELECT EXISTS(
|
||||
SELECT 1 FROM sync_conflicts
|
||||
@@ -16484,7 +16318,7 @@ SET name = $2,
|
||||
show_on_dashboard = true,
|
||||
deleted_at = NULL
|
||||
WHERE id = $1
|
||||
RETURNING id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
RETURNING id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
`
|
||||
|
||||
type RestoreSystemCollectionRowParams struct {
|
||||
@@ -16515,8 +16349,6 @@ func (q *Queries) RestoreSystemCollectionRow(ctx context.Context, arg RestoreSys
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
@@ -18230,24 +18062,20 @@ SET
|
||||
description = $3,
|
||||
color = $4,
|
||||
icon = $5,
|
||||
auto_assign_rules = $6,
|
||||
view_settings = $7,
|
||||
rule_match_mode = $8,
|
||||
filter_tree = $9
|
||||
view_settings = $6,
|
||||
filter_tree = $7
|
||||
WHERE id = $1
|
||||
RETURNING id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
RETURNING id, user_id, name, description, color, icon, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
`
|
||||
|
||||
type UpdateCollectionParams struct {
|
||||
ID pgtype.UUID `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Description pgtype.Text `db:"description" json:"description"`
|
||||
Color pgtype.Text `db:"color" json:"color"`
|
||||
Icon pgtype.Text `db:"icon" json:"icon"`
|
||||
AutoAssignRules []byte `db:"auto_assign_rules" json:"auto_assign_rules"`
|
||||
ViewSettings []byte `db:"view_settings" json:"view_settings"`
|
||||
RuleMatchMode string `db:"rule_match_mode" json:"rule_match_mode"`
|
||||
FilterTree []byte `db:"filter_tree" json:"filter_tree"`
|
||||
ID pgtype.UUID `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Description pgtype.Text `db:"description" json:"description"`
|
||||
Color pgtype.Text `db:"color" json:"color"`
|
||||
Icon pgtype.Text `db:"icon" json:"icon"`
|
||||
ViewSettings []byte `db:"view_settings" json:"view_settings"`
|
||||
FilterTree []byte `db:"filter_tree" json:"filter_tree"`
|
||||
}
|
||||
|
||||
// Update collection
|
||||
@@ -18258,9 +18086,7 @@ func (q *Queries) UpdateCollection(ctx context.Context, arg UpdateCollectionPara
|
||||
arg.Description,
|
||||
arg.Color,
|
||||
arg.Icon,
|
||||
arg.AutoAssignRules,
|
||||
arg.ViewSettings,
|
||||
arg.RuleMatchMode,
|
||||
arg.FilterTree,
|
||||
)
|
||||
var i Collections
|
||||
@@ -18271,59 +18097,6 @@ func (q *Queries) UpdateCollection(ctx context.Context, arg UpdateCollectionPara
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
&i.QueryType,
|
||||
&i.Priority,
|
||||
&i.IsSystemCollection,
|
||||
&i.CreatedAt,
|
||||
&i.DeletedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const UpdateCollectionMatchMode = `-- name: UpdateCollectionMatchMode :exec
|
||||
UPDATE collections SET rule_match_mode = $2 WHERE id = $1
|
||||
`
|
||||
|
||||
type UpdateCollectionMatchModeParams struct {
|
||||
ID pgtype.UUID `db:"id" json:"id"`
|
||||
RuleMatchMode string `db:"rule_match_mode" json:"rule_match_mode"`
|
||||
}
|
||||
|
||||
// Smart-collection match semantics (plan item 2.4): flip any|all on its
|
||||
// own channel — the rules page toggle writes exactly this column.
|
||||
func (q *Queries) UpdateCollectionMatchMode(ctx context.Context, arg UpdateCollectionMatchModeParams) error {
|
||||
_, err := q.db.Exec(ctx, UpdateCollectionMatchMode, arg.ID, arg.RuleMatchMode)
|
||||
return err
|
||||
}
|
||||
|
||||
const UpdateCollectionRulesJSON = `-- name: UpdateCollectionRulesJSON :one
|
||||
UPDATE collections SET auto_assign_rules = $2 WHERE id = $1 RETURNING id, user_id, name, description, color, icon, auto_assign_rules, rule_match_mode, filter_tree, view_settings, show_on_dashboard, query_type, priority, is_system_collection, created_at, deleted_at
|
||||
`
|
||||
|
||||
type UpdateCollectionRulesJSONParams struct {
|
||||
ID pgtype.UUID `db:"id" json:"id"`
|
||||
AutoAssignRules []byte `db:"auto_assign_rules" json:"auto_assign_rules"`
|
||||
}
|
||||
|
||||
// Replace ONLY the auto_assign_rules document (rule CRUD: read-modify-write
|
||||
// of the JSONB without touching metadata stale clients did not send).
|
||||
func (q *Queries) UpdateCollectionRulesJSON(ctx context.Context, arg UpdateCollectionRulesJSONParams) (Collections, error) {
|
||||
row := q.db.QueryRow(ctx, UpdateCollectionRulesJSON, arg.ID, arg.AutoAssignRules)
|
||||
var i Collections
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.Name,
|
||||
&i.Description,
|
||||
&i.Color,
|
||||
&i.Icon,
|
||||
&i.AutoAssignRules,
|
||||
&i.RuleMatchMode,
|
||||
&i.FilterTree,
|
||||
&i.ViewSettings,
|
||||
&i.ShowOnDashboard,
|
||||
|
||||
@@ -3489,8 +3489,8 @@ DELETE FROM device_file_aliases WHERE id = $1;
|
||||
|
||||
-- Create collection
|
||||
-- name: CreateCollection :one
|
||||
INSERT INTO collections (user_id, name, description, color, icon, auto_assign_rules, view_settings, show_on_dashboard, filter_tree)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
INSERT INTO collections (user_id, name, description, color, icon, view_settings, show_on_dashboard, filter_tree)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING *;
|
||||
|
||||
-- A2c §2.4: provenance + playlist flags on an existing collection without
|
||||
@@ -3502,11 +3502,6 @@ RETURNING *;
|
||||
-- dropping the phantom column write.)
|
||||
UPDATE collections SET view_settings = $2 WHERE id = $1;
|
||||
|
||||
-- Smart-collection match semantics (plan item 2.4): flip any|all on its
|
||||
-- own channel — the rules page toggle writes exactly this column.
|
||||
-- name: UpdateCollectionMatchMode :exec
|
||||
UPDATE collections SET rule_match_mode = $2 WHERE id = $1;
|
||||
|
||||
-- Get collection
|
||||
-- name: GetCollection :one
|
||||
SELECT * FROM collections WHERE id = $1;
|
||||
@@ -3533,18 +3528,11 @@ SET
|
||||
description = $3,
|
||||
color = $4,
|
||||
icon = $5,
|
||||
auto_assign_rules = $6,
|
||||
view_settings = $7,
|
||||
rule_match_mode = $8,
|
||||
filter_tree = $9
|
||||
view_settings = $6,
|
||||
filter_tree = $7
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- Replace ONLY the auto_assign_rules document (rule CRUD: read-modify-write
|
||||
-- of the JSONB without touching metadata stale clients did not send).
|
||||
-- name: UpdateCollectionRulesJSON :one
|
||||
UPDATE collections SET auto_assign_rules = $2 WHERE id = $1 RETURNING *;
|
||||
|
||||
-- Delete collection
|
||||
-- name: DeleteCollection :exec
|
||||
DELETE FROM collections WHERE id = $1;
|
||||
@@ -4034,8 +4022,8 @@ RETURNING *;
|
||||
DELETE FROM collection_items WHERE collection_id = $1;
|
||||
|
||||
-- name: CreateSystemCollection :one
|
||||
INSERT INTO collections (user_id, name, description, icon, color, show_on_dashboard, query_type, priority, is_system_collection, auto_assign_rules)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, true, 'null'::jsonb)
|
||||
INSERT INTO collections (user_id, name, description, icon, color, show_on_dashboard, query_type, priority, is_system_collection)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, true)
|
||||
RETURNING *;
|
||||
|
||||
-- name: ResetSystemCollectionMetadata :exec
|
||||
@@ -4151,22 +4139,6 @@ LIMIT sqlc.narg('limit');
|
||||
-- name: CountCollectionItems :one
|
||||
SELECT COUNT(*) FROM collection_items WHERE collection_id = $1;
|
||||
|
||||
-- Smart ("dynamic") collections: rules are evaluated LIVE at render, so
|
||||
-- membership comes from the candidate book set instead of collection_items.
|
||||
-- The set is scoped to the requesting user's visible libraries (family
|
||||
-- lens enforced at the source) and live items only — the same filters the
|
||||
-- static collection paths apply. The viewer's progress rides along (plan
|
||||
-- item 2.4): the played/unplayed episode rule evaluates per-user, exactly
|
||||
-- like the hide_finished predicate (percentage >= 0.999 = finished).
|
||||
-- name: GetVisibleLiveMediaForRules :many
|
||||
SELECT sqlc.embed(mi), COALESCE(rp.percentage, 0) AS viewer_percentage FROM media_items mi
|
||||
LEFT JOIN reading_progress rp ON rp.media_item_id = mi.id AND rp.user_id = sqlc.narg('viewer_user_id')::uuid
|
||||
WHERE (sqlc.narg('library_id')::uuid IS NULL OR mi.library_id = sqlc.narg('library_id')::uuid)
|
||||
AND (sqlc.narg('visible_library_ids')::uuid[] IS NULL OR mi.library_id = ANY(sqlc.narg('visible_library_ids')::uuid[]))
|
||||
AND mi.archived_at IS NULL
|
||||
AND mi.missing_scan_count = 0
|
||||
ORDER BY mi.title ASC;
|
||||
|
||||
-- Live item count for a STATIC collection (rule-free cards on the
|
||||
-- collections page); same live filter as GetCollectionItems, plus the
|
||||
-- future-hybrid excluded exclusion and an optional library scope.
|
||||
|
||||
+3
-12
@@ -49,7 +49,7 @@ var Schemas = map[string]Schema{
|
||||
Response: json.RawMessage(`{
|
||||
"id": "", "user_id": "", "name": "", "description": "",
|
||||
"color": "", "icon": "", "collection_type": "",
|
||||
"auto_assign_rules": null, "view_settings": {},
|
||||
"view_settings": {},
|
||||
"book_count": 0, "created_at": ""
|
||||
}`),
|
||||
},
|
||||
@@ -62,7 +62,7 @@ var Schemas = map[string]Schema{
|
||||
"GET /api/collections/:id": {
|
||||
Response: json.RawMessage(`{
|
||||
"id": "", "user_id": "", "name": "", "description": "",
|
||||
"color": "", "icon": "", "auto_assign_rules": null,
|
||||
"color": "", "icon": "",
|
||||
"view_settings": {}, "books": [], "book_count": 0,
|
||||
"is_smart": false, "up_next": [], "created_at": ""
|
||||
}`),
|
||||
@@ -71,7 +71,7 @@ var Schemas = map[string]Schema{
|
||||
Request: handlers.UpdateCollectionRequest{},
|
||||
Response: json.RawMessage(`{
|
||||
"id": "", "user_id": "", "name": "", "description": "",
|
||||
"color": "", "icon": "", "auto_assign_rules": null,
|
||||
"color": "", "icon": "",
|
||||
"view_settings": {}, "created_at": ""
|
||||
}`),
|
||||
},
|
||||
@@ -609,15 +609,6 @@ var Schemas = map[string]Schema{
|
||||
"id": "", "chain_order": "", "view_settings": {}
|
||||
}`),
|
||||
},
|
||||
"PUT /api/collections/:id/rules/:ruleId": {
|
||||
// Handler is SetRuleEnabled: toggle only, request {enabled required},
|
||||
// response {collection_id, is_smart}.
|
||||
Request: json.RawMessage(`{"enabled": false}`),
|
||||
Response: json.RawMessage(`{
|
||||
"collection_id": "", "is_smart": false
|
||||
}`),
|
||||
},
|
||||
|
||||
// --- Beam batch 7: scanner ---
|
||||
"POST /api/scanner/scan": {
|
||||
Request: handlers.ScanLibraryRequest{},
|
||||
|
||||
@@ -35,12 +35,17 @@ func NewCollectionHandler(db *database.Queries, libraryService *services.Library
|
||||
}
|
||||
|
||||
type CreateCollectionRequest struct {
|
||||
Name string `form:"name" json:"name" validate:"required"`
|
||||
Description string `form:"description" json:"description"`
|
||||
Color string `form:"color" json:"color"`
|
||||
Icon string `form:"icon" json:"icon"`
|
||||
CollectionType string `form:"collection_type" json:"collection_type"` // "static" (default) or "smart" (rule-based)
|
||||
AutoAssignRules []services.Rule `form:"auto_assign_rules" json:"auto_assign_rules"`
|
||||
Name string `form:"name" json:"name" validate:"required"`
|
||||
Description string `form:"description" json:"description"`
|
||||
Color string `form:"color" json:"color"`
|
||||
Icon string `form:"icon" json:"icon"`
|
||||
CollectionType string `form:"collection_type" json:"collection_type"` // "static" (default) or "smart"
|
||||
// Retired custom-section-builder payload (2026-10-31): the rules
|
||||
// ARRAY storage is gone — the filter tree is the only smart
|
||||
// membership. The field exists ONLY so the builder's legacy save
|
||||
// fails LOUDLY (400) instead of silently creating a hollow static
|
||||
// section. Remove it at the builder's tree migration.
|
||||
AutoAssignRules json.RawMessage `json:"auto_assign_rules"`
|
||||
ViewSettings map[string]interface{} `form:"view_settings" json:"view_settings"`
|
||||
// The unified filter tree (item 3.2): when present the collection's
|
||||
// smart membership runs through the qf engine. INVALID trees are a
|
||||
@@ -105,22 +110,17 @@ func resolveTreePayload(jsonTree any, formTree string) any {
|
||||
|
||||
type UpdateCollectionRequest struct {
|
||||
Name string `form:"name" json:"name" validate:"required"`
|
||||
// Smart-collection match semantics (item 2.4): "any" (OR) | "all"
|
||||
// (AND). Empty/invalid keeps the stored value.
|
||||
RuleMatchMode string `form:"rule_match_mode" json:"rule_match_mode"`
|
||||
// The unified filter tree (item 3.2): when present the collection's
|
||||
// smart membership runs through the qf engine. INVALID trees are a
|
||||
// 400 (ParseFilterTree at the handler); a tree supersedes
|
||||
// auto_assign_rules. JSON bodies bind FilterTree; form bodies bind
|
||||
// 400 (ParseFilterTree at the handler). JSON bodies bind FilterTree; form bodies bind
|
||||
// FilterTreeForm (echo's binder cannot set an any-typed field from
|
||||
// a form string — see CreateCollectionRequest).
|
||||
FilterTree any `json:"filter_tree"`
|
||||
FilterTreeForm string `form:"filter_tree"`
|
||||
Description string `form:"description" json:"description"`
|
||||
Color string `form:"color" json:"color"`
|
||||
Icon string `form:"icon" json:"icon"`
|
||||
AutoAssignRules []services.Rule `form:"auto_assign_rules" json:"auto_assign_rules"`
|
||||
ViewSettings map[string]interface{} `form:"view_settings" json:"view_settings"`
|
||||
FilterTree any `json:"filter_tree"`
|
||||
FilterTreeForm string `form:"filter_tree"`
|
||||
Description string `form:"description" json:"description"`
|
||||
Color string `form:"color" json:"color"`
|
||||
Icon string `form:"icon" json:"icon"`
|
||||
ViewSettings map[string]interface{} `form:"view_settings" json:"view_settings"`
|
||||
// A2c §3.3: playlist toggles — merged into view_settings server-side
|
||||
// (nil = untouched). []string because the form posts a hidden "false"
|
||||
// sibling next to the checkbox; the LAST value wins. JSON clients may
|
||||
@@ -158,15 +158,14 @@ type DeviceMappingSummary struct {
|
||||
// CollectionSummary is one entry in GET /api/collections. Package-level so
|
||||
// the docs schema registry (internal/docs) can pin the response shape.
|
||||
type CollectionSummary struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Color string `json:"color"`
|
||||
Icon string `json:"icon"`
|
||||
AutoAssignRules json.RawMessage `json:"auto_assign_rules"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
BookCount int `json:"book_count"`
|
||||
IsSmart bool `json:"is_smart"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Color string `json:"color"`
|
||||
Icon string `json:"icon"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
BookCount int `json:"book_count"`
|
||||
IsSmart bool `json:"is_smart"`
|
||||
}
|
||||
|
||||
// BookCollectionSummary is one entry in GET /api/collections/:id/books —
|
||||
@@ -207,6 +206,14 @@ func (h *CollectionHandler) CreateCollection(c *echo.Context) error {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
// The custom-section builder still posts the RETIRED rules payload
|
||||
// (its save was already failing to bind — verified 2026-10-31).
|
||||
// Reject loudly rather than silently creating a hollow section.
|
||||
if len(req.AutoAssignRules) > 0 {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"error": "auto_assign_rules was retired — collections carry filter trees now (the custom-section builder needs its flagged tree migration)",
|
||||
})
|
||||
}
|
||||
treeJSON, treeErr := parseRequestFilterTree(resolveTreePayload(req.FilterTree, req.FilterTreeForm))
|
||||
if treeErr != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": treeErr.Error()})
|
||||
@@ -218,7 +225,6 @@ func (h *CollectionHandler) CreateCollection(c *echo.Context) error {
|
||||
req.Description,
|
||||
req.Color,
|
||||
req.Icon,
|
||||
req.AutoAssignRules,
|
||||
req.ViewSettings,
|
||||
treeJSON,
|
||||
)
|
||||
@@ -230,19 +236,18 @@ func (h *CollectionHandler) CreateCollection(c *echo.Context) error {
|
||||
}
|
||||
|
||||
bookCount := int32(0)
|
||||
isSmart := req.CollectionType == "smart" || len(req.AutoAssignRules) > 0
|
||||
isSmart := req.CollectionType == "smart" || treeJSON != nil
|
||||
response := map[string]interface{}{
|
||||
"id": uuid.UUID(collection.ID.Bytes).String(),
|
||||
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
|
||||
"name": collection.Name,
|
||||
"description": utils.PgTextString(collection.Description),
|
||||
"color": utils.PgTextString(collection.Color),
|
||||
"icon": utils.PgTextString(collection.Icon),
|
||||
"collection_type": map[bool]string{true: "smart", false: "static"}[isSmart],
|
||||
"auto_assign_rules": collection.AutoAssignRules,
|
||||
"view_settings": collection.ViewSettings,
|
||||
"book_count": bookCount,
|
||||
"created_at": collection.CreatedAt.Time.String(),
|
||||
"id": uuid.UUID(collection.ID.Bytes).String(),
|
||||
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
|
||||
"name": collection.Name,
|
||||
"description": utils.PgTextString(collection.Description),
|
||||
"color": utils.PgTextString(collection.Color),
|
||||
"icon": utils.PgTextString(collection.Icon),
|
||||
"collection_type": map[bool]string{true: "smart", false: "static"}[isSmart],
|
||||
"view_settings": collection.ViewSettings,
|
||||
"book_count": bookCount,
|
||||
"created_at": collection.CreatedAt.Time.String(),
|
||||
}
|
||||
|
||||
// Add redirect header for HTMX requests
|
||||
@@ -292,7 +297,6 @@ func (h *CollectionHandler) GetCollections(c *echo.Context) error {
|
||||
matches, evalErr := h.collectionService.EvaluateSmartMembership(c.Request().Context(), col, services.SmartEvalOptions{
|
||||
LibraryID: libUUID,
|
||||
VisibleIDs: visibleIDs,
|
||||
MatchMode: col.RuleMatchMode,
|
||||
ViewerUserID: MustGetAuthenticatedUser(c).ID,
|
||||
})
|
||||
if evalErr == nil {
|
||||
@@ -310,15 +314,14 @@ func (h *CollectionHandler) GetCollections(c *echo.Context) error {
|
||||
}
|
||||
|
||||
response = append(response, CollectionSummary{
|
||||
ID: col.ID.Bytes,
|
||||
Name: col.Name,
|
||||
Description: utils.PgTextString(col.Description),
|
||||
Color: utils.PgTextString(col.Color),
|
||||
Icon: utils.PgTextString(col.Icon),
|
||||
AutoAssignRules: col.AutoAssignRules,
|
||||
CreatedAt: col.CreatedAt.Time.String(),
|
||||
BookCount: bookCount,
|
||||
IsSmart: isSmart,
|
||||
ID: col.ID.Bytes,
|
||||
Name: col.Name,
|
||||
Description: utils.PgTextString(col.Description),
|
||||
Color: utils.PgTextString(col.Color),
|
||||
Icon: utils.PgTextString(col.Icon),
|
||||
CreatedAt: col.CreatedAt.Time.String(),
|
||||
BookCount: bookCount,
|
||||
IsSmart: isSmart,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -371,7 +374,6 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error {
|
||||
matches, evalErr := h.collectionService.EvaluateSmartMembership(c.Request().Context(), collection, services.SmartEvalOptions{
|
||||
LibraryID: libUUID,
|
||||
VisibleIDs: visibleIDs,
|
||||
MatchMode: collection.RuleMatchMode,
|
||||
ViewerUserID: MustGetAuthenticatedUser(c).ID,
|
||||
})
|
||||
if evalErr != nil {
|
||||
@@ -479,21 +481,19 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error {
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"id": uuid.UUID(collection.ID.Bytes).String(),
|
||||
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
|
||||
"name": collection.Name,
|
||||
"description": utils.PgTextString(collection.Description),
|
||||
"color": utils.PgTextString(collection.Color),
|
||||
"icon": utils.PgTextString(collection.Icon),
|
||||
"auto_assign_rules": json.RawMessage(collection.AutoAssignRules),
|
||||
"view_settings": viewSettings,
|
||||
"rule_match_mode": collection.RuleMatchMode,
|
||||
"filter_tree": treeEnvelope(collection.FilterTree),
|
||||
"books": bookList,
|
||||
"book_count": len(bookList),
|
||||
"is_smart": isSmart,
|
||||
"up_next": upNext,
|
||||
"created_at": collection.CreatedAt.Time.String(),
|
||||
"id": uuid.UUID(collection.ID.Bytes).String(),
|
||||
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
|
||||
"name": collection.Name,
|
||||
"description": utils.PgTextString(collection.Description),
|
||||
"color": utils.PgTextString(collection.Color),
|
||||
"icon": utils.PgTextString(collection.Icon),
|
||||
"view_settings": viewSettings,
|
||||
"filter_tree": treeEnvelope(collection.FilterTree),
|
||||
"books": bookList,
|
||||
"book_count": len(bookList),
|
||||
"is_smart": isSmart,
|
||||
"up_next": upNext,
|
||||
"created_at": collection.CreatedAt.Time.String(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -539,9 +539,7 @@ func (h *CollectionHandler) UpdateCollection(c *echo.Context) error {
|
||||
req.Description,
|
||||
req.Color,
|
||||
req.Icon,
|
||||
req.AutoAssignRules,
|
||||
req.ViewSettings,
|
||||
req.RuleMatchMode,
|
||||
treeJSON,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -555,15 +553,14 @@ func (h *CollectionHandler) UpdateCollection(c *echo.Context) error {
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
"id": uuid.UUID(collection.ID.Bytes).String(),
|
||||
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
|
||||
"name": collection.Name,
|
||||
"description": utils.PgTextString(collection.Description),
|
||||
"color": utils.PgTextString(collection.Color),
|
||||
"icon": utils.PgTextString(collection.Icon),
|
||||
"auto_assign_rules": json.RawMessage(collection.AutoAssignRules),
|
||||
"view_settings": json.RawMessage(collection.ViewSettings),
|
||||
"created_at": collection.CreatedAt.Time.String(),
|
||||
"id": uuid.UUID(collection.ID.Bytes).String(),
|
||||
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
|
||||
"name": collection.Name,
|
||||
"description": utils.PgTextString(collection.Description),
|
||||
"color": utils.PgTextString(collection.Color),
|
||||
"icon": utils.PgTextString(collection.Icon),
|
||||
"view_settings": json.RawMessage(collection.ViewSettings),
|
||||
"created_at": collection.CreatedAt.Time.String(),
|
||||
}
|
||||
|
||||
// Add redirect header for HTMX requests
|
||||
@@ -624,9 +621,9 @@ func (h *CollectionHandler) SetCollectionChainOrder(c *echo.Context) error {
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "collection not found"})
|
||||
}
|
||||
// Same owner gate as the sibling config mutations (SetMatchMode,
|
||||
// ReorderItems) — my endpoint writes the row, so it must not let a
|
||||
// non-owner flip someone else's chain order.
|
||||
// Same owner gate as the sibling config mutation (ReorderItems) —
|
||||
// my endpoint writes the row, so it must not let a non-owner flip
|
||||
// someone else's chain order.
|
||||
if u, ok := c.Get("user").(database.Users); ok {
|
||||
owns, err := h.collectionService.UserOwnsCollection(c.Request().Context(), collectionID, uuid.UUID(u.ID.Bytes))
|
||||
if err != nil || !owns {
|
||||
@@ -1035,15 +1032,13 @@ func (h *CollectionHandler) rejectSmartMutation(c *echo.Context, collectionID uu
|
||||
return echo.NewHTTPError(http.StatusNotFound, "collection not found")
|
||||
}
|
||||
if services.IsCollectionSmart(collection) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "smart collections are rule-based — membership is managed by the collection's rules, not by hand")
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "smart collections are tree-based — membership is managed by the collection's filter tree, not by hand")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRule handles POST /api/collections/:id/rules — appends one rule to
|
||||
// the ruleset. The first rule converts a static collection to smart.
|
||||
// treeEnvelope renders the stored filter tree for the envelope: nil
|
||||
// (legacy/static collections) serializes as null, trees as the object.
|
||||
// (static collections) serializes as null, trees as the object.
|
||||
func treeEnvelope(t []byte) any {
|
||||
if len(t) == 0 {
|
||||
return nil
|
||||
@@ -1255,43 +1250,6 @@ func (h *CollectionHandler) PreviewCollection(c *echo.Context) error {
|
||||
// (A2c §3.2). Owner-only; the payload is the COMPLETE ordered id list —
|
||||
// ids omitted keep their relative order appended after (the service never
|
||||
// deletes membership).
|
||||
// SetMatchMode handles PUT /api/collections/:id/match-mode (plan item
|
||||
// 2.4): flips the smart ruleset's semantics any|all on its own channel
|
||||
// — the rules page toggle writes exactly this column, nothing else.
|
||||
func (h *CollectionHandler) SetMatchMode(c *echo.Context) error {
|
||||
collectionID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid collection id"})
|
||||
}
|
||||
user, ok := c.Get("user").(database.Users)
|
||||
if !ok {
|
||||
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "unauthorized"})
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Mode string `json:"mode" form:"mode" validate:"required,oneof=any all"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "mode must be any or all"})
|
||||
}
|
||||
if err := c.Validate(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
owns, err := h.collectionService.UserOwnsCollection(c.Request().Context(), collectionID, uuid.UUID(user.ID.Bytes))
|
||||
if err != nil || !owns {
|
||||
return c.JSON(http.StatusForbidden, map[string]string{"error": "only the collection owner can change it"})
|
||||
}
|
||||
|
||||
if err := h.db.UpdateCollectionMatchMode(c.Request().Context(), database.UpdateCollectionMatchModeParams{
|
||||
ID: pgtype.UUID{Bytes: collectionID, Valid: true},
|
||||
RuleMatchMode: req.Mode,
|
||||
}); err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
return c.JSON(http.StatusOK, map[string]string{"mode": req.Mode})
|
||||
}
|
||||
|
||||
func (h *CollectionHandler) ReorderItems(c *echo.Context) error {
|
||||
collectionID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
|
||||
@@ -17,7 +17,6 @@ func registerCollectionsRoutes(cfg *Config) {
|
||||
collections.GET("/:id/books", cfg.CollectionHandler.GetBookCollections)
|
||||
collections.POST("/:id/books", cfg.CollectionHandler.AddBooks)
|
||||
collections.PUT("/:id/items/order", cfg.CollectionHandler.ReorderItems)
|
||||
collections.PUT("/:id/match-mode", cfg.CollectionHandler.SetMatchMode)
|
||||
// The chain's listening order lives ON the collection (plan §10 R2):
|
||||
// merges into view_settings; the episode chain producer honors it.
|
||||
collections.PUT("/:id/view-settings", cfg.CollectionHandler.SetCollectionChainOrder)
|
||||
|
||||
@@ -569,7 +569,6 @@ func registerFrontendRoutes(cfg *Config) {
|
||||
col, services.SmartEvalOptions{
|
||||
LibraryID: libScope,
|
||||
VisibleIDs: visibleIDs,
|
||||
MatchMode: col.RuleMatchMode,
|
||||
ViewerUserID: func() pgtype.UUID {
|
||||
uid, _ := uuid.Parse(user.ID)
|
||||
return uuidToPGType(uid)
|
||||
@@ -702,9 +701,9 @@ func registerFrontendRoutes(cfg *Config) {
|
||||
}
|
||||
|
||||
// The editor's vocabulary + prefill (item 3.4): the unified
|
||||
// builder in the collection's scope. A stored tree renders as-is;
|
||||
// a legacy rules array preloads as its CONVERTED tree
|
||||
// (RulesToFilterTree) — the upgrade lands at the first Apply.
|
||||
// builder in the collection's scope. A stored tree renders
|
||||
// as-is. (The legacy rules-array preload retired 2026-10-31
|
||||
// with the rules-array storage — the tree is the only form.)
|
||||
scope := "books"
|
||||
treeJSON := "{}"
|
||||
if len(collection.FilterTree) > 0 {
|
||||
@@ -714,13 +713,6 @@ func registerFrontendRoutes(cfg *Config) {
|
||||
treeJSON = string(b)
|
||||
}
|
||||
}
|
||||
} else if rules := cfg.CollectionHandler.CollectionService().ParseRules(collection.AutoAssignRules); len(rules) > 0 {
|
||||
if t, cerr := services.RulesToFilterTree(rules, collection.RuleMatchMode); cerr == nil {
|
||||
scope = t.FilterScope()
|
||||
if b, merr := json.Marshal(t); merr == nil {
|
||||
treeJSON = string(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
fieldsPayload, merr := json.Marshal(services.FilterFieldsPayload(scope))
|
||||
if merr != nil {
|
||||
|
||||
@@ -288,7 +288,6 @@ func collectionDataFromRow(collection database.Collections, collectionID string,
|
||||
Icon: collection.Icon.String,
|
||||
IsSystem: collection.IsSystemCollection.Bool,
|
||||
IsSmart: isSmart,
|
||||
MatchMode: collection.RuleMatchMode,
|
||||
ChainOrder: chainOrder,
|
||||
BookCount: bookCount,
|
||||
}
|
||||
@@ -312,7 +311,6 @@ func (cfg *Config) collectionBooksForScope(c *echo.Context, userUUID uuid.UUID,
|
||||
collection, services.SmartEvalOptions{
|
||||
LibraryID: libScope,
|
||||
VisibleIDs: visibleIDs,
|
||||
MatchMode: collection.RuleMatchMode,
|
||||
ViewerUserID: pgtype.UUID{Bytes: userUUID, Valid: true},
|
||||
})
|
||||
if evalErr != nil {
|
||||
|
||||
@@ -341,7 +341,6 @@ func buildSmartCollectionSequenceContext(c *echo.Context, cfg *Config, item *dat
|
||||
}
|
||||
matches, err := colService.EvaluateSmartMembership(ctx, coll, services.SmartEvalOptions{
|
||||
VisibleIDs: visibleIDs,
|
||||
MatchMode: coll.RuleMatchMode,
|
||||
ViewerUserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -32,42 +32,6 @@ type Rule struct {
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
}
|
||||
|
||||
// Known rule fields and operators — the rules builder UI and the
|
||||
// persisted JSONB documents must agree with this vocabulary.
|
||||
// Episode fields (plan item 2.4): feed/downloaded/published_within ride
|
||||
// the media row; played reads the VIEWER's progress (the candidate
|
||||
// query carries it — SmartEvalOptions) so "unplayed" is per-user.
|
||||
var ruleFields = map[string]bool{
|
||||
"genre": true, "series": true, "author": true, "language": true,
|
||||
"publisher": true, "copyright_year": true, "tags": true,
|
||||
// Episode collections (plan item 2.4):
|
||||
"title": true, "description": true,
|
||||
"feed": true, "played": true, "downloaded": true, "published_within": true,
|
||||
}
|
||||
|
||||
var ruleOperators = map[string]bool{
|
||||
"equals": true, "not_equals": true, "contains": true, "not_contains": true,
|
||||
"starts_with": true, "ends_with": true, "greater_than": true, "less_than": true,
|
||||
"in": true, // multi-value (comma-separated) — the feed multi-select
|
||||
}
|
||||
|
||||
// Smart-collection match semantics (plan item 2.4): 'any' = OR (the
|
||||
// legacy behavior), 'all' = AND (the daily briefing).
|
||||
const (
|
||||
RuleMatchAny = "any"
|
||||
RuleMatchAll = "all"
|
||||
)
|
||||
|
||||
func ValidRuleMatchMode(m string) bool {
|
||||
return m == RuleMatchAny || m == RuleMatchAll
|
||||
}
|
||||
|
||||
// RuleValidationError reports a rule payload the evaluator could never
|
||||
// honor (unknown field/operator, empty value).
|
||||
type RuleValidationError struct{ Msg string }
|
||||
|
||||
func (e *RuleValidationError) Error() string { return e.Msg }
|
||||
|
||||
// RuleEvaluation represents the result of evaluating a rule
|
||||
type RuleEvaluation struct {
|
||||
RuleID string `json:"rule_id"`
|
||||
@@ -96,20 +60,10 @@ func NewCollectionService(db *database.Queries, search ...*SearchService) *Colle
|
||||
return svc
|
||||
}
|
||||
|
||||
// CreateCollection creates a new collection. filterTree (item 3.2)
|
||||
// replaces autoAssignRules as the smart engine's source when present —
|
||||
// both may coexist on the row (tree wins at evaluation).
|
||||
func (s *CollectionService) CreateCollection(ctx context.Context, userID uuid.UUID, name, description, color, icon string, autoAssignRules []Rule, viewSettings map[string]any, filterTree []byte) (database.Collections, error) {
|
||||
// Convert rules to JSONB ([]byte)
|
||||
var rulesJSON []byte
|
||||
if len(autoAssignRules) > 0 {
|
||||
var err error
|
||||
rulesJSON, err = json.Marshal(autoAssignRules)
|
||||
if err != nil {
|
||||
return database.Collections{}, fmt.Errorf("failed to marshal auto-assign rules: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// CreateCollection creates a new collection. filterTree (item 3.2), when
|
||||
// present, makes it smart — the qf engine is the only membership source
|
||||
// (the legacy rules-array storage retired 2026-10-31).
|
||||
func (s *CollectionService) CreateCollection(ctx context.Context, userID uuid.UUID, name, description, color, icon string, viewSettings map[string]any, filterTree []byte) (database.Collections, error) {
|
||||
// Convert view settings to JSONB ([]byte)
|
||||
var settingsJSON []byte
|
||||
if len(viewSettings) > 0 {
|
||||
@@ -121,14 +75,13 @@ func (s *CollectionService) CreateCollection(ctx context.Context, userID uuid.UU
|
||||
}
|
||||
|
||||
collection, err := s.db.CreateCollection(ctx, database.CreateCollectionParams{
|
||||
UserID: pgtype.UUID{Bytes: userID, Valid: true},
|
||||
Name: name,
|
||||
Description: pgtype.Text{String: description, Valid: description != ""},
|
||||
Color: pgtype.Text{String: color, Valid: color != ""},
|
||||
Icon: pgtype.Text{String: icon, Valid: icon != ""},
|
||||
AutoAssignRules: rulesJSON,
|
||||
ViewSettings: settingsJSON,
|
||||
FilterTree: filterTree,
|
||||
UserID: pgtype.UUID{Bytes: userID, Valid: true},
|
||||
Name: name,
|
||||
Description: pgtype.Text{String: description, Valid: description != ""},
|
||||
Color: pgtype.Text{String: color, Valid: color != ""},
|
||||
Icon: pgtype.Text{String: icon, Valid: icon != ""},
|
||||
ViewSettings: settingsJSON,
|
||||
FilterTree: filterTree,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -164,18 +117,13 @@ func (s *CollectionService) GetUserCollections(ctx context.Context, userID uuid.
|
||||
return collections, nil
|
||||
}
|
||||
|
||||
// UpdateCollection updates a collection
|
||||
// UpdateCollection rewrites the collection row. ruleMatchMode empty or
|
||||
// invalid = keep the stored value (the edit form only sends the toggles
|
||||
// it renders — plan item 2.4). filterTree semantics: nil = leave the
|
||||
// stored tree untouched.
|
||||
func (s *CollectionService) UpdateCollection(ctx context.Context, collectionID uuid.UUID, name, description, color, icon string, autoAssignRules []Rule, viewSettings map[string]any, ruleMatchMode string, filterTree []byte) (database.Collections, error) {
|
||||
// UpdateCollection rewrites the collection row. filterTree semantics:
|
||||
// nil = leave the stored tree untouched (forms don't send trees).
|
||||
func (s *CollectionService) UpdateCollection(ctx context.Context, collectionID uuid.UUID, name, description, color, icon string, viewSettings map[string]any, filterTree []byte) (database.Collections, error) {
|
||||
// A2c §3.3: the edit form carries only the toggles it renders — merge
|
||||
// them OVER the stored view_settings so provenance keys
|
||||
// (imported_from_series) and anything else on the row survive.
|
||||
existingMode := RuleMatchAny
|
||||
if existing, err := s.db.GetCollection(ctx, pgtype.UUID{Bytes: collectionID, Valid: true}); err == nil {
|
||||
existingMode = existing.RuleMatchMode
|
||||
if filterTree == nil {
|
||||
filterTree = existing.FilterTree // nil = untouched (forms don't send trees)
|
||||
}
|
||||
@@ -201,20 +149,6 @@ func (s *CollectionService) UpdateCollection(ctx context.Context, collectionID u
|
||||
}
|
||||
}
|
||||
}
|
||||
if !ValidRuleMatchMode(ruleMatchMode) {
|
||||
ruleMatchMode = existingMode
|
||||
}
|
||||
|
||||
// Convert rules to JSONB ([]byte)
|
||||
var rulesJSON []byte
|
||||
if len(autoAssignRules) > 0 {
|
||||
var err error
|
||||
rulesJSON, err = json.Marshal(autoAssignRules)
|
||||
if err != nil {
|
||||
return database.Collections{}, fmt.Errorf("failed to marshal auto-assign rules: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Convert view settings to JSONB ([]byte)
|
||||
var settingsJSON []byte
|
||||
if len(viewSettings) > 0 {
|
||||
@@ -226,15 +160,13 @@ func (s *CollectionService) UpdateCollection(ctx context.Context, collectionID u
|
||||
}
|
||||
|
||||
collection, err := s.db.UpdateCollection(ctx, database.UpdateCollectionParams{
|
||||
ID: pgtype.UUID{Bytes: collectionID, Valid: true},
|
||||
Name: name,
|
||||
Description: pgtype.Text{String: description, Valid: description != ""},
|
||||
Color: pgtype.Text{String: color, Valid: color != ""},
|
||||
Icon: pgtype.Text{String: icon, Valid: icon != ""},
|
||||
AutoAssignRules: rulesJSON,
|
||||
ViewSettings: settingsJSON,
|
||||
RuleMatchMode: ruleMatchMode,
|
||||
FilterTree: filterTree,
|
||||
ID: pgtype.UUID{Bytes: collectionID, Valid: true},
|
||||
Name: name,
|
||||
Description: pgtype.Text{String: description, Valid: description != ""},
|
||||
Color: pgtype.Text{String: color, Valid: color != ""},
|
||||
Icon: pgtype.Text{String: icon, Valid: icon != ""},
|
||||
ViewSettings: settingsJSON,
|
||||
FilterTree: filterTree,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -316,162 +248,7 @@ func (s *CollectionService) GetBookCollections(ctx context.Context, mediaItemID
|
||||
}
|
||||
|
||||
return collections, nil
|
||||
}
|
||||
|
||||
// ParseRules decodes a collection's auto_assign_rules JSONB into typed
|
||||
// rules. "null", empty, and malformed docs mean "no rules".
|
||||
func (s *CollectionService) ParseRules(raw []byte) []Rule {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
var rules []Rule
|
||||
if err := json.Unmarshal(raw, &rules); err != nil {
|
||||
return nil
|
||||
}
|
||||
return rules
|
||||
}
|
||||
|
||||
// IsSmartCollection derives the collection type: a non-empty ruleset
|
||||
// makes it SMART (live-matched), an empty/absent ruleset makes it STATIC
|
||||
// (stored collection_items links).
|
||||
func (s *CollectionService) IsSmartCollection(raw []byte) bool {
|
||||
return len(s.ParseRules(raw)) > 0
|
||||
}
|
||||
|
||||
// EnabledRules filters a ruleset down to the rules that participate in
|
||||
// live evaluation (absent enabled = enabled for backward compatibility).
|
||||
func (s *CollectionService) EnabledRules(rules []Rule) []Rule {
|
||||
enabled := make([]Rule, 0, len(rules))
|
||||
for _, rule := range rules {
|
||||
if rule.Enabled == nil || *rule.Enabled {
|
||||
enabled = append(enabled, rule)
|
||||
}
|
||||
}
|
||||
return enabled
|
||||
}
|
||||
|
||||
// validateRule checks a rule payload the evaluator will have to honor.
|
||||
func (s *CollectionService) validateRule(rule Rule) error {
|
||||
if !ruleFields[rule.Field] {
|
||||
return &RuleValidationError{Msg: "unknown rule field: " + rule.Field}
|
||||
}
|
||||
if !ruleOperators[rule.Operator] {
|
||||
return &RuleValidationError{Msg: "unknown rule operator: " + rule.Operator}
|
||||
}
|
||||
if strings.TrimSpace(rule.Value) == "" {
|
||||
return &RuleValidationError{Msg: "rule value is required"}
|
||||
}
|
||||
if rule.Priority < 0 || rule.Priority > 10 {
|
||||
return &RuleValidationError{Msg: "priority must be between 0 and 10"}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRule appends a rule to the collection's ruleset, generating an id
|
||||
// when the caller did not supply one. Adding the first rule CONVERTS a
|
||||
// static collection to smart (type is derived from rules presence).
|
||||
func (s *CollectionService) AddRule(ctx context.Context, collectionID uuid.UUID, rule Rule) (database.Collections, Rule, error) {
|
||||
if err := s.validateRule(rule); err != nil {
|
||||
return database.Collections{}, Rule{}, err
|
||||
}
|
||||
|
||||
collection, err := s.GetCollection(ctx, collectionID)
|
||||
if err != nil {
|
||||
return database.Collections{}, Rule{}, err
|
||||
}
|
||||
|
||||
rules := s.ParseRules(collection.AutoAssignRules)
|
||||
if rule.ID == "" {
|
||||
rule.ID = uuid.NewString()
|
||||
}
|
||||
rules = append(rules, rule)
|
||||
|
||||
rulesJSON, marshalErr := json.Marshal(rules)
|
||||
if marshalErr != nil {
|
||||
return database.Collections{}, Rule{}, fmt.Errorf("failed to marshal rules: %w", marshalErr)
|
||||
}
|
||||
|
||||
updated, err := s.db.UpdateCollectionRulesJSON(ctx, database.UpdateCollectionRulesJSONParams{
|
||||
ID: pgtype.UUID{Bytes: collectionID, Valid: true},
|
||||
AutoAssignRules: rulesJSON,
|
||||
})
|
||||
if err != nil {
|
||||
return database.Collections{}, Rule{}, fmt.Errorf("failed to save rules: %w", err)
|
||||
}
|
||||
return updated, rule, nil
|
||||
}
|
||||
|
||||
// SetRuleEnabled flips a single rule's enabled flag in place.
|
||||
func (s *CollectionService) SetRuleEnabled(ctx context.Context, collectionID uuid.UUID, ruleID string, enabled bool) (database.Collections, error) {
|
||||
collection, err := s.GetCollection(ctx, collectionID)
|
||||
if err != nil {
|
||||
return database.Collections{}, err
|
||||
}
|
||||
rules := s.ParseRules(collection.AutoAssignRules)
|
||||
found := false
|
||||
for i := range rules {
|
||||
if rules[i].ID == ruleID {
|
||||
rules[i].Enabled = &enabled
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return database.Collections{}, fmt.Errorf("rule not found: %s", ruleID)
|
||||
}
|
||||
|
||||
rulesJSON, marshalErr := json.Marshal(rules)
|
||||
if marshalErr != nil {
|
||||
return database.Collections{}, fmt.Errorf("failed to marshal rules: %w", marshalErr)
|
||||
}
|
||||
|
||||
updated, err := s.db.UpdateCollectionRulesJSON(ctx, database.UpdateCollectionRulesJSONParams{
|
||||
ID: pgtype.UUID{Bytes: collectionID, Valid: true},
|
||||
AutoAssignRules: rulesJSON,
|
||||
})
|
||||
if err != nil {
|
||||
return database.Collections{}, fmt.Errorf("failed to save rules: %w", err)
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
// RemoveRule deletes a single rule from the ruleset. Removing the LAST
|
||||
// rule converts the collection back to static (type is derived).
|
||||
func (s *CollectionService) RemoveRule(ctx context.Context, collectionID uuid.UUID, ruleID string) (database.Collections, error) {
|
||||
collection, err := s.GetCollection(ctx, collectionID)
|
||||
if err != nil {
|
||||
return database.Collections{}, err
|
||||
}
|
||||
rules := s.ParseRules(collection.AutoAssignRules)
|
||||
kept := make([]Rule, 0, len(rules))
|
||||
found := false
|
||||
for _, rule := range rules {
|
||||
if rule.ID == ruleID {
|
||||
found = true
|
||||
continue
|
||||
}
|
||||
kept = append(kept, rule)
|
||||
}
|
||||
if !found {
|
||||
return database.Collections{}, fmt.Errorf("rule not found: %s", ruleID)
|
||||
}
|
||||
|
||||
rulesJSON, marshalErr := json.Marshal(kept)
|
||||
if marshalErr != nil {
|
||||
return database.Collections{}, fmt.Errorf("failed to marshal rules: %w", marshalErr)
|
||||
}
|
||||
|
||||
updated, err := s.db.UpdateCollectionRulesJSON(ctx, database.UpdateCollectionRulesJSONParams{
|
||||
ID: pgtype.UUID{Bytes: collectionID, Valid: true},
|
||||
AutoAssignRules: rulesJSON,
|
||||
})
|
||||
if err != nil {
|
||||
return database.Collections{}, fmt.Errorf("failed to save rules: %w", err)
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
// SmartEvalOptions scopes a smart collection's live evaluation.
|
||||
} // EnabledRules filters a ruleset down to the rules that participate in// validateRule checks a rule payload the evaluator will have to honor.// AddRule appends a rule to the collection's ruleset, generating an id// SetRuleEnabled flips a single rule's enabled flag in place.// RemoveRule deletes a single rule from the ruleset. Removing the LAST// SmartEvalOptions scopes a smart collection's live evaluation.
|
||||
type SmartEvalOptions struct {
|
||||
// LibraryID, when valid, restricts matching to one library (the
|
||||
// detail page's library switcher).
|
||||
@@ -479,10 +256,6 @@ type SmartEvalOptions struct {
|
||||
// VisibleIDs is the requesting user's visible-library set (family
|
||||
// lens). Empty means no visible-library filter.
|
||||
VisibleIDs []pgtype.UUID
|
||||
// MatchMode is the collection's rule_match_mode: "any" (OR —
|
||||
// legacy default) or "all" (AND — the daily briefing). Empty
|
||||
// resolves to "any".
|
||||
MatchMode string
|
||||
// ViewerUserID scopes the candidate query's progress join — the
|
||||
// played/unplayed rule is per-user. Zero UUID = no viewer (played
|
||||
// rules then see "unplayed" for everything, honestly).
|
||||
@@ -490,126 +263,17 @@ type SmartEvalOptions struct {
|
||||
}
|
||||
|
||||
// IsCollectionSmart is the ONE smart detection: a collection is smart
|
||||
// when it carries the unified filter tree (item 3.2) or a legacy rules
|
||||
// array (item 2's shape). Every surface routes through this — rules
|
||||
// presence alone misses tree collections.
|
||||
// when it carries the unified filter tree (item 3.2). Every surface
|
||||
// routes through this. (The legacy rules-array branch retired
|
||||
// 2026-10-31 — the tree is the only smart storage.)
|
||||
func IsCollectionSmart(coll database.Collections) bool {
|
||||
if len(coll.FilterTree) > 0 {
|
||||
return true
|
||||
}
|
||||
var rules []Rule
|
||||
if err := json.Unmarshal(coll.AutoAssignRules, &rules); err != nil {
|
||||
return false
|
||||
}
|
||||
return len(rules) > 0
|
||||
return len(coll.FilterTree) > 0
|
||||
}
|
||||
|
||||
// EvaluateSmartMembership is THE single smart evaluator (item 3.2):
|
||||
// tree-bearing collections evaluate through the unified filter engine;
|
||||
// legacy rule arrays keep per-item evaluation until rewritten. One
|
||||
// call site surface, two storage epochs.
|
||||
// tree-bearing collections evaluate through the unified filter engine.
|
||||
func (s *CollectionService) EvaluateSmartMembership(ctx context.Context, coll database.Collections, opts SmartEvalOptions) ([]database.MediaItems, error) {
|
||||
if len(coll.FilterTree) > 0 {
|
||||
return s.evaluateTreeMembership(ctx, coll, opts)
|
||||
}
|
||||
return s.EvaluateSmartCollection(ctx, s.ParseRules(coll.AutoAssignRules), opts)
|
||||
}
|
||||
|
||||
// RulesToFilterTree converts a legacy rules array + its match mode
|
||||
// into a qf tree so a collection upgrades to the unified engine at its
|
||||
// first edit (item 3.2): zero-rewrite back-compat at read, one-way
|
||||
// upgrade at write. Episode rules map mechanically (feed→in-list,
|
||||
// played/downloaded→booleans, published_within→published_date
|
||||
// within_days); legacy operators without an engine equivalent
|
||||
// (starts_with/ends_with) fall back to contains — documented, and the
|
||||
// operator labels stay in the rules docs.
|
||||
func RulesToFilterTree(rules []Rule, matchMode string) (*FilterTree, error) {
|
||||
children := make([]FilterChild, 0, len(rules))
|
||||
scope := ""
|
||||
for _, r := range rules {
|
||||
child, err := ruleToFilterChild(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if episodeOnlyFields[r.Field] {
|
||||
scope = ScopeEpisodes
|
||||
}
|
||||
children = append(children, child)
|
||||
}
|
||||
if len(children) == 0 {
|
||||
return nil, fmt.Errorf("no rules to convert")
|
||||
}
|
||||
mode := RuleMatchAny
|
||||
if matchMode == RuleMatchAll {
|
||||
mode = RuleMatchAll
|
||||
}
|
||||
return &FilterTree{Scope: scope, Mode: mode, Children: children}, nil
|
||||
}
|
||||
|
||||
func ruleToFilterChild(r Rule) (FilterChild, error) {
|
||||
child := FilterChild{Field: r.Field}
|
||||
switch r.Field {
|
||||
case "feed":
|
||||
// The rules format stores comma-separated feed IDs in one value.
|
||||
child.Op = OpIn
|
||||
child.Values = splitRuleValues(r.Value)
|
||||
return child, nil
|
||||
case "downloaded":
|
||||
child.Op = isTrueFalse(r.Value == "true")
|
||||
return child, nil
|
||||
case "played":
|
||||
child.Op = isTrueFalse(r.Value == "true")
|
||||
return child, nil
|
||||
case "published_within":
|
||||
child.Field = "published_date"
|
||||
child.Op = OpWithinDays
|
||||
child.Values = []string{r.Value}
|
||||
return child, nil
|
||||
}
|
||||
// Registry fields: legacy operators → engine operators.
|
||||
switch r.Operator {
|
||||
case "equals":
|
||||
child.Op = OpIs
|
||||
case "not_equals":
|
||||
child.Op = OpIsNot
|
||||
case "contains":
|
||||
child.Op = OpContains
|
||||
case "not_contains":
|
||||
child.Op = OpNotContains
|
||||
case "greater_than":
|
||||
child.Op = OpAtLeast
|
||||
case "less_than":
|
||||
child.Op = OpAtMost
|
||||
case "in":
|
||||
child.Op = OpIn
|
||||
child.Values = splitRuleValues(r.Value)
|
||||
case "starts_with", "ends_with":
|
||||
// No engine equivalent — contains is the honest nearest.
|
||||
child.Op = OpContains
|
||||
default:
|
||||
return FilterChild{}, fmt.Errorf("legacy rule operator %q has no filter-engine equivalent", r.Operator)
|
||||
}
|
||||
child.Values = []string{r.Value}
|
||||
return child, nil
|
||||
}
|
||||
|
||||
func isTrueFalse(b bool) string {
|
||||
if b {
|
||||
return OpIsTrue
|
||||
}
|
||||
return OpIsFalse
|
||||
}
|
||||
|
||||
func splitRuleValues(v string) []string {
|
||||
parts := strings.Split(v, ",")
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
p = strings.TrimSpace(p)
|
||||
if p != "" {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out
|
||||
return s.evaluateTreeMembership(ctx, coll, opts)
|
||||
}
|
||||
|
||||
func (s *CollectionService) evaluateTreeMembership(ctx context.Context, coll database.Collections, opts SmartEvalOptions) ([]database.MediaItems, error) {
|
||||
@@ -621,87 +285,12 @@ func (s *CollectionService) evaluateTreeMembership(ctx context.Context, coll dat
|
||||
return nil, errors.New("tree collections require the search service (constructor wiring)")
|
||||
}
|
||||
return s.search.ComposedLiveMedia(ctx, opts.ViewerUserID, opts.LibraryID, opts.VisibleIDs, tree)
|
||||
}
|
||||
|
||||
// EvaluateSmartCollection resolves a smart collection's membership LIVE:
|
||||
// every visible live book matched by at least one enabled rule is in.
|
||||
// No stored links are consulted — "truly dynamic," always current.
|
||||
func (s *CollectionService) EvaluateSmartCollection(ctx context.Context, rules []Rule, opts SmartEvalOptions) ([]database.MediaItems, error) {
|
||||
enabled := s.EnabledRules(rules)
|
||||
if len(enabled) == 0 {
|
||||
return []database.MediaItems{}, nil
|
||||
}
|
||||
|
||||
var visibleArg []pgtype.UUID
|
||||
if len(opts.VisibleIDs) > 0 {
|
||||
visibleArg = opts.VisibleIDs
|
||||
}
|
||||
items, err := s.db.GetVisibleLiveMediaForRules(ctx, database.GetVisibleLiveMediaForRulesParams{
|
||||
LibraryID: opts.LibraryID,
|
||||
VisibleLibraryIds: visibleArg,
|
||||
ViewerUserID: opts.ViewerUserID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load candidate books: %w", err)
|
||||
}
|
||||
|
||||
mode := opts.MatchMode
|
||||
if !ValidRuleMatchMode(mode) {
|
||||
mode = RuleMatchAny
|
||||
}
|
||||
matches := make([]database.MediaItems, 0, len(items))
|
||||
for _, row := range items {
|
||||
if s.mediaItemMatches(mode, row.MediaItems, row.ViewerPercentage, enabled) {
|
||||
matches = append(matches, row.MediaItems)
|
||||
}
|
||||
}
|
||||
return matches, nil
|
||||
}
|
||||
|
||||
// mediaItemMatches evaluates the enabled rules under the collection's
|
||||
// match mode: "any" (OR — legacy) or "all" (AND — the briefing).
|
||||
// viewerPercentage is the requesting user's progress for this item
|
||||
// (0 when none) — the played/unplayed episode rule reads it.
|
||||
func (s *CollectionService) mediaItemMatches(mode string, item database.MediaItems, viewerPercentage float64, rules []Rule) bool {
|
||||
if len(rules) == 0 {
|
||||
return false
|
||||
}
|
||||
if mode == RuleMatchAll {
|
||||
for _, rule := range rules {
|
||||
if !s.mediaItemMatchesRule(item, viewerPercentage, rule) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
for _, rule := range rules {
|
||||
if s.mediaItemMatchesRule(item, viewerPercentage, rule) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// mediaItemMatchesAnyRule is the legacy OR path, kept for callers that
|
||||
// predate match modes.
|
||||
func (s *CollectionService) mediaItemMatchesAnyRule(item database.MediaItems, rules []Rule) bool {
|
||||
return s.mediaItemMatches(RuleMatchAny, item, 0, rules)
|
||||
}
|
||||
|
||||
// episodeFinished is THE canonical finished definition (same predicate
|
||||
} // mediaItemMatchesAnyRule is the legacy OR path, kept for callers that// episodeFinished is THE canonical finished definition (same predicate
|
||||
// as hide_finished): viewer progress percentage >= 0.999.
|
||||
func episodeFinished(viewerPercentage float64) bool {
|
||||
return viewerPercentage >= 0.999
|
||||
}
|
||||
|
||||
// mediaItemMatchesRule evaluates one rule against one item.
|
||||
func (s *CollectionService) mediaItemMatchesRule(item database.MediaItems, viewerPercentage float64, rule Rule) bool {
|
||||
if rule.Enabled != nil && !*rule.Enabled {
|
||||
return false
|
||||
}
|
||||
return s.FieldMatches(item, viewerPercentage, rule.Field, rule.Operator, rule.Value)
|
||||
}
|
||||
|
||||
// FieldMatches is THE single rule evaluator (the rules-builder preview
|
||||
// delegates here too — one vocabulary, one semantics). Episode/user-state
|
||||
// fields (feed/played/downloaded/published_within) evaluate directly;
|
||||
|
||||
@@ -2,12 +2,9 @@ package services
|
||||
|
||||
import (
|
||||
"bookhoard/internal/database"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEvaluateRules_Equality(t *testing.T) {
|
||||
@@ -280,44 +277,7 @@ func TestEvaluateRules_PriorityBoost(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesToFilterTree(t *testing.T) {
|
||||
t.Run("episode_rules_map_mechanically", func(t *testing.T) {
|
||||
rules := []Rule{
|
||||
{ID: "1", Field: "feed", Operator: "in", Value: "feed-a, feed-b"},
|
||||
{ID: "2", Field: "played", Operator: "equals", Value: "false"},
|
||||
{ID: "3", Field: "downloaded", Operator: "equals", Value: "true"},
|
||||
{ID: "4", Field: "published_within", Operator: "greater_than", Value: "7"},
|
||||
}
|
||||
tree, err := RulesToFilterTree(rules, RuleMatchAll)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, RuleMatchAll, tree.Mode, "the collection's match mode becomes the tree mode")
|
||||
assert.Equal(t, ScopeEpisodes, tree.Scope, "episode rules force the episodes scope")
|
||||
require.Len(t, tree.Children, 4)
|
||||
assert.Equal(t, FilterChild{Field: "feed", Op: OpIn, Values: []string{"feed-a", "feed-b"}}, tree.Children[0])
|
||||
assert.Equal(t, FilterChild{Field: "played", Op: OpIsFalse}, tree.Children[1])
|
||||
assert.Equal(t, FilterChild{Field: "downloaded", Op: OpIsTrue}, tree.Children[2])
|
||||
assert.Equal(t, FilterChild{Field: "published_date", Op: OpWithinDays, Values: []string{"7"}}, tree.Children[3])
|
||||
// The converted tree parses under the strict engine.
|
||||
b, _ := json.Marshal(tree)
|
||||
_, err = ParseFilterTree(string(b))
|
||||
require.NoError(t, err, "converted tree must satisfy the engine grammar")
|
||||
})
|
||||
|
||||
t.Run("book_rules_map_and_stay_books_scope", func(t *testing.T) {
|
||||
rules := []Rule{
|
||||
{Field: "genre", Operator: "equals", Value: "fantasy"},
|
||||
{Field: "author", Operator: "contains", Value: "Voss"},
|
||||
{Field: "copyright_year", Operator: "greater_than", Value: "1990"},
|
||||
{Field: "tags", Operator: "starts_with", Value: "dyst"},
|
||||
}
|
||||
tree, err := RulesToFilterTree(rules, RuleMatchAny)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "", tree.Scope, "no episode rules → the books scope (legacy default)")
|
||||
assert.Equal(t, RuleMatchAny, tree.Mode)
|
||||
assert.Equal(t, FilterChild{Field: "genre", Op: OpIs, Values: []string{"fantasy"}}, tree.Children[0])
|
||||
assert.Equal(t, FilterChild{Field: "copyright_year", Op: OpAtLeast, Values: []string{"1990"}}, tree.Children[2])
|
||||
// Documented fallback: rules ops without an engine equivalent
|
||||
// (starts_with/ends_with) become contains.
|
||||
assert.Equal(t, OpContains, tree.Children[3].Op)
|
||||
})
|
||||
}
|
||||
// TestRulesToFilterTree retired 2026-10-31 with the function it tested:
|
||||
// rules-array storage is gone, the filter tree is the only smart
|
||||
// membership. (Tests of deleted behavior are deleted — the asymmetry
|
||||
// rule.)
|
||||
@@ -292,56 +292,15 @@ func (s *DashboardService) getUserCollectionItems(ctx context.Context, coll data
|
||||
}
|
||||
|
||||
// The query filters excluded rows in SQL and embeds the media row,
|
||||
// so the items are MediaItems as-is.
|
||||
var manualNonExcluded []database.MediaItems
|
||||
// so the items are MediaItems as-is. (The legacy auto-assign branch
|
||||
// retired 2026-10-31 with the rules-array storage: smart user
|
||||
// collections evaluate on their detail page; sections render their
|
||||
// stored membership.)
|
||||
items := make([]database.MediaItems, 0, len(manualItems))
|
||||
for _, row := range manualItems {
|
||||
manualNonExcluded = append(manualNonExcluded, row.MediaItems)
|
||||
items = append(items, row.MediaItems)
|
||||
}
|
||||
|
||||
var autoItems []database.MediaItems
|
||||
if len(coll.AutoAssignRules) > 0 {
|
||||
var rules []Rule
|
||||
if err := json.Unmarshal(coll.AutoAssignRules, &rules); err == nil && len(rules) > 0 {
|
||||
allLibraryItems, err := s.db.GetLibraryItems(ctx, libraryID)
|
||||
if err == nil {
|
||||
for _, item := range allLibraryItems {
|
||||
alreadyInCollection := false
|
||||
for _, manualItem := range manualNonExcluded {
|
||||
manualUUID := uuid.UUID(manualItem.ID.Bytes)
|
||||
itemUUID := uuid.UUID(item.ID.Bytes)
|
||||
if manualUUID == itemUUID {
|
||||
alreadyInCollection = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if alreadyInCollection {
|
||||
continue
|
||||
}
|
||||
|
||||
// Legacy dashboard auto-assign path: no viewer progress
|
||||
// loaded here (the dashboard's own hide_finished handles
|
||||
// queue semantics) — played rules evaluate unplayed, honestly.
|
||||
evaluations := s.collectionService.EvaluateRules(item, 0, rules)
|
||||
for _, eval := range evaluations {
|
||||
if eval.Matches {
|
||||
autoItems = append(autoItems, item)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var finalItems []database.MediaItems
|
||||
finalItems = append(finalItems, manualNonExcluded...)
|
||||
finalItems = append(finalItems, autoItems...)
|
||||
|
||||
if len(finalItems) > limit {
|
||||
finalItems = finalItems[:limit]
|
||||
}
|
||||
|
||||
return finalItems, nil
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (s *DashboardService) GetDashboardPreferences(ctx context.Context, userID uuid.UUID, libraryID pgtype.UUID) (database.UserDashboardPreferences, error) {
|
||||
|
||||
@@ -433,7 +433,6 @@ func (s *MediaScanner) upsertPlaylistCollection(ctx context.Context, entry Serie
|
||||
UserID: owner,
|
||||
Name: entry.Name,
|
||||
Description: pgtype.Text{String: description, Valid: true},
|
||||
AutoAssignRules: nil, // jsonb NULL — an empty []byte encodes as bytea and 22P02s
|
||||
ViewSettings: viewSettings,
|
||||
ShowOnDashboard: pgtype.Bool{Bool: true, Valid: true}, // playlists land on the dashboard by design; hide via dashboard prefs
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user