crossmate

A collaborative crossword app for iOS
Log | Files | Refs | LICENSE

ShareLinkRoute.swift (1496B)


      1 import Foundation
      2 
      3 /// Parses a Crossmate short share link — `https://<host>/s/<token>[/<deco>]…`
      4 /// — into the iCloud share token and, when the link carries one, the grid
      5 /// silhouette. A universal-link tap can then paint a placeholder grid and
      6 /// accept the share itself, skipping the Safari → iCloud redirect bounce.
      7 ///
      8 /// Mirrors `link-worker.js`: the token is constrained to RFC 3986 unreserved
      9 /// characters so the reconstructed iCloud URL can only ever be a share link,
     10 /// and each decoration segment is classified by trying to decode it as a
     11 /// silhouette (the title segment simply fails that and is ignored).
     12 struct ShareLinkRoute: Equatable {
     13     let token: String
     14     let shape: GridSilhouette.Grid?
     15 
     16     init?(shortLink url: URL) {
     17         guard url.scheme == "https" else { return nil }
     18         // ["s", token, deco?, deco?]
     19         let parts = url.pathComponents.filter { $0 != "/" }
     20         guard (2...4).contains(parts.count), parts[0] == "s" else { return nil }
     21 
     22         let token = parts[1]
     23         guard token.wholeMatch(of: /[A-Za-z0-9._~-]{8,128}/) != nil else { return nil }
     24 
     25         self.token = token
     26         self.shape = parts.dropFirst(2).lazy.compactMap { GridSilhouette.decode($0) }.first
     27     }
     28 
     29     /// The iCloud share URL the token addresses, fed to `CloudService` for
     30     /// acceptance. Safe to force-unwrap: the token charset is validated above.
     31     var iCloudShareURL: URL {
     32         URL(string: "https://www.icloud.com/share/\(token)")!
     33     }
     34 }