summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.AdminApi/Controllers/TestControllers/EmptyDmsController.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-02-20 03:18:44 +0100
committerRory& <root@rory.gay>2026-02-20 03:20:33 +0100
commita06e981150359c9765b229c297671f75fc2e4704 (patch)
tree77bd7b7853a30305fd1b3c1c4e4243beb1e8a77f /extra/admin-api/Spacebar.AdminApi/Controllers/TestControllers/EmptyDmsController.cs
parentC# deps (diff)
downloadserver-ts-a06e981150359c9765b229c297671f75fc2e4704.tar.xz
Admin API work
Diffstat (limited to 'extra/admin-api/Spacebar.AdminApi/Controllers/TestControllers/EmptyDmsController.cs')
-rw-r--r--extra/admin-api/Spacebar.AdminApi/Controllers/TestControllers/EmptyDmsController.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/extra/admin-api/Spacebar.AdminApi/Controllers/TestControllers/EmptyDmsController.cs b/extra/admin-api/Spacebar.AdminApi/Controllers/TestControllers/EmptyDmsController.cs
new file mode 100644

index 000000000..862585b33 --- /dev/null +++ b/extra/admin-api/Spacebar.AdminApi/Controllers/TestControllers/EmptyDmsController.cs
@@ -0,0 +1,54 @@ +using System.Diagnostics; +using ArcaneLibs; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Spacebar.AdminApi.Extensions; +using Spacebar.Interop.Authentication; +using Spacebar.Interop.Authentication.AspNetCore; +using Spacebar.Interop.Replication.Abstractions; +using Spacebar.Models.AdminApi; +using Spacebar.Models.Db.Contexts; + +namespace Spacebar.AdminApi.Controllers.TestControllers; + +[ApiController] +public class EmptyDmsController( + ILogger<EmptyDmsController> logger, + SpacebarAuthenticationConfiguration config, + SpacebarDbContext db, + IServiceProvider sp, + SpacebarAspNetAuthenticationService auth, + ISpacebarReplication replication +) : ControllerBase { + [HttpGet("emptydms")] + public async IAsyncEnumerable<object> GetEmptyDms() { + (await auth.GetCurrentUserAsync(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR); + + // TODO channel type enum + var channels = db.Channels + .Include(x=>x.Recipients) + .Include(x=>x.Messages) + .Where(x => x.Type == 1) + .Where(x => !x.Messages.Any() && x.Recipients.Count == 1) + ; + + await using var db2Scope = sp.CreateAsyncScope(); + await using var db3Scope = sp.CreateAsyncScope(); + var db2 = db2Scope.ServiceProvider.GetRequiredService<SpacebarDbContext>(); + var db3 = db3Scope.ServiceProvider.GetRequiredService<SpacebarDbContext>(); + int count = 0; + await foreach (var channel in channels.AsAsyncEnumerable()) { + count++; + yield return new { + id = channel.Id, + msgs = await db2.Messages.Where(x => x.ChannelId == channel.Id).CountAsync(), + recips = await db3.Recipients.Where(x => x.ChannelId == channel.Id).CountAsync() + }; + } + + logger.LogInformation("Got {count} empty DM channels", count); + + yield break; + + } +} \ No newline at end of file