[backend/federation] Fix custom emoji reaction federation with Akkoma (ISH-153)

This commit is contained in:
Laura Hausmann 2024-03-09 02:21:37 +01:00
parent aaec212a08
commit 30818985fb
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 35 additions and 5 deletions

View file

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

View file

@ -46,7 +46,7 @@ public class ASEmoji : ASTag
[J($"{Constants.ActivityStreamsNs}#icon")]
[JC(typeof(ASImageConverter))]
public ASImage? Image { get; set; }
[J($"{Constants.ActivityStreamsNs}#name")]
[JC(typeof(VC))]
public string? Name { get; set; }
@ -54,7 +54,7 @@ public class ASEmoji : ASTag
public sealed class ASTagConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanWrite => true;
public override bool CanConvert(Type objectType)
{
@ -74,7 +74,27 @@ public sealed class ASTagConverter : JsonConverter
if (reader.TokenType == JsonToken.StartArray)
{
var array = JArray.Load(reader);
// We have to use @list here because otherwise Akkoma will refuse to process activities with only one Tag.
// 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>();
foreach (var token in array)
{
@ -105,6 +125,15 @@ public sealed class ASTagConverter : JsonConverter
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
throw new NotImplementedException();
if (value == null)
{
writer.WriteNull();
return;
}
writer.WriteStartObject();
writer.WritePropertyName("@list");
serializer.Serialize(writer, value);
writer.WriteEndObject();
}
}