SPB Git

Repository Public

Models.swift 12.1 KB 212 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : MacLustr SMS Gateway9// File             : ios/MacLustrSMS/Core/Models.swift10// Description      : Formes des réponses du serveur (API d'administration de la11//                    passerelle) et libellés honnêtes des états.12// Module           : ios13// Repository       : https://git.spboucher.ai/maclustr-sms14// Project Website  : https://sms.spboucher.ai15// Created          : 2026-11-3016// Last Modified    : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: admin@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : None specified (no license file in this repository)83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192enum MessageStatus: String, Codable, Sendable, CaseIterable {93    case held, queued, dispatched, accepted, submitting, submitted, delivered, failed, expired, cancelled, unknown, received94    init(from decoder: Decoder) throws { self = MessageStatus(rawValue: try decoder.singleValueContainer().decode(String.self)) ?? .unknown }9596    var label: String {97        switch self {98        case .held: "À approuver"; case .queued: "En file"; case .dispatched: "Transmis au téléphone"; case .accepted: "Accepté par le téléphone"99        case .submitting: "Remise au modem"; case .submitted: "Remis au réseau"; case .delivered: "Livré"; case .failed: "Échec"100        case .expired: "Expiré"; case .cancelled: "Annulé"; case .unknown: "Issue inconnue"; case .received: "Reçu"101        }102    }103    var tone: Tone {104        switch self {105        case .delivered, .submitted: .ok; case .received: .info; case .failed, .expired: .bad; case .unknown, .held: .warn; default: .neutral106        }107    }108    var isFinal: Bool { [.delivered, .failed, .expired, .cancelled, .unknown, .received].contains(self) }109    var isCancellable: Bool { self == .held || self == .queued }110    /// Explication en clair, sans promettre plus que ce que l'état prouve.111    var explanation: String {112        switch self {113        case .held: "Ce message attend une approbation avant d'entrer dans la file."114        case .queued: "Le message attend que le téléphone passerelle le prenne. Il peut encore être annulé."115        case .dispatched: "La commande est partie vers le téléphone passerelle."116        case .accepted: "Le téléphone a inscrit la commande dans son journal."117        case .submitting: "Le téléphone remet le SMS à son modem."118        case .submitted: "Le modem du téléphone a accepté le SMS : il est parti sur le réseau cellulaire. C'est l'issue normale d'un envoi ; ce n'est pas encore une preuve de livraison."119        case .delivered: "L'opérateur a confirmé la livraison au destinataire."120        case .failed: "Le SMS n'est pas parti, ou l'opérateur a signalé un échec."121        case .expired: "Le délai de validité est passé avant que le SMS ne puisse partir."122        case .cancelled: "Annulé avant d'être transmis au téléphone."123        case .unknown: "Le téléphone a été interrompu pendant l'envoi : impossible de savoir si le SMS est parti. Il n'est jamais renvoyé automatiquement ; vérifiez auprès du destinataire avant de le renvoyer."124        case .received: "SMS reçu par le téléphone passerelle."125        }126    }127}128enum Tone: Sendable { case ok, info, warn, bad, neutral }129130struct MessageError: Codable, Hashable, Sendable { let code: String; let detail: String? }131struct MessageEvent: Codable, Hashable, Sendable, Identifiable {132    let type: String; let from: String?; let to: String?; let source: String; let at: Date133    let ignored: Bool134    var id: String { "\(type)-\(at.timeIntervalSince1970)-\(to ?? "")" }135    enum CodingKeys: String, CodingKey { case type, from, to, source, at, detail }136    init(from d: Decoder) throws {137        let c = try d.container(keyedBy: CodingKeys.self)138        type = try c.decode(String.self, forKey: .type); from = try c.decodeIfPresent(String.self, forKey: .from); to = try c.decodeIfPresent(String.self, forKey: .to)139        source = try c.decode(String.self, forKey: .source); at = try c.decode(Date.self, forKey: .at)140        ignored = ((try? c.decode([String: JSONValue].self, forKey: .detail))?["ignored"]) == .bool(true)141    }142    func encode(to encoder: Encoder) throws { var c = encoder.container(keyedBy: CodingKeys.self); try c.encode(type, forKey: .type); try c.encode(at, forKey: .at) }143}144struct SMSMessage: Codable, Hashable, Sendable, Identifiable {145    let id: String; let direction: String; var status: MessageStatus146    let to: String?; let from: String?; let text: String?147    let textPurged: Bool?; let sensitive: Bool?148    let deviceId: String?; let simId: String?; let applicationId: String?; let conversationId: String?149    let encoding: String?; let segments: Int?; let characters: Int?150    let error: MessageError?151    let createdAt: Date; let expiresAt: Date?; let submittedAt: Date?; let deliveredAt: Date?152    var events: [MessageEvent]?153    var isOutbound: Bool { direction == "outbound" }154    var peer: String? { isOutbound ? to : from }155    var displayText: String { sensitive == true ? "Code de vérification (masqué)" : (text ?? "Contenu effacé") }156}157struct Page<T: Codable & Sendable>: Codable, Sendable { let data: [T]; let hasMore: Bool?; let nextCursor: String? }158struct ListOf<T: Codable & Sendable>: Codable, Sendable { let data: [T] }159160struct Sim: Codable, Equatable, Sendable, Identifiable {161    let id: String; let deviceId: String; let carrierName: String; let displayName: String?; let label: String?162    let type: String; let number: String?; let state: String; let signalLevel: Int?; let active: Bool; let sendEnabled: Bool163    let dailyLimit: Int?; let sentToday: Int?; let deviceName: String?; let deviceOnline: Bool?164    var usable: Bool { active && sendEnabled }165    var title: String { [carrierName.isEmpty ? nil : carrierName, number.map(PhoneNumber.pretty)].compactMap { $0 }.joined(separator: " · ") }166}167struct SimList: Codable, Sendable { let data: [Sim]; let defaultSimId: String? }168struct Device: Codable, Equatable, Sendable, Identifiable {169    let id: String; let name: String; let manufacturer: String?; let model: String?; let androidVersion: String?; let appVersion: String?170    let online: Bool; let enabled: Bool; let maintenance: Bool; let revoked: Bool; let available: Bool171    let smsPermissionsMissing: Bool?172    let lastSeenAt: Date?; let batteryPct: Int?; let charging: Bool?; let network: String?; let gatewayState: String?173    let sims: [Sim]?174    var sims_: [Sim] { sims ?? [] }175    var stateLabel: String {176        if revoked { return "Révoqué" }; if !online { return "Hors ligne" }; if smsPermissionsMissing == true { return "Autorisation SMS manquante" }177        if !enabled { return "Envoi désactivé" }; if maintenance { return "Maintenance" }178        if let g = gatewayState, g != "running" { return g == "paused" ? "Passerelle en pause" : "Passerelle arrêtée" }179        return "Disponible"180    }181    var tone: Tone { revoked || !online || smsPermissionsMissing == true ? .bad : available ? .ok : .warn }182}183struct Overview: Codable, Sendable {184    struct Devices: Codable, Sendable { let online: Int; let offline: Int }185    struct Sims: Codable, Sendable { let active: Int; let enabled: Int }186    struct Messages: Codable, Sendable { let sentToday: Int; let receivedToday: Int; let failed24H: Int; let unknown24H: Int;   // « failed_24h » : la conversion snake_case d'Apple met le h en majuscule187        let medianSubmitLatencyS: Double? }188    struct Queue: Codable, Sendable { let pending: Int; let inFlight: Int; let held: Int }189    let version: String; let devices: Devices; let sims: Sims; let killSwitch: Bool; let sendMode: String; let messages: Messages; let queue: Queue190}191struct Conversation: Codable, Hashable, Sendable, Identifiable {192    let id: String; let simId: String; let number: String?; let peer: String; let lastMessageAt: Date; let lastDirection: String?193    let inboundCount: Int; let outboundCount: Int; let unread: Int194}195struct AllowedRecipient: Codable, Equatable, Sendable, Identifiable { let number: String; let label: String?; var id: String { number } }196struct Me: Codable, Sendable { struct User: Codable, Sendable { let id: String; let handle: String; let displayName: String }; let user: User }197198/// Valeur JSON quelconque (réglages du serveur, détails d'événements).199enum JSONValue: Codable, Equatable, Sendable {200    case null, bool(Bool), number(Double), string(String), array([JSONValue]), object([String: JSONValue])201    init(from decoder: Decoder) throws {202        let c = try decoder.singleValueContainer()203        if c.decodeNil() { self = .null } else if let b = try? c.decode(Bool.self) { self = .bool(b) } else if let n = try? c.decode(Double.self) { self = .number(n) }204        else if let s = try? c.decode(String.self) { self = .string(s) } else if let a = try? c.decode([JSONValue].self) { self = .array(a) }205        else { self = .object(try c.decode([String: JSONValue].self)) }206    }207    func encode(to encoder: Encoder) throws {208        var c = encoder.singleValueContainer()209        switch self { case .null: try c.encodeNil(); case .bool(let b): try c.encode(b); case .number(let n): try c.encode(n); case .string(let s): try c.encode(s); case .array(let a): try c.encode(a); case .object(let o): try c.encode(o) }210    }211}212
Files main
Menu

Theme