[backend/federation] Correctly serialize value types

This commit is contained in:
Laura Hausmann 2024-02-22 19:04:46 +01:00
parent 0148710e00
commit f895a9e541
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
8 changed files with 36 additions and 11 deletions

View file

@ -103,8 +103,8 @@ public class NoteRenderer(
Renote = renote, Renote = renote,
Quote = quote, Quote = quote,
ContentType = "text/x.misskeymarkdown", ContentType = "text/x.misskeymarkdown",
CreatedAt = note.CreatedAt.ToStringMastodon(), CreatedAt = note.CreatedAt.ToStringIso8601Like(),
EditedAt = note.UpdatedAt?.ToStringMastodon(), EditedAt = note.UpdatedAt?.ToStringIso8601Like(),
RepliesCount = note.RepliesCount, RepliesCount = note.RepliesCount,
RenoteCount = note.RenoteCount, RenoteCount = note.RenoteCount,
FavoriteCount = likeCount, FavoriteCount = likeCount,

View file

@ -37,7 +37,7 @@ public class NotificationRenderer(NoteRenderer noteRenderer, UserRenderer userRe
Type = NotificationEntity.EncodeType(notification.Type), Type = NotificationEntity.EncodeType(notification.Type),
Note = note, Note = note,
Notifier = notifier, Notifier = notifier,
CreatedAt = notification.CreatedAt.ToStringMastodon() CreatedAt = notification.CreatedAt.ToStringIso8601Like()
}; };
return res; return res;

View file

@ -26,7 +26,7 @@ public class UserRenderer(IOptions<Config.InstanceSection> config, MfmConverter
Acct = acct, Acct = acct,
FullyQualifiedName = $"{user.Username}@{user.Host ?? config.Value.AccountDomain}", FullyQualifiedName = $"{user.Username}@{user.Host ?? config.Value.AccountDomain}",
IsLocked = user.IsLocked, IsLocked = user.IsLocked,
CreatedAt = user.CreatedAt.ToStringMastodon(), CreatedAt = user.CreatedAt.ToStringIso8601Like(),
FollowersCount = user.FollowersCount, FollowersCount = user.FollowersCount,
FollowingCount = user.FollowingCount, FollowingCount = user.FollowingCount,
StatusesCount = user.NotesCount, StatusesCount = user.NotesCount,

View file

@ -5,6 +5,7 @@ public static class Constants
public const string ActivityStreamsNs = "https://www.w3.org/ns/activitystreams"; public const string ActivityStreamsNs = "https://www.w3.org/ns/activitystreams";
public const string W3IdSecurityNs = "https://w3id.org/security"; public const string W3IdSecurityNs = "https://w3id.org/security";
public const string PurlDcNs = "http://purl.org/dc/terms"; public const string PurlDcNs = "http://purl.org/dc/terms";
public const string XsdNs = "http://www.w3.org/2001/XMLSchema";
public static readonly string[] SystemUsers = ["instance.actor", "relay.actor"]; public static readonly string[] SystemUsers = ["instance.actor", "relay.actor"];
public static readonly string[] BrowserSafeMimeTypes = public static readonly string[] BrowserSafeMimeTypes =

View file

@ -2,7 +2,7 @@ namespace Iceshrimp.Backend.Core.Extensions;
public static class DateTimeExtensions public static class DateTimeExtensions
{ {
public static string ToStringMastodon(this DateTime dateTime) public static string ToStringIso8601Like(this DateTime dateTime)
{ {
return dateTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK"); return dateTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK");
} }

View file

@ -20,7 +20,7 @@ public class ASCollection<T>() : ASObjectBase where T : ASObject
[J($"{Constants.ActivityStreamsNs}#totalItems")] [J($"{Constants.ActivityStreamsNs}#totalItems")]
[JC(typeof(VC))] [JC(typeof(VC))]
public long? TotalItems { get; set; } public uint? TotalItems { get; set; }
} }
public sealed class ASCollectionConverter : ASSerializer.ListSingleObjectConverter<ASCollection<ASObject>>; public sealed class ASCollectionConverter : ASSerializer.ListSingleObjectConverter<ASCollection<ASObject>>;

View file

@ -1,3 +1,5 @@
using Iceshrimp.Backend.Core.Configuration;
using Iceshrimp.Backend.Core.Extensions;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using J = Newtonsoft.Json.JsonPropertyAttribute; using J = Newtonsoft.Json.JsonPropertyAttribute;
@ -12,7 +14,7 @@ public class LDValueObject<T>
public class ValueObjectConverter : JsonConverter public class ValueObjectConverter : JsonConverter
{ {
public override bool CanWrite => false; public override bool CanWrite => true;
public override bool CanConvert(Type objectType) public override bool CanConvert(Type objectType)
{ {
@ -43,6 +45,28 @@ public class ValueObjectConverter : JsonConverter
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{ {
throw new NotImplementedException(); writer.WriteStartObject();
switch (value)
{
case DateTime dt:
writer.WritePropertyName("@type");
writer.WriteValue($"{Constants.XsdNs}#dateTime");
writer.WritePropertyName("@value");
writer.WriteValue(dt.ToStringIso8601Like());
break;
case uint ui:
writer.WritePropertyName("@type");
writer.WriteValue($"{Constants.XsdNs}#nonNegativeInteger");
writer.WritePropertyName("@value");
writer.WriteValue(ui);
break;
default:
writer.WritePropertyName("@value");
writer.WriteValue(value);
break;
}
writer.WriteEndObject();
} }
} }

View file

@ -219,7 +219,7 @@ public class JobQueue<T>(
else else
{ {
logger.LogTrace("Job in queue {queue} completed after {duration} ms, has been queued since {time}", logger.LogTrace("Job in queue {queue} completed after {duration} ms, has been queued since {time}",
name, job.Duration, job.QueuedAt.ToStringMastodon()); name, job.Duration, job.QueuedAt.ToStringIso8601Like());
} }
} }
@ -236,8 +236,8 @@ public class JobQueue<T>(
job.DelayedUntil ??= DateTime.Now; job.DelayedUntil ??= DateTime.Now;
var logger = scope.ServiceProvider.GetRequiredService<ILogger<QueueService>>(); var logger = scope.ServiceProvider.GetRequiredService<ILogger<QueueService>>();
logger.LogTrace("Job in queue {queue} was delayed to {time} after {duration} ms, has been queued since {time}", logger.LogTrace("Job in queue {queue} was delayed to {time} after {duration} ms, has been queued since {time}",
name, job.DelayedUntil.Value.ToStringMastodon(), job.Duration, name, job.DelayedUntil.Value.ToStringIso8601Like(), job.Duration,
job.QueuedAt.ToStringMastodon()); job.QueuedAt.ToStringIso8601Like());
var timestamp = (long)job.DelayedUntil.Value.Subtract(DateTime.UnixEpoch).TotalSeconds; var timestamp = (long)job.DelayedUntil.Value.Subtract(DateTime.UnixEpoch).TotalSeconds;
await _redisDb.SortedSetAddAsync(targetQueue, RedisValue.Unbox(RedisHelpers.Serialize(job)), timestamp); await _redisDb.SortedSetAddAsync(targetQueue, RedisValue.Unbox(RedisHelpers.Serialize(job)), timestamp);