From 9ac2284dd57f1c85a62cda4b2529f61ce9259ae1 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 21 Jun 2024 20:26:30 +0200 Subject: [PATCH] [backend/razor] Refactor queue dashboard job view to better accommodate different job data formats --- .../Core/Extensions/StringExtensions.cs | 7 ++ Iceshrimp.Backend/Pages/QueueJob.cshtml | 111 ++++-------------- Iceshrimp.Backend/Pages/QueueJob.cshtml.cs | 10 ++ 3 files changed, 41 insertions(+), 87 deletions(-) diff --git a/Iceshrimp.Backend/Core/Extensions/StringExtensions.cs b/Iceshrimp.Backend/Core/Extensions/StringExtensions.cs index e15c9e6e..5a044199 100644 --- a/Iceshrimp.Backend/Core/Extensions/StringExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/StringExtensions.cs @@ -26,6 +26,13 @@ public static class StringExtensions { return new IdnMapping().GetUnicode(target); } + + public static string ToTitleCase(this string input) => input switch + { + null => throw new ArgumentNullException(nameof(input)), + "" => throw new ArgumentException(@$"{nameof(input)} cannot be empty", nameof(input)), + _ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1)) + }; } [SuppressMessage("ReSharper", "StringCompareToIsCultureSpecific")] diff --git a/Iceshrimp.Backend/Pages/QueueJob.cshtml b/Iceshrimp.Backend/Pages/QueueJob.cshtml index bfe1eadd..a23dd334 100644 --- a/Iceshrimp.Backend/Pages/QueueJob.cshtml +++ b/Iceshrimp.Backend/Pages/QueueJob.cshtml @@ -1,8 +1,9 @@ @page "/queue/job/{id::guid:required}" @using System.Text.Json +@using System.Text.Json.Nodes +@using System.Text.Json.Serialization @using Iceshrimp.Backend.Core.Database.Tables @using Iceshrimp.Backend.Core.Extensions -@using Iceshrimp.Backend.Core.Queues @model QueueJobModel @{ @@ -117,98 +118,34 @@

Job data

@{ - if (Model.Job.Queue == "inbox") + var dataOpts = new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; + var payloadOpts = new JsonSerializerOptions { WriteIndented = true }; + + if (Model.Lookup.TryGetValue(Model.Job.Queue, out var payloadKey)) { - var data = JsonSerializer.Deserialize(Model.Job.Data) ?? - throw new Exception("Failed to deserialize inbox job data"); + var data = JsonNode.Parse(Model.Job.Data)?.AsObject() ?? + throw new Exception($"Failed to deserialize {Model.Job.Queue} job data"); + var payloadElem = data[payloadKey]; + var payload = payloadElem?.GetValue() ?? + throw new Exception($"Failed to deserialize {Model.Job.Queue} job data"); + var payloadJson = JsonNode.Parse(payload)?.ToJsonString(payloadOpts) ?? + throw new Exception($"Failed to serialize {Model.Job.Queue} job data"); - @if (data.InboxUserId != null || data.AuthenticatedUserId != null) - { - - - @if (data.InboxUserId != null) - { - - - - - } - @if (data.AuthenticatedUserId != null) - { - - - - - } - -
Inbox user id@data.InboxUserId
Authenticated user id@data.AuthenticatedUserId
- } + data.Remove(payloadKey); + foreach (var item in data.Where(p => p.Value?.GetValueKind() is null or JsonValueKind.Null).ToList()) + data.Remove(item.Key); -

Body

- var json = JsonSerializer.Serialize(JsonDocument.Parse(data.Body), new JsonSerializerOptions { WriteIndented = true }); -
@json
- } - else if (Model.Job.Queue == "deliver") - { - var data = JsonSerializer.Deserialize(Model.Job.Data) ?? - throw new Exception("Failed to deserialize deliver job data"); - - - - - - - - - - - - - - - - - - - - -
Inbox URL@data.InboxUrl
Content type@data.ContentType
User ID@data.UserId
Recipient host@data.RecipientHost
- -

Payload

- var json = JsonSerializer.Serialize(JsonDocument.Parse(data.Payload), new JsonSerializerOptions { WriteIndented = true }); -
@json
- } - else if (Model.Job.Queue == "pre-deliver") - { - var data = JsonSerializer.Deserialize(Model.Job.Data) ?? - throw new Exception("Failed to deserialize pre-deliver job data"); - - - - - - - - - - - - - - - - -
Actor ID@data.ActorId
Recipient IDs[@string.Join(", ", data.RecipientIds)]
Deliver to followers@data.DeliverToFollowers
- -

Serialized activity

- var json = JsonSerializer.Serialize(JsonDocument.Parse(data.SerializedActivity), new JsonSerializerOptions { WriteIndented = true }); -
@json
+ var dataJson = data.ToJsonString(dataOpts); +
@dataJson
+

Job payload

+
@payloadJson
} else { - var json = JsonSerializer.Serialize(JsonDocument.Parse(Model.Job.Data), new JsonSerializerOptions { WriteIndented = true }); -
@json
+ var json = JsonNode.Parse(Model.Job.Data)?.ToJsonString(payloadOpts) ?? + throw new Exception($"Failed to serialize {Model.Job.Queue} job data"); +
@json
} } - \ No newline at end of file + \ No newline at end of file diff --git a/Iceshrimp.Backend/Pages/QueueJob.cshtml.cs b/Iceshrimp.Backend/Pages/QueueJob.cshtml.cs index 8b4ba3ee..a6b739d3 100644 --- a/Iceshrimp.Backend/Pages/QueueJob.cshtml.cs +++ b/Iceshrimp.Backend/Pages/QueueJob.cshtml.cs @@ -1,6 +1,7 @@ using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.Core.Middleware; +using Iceshrimp.Backend.Core.Queues; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; @@ -22,4 +23,13 @@ public class QueueJobModel(DatabaseContext db) : PageModel throw GracefulException.NotFound($"Job {id} not found"); return Page(); } + + private static Dictionary _lookup = new() + { + { "inbox", "body" }, + { "deliver", "payload" }, + { "pre-deliver", "serializedActivity" } + }; + + public Dictionary Lookup => _lookup; } \ No newline at end of file