import Foundation
/// Invite links carry the room secret in the URL fragment, which browsers and
/// URL loaders never send to a server.
public enum InviteLink {
/// `base` with its fragment replaced by the secret, e.g. `myapp://join#<secret>`.
public static func url(base: URL, secret: RoomSecret) -> URL {
var comps = URLComponents(url: base, resolvingAgainstBaseURL: false) ?? URLComponents()
comps.fragment = secret.base64URL
return comps.url ?? base
}
/// The secret in a link's fragment, if it is a valid room secret.
public static func secret(from url: URL) -> RoomSecret? {
guard let frag = URLComponents(url: url, resolvingAgainstBaseURL: false)?.fragment, !frag.isEmpty else { return nil }
return try? RoomSecret(base64URL: frag)
}
}