73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace MastodonBackupFilter
|
|
{
|
|
public class MastodonBackup {
|
|
|
|
public static Task<MastodonBackup> FromFileAsync(FileInfo? filename){
|
|
|
|
if (filename is null || !filename.Exists)
|
|
{
|
|
throw new FileNotFoundException($"File doesn't exist: {filename?.FullName}");
|
|
}
|
|
|
|
Console.WriteLine($"Reading {filename.FullName}");
|
|
|
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
|
|
|
using var jsonStream = filename.OpenRead();
|
|
var root = JsonNode.Parse(jsonStream);
|
|
|
|
var items = root!["orderedItems"].AsArray().ToArray();//.Take(150).ToArray();
|
|
|
|
Console.WriteLine($"{items.Length} posts");
|
|
|
|
var keysToRemove = new Stack<uint>();
|
|
var visibilityCounters = new Dictionary<Visibility, uint>();
|
|
|
|
for (var i = 0; i < items.Length; i++)
|
|
{
|
|
var item = items[i];
|
|
|
|
var mastodonObject = MastodonOutboxNodeReader.ReadNodes(item);
|
|
|
|
var msg = mastodonObject switch {
|
|
CreateActivityJsonNode create => $"tooted {create.Payload switch {
|
|
NoteObject n => $"toot {n.Url} to {n.Recipients} {(n.Recipients != create.Recipients ? $"(create activity recipients differ: {create.Recipients}" : "")}",
|
|
MastodonObject idk => $"other object with id {idk.Id}",
|
|
_ => throw new InvalidDataException("shouldn't happen")
|
|
}}",
|
|
AnnounceActivityJsonNode<string> announce => $"boosted {announce.BoostTarget} to {announce.Recipients}",
|
|
AnnounceActivityJsonNode<UrlObject> announce => $"boosted {announce.BoostTarget.Url} to {announce.Recipients}",
|
|
MastodonObject idk => $"some unhandled kind of object with id {idk.Id}",
|
|
_ => throw new InvalidDataException("shouldn't happen")
|
|
};
|
|
|
|
try {
|
|
var vis = (mastodonObject as OutboxObject).Visibility;
|
|
Console.WriteLine($"{msg} {vis}");
|
|
|
|
if (visibilityCounters.TryGetValue(vis, out uint vCount)) {
|
|
visibilityCounters[vis] = vCount + 1;
|
|
} else {
|
|
visibilityCounters[vis] = 1;
|
|
}
|
|
}
|
|
catch (Exception ex) {
|
|
Console.WriteLine(msg);
|
|
Console.WriteLine($"Failed to determine visibility for object: {ex}");
|
|
throw;
|
|
}
|
|
|
|
}
|
|
|
|
foreach (var k in visibilityCounters.Keys){
|
|
Console.WriteLine($"{k}: {visibilityCounters[k]}");
|
|
}
|
|
|
|
return Task.FromResult(new MastodonBackup {});
|
|
}
|
|
}
|
|
} |