🐛 Trying to fix activity handler

This commit is contained in:
2026-01-01 01:26:14 +08:00
parent 14d5254461
commit 78f1d0ecd3

View File

@@ -564,6 +564,23 @@ public class ActivityPubActivityHandler(
}; };
} }
private static int? TryGetIntValue(Dictionary<string, object> dict, string key)
{
var value = dict.GetValueOrDefault(key);
if (value == null)
return null;
return value switch
{
int i => i,
long l => (int)l,
JsonElement { ValueKind: JsonValueKind.Number } element => element.TryGetInt32(out var result)
? result
: (int?)element.GetDouble(),
_ => null
};
}
private static Dictionary<string, object>? ConvertToDictionary(object? value) private static Dictionary<string, object>? ConvertToDictionary(object? value)
{ {
if (value == null) if (value == null)
@@ -579,6 +596,7 @@ public class ActivityPubActivityHandler(
{ {
result[property.Name] = ConvertJsonElementToObject(property.Value); result[property.Name] = ConvertJsonElementToObject(property.Value);
} }
return result; return result;
} }
@@ -616,18 +634,27 @@ public class ActivityPubActivityHandler(
private static List<SnCloudFileReferenceObject>? ParseAttachments(object? value) private static List<SnCloudFileReferenceObject>? ParseAttachments(object? value)
{ {
if (value is JsonElement { ValueKind: JsonValueKind.Array } element) if (value == null)
return null;
var attachments = value switch
{ {
return element.EnumerateArray() JsonElement { ValueKind: JsonValueKind.Array } element
.Select(attachment => new SnCloudFileReferenceObject => element.EnumerateArray().Select(ConvertJsonElementToObject).ToList(),
List<object> list => list,
_ => null
};
return attachments?.OfType<Dictionary<string, object>>()
.Select(dict => new SnCloudFileReferenceObject
{ {
Id = Guid.NewGuid().ToString(), Id = Guid.NewGuid().ToString(),
Name = attachment.GetProperty("name").GetString() ?? string.Empty, Name = GetStringValue(dict, "name") ?? string.Empty,
Url = attachment.GetProperty("url").GetString(), Url = GetStringValue(dict, "url"),
MimeType = attachment.GetProperty("mediaType").GetString(), MimeType = GetStringValue(dict, "mediaType"),
Width = attachment.GetProperty("width").GetInt32(), Width = TryGetIntValue(dict, "width"),
Height = attachment.GetProperty("height").GetInt32(), Height = TryGetIntValue(dict, "height"),
Blurhash = attachment.GetProperty("blurhash").GetString(), Blurhash = GetStringValue(dict, "blurhash"),
FileMeta = new Dictionary<string, object?>(), FileMeta = new Dictionary<string, object?>(),
UserMeta = new Dictionary<string, object?>(), UserMeta = new Dictionary<string, object?>(),
Size = 0, Size = 0,
@@ -637,26 +664,29 @@ public class ActivityPubActivityHandler(
.ToList(); .ToList();
} }
return null;
}
private static List<ContentMention>? ParseMentions(object? value) private static List<ContentMention>? ParseMentions(object? value)
{ {
if (value is JsonElement { ValueKind: JsonValueKind.Array } element) if (value == null)
return null;
var tags = value switch
{ {
return element.EnumerateArray() JsonElement { ValueKind: JsonValueKind.Array } element
.Where(e => e.GetProperty("type").GetString() == "Mention") => element.EnumerateArray().Select(ConvertJsonElementToObject).ToList(),
.Select(mention => new ContentMention List<object> list => list,
_ => null
};
return tags?.Where(tag => tag is Dictionary<string, object> dict && GetStringValue(dict, "type") == "Mention")
.Select(tag => (Dictionary<string, object>)tag)
.Select(dict => new ContentMention
{ {
Username = mention.GetProperty("name").GetString(), Username = GetStringValue(dict, "name"),
ActorUri = mention.GetProperty("href").GetString() ActorUri = GetStringValue(dict, "href")
}) })
.ToList(); .ToList();
} }
return null;
}
private static Dictionary<string, object> BuildMetadataFromActivity(Dictionary<string, object> objectDict) private static Dictionary<string, object> BuildMetadataFromActivity(Dictionary<string, object> objectDict)
{ {
var metadata = new Dictionary<string, object>(); var metadata = new Dictionary<string, object>();