using System.Globalization; namespace Febe.SitoDiProva; /// One file of a post or story: a photo or a short video, served by the «CDN» in several sizes. public sealed class SiteFile { public required string Id { get; init; } public required bool IsVideo { get; init; } /// Size of the largest version. public required int Width { get; init; } public required int Height { get; init; } /// Video duration in seconds. public double DurationSeconds { get; init; } /// Key of the generated content on the CDN: same key, same picture at every size. public string MediaKey => "f" + Id; } /// A post of the grid (a reel when it is a single video). public sealed class SitePost { public required string Id { get; init; } public required string Code { get; init; } public required DateTimeOffset PublishedAt { get; init; } public required string Caption { get; init; } public required SiteAccount Owner { get; init; } public bool Pinned { get; set; } public bool IsReel { get; init; } public required IReadOnlyList Files { get; init; } /// Accounts tagged in the post (it appears in their «Taggati» tab). public List Tagged { get; } = []; } /// A story (24 hours) or an item of a highlight collection. public sealed class SiteStory { public required string Id { get; init; } public required DateTimeOffset PublishedAt { get; init; } public required SiteAccount Owner { get; init; } public required SiteFile File { get; init; } public DateTimeOffset ExpiresAt => PublishedAt.AddHours(24); } public sealed class SiteHighlight { public required string Id { get; init; } public required string Title { get; init; } public required SiteAccount Owner { get; init; } public List Items { get; } = []; } public sealed class SiteAccount { public required string Id { get; init; } /// Current user name (it can change: rename detection). public required string Username { get; set; } public required string DisplayName { get; init; } public bool IsPrivate { get; init; } /// Alberto follows this profile. public bool FollowedByViewer { get; init; } /// This is Alberto's account (the connected one). public bool IsViewer { get; init; } /// Incremented when the profile picture changes. public int PictureVersion { get; set; } = 1; /// Posts, newest first. public List Posts { get; } = []; /// Active stories, oldest first (viewing order). public List Stories { get; } = []; public List Highlights { get; } = []; public string PictureKey => $"ritratto-{Id}-{PictureVersion.ToString(CultureInfo.InvariantCulture)}"; public DateTimeOffset? LatestStoryAt(DateTimeOffset now) => Stories.Where(s => s.ExpiresAt > now).Select(s => (DateTimeOffset?)s.PublishedAt).Max(); } /// Which tab of a profile. public enum ProfileTab { Posts, Reels, Tagged, } /// /// The synthetic world of the test site, generated from a seed (spec §14.2): Alberto's account, followed and not /// followed profiles, a private one he follows and one he does not, posts with several files, videos, pinned posts, /// stories, highlight collections, tagged posts and reels. Nothing here is real or imitates Instagram's data. /// /// Thread-safety: callers hold while reading or changing the world. public sealed class World { private const string CodeAlphabet = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789"; private static readonly string[] Words = [ "mare", "montagna", "colazione", "tramonto", "libro", "gatto", "bicicletta", "concerto", "pioggia", "giardino", "lago", "treno", "mercato", "pane", "neve", "festa", "museo", "ponte", "piazza", "sentiero", ]; private readonly Random _random; private readonly HashSet _codes = new(StringComparer.Ordinal); private long _nextPostId = 3_100_000_000; private long _nextFileId = 5_200_000_000; private long _nextStoryId = 7_300_000_000; private long _nextHighlightId = 8_400_000_000; private World(int seed, DateTimeOffset now) { _random = new Random(seed); Seed = seed; CreatedAt = now; } public Lock Gate { get; } = new(); public int Seed { get; } public DateTimeOffset CreatedAt { get; } public List Accounts { get; } = []; public SiteAccount Viewer => Accounts.First(a => a.IsViewer); /// Generates the world. anchors every date (stories are active for 24 hours from it). public static World Generate(int seed, DateTimeOffset now) { var world = new World(seed, new DateTimeOffset(now.UtcDateTime.Ticks - (now.UtcDateTime.Ticks % TimeSpan.TicksPerSecond), TimeSpan.Zero)); world.Build(); return world; } public SiteAccount? FindByUsername(string username) => Accounts.FirstOrDefault(a => string.Equals(a.Username, username, StringComparison.OrdinalIgnoreCase)); public SiteAccount? FindById(string id) => Accounts.FirstOrDefault(a => a.Id == id); public SitePost? FindPost(string code) => Accounts.SelectMany(a => a.Posts).FirstOrDefault(p => string.Equals(p.Code, code, StringComparison.Ordinal)); public SiteHighlight? FindHighlight(string id) => Accounts.SelectMany(a => a.Highlights).FirstOrDefault(h => h.Id == id); public SiteFile? FindFile(string mediaKey) => Accounts.SelectMany(a => a.Posts.SelectMany(p => p.Files) .Concat(a.Stories.Select(s => s.File)) .Concat(a.Highlights.SelectMany(h => h.Items.Select(i => i.File)))) .FirstOrDefault(f => f.MediaKey == mediaKey); /// Profiles Alberto follows, in the order of his list. public IReadOnlyList Following => [.. Accounts.Where(a => a.FollowedByViewer)]; /// Followed profiles with active stories, latest story first (the stories bar). public IReadOnlyList StoryBar(DateTimeOffset now) => [.. Accounts.Where(a => a.FollowedByViewer && a.LatestStoryAt(now) is not null).OrderByDescending(a => a.LatestStoryAt(now))]; /// Recent posts of followed profiles, newest first (the home feed). public IReadOnlyList Feed() => [.. Accounts.Where(a => a.FollowedByViewer).SelectMany(a => a.Posts).OrderByDescending(p => p.PublishedAt).ThenBy(p => p.Id, StringComparer.Ordinal)]; /// The posts of a profile tab: pinned posts first on the main grid, then newest first. public IReadOnlyList Grid(SiteAccount account, ProfileTab tab) => tab switch { ProfileTab.Posts => [.. account.Posts.Where(p => p.Pinned).OrderByDescending(p => p.PublishedAt), .. account.Posts.Where(p => !p.Pinned)], ProfileTab.Reels => [.. account.Posts.Where(p => p.IsReel)], ProfileTab.Tagged => [.. Accounts.SelectMany(a => a.Posts).Where(p => p.Tagged.Contains(account)).OrderByDescending(p => p.PublishedAt)], _ => [], }; /// A new post on top of an account (control endpoint «pubblica»). public SitePost Publish(SiteAccount account, string kind, DateTimeOffset at) { var post = NewPost(account, at, kind); account.Posts.Insert(0, post); return post; } /// A new active story (control endpoint «storia»). public SiteStory AddStory(SiteAccount account, bool video, DateTimeOffset at) { var story = NewStory(account, at, video); account.Stories.Add(story); return story; } private void Build() { var now = CreatedAt; var me = Add("1000", "alberto.prova", "Alberto (prova)", followed: false, viewer: true); var marta = Add("1001", "marta.prova", "Marta di prova", followed: true); var luca = Add("1002", "luca.prova", "Luca di prova", followed: true); var giulia = Add("1003", "giulia.prova", "Giulia di prova", followed: true, isPrivate: true); var nadia = Add("1004", "nadia.prova", "Nadia di prova", followed: true); var paolo = Add("1005", "paolo.prova", "Paolo di prova", followed: false); var riservato = Add("1006", "riservato.prova", "Profilo riservato", followed: false, isPrivate: true); for (var i = 1; i <= 24; i++) { var n = i.ToString("00", CultureInfo.InvariantCulture); Add((1100 + i).ToString(CultureInfo.InvariantCulture), $"amico{n}.prova", $"Amico {n}", followed: true); } // Posts, oldest first, so that each list ends newest first. AddPosts(me, 4, now.AddDays(-30), TimeSpan.FromDays(5), i => "foto"); AddPosts(marta, 18, now.AddDays(-30), TimeSpan.FromHours(38), i => (i % 5) switch { 0 => "serie", 2 => "video", _ => "foto" }); AddPosts(luca, 40, now.AddDays(-80), TimeSpan.FromHours(46), i => i % 7 == 3 ? "video" : "foto"); AddPosts(giulia, 6, now.AddDays(-20), TimeSpan.FromDays(3), i => i == 2 ? "serie" : "foto"); AddPosts(nadia, 3, now.AddDays(-10), TimeSpan.FromDays(3), i => "foto"); AddPosts(paolo, 8, now.AddDays(-15), TimeSpan.FromDays(1.5), i => i == 5 ? "serie" : "foto"); AddPosts(riservato, 5, now.AddDays(-15), TimeSpan.FromDays(2), i => "foto"); foreach (var friend in Accounts.Where(a => a.Username.StartsWith("amico", StringComparison.Ordinal))) { AddPosts(friend, 1 + _random.Next(2), now.AddDays(-40 - _random.Next(20)), TimeSpan.FromDays(4), i => "foto"); } // Two of Marta's older posts are pinned on top of her grid. marta.Posts[8].Pinned = true; marta.Posts[12].Pinned = true; // Tagged posts: Luca and Paolo tag Marta; Marta tags Alberto. luca.Posts[3].Tagged.Add(marta); luca.Posts[9].Tagged.Add(marta); paolo.Posts[2].Tagged.Add(marta); marta.Posts[4].Tagged.Add(me); // Stories, oldest first; the bar order is Marta, Nadia, Luca, Giulia. AddStories(marta, now, [-10, -8, -5, -1], videoAt: 2); AddStories(nadia, now, [-6, -4, -2], videoAt: -1); AddStories(luca, now, [-9, -3], videoAt: -1); AddStories(giulia, now, [-20, -16, -12, -8, -4], videoAt: 3); AddStories(paolo, now, [-7], videoAt: -1); // Highlights: Marta's «Estate» also holds one of her active stories, with the same identifier (H9). var summer = NewHighlight(marta, "Estate"); summer.Items.Add(NewStory(marta, now.AddDays(-60), video: false)); summer.Items.Add(NewStory(marta, now.AddDays(-59), video: true)); summer.Items.Add(marta.Stories[1]); var travels = NewHighlight(marta, "Viaggi"); travels.Items.Add(NewStory(marta, now.AddDays(-90), video: false)); travels.Items.Add(NewStory(marta, now.AddDays(-89), video: false)); var mountains = NewHighlight(luca, "Montagna"); mountains.Items.Add(NewStory(luca, now.AddDays(-120), video: false)); mountains.Items.Add(NewStory(luca, now.AddDays(-119), video: false)); } private SiteAccount Add(string id, string username, string displayName, bool followed, bool viewer = false, bool isPrivate = false) { var account = new SiteAccount { Id = id, Username = username, DisplayName = displayName, FollowedByViewer = followed, IsViewer = viewer, IsPrivate = isPrivate, }; Accounts.Add(account); return account; } private void AddPosts(SiteAccount account, int count, DateTimeOffset first, TimeSpan step, Func kind) { for (var i = 0; i < count; i++) { var jitter = TimeSpan.FromMinutes(_random.Next(0, 180)); account.Posts.Insert(0, NewPost(account, first + (step * i) + jitter, kind(i))); } } private void AddStories(SiteAccount account, DateTimeOffset now, int[] hoursAgo, int videoAt) { for (var i = 0; i < hoursAgo.Length; i++) { account.Stories.Add(NewStory(account, now.AddHours(hoursAgo[i]).AddMinutes(-_random.Next(0, 30)), video: i == videoAt)); } } private SitePost NewPost(SiteAccount account, DateTimeOffset at, string kind) { var files = kind switch { "serie" => new List { NewPhoto(960, 1200), NewVideo(720, 1280), NewPhoto(960, 1200) }, "video" => [NewVideo(720, 1280)], _ => [NewPhoto(960, 1200)], }; return new SitePost { Id = (_nextPostId += 7919 + _random.Next(1000)).ToString(CultureInfo.InvariantCulture), Code = NewCode(), PublishedAt = at, Caption = Caption(), Owner = account, IsReel = kind == "video", Files = files, }; } private SiteStory NewStory(SiteAccount account, DateTimeOffset at, bool video) => new() { Id = (_nextStoryId += 104_729 + _random.Next(1000)).ToString(CultureInfo.InvariantCulture), PublishedAt = at, Owner = account, File = video ? NewVideo(720, 1280) : NewPhoto(720, 1280), }; private SiteHighlight NewHighlight(SiteAccount account, string title) { var highlight = new SiteHighlight { Id = (_nextHighlightId += 15_485_863).ToString(CultureInfo.InvariantCulture), Title = title, Owner = account, }; account.Highlights.Add(highlight); return highlight; } private SiteFile NewPhoto(int width, int height) => new() { Id = (_nextFileId += 1_299_709 + _random.Next(1000)).ToString(CultureInfo.InvariantCulture), IsVideo = false, Width = width, Height = height, }; private SiteFile NewVideo(int width, int height) => new() { Id = (_nextFileId += 1_299_709 + _random.Next(1000)).ToString(CultureInfo.InvariantCulture), IsVideo = true, Width = width, Height = height, DurationSeconds = 2, }; private string NewCode() { while (true) { var chars = new char[7]; for (var i = 0; i < chars.Length; i++) { chars[i] = CodeAlphabet[_random.Next(CodeAlphabet.Length)]; } var code = new string(chars); if (_codes.Add(code)) { return code; } } } private string Caption() { var a = Words[_random.Next(Words.Length)]; var b = Words[_random.Next(Words.Length)]; return $"Una giornata fra {a} e {b} (prova {_random.Next(1, 1000).ToString(CultureInfo.InvariantCulture)})"; } }