diff --git a/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs b/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs
index 331b3cdc..7d3a9744 100644
--- a/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs
+++ b/extra/admin-api/Spacebar.AdminApi/Controllers/UserController.cs
@@ -3,13 +3,12 @@ using ArcaneLibs;
using ArcaneLibs.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
-using RabbitMQ.Client;
+using Spacebar.Interop.Replication.Abstractions;
using Spacebar.AdminApi.Extensions;
using Spacebar.Models.AdminApi;
using Spacebar.AdminApi.Services;
using Spacebar.Models.Db.Contexts;
using Spacebar.Models.Db.Models;
-using Spacebar.RabbitMqUtilities;
namespace Spacebar.AdminApi.Controllers;
@@ -18,13 +17,11 @@ namespace Spacebar.AdminApi.Controllers;
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;
-
+ AuthenticationService auth,
+ ISpacebarReplication replication
+) : ControllerBase {
[HttpGet]
public async IAsyncEnumerable<UserModel> Get() {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
@@ -80,87 +77,6 @@ public class UserController(
}
}
- [HttpGet("meow")]
- public async Task Meow() {
- (await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
- Console.WriteLine("meow");
-
- ConnectionFactory factory = new ConnectionFactory();
- factory.Uri = new Uri("amqp://guest:guest@127.0.0.1/");
- using var connection = await factory.CreateConnectionAsync();
- using var channel = await connection.CreateChannelAsync();
-
- // await using var channel = mq.CreateChannel();
- // var channel2 = await channel.CreateChannelAsync();
-
- var body =
- $$"""
- {
- "id": "{{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}}",
- "channel_id": "1322343566206308390",
- "guild_id": "1322343566084673571",
- "author": {
- "username": "test",
- "discriminator": "9177",
- "id": "1322329228934500382",
- "public_flags": 0,
- "avatar": null,
- "accent_color": null,
- "banner": null,
- "bio": "",
- "bot": false,
- "premium_since": "2024-12-27T22:24:15.867Z",
- "premium_type": 2,
- "theme_colors": null,
- "pronouns": null,
- "badge_ids": null
- },
- "member": {
- "index": 2,
- "id": "1322329228934500382",
- "guild_id": "1322343566084673571",
- "nick": null,
- "joined_at": "2024-12-27T23:21:14.396Z",
- "premium_since": null,
- "deaf": false,
- "mute": false,
- "pending": false,
- "last_message_id": "1322346635635753061",
- "joined_by": null,
- "avatar": null,
- "banner": null,
- "bio": "",
- "theme_colors": null,
- "pronouns": null,
- "communication_disabled_until": null,
- "roles": []
- },
- "content": "{{Random.Shared.NextInt64()}}",
- "timestamp": "{{DateTime.UtcNow:O}}",
- "edited_timestamp": null,
- "tts": false,
- "mention_everyone": false,
- "mentions": [],
- "mention_roles": [],
- "attachments": [],
- "embeds": [],
- "reactions": [],
- "nonce": "{{Random.Shared.NextInt64()}}",
- "pinned": false,
- "type": 0
- }
- """
- .AsBytes().ToArray();
-
- await channel.ExchangeDeclareAsync(exchange: "1322343566206308390", type: ExchangeType.Fanout, durable: false);
- var props = new BasicProperties() { Type = "MESSAGE_CREATE" };
- await channel.BasicPublishAsync(exchange: "1322343566206308390", routingKey: "", mandatory: true, basicProperties: props, body: body);
-
- await channel.CloseAsync();
- await connection.CloseAsync();
- Console.WriteLine("meowww");
- }
-
[HttpGet("{id}/delete")]
public async IAsyncEnumerable<AsyncActionResult> DeleteUser(string id, [FromQuery] int messageDeleteChunkSize = 100) {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
@@ -179,12 +95,6 @@ public class UserController(
db.Users.Update(user);
await db.SaveChangesAsync();
- var factory = new ConnectionFactory {
- Uri = new Uri("amqp://guest:guest@127.0.0.1/")
- };
- await using var mqConnection = await factory.CreateConnectionAsync();
- await using var mqChannel = await mqConnection.CreateChannelAsync();
-
var messages = db.Messages
.AsNoTracking()
.Where(m => m.AuthorId == id);
@@ -199,7 +109,7 @@ public class UserController(
});
if (messages.Any()) {
var results = channels
- .Select(ctx => DeleteMessagesForChannel(ctx.GuildId, ctx.ChannelId!, id, mqChannel, messageDeleteChunkSize))
+ .Select(ctx => DeleteMessagesForChannel(ctx.GuildId, ctx.ChannelId!, id, messageDeleteChunkSize))
.ToList();
var a = AggregateAsyncEnumerablesWithoutOrder(results);
await foreach (var result in a) {
@@ -216,15 +126,12 @@ public class UserController(
private async IAsyncEnumerable<AsyncActionResult> DeleteMessagesForChannel(
// context
string? guildId, string channelId, string authorId,
- // connections
- IChannel mqChannel,
// options
int messageDeleteChunkSize = 100
) {
{
await using var ctx = sp.CreateAsyncScope();
await using var _db = ctx.ServiceProvider.GetRequiredService<SpacebarDbContext>();
- await mqChannel.ExchangeDeclareAsync(exchange: channelId!, type: ExchangeType.Fanout, durable: false);
var messagesInChannel = _db.Messages.AsNoTracking().Count(m => m.AuthorId == authorId && m.ChannelId == channelId && m.GuildId == guildId);
var remaining = messagesInChannel;
while (true) {
@@ -242,22 +149,16 @@ public class UserController(
break;
}
- var props = new BasicProperties() { Type = "MESSAGE_BULK_DELETE" };
- var publishSuccess = false;
- do {
- try {
- await mqChannel.BasicPublishAsync(exchange: channelId!, routingKey: "", mandatory: true, basicProperties: props, body: new {
- ids = messageIds,
- channel_id = channelId,
- guild_id = guildId,
- }.ToJson().AsBytes().ToArray());
- publishSuccess = true;
- }
- catch (Exception e) {
- Console.WriteLine($"[RabbitMQ] Error publishing bulk delete: {e.Message}");
- await Task.Delay(10);
- }
- } while (!publishSuccess);
+ await replication.SendAsync(new() {
+ Event = "MESSAGE_BULK_DELETE",
+ ChannelId = channelId,
+ Payload = new {
+ channel_id = channelId,
+ guild_id = guildId,
+ ids = messageIds,
+ },
+ Origin = "AdminApi/DeleteMessagesForChannel"
+ });
yield return new("BULK_DELETED", new {
channel_id = channelId,
@@ -473,21 +374,12 @@ public class UserController(
public async IAsyncEnumerable<string> Test() {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
- var factory = new ConnectionFactory {
- Uri = new Uri(amqpConfig.ToConnectionString())
- };
- await using var mqConnection = await factory.CreateConnectionAsync();
- await using var mqChannel = await mqConnection.CreateChannelAsync();
-
var guildId = "1006649183970562092";
// var roleId = "1006706520514028812"; //Administrator
var roleId = "1391303296148639051"; //Spacebar Maintainer
// int color = 16711680; //Administrator
int color = 99839; //Spacebar Maintainer
- await mqChannel.ExchangeDeclareAsync(exchange: guildId, type: ExchangeType.Fanout, durable: false);
-
- var props = new BasicProperties() { Type = "GUILD_ROLE_UPDATE" };
int framerate = 30;
float delay = 1000f / framerate;
var secondsPerRotation = 6.243f;
@@ -499,32 +391,27 @@ public class UserController(
while (true) {
var clr = re.Next();
color = clr.r << 16 | clr.g << 8 | clr.b;
- var publishSuccess = false;
- do {
- try {
- await mqChannel.BasicPublishAsync(exchange: guildId, routingKey: "", mandatory: false, basicProperties: props, body: new {
+ await replication.SendAsync(new() {
+ Event = "GUILD_ROLE_UPDATE",
+ GuildId = guildId,
+ Origin = "Admin API (GET /users/test)",
+ Payload = new {
+ guild_id = guildId,
+ role = new {
+ id = roleId,
guild_id = guildId,
- role = new {
- id = roleId,
- guild_id = guildId,
- color,
- hoist = false,
- managed = false,
- mentionable = true,
- name = "Spacebar Maintainer",
- permissions = "8",
- position = 5,
- unicode_emoji = "",
- flags = 0
- }
- }.ToJson().AsBytes().ToArray());
- publishSuccess = true;
- }
- catch (Exception e) {
- Console.WriteLine($"[RabbitMQ] Error publishing bulk delete: {e.Message}");
- await Task.Delay(10);
+ color,
+ hoist = false,
+ managed = false,
+ mentionable = true,
+ name = "Spacebar Maintainer",
+ permissions = "8",
+ position = 5,
+ unicode_emoji = "",
+ flags = 0
+ }
}
- } 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));
|