SPB Git

Repository Public

SpotlightIndexer.swift 10.8 KB 261 lines swift

1// SP Notes iOS2// File: App/Intents/SpotlightIndexer.swift3// Description: Keeps the system search (Core Spotlight) in step with the local notes: title and an excerpt of the text, the note4//              id as the item identifier. Updated incrementally from the store's change stream; notes that go to the trash, are5//              archived or disappear are removed, and erasing the device's data empties the index. 2.0: the open tasks are6//              indexed too, in their own domain (`SpotlightTaskIndexer`): title, due date, title of their note.7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import CoreSpotlight11import Foundation12import SPNotesCore13import UniformTypeIdentifiers1415/// What is written to the system index for one note.16struct SpotlightNote: Sendable, Equatable {17    var id: String18    var title: String19    var text: String20    var updatedAt: Date21    var tags: [String] = []22    /// Tasks (2.0): the due date and the title of the note the task belongs to.23    var dueDate: Date?24    var container: String?25}2627/// The system index, behind a protocol so the indexing rules can be tested without Core Spotlight.28protocol SearchIndexing: Sendable {29    func index(_ notes: [SpotlightNote]) async30    func remove(ids: [String]) async31    func removeAll() async32}3334struct CoreSpotlightIndex: SearchIndexing {35    static let domain = "ai.spboucher.spnotes.ios.notes"36    static let tasksDomain = "ai.spboucher.spnotes.ios.tasks"37    /// The domain this index writes to and empties.38    var domain = CoreSpotlightIndex.domain39    private var index: CSSearchableIndex { .default() }4041    func index(_ notes: [SpotlightNote]) async {42        guard CSSearchableIndex.isIndexingAvailable(), !notes.isEmpty else { return }43        let items = notes.map { note -> CSSearchableItem in44            let attributes = CSSearchableItemAttributeSet(contentType: .text)45            attributes.title = note.title46            attributes.displayName = note.title47            attributes.contentDescription = note.text48            attributes.contentModificationDate = note.updatedAt49            attributes.keywords = note.tags50            if let due = note.dueDate { attributes.dueDate = due }51            if let container = note.container { attributes.containerTitle = container }52            let item = CSSearchableItem(uniqueIdentifier: note.id, domainIdentifier: domain, attributeSet: attributes)53            item.expirationDate = .distantFuture54            return item55        }56        try? await index.indexSearchableItems(items)57    }5859    func remove(ids: [String]) async {60        guard CSSearchableIndex.isIndexingAvailable(), !ids.isEmpty else { return }61        try? await index.deleteSearchableItems(withIdentifiers: ids)62    }6364    func removeAll() async {65        guard CSSearchableIndex.isIndexingAvailable() else { return }66        try? await index.deleteSearchableItems(withDomainIdentifiers: [domain])67    }68}6970actor SpotlightIndexer {71    /// Number of notes kept in the system index (the most recently modified).72    static let capacity = 40073    static let excerptLength = 1_0007475    private let store: NotesStore76    private let index: any SearchIndexing77    private var indexed: Set<String> = []78    private var observer: Task<Void, Never>?7980    init(store: NotesStore, index: any SearchIndexing = CoreSpotlightIndex()) {81        self.store = store82        self.index = index83    }8485    /// Indexes the recent notes, then follows the store. Idempotent.86    func start() async {87        guard observer == nil else { return }88        let changes = await store.changes()89        observer = Task { [weak self] in90            await self?.reindexAll()91            for await change in changes { await self?.apply(change) }92        }93    }9495    func stop() {96        observer?.cancel()97        observer = nil98    }99100    func apply(_ change: StoreChange) async {101        switch change {102        case .pages(let ids): await update(ids: ids)103        case .reset: await reindexAll()104        default: break105        }106    }107108    /// A note is searchable when it is a live note: not a folder, not in the trash, not archived.109    static func isSearchable(_ page: PageRecord) -> Bool {110        page.deletedAt == nil && !page.archived && !page.isFolder && page.type != "database"111    }112113    private func update(ids: Set<String>) async {114        var upserts: [SpotlightNote] = []115        var removals: [String] = []116        for id in ids {117            if let page = try? await store.page(id: id), Self.isSearchable(page) {118                upserts.append(SpotlightNote(id: page.id, title: WidgetNote.displayTitle(page.title), text: String(page.plainText.prefix(Self.excerptLength)),119                                             updatedAt: page.updatedAt, tags: page.tags))120            } else {121                removals.append(id)122            }123        }124        await index.index(upserts)125        await index.remove(ids: removals)126        indexed.formUnion(upserts.map(\.id))127        indexed.subtract(removals)128    }129130    /// Full pass (launch, external change, erase): the recent notes are indexed and whatever is no longer listed is removed.131    func reindexAll() async {132        guard let summaries = try? await store.summaries(list: .all, limit: Self.capacity) else { return }133        let notes = summaries.filter(WidgetDataProvider.isNote).map {134            SpotlightNote(id: $0.id, title: WidgetNote.displayTitle($0.title), text: $0.preview, updatedAt: $0.updatedAt, tags: $0.tags)135        }136        if notes.isEmpty {137            await index.removeAll()138            indexed = []139            return140        }141        let current = Set(notes.map(\.id))142        if indexed.isEmpty {143            // First pass of this process: the index may hold notes deleted while the app was not running.144            await index.removeAll()145        } else {146            await index.remove(ids: Array(indexed.subtracting(current)))147        }148        await index.index(notes)149        indexed = current150    }151}152153// MARK: - tasks (2.0)154155/// Open tasks in the system search, in a second domain. An item's identifier is `task:<taskId>` — never a valid note id, so156/// a task result is not taken for a note; `AppModel.continueTaskActivity` routes it to the task's note at its block157/// (`spnotes://note/<pageId>?block=<blockId>`) or to the task list (`spnotes://tasks`).158actor SpotlightTaskIndexer {159    /// Number of tasks kept in the system index (the nearest due dates first, then the newest).160    static let capacity = 300161    static let identifierPrefix = "task:"162163    static func identifier(_ taskId: String) -> String { identifierPrefix + taskId }164    static func taskId(fromIdentifier identifier: String) -> String? {165        guard identifier.hasPrefix(identifierPrefix) else { return nil }166        let id = String(identifier.dropFirst(identifierPrefix.count))167        return DeepLink.isValidNoteID(id) ? id.lowercased() : nil168    }169170    private let store: NotesStore171    private let index: any SearchIndexing172    private var indexed: Set<String> = []173    private var observer: Task<Void, Never>?174175    init(store: NotesStore, index: any SearchIndexing = CoreSpotlightIndex(domain: CoreSpotlightIndex.tasksDomain)) {176        self.store = store177        self.index = index178    }179180    /// Indexes the open tasks, then follows the task changes of the store. Idempotent.181    func start() async {182        guard observer == nil else { return }183        let changes = await store.changes()184        observer = Task { [weak self] in185            await self?.reindexAll()186            for await change in changes { await self?.apply(change) }187        }188    }189190    func stop() {191        observer?.cancel()192        observer = nil193    }194195    func apply(_ change: StoreChange) async {196        switch change {197        case .entities(let kind, let ids) where kind == EntityKind.task.rawValue: await update(ids: ids)198        case .reset: await reindexAll()199        default: break200        }201    }202203    /// What is written for one task.204    static func item(for task: TaskItem, noteTitle: String?) -> SpotlightNote {205        var parts: [String] = []206        if let due = task.dueAt { parts.append(String(localized: "Échéance : \(TaskDueRule.localDay(due).formatted(date: .abbreviated, time: .omitted))")) }207        if let noteTitle, !noteTitle.isEmpty { parts.append(noteTitle) }208        return SpotlightNote(id: identifier(task.id), title: task.title, text: parts.joined(separator: " · "), updatedAt: task.updatedAt,209                             tags: [String(localized: "Tâche")], dueDate: task.dueAt, container: noteTitle)210    }211212    private func update(ids: Set<String>) async {213        var upserts: [SpotlightNote] = []214        var removals: [String] = []215        let decoder = APIJSON.decoder()216        for id in ids {217            if let record = try? await store.entity(kind: .task, id: id), !record.deleted, let task = try? decoder.decode(TaskItem.self, from: record.json),218               !task.isDone, task.deletedAt == nil, indexed.contains(Self.identifier(id)) || indexed.count + upserts.count < Self.capacity {219                upserts.append(Self.item(for: task, noteTitle: await WidgetDataProvider.noteTitle(of: task, in: store)))220            } else {221                removals.append(Self.identifier(id))222            }223        }224        await index.index(upserts)225        await index.remove(ids: removals)226        indexed.formUnion(upserts.map(\.id))227        indexed.subtract(removals)228    }229230    /// Full pass (launch, external change, erase): the open tasks are indexed and whatever is no longer open is removed.231    func reindexAll() async {232        guard let open = try? await WidgetDataProvider.openTasks(in: store) else { return }233        var items: [SpotlightNote] = []234        for task in open.prefix(Self.capacity) { items.append(Self.item(for: task, noteTitle: await WidgetDataProvider.noteTitle(of: task, in: store))) }235        if items.isEmpty {236            await index.removeAll()237            indexed = []238            return239        }240        let current = Set(items.map(\.id))241        if indexed.isEmpty { await index.removeAll() } else { await index.remove(ids: Array(indexed.subtracting(current))) }242        await index.index(items)243        indexed = current244    }245}246247extension AppModel {248    /// A task result of the system search: opens the task's note at its block, or the task list. Returns false when the249    /// activity is not a task result (the caller then handles notes, Handoff, "search in app").250    @discardableResult251    func continueTaskActivity(_ activity: NSUserActivity) -> Bool {252        guard activity.activityType == CSSearchableItemActionType, let identifier = activity.userInfo?[CSSearchableItemActivityIdentifier] as? String,253              let taskId = SpotlightTaskIndexer.taskId(fromIdentifier: identifier) else { return false }254        Task {255            let task = try? await repositories.tasks.task(id: taskId)256            open(url: SystemLinks.task(pageId: task?.pageId, blockId: task?.blockId))257        }258        return true259    }260}261
Files main
Menu

Theme