Files
Febe/tools/Febe.SitoDiProva/DataApi.cs
T

324 lines
12 KiB
C#

using System.Globalization;
using System.Text.Json.Nodes;
namespace Febe.SitoDiProva;
/// <summary>
/// The JSON the pages fetch, in the site's own synthetic format (LEGGIMI.md). It is what the response tap captures
/// and what the readers of <c>lettori.prova.json</c> read. Keys are Italian and the shape is ours: it does not
/// imitate Instagram.
/// </summary>
public sealed class DataApi(World world, CdnSigner signer, SiteControl control, Func<string> cdnBase, TimeProvider time)
{
public const int FeedPageSize = 6;
public const int GridPageSize = 12;
public const int FollowingPageSize = 10;
private DateTimeOffset Now => time.GetUtcNow();
/// <summary>Home: the connected account, the stories bar and the first page of the feed.</summary>
public JsonObject Home()
{
var feed = world.Feed();
return new JsonObject
{
["io"] = ProfileLite(world.Viewer),
["barra"] = new JsonArray([.. world.StoryBar(Now).Select(a => (JsonNode)Profile(a))]),
["contenuti"] = new JsonArray([.. feed.Take(FeedPageSize).Select(p => (JsonNode)Post(p, inFeed: true))]),
["pagina"] = Page(feed.Count, 0, FeedPageSize),
};
}
public JsonObject HomeMore(string? after)
{
var feed = world.Feed();
var offset = ParseCursor(after);
return new JsonObject
{
["contenuti"] = new JsonArray([.. feed.Skip(offset).Take(FeedPageSize).Select(p => (JsonNode)Post(p, inFeed: true))]),
["pagina"] = Page(feed.Count, offset, FeedPageSize),
};
}
/// <summary>A profile page: profile, highlights (first page only) and a page of the tab. Null when it does not exist.</summary>
public JsonObject? Profile(string username, ProfileTab tab, string? after)
{
var account = world.FindByUsername(username);
if (account is null)
{
return null;
}
var result = new JsonObject { ["profilo"] = Profile(account) };
if (account.IsPrivate && !account.FollowedByViewer)
{
result["contenuti"] = new JsonArray();
result["pagina"] = new JsonObject { ["altro"] = false, ["dopo"] = null };
return result;
}
var offset = ParseCursor(after);
if (tab == ProfileTab.Posts && offset == 0)
{
result["raccolte"] = new JsonArray([.. account.Highlights.Select(h => (JsonNode)Highlight(h, withItems: false))]);
}
var grid = world.Grid(account, tab);
result["contenuti"] = new JsonArray([.. grid.Skip(offset).Take(GridPageSize).Select(p => (JsonNode)Post(p, inFeed: false))]);
result["pagina"] = Page(grid.Count, offset, GridPageSize);
return result;
}
public JsonObject? ProfileMore(string username, ProfileTab tab, string? after)
{
var account = world.FindByUsername(username);
if (account is null || (account.IsPrivate && !account.FollowedByViewer))
{
return null;
}
var grid = world.Grid(account, tab);
var offset = ParseCursor(after);
return new JsonObject
{
["contenuti"] = new JsonArray([.. grid.Skip(offset).Take(GridPageSize).Select(p => (JsonNode)Post(p, inFeed: false))]),
["pagina"] = Page(grid.Count, offset, GridPageSize),
};
}
public JsonObject? SinglePost(string code)
{
var post = world.FindPost(code);
if (post is null || (post.Owner.IsPrivate && !post.Owner.FollowedByViewer))
{
return null;
}
return new JsonObject { ["contenuto"] = Post(post, inFeed: false) };
}
/// <summary>All the active stories of an account (the page receives the whole list in advance), and who comes next.</summary>
public JsonObject? Stories(string username)
{
var account = world.FindByUsername(username);
if (account is null || (account.IsPrivate && !account.FollowedByViewer))
{
return null;
}
var bar = world.StoryBar(Now);
var index = bar.ToList().IndexOf(account);
var next = index >= 0 && index + 1 < bar.Count ? bar[index + 1].Username : null;
return new JsonObject
{
["autore"] = ProfileLite(account),
["storie"] = new JsonArray([.. account.Stories.Where(s => s.ExpiresAt > Now).Select(s => (JsonNode)Story(s))]),
["successivo"] = next,
};
}
public JsonObject? HighlightItems(string id)
{
var highlight = world.FindHighlight(id);
if (highlight is null)
{
return null;
}
var siblings = highlight.Owner.Highlights;
var index = siblings.IndexOf(highlight);
return new JsonObject
{
["raccolta"] = Highlight(highlight, withItems: true),
["successiva"] = index + 1 < siblings.Count ? siblings[index + 1].Id : null,
};
}
public JsonObject Following(string? after)
{
var list = world.Following;
var offset = ParseCursor(after);
return new JsonObject
{
["seguiti"] = new JsonArray([.. list.Skip(offset).Take(FollowingPageSize).Select(a => (JsonNode)Profile(a))]),
["pagina"] = Page(list.Count, offset, FollowingPageSize),
};
}
public static JsonObject EmptyPage() => new()
{
["contenuti"] = new JsonArray(),
["pagina"] = new JsonObject { ["altro"] = false, ["dopo"] = null },
};
/// <summary>A response in a shape the readers do not know: what a site that changed its format would send.</summary>
public static JsonObject UnknownShape() => new()
{
["risposta"] = new JsonObject
{
["voci"] = new JsonArray(
new JsonObject { ["chiave"] = "A1", ["valore"] = "un formato nuovo", ["dettagli"] = new JsonObject { ["x"] = 1 } },
new JsonObject { ["chiave"] = "B2", ["valore"] = "che nessun lettore conosce", ["dettagli"] = new JsonObject { ["x"] = 2 } }),
},
["versione"] = 2,
};
public static int ParseCursor(string? cursor) =>
cursor is { Length: > 1 } && cursor[0] == 'c' && int.TryParse(cursor.AsSpan(1), NumberStyles.Integer, CultureInfo.InvariantCulture, out var n) && n >= 0 ? n : 0;
public static string Cursor(int offset) => "c" + offset.ToString(CultureInfo.InvariantCulture);
private static JsonObject Page(int total, int offset, int size)
{
var more = offset + size < total;
return new JsonObject { ["altro"] = more, ["dopo"] = more ? Cursor(offset + size) : null };
}
private JsonObject ProfileLite(SiteAccount account) => new()
{
["numero"] = account.Id,
["nome"] = account.Username,
};
private JsonObject Profile(SiteAccount account)
{
var node = new JsonObject
{
["numero"] = account.Id,
["nome"] = account.Username,
["nome_visibile"] = account.DisplayName,
["riservato"] = account.IsPrivate,
["seguito"] = account.FollowedByViewer,
["pubblicazioni"] = account.Posts.Count,
["ritratto"] = new JsonArray(
Version(account.PictureKey, 160, 160, video: false),
Version(account.PictureKey, 320, 320, video: false)),
};
if (account.LatestStoryAt(Now) is { } latest && (!account.IsPrivate || account.FollowedByViewer))
{
node["ultima_storia"] = Date(latest);
}
return node;
}
private JsonObject Post(SitePost post, bool inFeed)
{
var node = new JsonObject
{
["numero"] = post.Id,
["sigla"] = post.Code,
["pubblicato"] = Date(post.PublishedAt),
["testo"] = post.Caption,
["fissato"] = post.Pinned && !inFeed,
["autore"] = ProfileLite(post.Owner),
};
if (post.Files.Count == 1)
{
AddFile(node, post.Files[0]);
}
else
{
// The feed carries only the first file of a series, with the declared count: the post is incomplete there
// and «Post singolo» completes it. Profile grids and post pages carry the whole series.
var parts = inFeed ? post.Files.Take(1) : post.Files;
node["parti"] = new JsonArray([.. parts.Select(f =>
{
var part = new JsonObject { ["numero"] = f.Id };
AddFile(part, f);
return (JsonNode)part;
})]);
node["parti_totali"] = post.Files.Count;
}
return node;
}
private JsonObject Story(SiteStory story)
{
var node = new JsonObject
{
["numero"] = story.Id,
["pubblicato"] = Date(story.PublishedAt),
["scade"] = Date(story.ExpiresAt),
["autore"] = ProfileLite(story.Owner),
};
AddFile(node, story.File);
return node;
}
private JsonObject Highlight(SiteHighlight highlight, bool withItems)
{
var node = new JsonObject
{
["numero"] = highlight.Id,
["titolo"] = highlight.Title,
["autore"] = ProfileLite(highlight.Owner),
["copertina"] = highlight.Items.Count > 0 ? Version(highlight.Items[0].File.MediaKey, 160, 284, video: false) : null,
};
if (withItems)
{
node["contenuti"] = new JsonArray([.. highlight.Items.Select(i =>
{
var item = new JsonObject
{
["numero"] = i.Id,
["pubblicato"] = Date(i.PublishedAt),
["autore"] = ProfileLite(i.Owner),
};
AddFile(item, i.File);
return (JsonNode)item;
})]);
}
return node;
}
/// <summary>
/// The versions of a file. Images: a medium, the largest and a small one (in this order: the largest is not the
/// first). Videos: two renditions with audio and a larger one without audio (the reader must not pick it), plus
/// cover images.
/// </summary>
private void AddFile(JsonObject node, SiteFile file)
{
if (file.IsVideo)
{
node["filmati"] = new JsonArray(
Version(file.MediaKey, file.Width / 2, file.Height / 2, video: true, audio: true),
Version(file.MediaKey, file.Width, file.Height, video: true, audio: true),
Version(file.MediaKey, file.Width * 3 / 2, file.Height * 3 / 2, video: true, audio: false));
node["foto"] = new JsonArray(
Version(file.MediaKey, file.Width / 2, file.Height / 2, video: false),
Version(file.MediaKey, file.Width, file.Height, video: false));
node["durata"] = file.DurationSeconds;
}
else
{
node["foto"] = new JsonArray(
Version(file.MediaKey, file.Width / 2, file.Height / 2, video: false),
Version(file.MediaKey, file.Width, file.Height, video: false),
Version(file.MediaKey, file.Width / 4, file.Height / 4, video: false));
}
}
private JsonObject Version(string key, int width, int height, bool video, bool audio = true)
{
var size = $"{width.ToString(CultureInfo.InvariantCulture)}x{height.ToString(CultureInfo.InvariantCulture)}";
var path = video ? $"/m/{key}/{size}{(audio ? string.Empty : "-muto")}.mp4" : $"/m/{key}/{size}.jpg";
var node = new JsonObject
{
["indirizzo"] = cdnBase() + signer.Sign(path, Now + control.AddressLifetime),
["larghezza"] = width,
["altezza"] = height,
};
if (video)
{
node["audio"] = audio;
}
return node;
}
private static string Date(DateTimeOffset at) => at.UtcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture);
}