diff --git a/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs b/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs
index 505f789e..331b3cdc 100644
--- a/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs
+++ b/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs
@@ -5,62 +5,76 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RabbitMQ.Client;
using Spacebar.AdminApi.Extensions;
-using Spacebar.AdminApi.Models;
+using Spacebar.Models.AdminApi;
using Spacebar.AdminApi.Services;
-using Spacebar.Db.Contexts;
-using Spacebar.Db.Models;
+using Spacebar.Models.Db.Contexts;
+using Spacebar.Models.Db.Models;
using Spacebar.RabbitMqUtilities;
namespace Spacebar.AdminApi.Controllers;
[ApiController]
[Route("/users")]
-public class UserController(ILogger<UserController> logger, Configuration config, RabbitMQConfiguration amqpConfig, SpacebarDbContext db, RabbitMQService mq, IServiceProvider sp, AuthenticationService auth) : ControllerBase {
+public class UserController(
+ ILogger<UserController> logger,
+ Configuration config,
+ RabbitMQConfiguration amqpConfig,
+ SpacebarDbContext db,
+ RabbitMQService mq,
+ IServiceProvider sp,
+ AuthenticationService auth) : ControllerBase {
private readonly ILogger<UserController> _logger = logger;
[HttpGet]
public async IAsyncEnumerable<UserModel> Get() {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
- var results = db.Users.Select(x => new UserModel {
- Id = x.Id,
- Username = x.Username,
- Discriminator = x.Discriminator,
- Avatar = x.Avatar,
- AccentColor = x.AccentColor,
- Banner = x.Banner,
- ThemeColors = x.ThemeColors,
- Pronouns = x.Pronouns,
- Phone = x.Phone,
- Desktop = x.Desktop,
- Mobile = x.Mobile,
- Premium = x.Premium,
- PremiumType = x.PremiumType,
- Bot = x.Bot,
- Bio = x.Bio,
- System = x.System,
- NsfwAllowed = x.NsfwAllowed,
- MfaEnabled = x.MfaEnabled,
- WebauthnEnabled = x.WebauthnEnabled,
- CreatedAt = x.CreatedAt,
- PremiumSince = x.PremiumSince,
- Verified = x.Verified,
- Disabled = x.Disabled,
- Deleted = x.Deleted,
- Email = x.Email,
- Flags = x.Flags,
- PublicFlags = x.PublicFlags,
- Rights = x.Rights,
- ApplicationBotUser = x.ApplicationBotUser == null ? null : new(),
- ConnectedAccounts = new List<UserModel.ConnectedAccountModel>(),
- MessageCount = x.MessageAuthors.Count, // This property is weirdly named due to scaffolding, might patch later
- SessionCount = x.Sessions.Count,
- TemplateCount = x.Templates.Count,
- VoiceStateCount = x.VoiceStates.Count,
- GuildCount = x.Guilds.Count,
- OwnedGuildCount = x.Guilds.Count(g => g.OwnerId == x.Id)
- }).AsAsyncEnumerable();
-
+
+ var results = db.Users
+ .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 {
+ Id = x.Id,
+ Username = x.Username,
+ Discriminator = x.Discriminator,
+ Avatar = x.Avatar,
+ AccentColor = x.AccentColor,
+ Banner = x.Banner,
+ ThemeColors = x.ThemeColors,
+ Pronouns = x.Pronouns,
+ Phone = x.Phone,
+ Desktop = x.Desktop,
+ Mobile = x.Mobile,
+ Premium = x.Premium,
+ PremiumType = x.PremiumType,
+ Bot = x.Bot,
+ Bio = x.Bio,
+ System = x.System,
+ NsfwAllowed = x.NsfwAllowed,
+ MfaEnabled = x.MfaEnabled,
+ WebauthnEnabled = x.WebauthnEnabled,
+ CreatedAt = x.CreatedAt,
+ PremiumSince = x.PremiumSince,
+ Verified = x.Verified,
+ Disabled = x.Disabled,
+ Deleted = x.Deleted,
+ Email = x.Email,
+ Flags = x.Flags,
+ PublicFlags = x.PublicFlags,
+ Rights = x.Rights,
+ ApplicationBotUser = x.ApplicationBotUser == null ? null : new(),
+ ConnectedAccounts = new List<UserModel.ConnectedAccountModel>(),
+ MessageCount = x.MessageAuthors.Count, // This property is weirdly named due to scaffolding, might patch later
+ SessionCount = x.Sessions.Count,
+ TemplateCount = x.Templates.Count,
+ VoiceStateCount = x.VoiceStates.Count,
+ GuildCount = x.Guilds.Count,
+ OwnedGuildCount = x.Guilds.Count(g => g.OwnerId == x.Id)
+ });
+
await foreach (var user in results) {
yield return user;
}
@@ -150,7 +164,7 @@ public class UserController(ILogger<UserController> logger, Configuration config
[HttpGet("{id}/delete")]
public async IAsyncEnumerable<AsyncActionResult> DeleteUser(string id, [FromQuery] int messageDeleteChunkSize = 100) {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
+
var user = await db.Users.FindAsync(id);
if (user == null) {
Console.WriteLine($"User {id} not found");
@@ -259,7 +273,7 @@ public class UserController(ILogger<UserController> logger, Configuration config
[HttpGet("duplicate")]
public async Task<IActionResult> Duplicate() {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
+
var msg = db.Messages.First();
var channels = db.Channels.Select(x => new { x.Id, x.GuildId }).ToList();
int count = 1;
@@ -304,7 +318,7 @@ public class UserController(ILogger<UserController> logger, Configuration config
[HttpGet("duplicate/{id}")]
public async Task<IActionResult> DuplicateMessage(ulong id, [FromQuery] int count = 100) {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
+
var msg = await db.Messages.FindAsync(id.ToString());
int createdCount = 1;
while (true) {
@@ -350,7 +364,7 @@ public class UserController(ILogger<UserController> logger, Configuration config
[HttpGet("truncate_messages")]
public async Task TruncateMessages() {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
+
var channels = db.Channels.Select(x => new { x.Id, x.GuildId }).ToList();
var ss = new SemaphoreSlim(12, 12);
@@ -363,11 +377,11 @@ public class UserController(ILogger<UserController> logger, Configuration config
// set timeout
_db.Database.SetCommandTimeout(6000);
await _db.Database.ExecuteSqlAsync($"""
- DELETE FROM messages
- WHERE channel_id = '{channelId}'
- AND guild_id = '{guildId}'
- AND id LIKE '%{i:00}';
- """);
+ DELETE FROM messages
+ WHERE channel_id = '{channelId}'
+ AND guild_id = '{guildId}'
+ AND id LIKE '%{i:00}';
+ """);
Console.WriteLine($"Truncated messages for {channelId} in {guildId} ending with {i}");
})).ToList();
@@ -381,7 +395,7 @@ public class UserController(ILogger<UserController> logger, Configuration config
private async IAsyncEnumerable<T> AggregateAsyncEnumerablesWithoutOrder<T>(params IEnumerable<IAsyncEnumerable<T>> enumerables) {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
+
var enumerators = enumerables.Select(e => e.GetAsyncEnumerator()).ToList();
var tasks = enumerators.Select(e => e.MoveNextAsync().AsTask()).ToList();
@@ -434,31 +448,31 @@ public class UserController(ILogger<UserController> logger, Configuration config
}
// {
- // "op": 0,
- // "t": "GUILD_ROLE_UPDATE",
- // "d": {
- // "guild_id": "1006649183970562092",
- // "role": {
- // "id": "1006706520514028812",
- // "guild_id": "1006649183970562092",
- // "color": 16711680,
- // "hoist": true,
- // "managed": false,
- // "mentionable": true,
- // "name": "Adminstrator",
- // "permissions": "9",
- // "position": 5,
- // "unicode_emoji": "💖",
- // "flags": 0
- // }
- // },
- // "s": 38
+ // "op": 0,
+ // "t": "GUILD_ROLE_UPDATE",
+ // "d": {
+ // "guild_id": "1006649183970562092",
+ // "role": {
+ // "id": "1006706520514028812",
+ // "guild_id": "1006649183970562092",
+ // "color": 16711680,
+ // "hoist": true,
+ // "managed": false,
+ // "mentionable": true,
+ // "name": "Adminstrator",
+ // "permissions": "9",
+ // "position": 5,
+ // "unicode_emoji": "💖",
+ // "flags": 0
+ // }
+ // },
+ // "s": 38
// }
[HttpGet("test")]
public async IAsyncEnumerable<string> Test() {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
+
var factory = new ConnectionFactory {
Uri = new Uri(amqpConfig.ToConnectionString())
};
@@ -511,9 +525,10 @@ public class UserController(ILogger<UserController> logger, Configuration config
await Task.Delay(10);
}
} while (!publishSuccess);
+
yield return $"{clr.r:X2} {clr.g:X2} {clr.b:X2} | {color:X8} | {sw.Elapsed} (waiting {Math.Max(0, (int)delay - (int)sw.ElapsedMilliseconds)} out of {delay} ms)";
await Task.Delay(Math.Max(0, (int)delay - (int)sw.ElapsedMilliseconds));
sw.Restart();
}
}
-}
+}
\ No newline at end of file
|