[backend/federation] Use dotNetRdf ForceArray option instead of patching the AS context (ISH-153)

This commit is contained in:
Laura Hausmann 2024-03-10 23:46:23 +01:00
parent 46140b3ace
commit 7c0e6dfea1
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 9 additions and 35 deletions

View file

@ -200,8 +200,7 @@
}, },
"tag": { "tag": {
"@id": "as:tag", "@id": "as:tag",
"@type": "@id", "@type": "@id"
"@container": "@list"
}, },
"target": { "target": {
"@id": "as:target", "@id": "as:target",

View file

@ -1,3 +1,4 @@
using Iceshrimp.Backend.Core.Configuration;
using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Database.Tables;
using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types; using Iceshrimp.Backend.Core.Federation.ActivityStreams.Types;
using Iceshrimp.Backend.Core.Federation.Cryptography; using Iceshrimp.Backend.Core.Federation.Cryptography;
@ -60,12 +61,15 @@ public static class LdHelpers
JToken.Parse(File.ReadAllText(Path.Combine("Core", "Federation", "ActivityStreams", "Contexts", JToken.Parse(File.ReadAllText(Path.Combine("Core", "Federation", "ActivityStreams", "Contexts",
"as-extensions.json"))); "as-extensions.json")));
private static IEnumerable<string> ASForceArray => ["tag", "to", "cc", "bcc", "bto"];
private static readonly JsonLdProcessorOptions Options = new() private static readonly JsonLdProcessorOptions Options = new()
{ {
DocumentLoader = CustomLoader, DocumentLoader = CustomLoader,
ExpandContext = ASExtensions, ExpandContext = ASExtensions,
ProcessingMode = JsonLdProcessingMode.JsonLd11, ProcessingMode = JsonLdProcessingMode.JsonLd11,
KeepIRIs = ["https://www.w3.org/ns/activitystreams#Public"] KeepIRIs = [$"{Constants.ActivityStreamsNs}#Public"],
ForceArray = ASForceArray.Select(p => $"{Constants.ActivityStreamsNs}#{p}").ToList()
}; };
public static readonly JsonSerializerSettings JsonSerializerSettings = new() public static readonly JsonSerializerSettings JsonSerializerSettings = new()

View file

@ -54,7 +54,7 @@ public class ASEmoji : ASTag
public sealed class ASTagConverter : JsonConverter public sealed class ASTagConverter : JsonConverter
{ {
public override bool CanWrite => true; public override bool CanWrite => false;
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)
{ {
@ -74,27 +74,7 @@ public sealed class ASTagConverter : JsonConverter
if (reader.TokenType == JsonToken.StartArray) if (reader.TokenType == JsonToken.StartArray)
{ {
// We have to use @list here because otherwise Akkoma will refuse to process activities with only one Tag. var array = JArray.Load(reader);
// To solve this, we've patched the AS2 context to render & parse the tag type as @list, forcing it to be an array.
// Ref: https://akkoma.dev/AkkomaGang/akkoma/issues/720
var array = JArray.Load(reader);
if (array.Count > 0)
{
try
{
return array.SelectToken("$.[*].@list")
?.Children()
.OfType<JObject>()
.Select(HandleObject)
.OfType<ASTag>()
.ToList();
}
catch
{
//ignored
}
}
var result = new List<ASTag>(); var result = new List<ASTag>();
foreach (var token in array) foreach (var token in array)
{ {
@ -125,15 +105,6 @@ public sealed class ASTagConverter : JsonConverter
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{ {
if (value == null) throw new NotImplementedException();
{
writer.WriteNull();
return;
}
writer.WriteStartObject();
writer.WritePropertyName("@list");
serializer.Serialize(writer, value);
writer.WriteEndObject();
} }
} }