Open History to see the last commit
1import Foundation
2
3public enum FeatureDeviceType: String, Sendable, Equatable, Codable {
4 case desktop
5 case mobile
6 case tablet
7 case bot
8 case unknown
9
10 var displayName: String {
11 switch self {
12 case .desktop: "Desktop"
13 case .mobile: "Phone"
14 case .tablet: "Tablet"
15 case .bot: "Automation"
16 case .unknown: "Device"
17 }
18 }
19
20 var systemImage: String {
21 switch self {
22 case .desktop: "desktopcomputer"
23 case .mobile: "iphone"
24 case .tablet: "ipad"
25 case .bot: "gearshape.2"
26 case .unknown: "network"
27 }
28 }
29}
30
31public struct FeatureDeviceSession: Identifiable, Sendable, Equatable, Codable {
32 public var id: String { sessionID }
33
34 public let sessionID: String
35 public var label: String?
36 public var deviceType: FeatureDeviceType
37 public var operatingSystem: String?
38 public var browser: String?
39 public var ipAddress: String?
40 public var issuedAt: Date
41 public var expiresAt: Date
42 public var lastConnectedAt: Date?
43 public var isConnected: Bool
44 public var isCurrent: Bool
45
46 public init(
47 sessionID: String,
48 label: String? = nil,
49 deviceType: FeatureDeviceType = .unknown,
50 operatingSystem: String? = nil,
51 browser: String? = nil,
52 ipAddress: String? = nil,
53 issuedAt: Date,
54 expiresAt: Date,
55 lastConnectedAt: Date? = nil,
56 isConnected: Bool = false,
57 isCurrent: Bool = false
58 ) {
59 self.sessionID = sessionID
60 self.label = label
61 self.deviceType = deviceType
62 self.operatingSystem = operatingSystem
63 self.browser = browser
64 self.ipAddress = ipAddress
65 self.issuedAt = issuedAt
66 self.expiresAt = expiresAt
67 self.lastConnectedAt = lastConnectedAt
68 self.isConnected = isConnected
69 self.isCurrent = isCurrent
70 }
71
72 var displayName: String {
73 let value = label?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
74 if !value.isEmpty {
75 return value
76 }
77 return isCurrent ? "This device" : deviceType.displayName
78 }
79
80 var platformDescription: String {
81 [operatingSystem, browser]
82 .compactMap { value in
83 let trimmed = value?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
84 return trimmed.isEmpty ? nil : trimmed
85 }
86 .joined(separator: " · ")
87 }
88
89 var lastSeenAt: Date {
90 lastConnectedAt ?? issuedAt
91 }
92
93 static func sortedForDisplay(_ sessions: [Self]) -> [Self] {
94 sessions.sorted { left, right in
95 if left.isCurrent != right.isCurrent {
96 return left.isCurrent
97 }
98 if left.isConnected != right.isConnected {
99 return left.isConnected
100 }
101 return left.lastSeenAt > right.lastSeenAt
102 }
103 }
104}
105
106/// The app adapter can opt into access management without making it a requirement
107/// for environments that do not grant the access:read/access:write scopes.
108@MainActor
109public protocol FeatureDeviceManaging: AnyObject {
110 var managesServerSessions: Bool { get }
111 func loadDeviceSessions() async throws -> [FeatureDeviceSession]
112 func revokeDeviceSession(id: String) async throws
113 func revokeOtherDeviceSessions() async throws
114}
115
116extension FeatureDeviceManaging {
117 var managesServerSessions: Bool { true }
118}
119
120@MainActor
121final class EmptyFeatureDeviceManager: FeatureDeviceManaging {
122 static let shared = EmptyFeatureDeviceManager()
123
124 private init() {}
125
126 func loadDeviceSessions() async throws -> [FeatureDeviceSession] {
127 []
128 }
129
130 func revokeDeviceSession(id: String) async throws {}
131 func revokeOtherDeviceSessions() async throws {}
132}
133
134enum DeviceManagementErrorCopy {
135 static func message(for error: Error) -> String {
136 let value = error.localizedDescription.lowercased()
137 if value.contains("scope") || value.contains("403") || value.contains("forbidden")
138 || value.contains("permission") {
139 return "This connection does not have permission to manage devices."
140 }
141 if value.contains("offline") || value.contains("network")
142 || value.contains("not connected") || value.contains("timed out") {
143 return "Device access could not be updated. Check your connection and try again."
144 }
145 return "Device access could not be updated. Try again in a moment."
146 }
147}