Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Browser9// File : SPBrowser/Core/Aura/Portal.swift10// Module : SPBrowser11// Repository : https://git.spboucher.ai/sp-browser12// Created : 2026-11-1913// Last Modified : 2026-11-1914//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : None specified (no license file in this repository)80// Maintainer : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation8889/// AURA Portal: an explicit hand-off of tabs from one of the user's devices to another, carried by SP Sync (end-to-end encrypted).90struct HandoffItem: Codable, Equatable, Identifiable, Sendable {91 var id: String = UUID().uuidString92 var fromDeviceID: String93 var fromDeviceName: String94 var fromPlatform: String95 /// The device meant to receive it, or nil for "any of my devices".96 var toDeviceID: String?97 var title: String = ""98 var tabs: [RemoteTab]99 var sentAt: Date = Date()100101 static let lifetime: TimeInterval = 24 * 3600102 func isExpired(now: Date = Date()) -> Bool { now.timeIntervalSince(sentAt) > Self.lifetime }103}104105/// One of the user's other devices as Portal presents it.106struct PortalDevice: Equatable, Identifiable, Sendable {107 var id: String108 var name: String109 var platform: String110 var tabs: [RemoteTab]111 var closed: [RemoteTab]112 /// When that device last published its tabs (not "now": sync is periodic, not instantaneous).113 var updatedAt: Date114115 var symbol: String { PortalDevice.symbol(for: platform) }116 static func symbol(for platform: String) -> String {117 switch platform {118 case "macos": return "laptopcomputer"119 case "ipados": return "ipad"120 default: return "iphone"121 }122 }123124 /// How fresh the device's tab list is.125 enum Freshness: Sendable { case live, recent, stale }126 func freshness(now: Date = Date()) -> Freshness {127 let age = now.timeIntervalSince(updatedAt)128 return age < 15 * 60 ? .live : (age < 24 * 3600 ? .recent : .stale)129 }130}131132extension SyncedLibrary {133 /// Other devices with their open and recently closed tabs merged, most recently active first.134 func portalDevices(excluding deviceID: String?) -> [PortalDevice] {135 let open = otherDevicesTabs(excluding: deviceID)136 let closed = Dictionary(uniqueKeysWithValues: otherDevicesClosedTabs(excluding: deviceID).map { ($0.deviceID, $0) })137 var out = open.map { PortalDevice(id: $0.deviceID, name: $0.deviceName, platform: $0.platform, tabs: $0.tabs, closed: closed[$0.deviceID]?.tabs ?? [], updatedAt: $0.updatedAt) }138 for (id, device) in closed where !out.contains(where: { $0.id == id }) {139 out.append(PortalDevice(id: id, name: device.deviceName, platform: device.platform, tabs: [], closed: device.tabs, updatedAt: device.updatedAt))140 }141 return out.sorted { $0.updatedAt > $1.updatedAt }142 }143144 /// Sends tabs to another device. Callers pass regular tabs only: private tabs are never handed off.145 @discardableResult146 func sendHandoff(tabs: [RemoteTab], title: String = "", to deviceID: String?, fromDeviceID: String, fromDeviceName: String, fromPlatform: String, now: Date = Date()) -> HandoffItem? {147 let clean = tabs.filter { URL(string: $0.url).map(HistoryStore.isRecordable) ?? false }148 guard !clean.isEmpty else { return nil }149 let item = HandoffItem(fromDeviceID: fromDeviceID, fromDeviceName: fromDeviceName, fromPlatform: fromPlatform, toDeviceID: deviceID,150 title: String(title.prefix(120)), tabs: Array(clean.prefix(50)), sentAt: now)151 mirror.put(SyncCollection.handoff, key: item.id, value: item, modifiedAt: now)152 return item153 }154155 /// Hand-offs waiting for this device, newest first. Expired ones are ignored.156 func pendingHandoffs(for deviceID: String?, now: Date = Date()) -> [HandoffItem] {157 mirror.values(SyncCollection.handoff, as: HandoffItem.self)158 .filter { $0.fromDeviceID != deviceID && ($0.toDeviceID == nil || $0.toDeviceID == deviceID) && !$0.isExpired(now: now) }159 .sorted { $0.sentAt > $1.sentAt }160 }161162 /// The receiving device took (or dismissed) the hand-off: the record is deleted everywhere.163 func consumeHandoff(_ item: HandoffItem) { mirror.remove(SyncCollection.handoff, key: item.id) }164165 /// Removes hand-offs nobody picked up within a day.166 func purgeExpiredHandoffs(now: Date = Date()) {167 for item in mirror.values(SyncCollection.handoff, as: HandoffItem.self) where item.isExpired(now: now) { mirror.remove(SyncCollection.handoff, key: item.id) }168 }169}170