41 lines
1.7 KiB
Swift
41 lines
1.7 KiB
Swift
import CoreSpotlight
|
|
import Foundation
|
|
import UniformTypeIdentifiers
|
|
|
|
enum SourceSpotlightIndexer {
|
|
private static let domainIdentifier = "com.magicive.marks.sources"
|
|
|
|
static func index(_ sources: [IngestedSource]) async {
|
|
guard CSSearchableIndex.isIndexingAvailable(), !sources.isEmpty else { return }
|
|
let items = sources.map { source in
|
|
let attributes = CSSearchableItemAttributeSet(contentType: source.kind == .pdf ? .pdf : .text)
|
|
attributes.title = source.displayTitle
|
|
attributes.contentDescription = source.bodyText
|
|
attributes.keywords = source.tags + [source.kind.label, source.originalFilename].compactMap { $0 }
|
|
attributes.displayName = source.originalFilename ?? source.displayTitle
|
|
return CSSearchableItem(
|
|
uniqueIdentifier: source.id.uuidString,
|
|
domainIdentifier: domainIdentifier,
|
|
attributeSet: attributes
|
|
)
|
|
}
|
|
|
|
do {
|
|
try await CSSearchableIndex.default().indexSearchableItems(items)
|
|
Log.spotlight.info("Indexed \(sources.count, privacy: .public) sources")
|
|
} catch {
|
|
Log.spotlight.error("Source index failed: \(error.localizedDescription, privacy: .public)")
|
|
}
|
|
}
|
|
|
|
static func remove(ids: [UUID]) async {
|
|
guard CSSearchableIndex.isIndexingAvailable(), !ids.isEmpty else { return }
|
|
do {
|
|
try await CSSearchableIndex.default().deleteSearchableItems(withIdentifiers: ids.map(\.uuidString))
|
|
Log.spotlight.info("Removed \(ids.count, privacy: .public) sources")
|
|
} catch {
|
|
Log.spotlight.error("Source delete failed: \(error.localizedDescription, privacy: .public)")
|
|
}
|
|
}
|
|
}
|