derive all(?) kinds of post visibility
This commit is contained in:
parent
f2a1826f29
commit
4cc08014ad
76
Activity.cs
76
Activity.cs
|
@ -26,39 +26,53 @@ namespace MastodonBackupFilter
|
|||
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")
|
||||
},
|
||||
JsonObject o => ReadContentNode(o),
|
||||
_ => throw new InvalidDataException("Can't handle a create payload that isn't a JsonObject")
|
||||
}
|
||||
},
|
||||
"Announce" => new AnnounceActivityJsonNode
|
||||
"Announce" => node["object"] switch
|
||||
{
|
||||
Node = node,
|
||||
Recipients = baseObj.Recipients,
|
||||
BoostTarget = node["object"] switch
|
||||
JsonValue v => new AnnounceActivityJsonNode<string>()
|
||||
{
|
||||
JsonValue v => v.GetValue<string>(),
|
||||
_ => throw new InvalidDataException("Can't handle announce with object that isn't a value.")
|
||||
}
|
||||
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
|
||||
|
@ -83,14 +97,24 @@ namespace MastodonBackupFilter
|
|||
public required OutboxObject Payload { get; init; }
|
||||
}
|
||||
|
||||
public record AnnounceActivityJsonNode : OutboxObject
|
||||
public record AnnounceActivityJsonNode<T> : OutboxObject
|
||||
{
|
||||
public required string BoostTarget { get; init; }
|
||||
public required T BoostTarget { get; init; }
|
||||
}
|
||||
|
||||
public record NoteObject : OutboxObject
|
||||
public record UrlObject : OutboxObject
|
||||
{
|
||||
public string Url => Node["url"]!.GetValue<string>();
|
||||
|
||||
}
|
||||
|
||||
public record NoteObject : UrlObject
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public record QuestionObject : UrlObject
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -20,11 +20,12 @@ namespace MastodonBackupFilter
|
|||
using var jsonStream = filename.OpenRead();
|
||||
var root = JsonNode.Parse(jsonStream);
|
||||
|
||||
var items = root!["orderedItems"].AsArray().Take(150).ToArray();
|
||||
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++)
|
||||
{
|
||||
|
@ -38,7 +39,8 @@ namespace MastodonBackupFilter
|
|||
MastodonObject idk => $"other object with id {idk.Id}",
|
||||
_ => throw new InvalidDataException("shouldn't happen")
|
||||
}}",
|
||||
AnnounceActivityJsonNode announce => $"boosted {announce.BoostTarget} to {announce.Recipients}",
|
||||
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")
|
||||
};
|
||||
|
@ -46,6 +48,12 @@ namespace MastodonBackupFilter
|
|||
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);
|
||||
|
@ -55,6 +63,10 @@ namespace MastodonBackupFilter
|
|||
|
||||
}
|
||||
|
||||
foreach (var k in visibilityCounters.Keys){
|
||||
Console.WriteLine($"{k}: {visibilityCounters[k]}");
|
||||
}
|
||||
|
||||
return Task.FromResult(new MastodonBackup {});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
namespace MastodonBackupFilter
|
||||
{
|
||||
public record struct 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 Visibility AsVisibility() => this switch {
|
||||
{ToPublic: false, CcPublic: true, ToFollowers: true, CcFollowers: false} => Visibility.Unlisted,
|
||||
{ToPublic: true, CcPublic: false, ToFollowers: false, CcFollowers: true} => Visibility.Public,
|
||||
{ToPublic: false, CcPublic: false, ToFollowers: true, CcFollowers: false} => Visibility.Private,
|
||||
{ToPublic: false, CcPublic: false, ToFollowers: false, CcFollowers: false} => Visibility.DM,
|
||||
_ => throw new InvalidDataException($"Unable to determine visibility for {this}")
|
||||
};
|
||||
|
||||
}
|
||||
}
|
|
@ -62,23 +62,4 @@ namespace MastodonBackupFilter
|
|||
};
|
||||
}
|
||||
}
|
||||
|
||||
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}")
|
||||
};
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue