diff --git a/Iceshrimp.Backend/Core/Database/Tables/Note.cs b/Iceshrimp.Backend/Core/Database/Tables/Note.cs index 9a170fc8..8f342d5e 100644 --- a/Iceshrimp.Backend/Core/Database/Tables/Note.cs +++ b/Iceshrimp.Backend/Core/Database/Tables/Note.cs @@ -230,6 +230,9 @@ public class Note : IEntity { [InverseProperty(nameof(InverseReply))] public virtual Note? Reply { get; set; } + [NotMapped] [Projectable] public bool IsPureRenote => RenoteId != null && !IsQuote; + [NotMapped] [Projectable] public bool IsQuote => RenoteId != null && (Text != null || HasPoll || FileIds.Count > 0); + [ForeignKey("UserId")] [InverseProperty(nameof(Tables.User.Notes))] public virtual User User { get; set; } = null!; diff --git a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs index b2a25870..27d92dc8 100644 --- a/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs +++ b/Iceshrimp.Backend/Core/Extensions/ServiceExtensions.cs @@ -32,9 +32,10 @@ public static class ServiceExtensions { .AddScoped() .AddScoped() .AddScoped() - .AddScoped() .AddScoped() .AddScoped() + .AddScoped() + .AddScoped() .AddScoped() .AddScoped() .AddScoped() diff --git a/Iceshrimp.Backend/Core/Services/DatabaseMaintenanceService.cs b/Iceshrimp.Backend/Core/Services/DatabaseMaintenanceService.cs new file mode 100644 index 00000000..1e411940 --- /dev/null +++ b/Iceshrimp.Backend/Core/Services/DatabaseMaintenanceService.cs @@ -0,0 +1,23 @@ +using Iceshrimp.Backend.Core.Database; +using Microsoft.EntityFrameworkCore; + +namespace Iceshrimp.Backend.Core.Services; + +public class DatabaseMaintenanceService(DatabaseContext db) { + public async Task RecomputeNoteCountersAsync() { + await db.Notes.ExecuteUpdateAsync(p => p.SetProperty(n => n.RenoteCount, + n => db.Notes.Count(r => r.IsPureRenote)) + .SetProperty(n => n.RepliesCount, + n => db.Notes.Count(r => r.Reply == n))); + //TODO: update reaction counts as well? (can likely not be done database-side :/) + } + + public async Task RecomputeUserCountersAsync() { + await db.Users.ExecuteUpdateAsync(p => p.SetProperty(u => u.FollowersCount, + u => db.Followings.Count(f => f.Followee == u)) + .SetProperty(u => u.FollowingCount, + u => db.Followings.Count(f => f.Follower == u)) + .SetProperty(u => u.NotesCount, + u => db.Notes.Count(n => n.User == u && !n.IsPureRenote))); + } +} \ No newline at end of file