Files
andros 073d3346ef Fix mention autocomplete: close on Enter, add Orgro; extract inferredNick + tests; bump 1.3 (16)
- MentionAutocompleteField: use \z instead of $ so dropdown closes after Enter
- MentionDirectory: handle path-less feed URLs (e.g. social.orgro.org → orgro)
  via penultimate host segment heuristic
- Extract derivedNick logic to public inferredNick(from:) in OrgSocialKit/Util
- Add URLNickTests covering 16 real relay URL patterns (vhost, root, path-less,
  deep GitHub/Codeberg, tilde, custom filename)
- Bump build to 1.3 (16)
2026-05-19 22:03:44 +02:00

28 lines
1.1 KiB
Swift

import Foundation
/// Infers a display nick from a feed URL when the profile has not been fetched.
///
/// Heuristic, in priority order:
/// 1. If the last path segment ends in `.org` and there is a preceding segment,
/// that segment is the nick (`/user/social.org` `user`).
/// 2. If the last path segment ends in `.org` but has no preceding segment,
/// fall back to the host (`/social.org` `example.com`).
/// 3. If there is no path at all, use the penultimate host segment
/// (`social.orgro.org` `orgro`).
/// 4. Otherwise return the last path segment as-is.
public func inferredNick(from url: URL) -> String {
let parts = url.path.split(separator: "/").filter { !$0.isEmpty }
if let last = parts.last, last.lowercased().hasSuffix(".org") {
if parts.count >= 2 { return String(parts[parts.count - 2]) }
return url.host ?? ""
}
if let last = parts.last {
return String(last)
}
let hostParts = (url.host ?? "").split(separator: ".").map(String.init)
if hostParts.count >= 2 {
return hostParts[hostParts.count - 2]
}
return url.host ?? ""
}