DomainDig/DomainDigIntents.swift
191 lines · 6964 bytes
11 symbols in this file
1import AppIntents
2import Foundation
3
4/// App Intent that runs a point-in-time domain inspection through the same
5/// headless pipeline used by the CLI (`DomainInspectionService` ->
6/// `DomainReportBuilder`) and returns a concise summary. Usable from
7/// Shortcuts, Spotlight, the Action button, and Siri.
8struct InspectDomainIntent: AppIntent {
9 static let title: LocalizedStringResource = "Inspect Domain"
10 static let description = IntentDescription(
11 "Run a DomainDig inspection and return a summary of availability, risk, TLS, email security, and certificate health."
12 )
13
14 // Read-only inspection; no need to foreground the app.
15 static let openAppWhenRun = false
16
17 @Parameter(
18 title: "Domain",
19 description: "The domain to inspect, e.g. example.com",
20 inputOptions: String.IntentInputOptions(
21 keyboardType: .URL,
22 capitalizationType: .none
23 )
24 )
25 var domain: String
26
27 static var parameterSummary: some ParameterSummary {
28 Summary("Inspect \(\.$domain)")
29 }
30
31 @MainActor
32 func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog {
33 let requested = domain.trimmingCharacters(in: .whitespacesAndNewlines)
34 guard !requested.isEmpty else {
35 throw InspectDomainError.emptyDomain
36 }
37
38 let snapshot = await DomainInspectionService().inspectSnapshot(
39 domain: requested,
40 scope: .current()
41 )
42 let report = DomainReportBuilder().build(from: snapshot)
43
44 let summary = Self.summaryText(for: report)
45 let dialog = IntentDialog(stringLiteral: Self.spokenSummary(for: report))
46 return .result(value: summary, dialog: dialog)
47 }
48
49 /// Multi-line summary suitable for a returned Shortcuts text value.
50 @MainActor
51 static func summaryText(for report: DomainReport) -> String {
52 let dnssec: String
53 switch report.dns.dnssecSigned {
54 case true?: dnssec = "Yes"
55 case false?: dnssec = "No"
56 case nil: dnssec = "Unknown"
57 }
58
59 var lines = [
60 "\(report.domain) — \(report.availability.rawValue.capitalized)",
61 "Risk: \(report.riskAssessment.level.title) (score \(report.riskAssessment.score))",
62 "Health: \(report.health.title)",
63 "TLS: \(report.web.tlsGrade.rawValue) · Email: \(report.email.grade?.rawValue ?? "—") · Cert: \(report.certificateExpiryState.title)",
64 "IP: \(report.dns.primaryIP ?? "unknown") · DNSSEC: \(dnssec)"
65 ]
66
67 if let insight = report.insights.first {
68 lines.append(insight)
69 }
70
71 return lines.joined(separator: "\n")
72 }
73
74 /// Short spoken/dialog line for Siri and the Shortcuts result banner.
75 @MainActor
76 static func spokenSummary(for report: DomainReport) -> String {
77 "\(report.domain) is \(report.availability.rawValue). Risk \(report.riskAssessment.level.title.lowercased()), health \(report.health.title.lowercased())."
78 }
79}
80
81enum InspectDomainError: Error, CustomLocalizedStringResourceConvertible {
82 case emptyDomain
83
84 var localizedStringResource: LocalizedStringResource {
85 switch self {
86 case .emptyDomain:
87 return "Enter a domain to inspect."
88 }
89 }
90}
91
92/// App Intent that opens DomainDig and adds a domain to the watchlist. It opens
93/// the app via the `domaindig://watch` deep link so tracking goes through the
94/// existing view-model path (premium limits, monitoring, history linking,
95/// cloud-sync recording, and the paywall when over the free limit).
96struct AddToWatchlistIntent: AppIntent {
97 static let title: LocalizedStringResource = "Add Domain to Watchlist"
98 static let description = IntentDescription(
99 "Open DomainDig and add a domain to your watchlist."
100 )
101
102 static let openAppWhenRun = true
103
104 @Parameter(
105 title: "Domain",
106 description: "The domain to add, e.g. example.com",
107 inputOptions: String.IntentInputOptions(
108 keyboardType: .URL,
109 capitalizationType: .none
110 )
111 )
112 var domain: String
113
114 static var parameterSummary: some ParameterSummary {
115 Summary("Add \(\.$domain) to the watchlist")
116 }
117
118 @MainActor
119 func perform() async throws -> some IntentResult {
120 let requested = domain.trimmingCharacters(in: .whitespacesAndNewlines)
121 guard !requested.isEmpty else {
122 throw InspectDomainError.emptyDomain
123 }
124
125 // `openAppWhenRun` runs this in the app process, so the router hands the
126 // action off to the running UI, which tracks through the existing path.
127 DomainDigIntentRouter.shared.pendingAction = .watch(requested)
128 return .result()
129 }
130}
131
132/// App Intent that opens DomainDig and re-inspects every tracked domain. It runs
133/// through the existing view-model batch path (`refreshAllTrackedDomains`), which
134/// enforces the batch feature gate and surfaces the paywall when needed.
135struct RunSweepIntent: AppIntent {
136 static let title: LocalizedStringResource = "Run Watchlist Sweep"
137 static let description = IntentDescription(
138 "Open DomainDig and re-inspect every domain on your watchlist."
139 )
140
141 static let openAppWhenRun = true
142
143 @MainActor
144 func perform() async throws -> some IntentResult {
145 DomainDigIntentRouter.shared.pendingAction = .sweep
146 return .result()
147 }
148}
149
150/// In-process hand-off from an `openAppWhenRun` intent to the running SwiftUI
151/// layer. `RootTabView` observes `pendingAction` and performs it.
152@MainActor
153@Observable
154final class DomainDigIntentRouter {
155 static let shared = DomainDigIntentRouter()
156 var pendingAction: DomainDigDeepLink.Action?
157 private init() { /* Singleton; use the shared instance. */ }
158}
159
160/// Exposes DomainDig intents to Spotlight and Siri with invocation phrases.
161struct DomainDigShortcuts: AppShortcutsProvider {
162 static var appShortcuts: [AppShortcut] {
163 AppShortcut(
164 intent: InspectDomainIntent(),
165 phrases: [
166 "Inspect a domain with \(.applicationName)",
167 "Dig a domain with \(.applicationName)"
168 ],
169 shortTitle: "Inspect Domain",
170 systemImageName: "magnifyingglass"
171 )
172 AppShortcut(
173 intent: AddToWatchlistIntent(),
174 phrases: [
175 "Add a domain to \(.applicationName)",
176 "Watch a domain with \(.applicationName)"
177 ],
178 shortTitle: "Add to Watchlist",
179 systemImageName: "plus.circle"
180 )
181 AppShortcut(
182 intent: RunSweepIntent(),
183 phrases: [
184 "Run a sweep with \(.applicationName)",
185 "Sweep my \(.applicationName) watchlist"
186 ],
187 shortTitle: "Run Sweep",
188 systemImageName: "arrow.trianglehead.2.clockwise"
189 )
190 }
191}