From 0d5f987a8df130ed46384f44b2ecb4cc967d5889 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 17 Feb 2024 00:48:53 +0100 Subject: [PATCH] [backend/services] Set denormalized note properties replyUserId and replyuserHost correctly --- .../Controllers/Mastodon/StatusController.cs | 11 ++++--- .../Core/Services/NoteService.cs | 29 ++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Iceshrimp.Backend/Controllers/Mastodon/StatusController.cs b/Iceshrimp.Backend/Controllers/Mastodon/StatusController.cs index ef6646e8..7fb92f26 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/StatusController.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/StatusController.cs @@ -130,7 +130,7 @@ public class StatusController( if (idempotencyKey != null) { var hit = await cache.FetchAsync($"idempotency:{idempotencyKey}", TimeSpan.FromHours(24), () => $"_:{HttpContext.TraceIdentifier}"); - + if (hit != $"_:{HttpContext.TraceIdentifier}") { for (var i = 0; i <= 10; i++) { if (!hit.StartsWith('_')) break; @@ -153,8 +153,11 @@ public class StatusController( var visibility = StatusEntity.DecodeVisibility(request.Visibility); var reply = request.ReplyId != null - ? await db.Notes.Where(p => p.Id == request.ReplyId).EnsureVisibleFor(user).FirstOrDefaultAsync() ?? - throw GracefulException.BadRequest("Reply target is nonexistent or inaccessible") + ? await db.Notes.Where(p => p.Id == request.ReplyId) + .IncludeCommonProperties() + .EnsureVisibleFor(user) + .FirstOrDefaultAsync() + ?? throw GracefulException.BadRequest("Reply target is nonexistent or inaccessible") : null; var attachments = request.MediaIds != null @@ -163,7 +166,7 @@ public class StatusController( var note = await noteSvc.CreateNoteAsync(user, visibility, request.Text, request.Cw, reply, attachments: attachments); - + if (idempotencyKey != null) await cache.SetAsync($"idempotency:{idempotencyKey}", note.Id, TimeSpan.FromHours(24)); diff --git a/Iceshrimp.Backend/Core/Services/NoteService.cs b/Iceshrimp.Backend/Core/Services/NoteService.cs index 0d2d0487..e66a6888 100644 --- a/Iceshrimp.Backend/Core/Services/NoteService.cs +++ b/Iceshrimp.Backend/Core/Services/NoteService.cs @@ -65,15 +65,19 @@ public class NoteService( var actor = await userRenderer.RenderAsync(user); var note = new Note { - Id = IdHelpers.GenerateSlowflakeId(), - Text = text, - Cw = cw, - Reply = reply, - Renote = renote, - UserId = user.Id, - CreatedAt = DateTime.UtcNow, - UserHost = null, - Visibility = visibility, + Id = IdHelpers.GenerateSlowflakeId(), + Text = text, + Cw = cw, + Reply = reply, + ReplyUserId = reply?.UserId, + ReplyUserHost = reply?.UserHost, + Renote = renote, + RenoteUserId = renote?.UserId, + RenoteUserHost = renote?.UserHost, + UserId = user.Id, + CreatedAt = DateTime.UtcNow, + UserHost = null, + Visibility = visibility, FileIds = attachments?.Select(p => p.Id).ToList() ?? [], AttachedFileTypes = attachments?.Select(p => p.Type).ToList() ?? [], @@ -182,9 +186,14 @@ public class NoteService( CreatedAt = createdAt, UserHost = user.Host, Visibility = note.GetVisibility(actor), - Reply = note.InReplyTo?.Id != null ? await ResolveNoteAsync(note.InReplyTo.Id) : null + Reply = note.InReplyTo?.Id != null ? await ResolveNoteAsync(note.InReplyTo.Id) : null, }; + if (dbNote.Reply != null) { + dbNote.ReplyUserId = dbNote.Reply.UserId; + dbNote.ReplyUserHost = dbNote.Reply.UserHost; + } + if (dbNote.Text is { Length: > 100000 }) throw GracefulException.UnprocessableEntity("Content cannot be longer than 100.000 characters");