Repository Public
1// SP Notes iOS2// File: Features/Backlinks/BacklinksModel.swift3// Description: The links of a note, from the local copy (works offline): the notes that link to it (`localBacklinks`), its typed4// relations (`localRelations` — those an AI suggested are marked and never accepted here) and the notes it5// mentions (`pageReferences` of its document). Each row opens the other note, at the block when the edge has one.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import Foundation10import Observation11import SPNotesCore1213struct NoteLinkRow: Identifiable, Equatable, Sendable {14 enum Group: String, Sendable { case incoming, relation, outgoing }15 var id: String16 var group: Group17 /// The other note.18 var pageId: String19 var title: String20 /// The block to show in the other note.21 var blockId: String?22 /// For a relation: what it means, read from the open note (« appuie », « est appuyée par »…).23 var relationLabel: String?24 var isAI = false25 var isSuggested = false26 var model: String?27 var note: String?28}2930@MainActor31@Observable32final class BacklinksModel {33 let pageId: String34 private(set) var incoming: [NoteLinkRow] = []35 private(set) var relations: [NoteLinkRow] = []36 private(set) var outgoing: [NoteLinkRow] = []37 private(set) var isLoaded = false3839 @ObservationIgnored private let graph: GraphRepository40 @ObservationIgnored private let store: NotesStore4142 init(pageId: String, graph: GraphRepository, store: NotesStore) {43 self.pageId = pageId44 self.graph = graph45 self.store = store46 }4748 var isEmpty: Bool { incoming.isEmpty && relations.isEmpty && outgoing.isEmpty }49 var count: Int { incoming.count + relations.count + outgoing.count }5051 func load() async {52 var titles: [String: String?] = [:]53 /// The title of a live page (nil: unknown on this device, or in the trash).54 func title(_ id: String, fallback: String?) async -> String? {55 if let known = titles[id] { return known ?? fallback }56 let summary = (try? await store.summary(id: id)).flatMap { $0 }57 if let summary, summary.deletedAt != nil { titles[id] = .some(nil); return nil }58 titles[id] = .some(summary?.title)59 return summary?.title ?? fallback60 }6162 var inRows: [NoteLinkRow] = []63 var seenIncoming = Set<String>()64 for edge in (try? await graph.localBacklinks(pageId: pageId)) ?? [] where edge.sourceKind == GraphEntityKind.page && edge.kind != RelationKind.relation {65 guard edge.fromId != pageId, seenIncoming.insert(edge.fromId + "#" + (edge.sourceBlockId ?? "")).inserted,66 let name = await title(edge.fromId, fallback: edge.fromTitle) else { continue }67 inRows.append(NoteLinkRow(id: "in:" + edge.id, group: .incoming, pageId: edge.fromId, title: name, blockId: edge.sourceBlockId))68 }6970 var relationRows: [NoteLinkRow] = []71 for edge in (try? await graph.localRelations(touching: pageId)) ?? [] {72 // typed relations and AI suggestions; the plain document links are the two other groups73 guard edge.kind == RelationKind.relation || edge.kind == RelationKind.aiSuggested || edge.isAI, edge.status != DecisionStatus.rejected else { continue }74 let outward = edge.fromId == pageId75 let other = outward ? edge.toId : edge.fromId76 guard other != pageId, (outward ? edge.targetKind : edge.sourceKind) == GraphEntityKind.page,77 let name = await title(other, fallback: outward ? edge.toTitle : edge.fromTitle) else { continue }78 relationRows.append(NoteLinkRow(id: "rel:" + edge.id, group: .relation, pageId: other, title: name, blockId: outward ? edge.targetBlockId : edge.sourceBlockId,79 relationLabel: BacklinksText.relation(edge.relType, outward: outward), isAI: edge.isAI,80 isSuggested: edge.status == DecisionStatus.suggested, model: edge.model, note: edge.note ?? edge.rationale))81 }8283 var outRows: [NoteLinkRow] = []84 if let page = try? await store.page(id: pageId), let document = try? NoteDocument(json: page.content) {85 var seen = Set<String>()86 for id in document.pageReferences() where id != pageId && seen.insert(id).inserted {87 guard let name = await title(id, fallback: nil) else { continue }88 outRows.append(NoteLinkRow(id: "out:" + id, group: .outgoing, pageId: id, title: name))89 }90 }91 incoming = inRows.sorted { $0.title.localizedStandardCompare($1.title) == .orderedAscending }92 relations = relationRows.sorted { ($0.isSuggested ? 1 : 0, $0.title) < ($1.isSuggested ? 1 : 0, $1.title) }93 outgoing = outRows94 isLoaded = true95 }96}9798enum BacklinksText {99 /// The meaning of a relation read from the open note: `outward` = the open note is the source.100 static func relation(_ type: String, outward: Bool) -> String {101 switch (type, outward) {102 case (RelationType.related, _): return String(localized: "liée à")103 case (RelationType.supports, true): return String(localized: "appuie")104 case (RelationType.supports, false): return String(localized: "appuyée par")105 case (RelationType.contradicts, true): return String(localized: "contredit")106 case (RelationType.contradicts, false): return String(localized: "contredite par")107 case (RelationType.extends, true): return String(localized: "prolonge")108 case (RelationType.extends, false): return String(localized: "prolongée par")109 case (RelationType.exampleOf, true): return String(localized: "exemple de")110 case (RelationType.exampleOf, false): return String(localized: "a pour exemple")111 case (RelationType.partOf, true): return String(localized: "partie de")112 case (RelationType.partOf, false): return String(localized: "contient")113 case (RelationType.cites, true): return String(localized: "cite")114 case (RelationType.cites, false): return String(localized: "citée par")115 case (RelationType.precedes, true), (RelationType.follows, false): return String(localized: "précède")116 case (RelationType.follows, true), (RelationType.precedes, false): return String(localized: "suit")117 case (RelationType.about, true): return String(localized: "à propos de")118 case (RelationType.about, false): return String(localized: "sujet de")119 case (RelationType.answers, true): return String(localized: "répond à")120 case (RelationType.answers, false): return String(localized: "a pour réponse")121 case (RelationType.duplicates, _): return String(localized: "doublon de")122 default: return String(localized: "liée à")123 }124 }125126 static func count(_ count: Int) -> String {127 switch count {128 case 0: return String(localized: "Aucun lien")129 case 1: return String(localized: "1 lien")130 default: return String(localized: "\(count) liens")131 }132 }133}134