namespace Febe.Core.Collection.Privacy;
///
/// Turns an address path into a pattern that names nobody (issue #34, issue #39): every segment that is not a known word
/// of the site's addresses becomes {numero} or {testo}. Used by the request shapes of the probe and by the
/// diagnostic report. A missing word only costs detail, never privacy.
///
public static class PathPatterns
{
/// Written in place of a segment made only of digits.
public const string Number = "{numero}";
/// Written in place of any other segment that is not a known word.
public const string Text = "{testo}";
///
/// Path segments kept as they are: words of the site's addresses, the same for everyone. Anything else (user names,
/// identifiers, codes) becomes or .
///
private static readonly HashSet Known = new(StringComparer.Ordinal)
{
"api", "v1", "graphql", "graphql_www", "query", "ajax", "bulk-route-definitions", "navigation", "web", "users",
"user", "web_profile_info", "usernameinfo", "info", "feed", "timeline", "reels_media", "reels_tray", "highlights",
"highlights_tray", "friendships", "following", "followers", "show", "show_many", "search", "topsearch", "fbsearch",
"web_search", "typeahead", "accounts_typeahead", "top_serp", "media", "comments", "usertags", "clips", "stories",
"story", "tags", "sections", "discover", "explore", "chaining", "profile", "posts", "reels", "tagged", "saved",
"qp", "batch_fetch_web", "logging_client_events", "direct_v2", "inbox", "has_new", "get_presence", "wwwgraphql",
"ig", "bz", "web_info", "current_user", "user_info", "locations", "seen", "media_seen", "reel", "p", "accounts",
"edit", "activity", "news", "inbox_v2", "notifications", "badge", "home", "graphql_query",
// Fixed roots of the site's own pages (verification, login, messages...): a stop that lands there must say so,
// not «/{testo}/», and the site map must not take them for a profile (SiteMap.Classify).
"challenge", "checkpoint", "direct", "login", "logout", "emails", "password", "reset", "two_factor", "onetap",
"suspended", "consent", "settings", "help", "legal", "privacy", "terms", "about",
};
///
/// File extensions a CDN or other address may show in a report: a closed list, so that the last part of a name
/// with a dot («nome.cognome») never passes as an extension.
///
private static readonly HashSet Extensions = new(StringComparer.Ordinal)
{
"jpg", "jpeg", "png", "webp", "heic", "gif", "avif", "mp4", "m4a", "m4v", "webm", "mov", "js", "css", "json",
"woff", "woff2", "ttf", "ico", "svg", "html", "txt",
};
/// The known words, the same for everyone.
public static IReadOnlySet KnownSegments => Known;
/// The extensions may answer.
public static IReadOnlySet KnownExtensions => Extensions;
/// The path with each segment that is not a known word replaced by {numero} or {testo}.
/// Anything after «?» or «#» is dropped first: only a path is a path.
public static string Of(string path)
{
ArgumentNullException.ThrowIfNull(path);
var cut = path.IndexOfAny(['?', '#']);
if (cut >= 0)
{
path = path[..cut];
}
var segments = path.Split('/');
for (var i = 0; i < segments.Length; i++)
{
var segment = segments[i];
if (segment.Length == 0 || Known.Contains(segment))
{
continue;
}
segments[i] = IsDigits(segment) ? Number : Text;
}
return string.Join('/', segments);
}
/// True for a word of the site kept by .
public static bool IsKnownSegment(string segment) => Known.Contains(segment);
/// The extension of the last segment of (lower case, no dot) when it is on the closed list; otherwise null.
public static string? ExtensionOf(string? path)
{
if (string.IsNullOrEmpty(path))
{
return null;
}
var cut = path.IndexOfAny(['?', '#']);
if (cut >= 0)
{
path = path[..cut];
}
var last = path[(path.LastIndexOf('/') + 1)..];
var dot = last.LastIndexOf('.');
if (dot < 0 || dot == last.Length - 1)
{
return null;
}
var extension = last[(dot + 1)..].ToLowerInvariant();
return Extensions.Contains(extension) ? extension : null;
}
private static bool IsDigits(string segment)
{
foreach (var c in segment)
{
if (!char.IsAsciiDigit(c))
{
return false;
}
}
return segment.Length > 0;
}
}