Skip to content

Commit

Permalink
Prevent Spotlight index when library is to big until paginated (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmuslos committed Dec 9, 2024
1 parent 8e58e9f commit 3650b24
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions Multiplatform/Intents/SpotlightIndexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ internal struct SpotlightIndexer {
var items: [CSSearchableItem] = []

for library in libraries {
switch library.type {
case .audiobooks:
let (libraryItems, remaining, updated) = try await indexAudiobookLibrary(library, remainingIdentifiers: remainingIdentifiers)

items += libraryItems
remainingIdentifiers = remaining
indexedIdentifiers += updated
case .podcasts:
let (libraryItems, remaining, updated) = try await indexPodcastLibrary(library, remainingIdentifiers: remainingIdentifiers)

items += libraryItems
remainingIdentifiers = remaining
indexedIdentifiers += updated
default:
do {
switch library.type {
case .audiobooks:
let (libraryItems, remaining, updated) = try await indexAudiobookLibrary(library, remainingIdentifiers: remainingIdentifiers)

items += libraryItems
remainingIdentifiers = remaining
indexedIdentifiers += updated
case .podcasts:
let (libraryItems, remaining, updated) = try await indexPodcastLibrary(library, remainingIdentifiers: remainingIdentifiers)

items += libraryItems
remainingIdentifiers = remaining
indexedIdentifiers += updated
default:
continue
}
} catch {
continue
}
}
Expand Down Expand Up @@ -82,6 +86,12 @@ internal struct SpotlightIndexer {
}

static func indexAudiobookLibrary(_ library: Library, remainingIdentifiers: [String]) async throws -> ([CSSearchableItem], [String], [String]) {
let (_, count) = try await AudiobookshelfClient.shared.audiobooks(libraryID: library.id, sortOrder: .added, ascending: false, limit: 0, page: nil)

guard count < 300 else {
throw IndexError.libraryToBig
}

var remainingIdentifiers = remainingIdentifiers
var indexedIdentifiers: [String] = []
var items: [CSSearchableItem] = []
Expand Down Expand Up @@ -174,6 +184,12 @@ internal struct SpotlightIndexer {
return (items, remainingIdentifiers, indexedIdentifiers)
}
static func indexPodcastLibrary(_ library: Library, remainingIdentifiers: [String]) async throws -> ([CSSearchableItem], [String], [String]) {
let (_, count) = try await AudiobookshelfClient.shared.podcasts(libraryID: library.id, limit: 0, page: nil)

guard count < 25 else {
throw IndexError.libraryToBig
}

var remainingIdentifiers = remainingIdentifiers
var indexedIdentifiers: [String] = []
var items: [CSSearchableItem] = []
Expand Down Expand Up @@ -245,4 +261,8 @@ internal struct SpotlightIndexer {

return (items, remainingIdentifiers, indexedIdentifiers)
}

enum IndexError: Error {
case libraryToBig
}
}

0 comments on commit 3650b24

Please sign in to comment.