summary refs log tree commit diff
path: root/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs')
-rw-r--r--extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs79
1 files changed, 63 insertions, 16 deletions
diff --git a/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs b/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs

index 5b76c72d..51ec84e4 100644 --- a/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs +++ b/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs
@@ -1,3 +1,4 @@ +using System.Linq.Expressions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Spacebar.AdminApi.Extensions; @@ -25,17 +26,17 @@ public class UserController( /// </summary> /// <returns>List of user objects</returns> [HttpGet] - public async IAsyncEnumerable<UserModel> Get() { + public async IAsyncEnumerable<UserModel> Get([FromQuery] string? orderBy, [FromQuery] bool ascending = false, [FromQuery] int? skip = null, [FromQuery] int? limit = null) { (await auth.GetCurrentUserAsync(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR); - - var results = db.Users + var query = db.Users + .AsNoTracking() .Include(user => user.ApplicationBotUser) .Include(user => user.MessageAuthors) .Include(user => user.Sessions) .Include(user => user.Templates) .Include(user => user.VoiceStates) .Include(user => user.Guilds) - .AsAsyncEnumerable().Select(x => new UserModel { + .Select(x => new UserModel { Id = x.Id, Username = x.Username, Discriminator = x.Discriminator, @@ -74,7 +75,53 @@ public class UserController( OwnedGuildCount = x.Guilds.Count(g => g.OwnerId == x.Id) }); - await foreach (var user in results) { + if (!string.IsNullOrWhiteSpace(orderBy)) { + query = orderBy switch { + "id" => query.OrderByDescending(x => x.Id), + "username" => query.OrderByDescending(x => x.Username), + "discriminator" => query.OrderByDescending(x => x.Discriminator), + "avatar" => query.OrderByDescending(x => x.Avatar), + "accentColor" => query.OrderByDescending(x => x.AccentColor), + "banner" => query.OrderByDescending(x => x.Banner), + "themeColors" => query.OrderByDescending(x => x.ThemeColors), + "pronouns" => query.OrderByDescending(x => x.Pronouns), + "phone" => query.OrderByDescending(x => x.Phone), + "desktop" => query.OrderByDescending(x => x.Desktop), + "mobile" => query.OrderByDescending(x => x.Mobile), + "premium" => query.OrderByDescending(x => x.Premium), + "premiumType" => query.OrderByDescending(x => x.PremiumType), + "bot" => query.OrderByDescending(x => x.Bot), + "bio" => query.OrderByDescending(x => x.Bio), + "system" => query.OrderByDescending(x => x.System), + "nsfwAllowed" => query.OrderByDescending(x => x.NsfwAllowed), + "mfaEnabled" => query.OrderByDescending(x => x.MfaEnabled), + "webauthnEnabled" => query.OrderByDescending(x => x.WebauthnEnabled), + "createdAt" => query.OrderByDescending(x => x.CreatedAt), + "premiumSince" => query.OrderByDescending(x => x.PremiumSince), + "verified" => query.OrderByDescending(x => x.Verified), + "disabled" => query.OrderByDescending(x => x.Disabled), + "deleted" => query.OrderByDescending(x => x.Deleted), + "email" => query.OrderByDescending(x => x.Email), + "flags" => query.OrderByDescending(x => x.Flags), + "publicFlags" => query.OrderByDescending(x => x.PublicFlags), + "rights" => query.OrderByDescending(x => x.Rights), + "applicationBotUser" => query.OrderByDescending(x => x.ApplicationBotUser), + "connectedAccounts" => query.OrderByDescending(x => x.ConnectedAccounts), + "messageCount" => query.OrderByDescending(x => x.MessageCount), + "sessionCount" => query.OrderByDescending(x => x.SessionCount), + "templateCount" => query.OrderByDescending(x => x.TemplateCount), + "voiceStateCount" => query.OrderByDescending(x => x.VoiceStateCount), + "guildCount" => query.OrderByDescending(x => x.GuildCount), + "ownedGuildCount" => query.OrderByDescending(x => x.OwnedGuildCount), + _ => throw new Exception("orderBy is not oneOf(id)") + }; + if (ascending) query = query.Reverse(); + } + + if (skip.HasValue) query = query.Skip(skip.Value); + if (limit.HasValue) query = query.Take(limit.Value); + + await foreach (var user in query.AsAsyncEnumerable()) { yield return user; } } @@ -170,7 +217,7 @@ public class UserController( yield return new("STATS", new { total_messages = messages.Count(), total_channels = channels.Count, - messages_per_channel = channels.ToDictionary(c => c.ChannelId!, c => messages.Count(m => m.ChannelId == c.ChannelId)) + messages_per_channel = channels.ToDictionary(c => c.ChannelId!.ToString(), c => messages.Count(m => m.ChannelId == c.ChannelId)) }); if (messages.Any()) { var results = channels @@ -190,7 +237,7 @@ public class UserController( // [HttpGet("{id}/Dms")] // public async IEnumerable<object> GetDmsAsync(string userId) { - // yield break; // TODO + // yield break; // TODO // } private async IAsyncEnumerable<AsyncActionResult> DeleteMessagesForChannel( @@ -206,15 +253,15 @@ public class UserController( var remaining = messagesInChannel; while (true) { var messageIds = _db.Database.SqlQuery<long>($""" - DELETE FROM messages - WHERE id IN ( - SELECT id FROM messages - WHERE author_id = {authorId} - AND channel_id = {channelId} - AND guild_id = {guildId} - LIMIT {messageDeleteChunkSize} - ) RETURNING id; - """).ToList(); + DELETE FROM messages + WHERE id IN ( + SELECT id FROM messages + WHERE author_id = {authorId} + AND channel_id = {channelId} + AND guild_id = {guildId} + LIMIT {messageDeleteChunkSize} + ) RETURNING id; + """).ToList(); if (messageIds.Count == 0) { break; }