initial commit

This commit is contained in:
Viv Lim 2023-09-10 23:11:22 -07:00
commit f2a1826f29
9 changed files with 348 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
bin/*
obj/*
archive-*/*
.DS_Store

23
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,23 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"program": "${workspaceFolder}/bin/Debug/net7.0/mastodon-backup-filter",
"args": ["--file", "archive-cybre-20220827010851-9c0da7bedd287e34c90dd0c40e02bc6d/outbox.json"],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}

96
Activity.cs Normal file
View File

@ -0,0 +1,96 @@
using System.Text.Json.Nodes;
namespace MastodonBackupFilter
{
public class MastodonOutboxNodeReader
{
public static OutboxObject ReadNodes(JsonNode? node)
{
if (node == null)
{
throw new ArgumentNullException();
}
var baseObj = new OutboxObject
{
Node = node,
Recipients = VisibilityExtractor.ExtractRecipients(node)
};
return baseObj.TypeString switch
{
"Create" => new CreateActivityJsonNode
{
Node = node,
Recipients = baseObj.Recipients,
Payload = node["object"] switch
{
null => throw new InvalidDataException("Create has a null object"),
JsonObject o => o["type"] switch
{
null => throw new InvalidDataException("Created post object has null type"),
JsonValue v => v.GetValue<string>() switch
{
"Note" => new NoteObject
{
Node = o,
Recipients = VisibilityExtractor.ExtractRecipients(o)
},
_ => throw new InvalidDataException($"Unhandled kind of create payload {o}")
},
_ => throw new InvalidDataException("Object type should be a JsonValue")
},
_ => throw new InvalidDataException("Can't handle a create payload that isn't a JsonObject")
}
},
"Announce" => new AnnounceActivityJsonNode
{
Node = node,
Recipients = baseObj.Recipients,
BoostTarget = node["object"] switch
{
JsonValue v => v.GetValue<string>(),
_ => throw new InvalidDataException("Can't handle announce with object that isn't a value.")
}
},
_ => baseObj // No special handling...
};
}
}
public record MastodonObject
{
public required JsonNode Node { get; init; }
public string Id => Node["id"]!.GetValue<string>();
public string Published => Node["published"]!.GetValue<string>();
public string TypeString => Node["type"]!.GetValue<string>();
}
public record OutboxObject : MastodonObject
{
public required PostRecipients Recipients { get; init; }
public JsonArray To => Node["to"]!.AsArray();
public JsonArray Cc => Node["to"]!.AsArray();
public Visibility Visibility => Recipients.AsVisibility();
}
public record CreateActivityJsonNode : OutboxObject
{
public required OutboxObject Payload { get; init; }
}
public record AnnounceActivityJsonNode : OutboxObject
{
public required string BoostTarget { get; init; }
}
public record NoteObject : OutboxObject
{
public string Url => Node["url"]!.GetValue<string>();
}
}

61
MastodonBackup.cs Normal file
View File

@ -0,0 +1,61 @@
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().Take(150).ToArray();
Console.WriteLine($"{items.Length} posts");
var keysToRemove = new Stack<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 announce => $"boosted {announce.BoostTarget} 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}");
}
catch (Exception ex) {
Console.WriteLine(msg);
Console.WriteLine($"Failed to determine visibility for object: {ex}");
throw;
}
}
return Task.FromResult(new MastodonBackup {});
}
}
}

30
Program.cs Normal file
View File

@ -0,0 +1,30 @@
// See https://aka.ms/new-console-template for more information
using System.Text.Json;
using System.Text.Json.Nodes;
using MastodonBackupFilter;
using System.CommandLine;
internal class Program
{
static async Task<int> Main(string[] args)
{
var fileOption = new Option<FileInfo?>(
name: "--file",
description: "The file to read");
var rootCommand = new RootCommand("Sample app for System.CommandLine");
rootCommand.AddOption(fileOption);
rootCommand.SetHandler((file) =>
{
MastodonBackup.FromFileAsync(file);
},
fileOption);
return await rootCommand.InvokeAsync(args);
var path = Environment.GetCommandLineArgs()[1];
}
}

10
Visibility.cs Normal file
View File

@ -0,0 +1,10 @@
namespace MastodonBackupFilter
{
public enum Visibility
{
Public,
Unlisted,
Private,
DM
}
}

84
VisibilityExtractor.cs Normal file
View File

@ -0,0 +1,84 @@
using System.Text.Json.Nodes;
namespace MastodonBackupFilter
{
public class VisibilityExtractor
{
private static bool IsRecipientFollowers(JsonNode? recipientNode)
{
if (recipientNode is null)
{
return false;
}
try
{
var val = recipientNode.GetValue<string>();
var recipientIsFollowers = val.EndsWith("/followers");
return recipientIsFollowers;
}
catch (Exception ex)
{
Console.WriteLine($"Mismatch, unexpected recipient node: {recipientNode}");
return false;
}
}
private static bool IsRecipientPublic(JsonNode? recipientNode)
{
if (recipientNode is null)
{
return false;
}
try
{
var val = recipientNode.GetValue<string>();
var recipientIsPublic = val == "https://www.w3.org/ns/activitystreams#Public";
return recipientIsPublic;
}
catch (Exception ex)
{
Console.WriteLine($"Mismatch, unexpected recipient node: {recipientNode}");
return false;
}
}
public static PostRecipients ExtractRecipients(JsonNode node)
{
var to = node["to"].AsArray();
var cc = node["cc"].AsArray();
var recipients = to.Concat(cc).ToArray();
return new PostRecipients {
ToPublic = to.Where(r => IsRecipientPublic(r)).Any(),
CcPublic = cc.Where(r => IsRecipientPublic(r)).Any(),
ToFollowers = to.Where(r => IsRecipientFollowers(r)).Any(),
CcFollowers = cc.Where(r => IsRecipientFollowers(r)).Any(),
};
}
}
public record PostRecipients {
public required bool ToPublic {get; init;}
public required bool CcPublic {get; init;}
public required bool ToFollowers {get; init;}
public required bool CcFollowers {get; init;}
public bool FlagsEqual(PostRecipients other) =>
ToPublic == other.ToPublic
&& CcPublic == other.CcPublic
&& ToFollowers == other.ToFollowers
&& CcFollowers == other.CcFollowers;
public Visibility AsVisibility() => this switch {
{ToPublic: false, CcPublic: true, ToFollowers: true, CcFollowers: false} => Visibility.Unlisted,
_ => throw new InvalidDataException($"Unable to determine visibility for {this}")
};
}
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>MastodonBackupFilter</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "mastodon-backup-filter", "mastodon-backup-filter.csproj", "{3F2F9261-8A97-428C-BB71-CD059B925410}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3F2F9261-8A97-428C-BB71-CD059B925410}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F2F9261-8A97-428C-BB71-CD059B925410}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F2F9261-8A97-428C-BB71-CD059B925410}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F2F9261-8A97-428C-BB71-CD059B925410}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {521CC4F9-F736-490B-BBBE-634D0F79F286}
EndGlobalSection
EndGlobal