mastodon-backup-filter/VisibilityExtractor.cs

65 lines
1.9 KiB
C#

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(),
};
}
}
}