120 lines
3.8 KiB
C#
120 lines
3.8 KiB
C#
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 => ReadContentNode(o),
|
|
_ => throw new InvalidDataException("Can't handle a create payload that isn't a JsonObject")
|
|
}
|
|
},
|
|
"Announce" => node["object"] switch
|
|
{
|
|
JsonValue v => new AnnounceActivityJsonNode<string>()
|
|
{
|
|
Node = node,
|
|
Recipients = baseObj.Recipients,
|
|
BoostTarget = v.GetValue<string>()
|
|
},
|
|
JsonObject o => new AnnounceActivityJsonNode<OutboxObject>()
|
|
{
|
|
Node = node,
|
|
Recipients = baseObj.Recipients,
|
|
BoostTarget = ReadContentNode(o),
|
|
},
|
|
_ => throw new InvalidDataException("Unhandled announce node type")
|
|
},
|
|
_ => baseObj // No special handling...
|
|
};
|
|
}
|
|
|
|
public static UrlObject ReadContentNode(JsonObject node)
|
|
{
|
|
return node["type"] switch
|
|
{
|
|
null => throw new InvalidDataException("Created post object has null type"),
|
|
JsonValue v => v.GetValue<string>() switch
|
|
{
|
|
"Note" => new NoteObject
|
|
{
|
|
Node = node,
|
|
Recipients = VisibilityExtractor.ExtractRecipients(node)
|
|
},
|
|
"Question" => new QuestionObject
|
|
{
|
|
Node = node,
|
|
Recipients = VisibilityExtractor.ExtractRecipients(node)
|
|
},
|
|
_ => throw new InvalidDataException($"Unhandled kind of content {node}")
|
|
},
|
|
_ => throw new InvalidDataException("Object type should be a JsonValue")
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
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<T> : OutboxObject
|
|
{
|
|
public required T BoostTarget { get; init; }
|
|
}
|
|
|
|
public record UrlObject : OutboxObject
|
|
{
|
|
public string Url => Node["url"]!.GetValue<string>();
|
|
|
|
}
|
|
|
|
public record NoteObject : UrlObject
|
|
{
|
|
|
|
}
|
|
|
|
public record QuestionObject : UrlObject
|
|
{
|
|
|
|
}
|
|
} |