diff --git a/extra/admin-api/Spacebar.AdminApi/Controllers/GuildController.cs b/extra/admin-api/Spacebar.AdminApi/Controllers/GuildController.cs
index a7a623f2..dc2f582f 100644
--- a/extra/admin-api/Spacebar.AdminApi/Controllers/GuildController.cs
+++ b/extra/admin-api/Spacebar.AdminApi/Controllers/GuildController.cs
@@ -1,25 +1,30 @@
-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;
[ApiController]
[Route("/Guilds")]
-public class GuildController(ILogger<GuildController> logger, Configuration config, RabbitMQConfiguration amqpConfig, SpacebarDbContext db, RabbitMQService mq, IServiceProvider sp, AuthenticationService auth) : ControllerBase {
+public class GuildController(
+ ILogger<GuildController> logger,
+ Configuration config,
+ SpacebarDbContext db,
+ IServiceProvider sp,
+ AuthenticationService auth,
+ ISpacebarReplication replication
+) : ControllerBase {
private readonly ILogger<GuildController> _logger = logger;
[HttpGet]
public async IAsyncEnumerable<GuildModel> Get() {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
+
var results = db.Guilds.Select(x => new GuildModel {
Id = x.Id,
AfkChannelId = x.AfkChannelId,
@@ -74,22 +79,22 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
yield return result;
}
}
-
+
[HttpPost("{id}/force_join")]
public async Task<IActionResult> ForceJoinGuild([FromBody] ForceJoinRequest request, string id) {
(await auth.GetCurrentUser(Request)).GetRights().AssertHasAllRights(SpacebarRights.Rights.OPERATOR);
-
+
var guild = await db.Guilds.FindAsync(id);
if (guild == null) {
return NotFound(new { entity = "Guild", id, message = "Guild not found" });
}
-
+
var userId = request.UserId ?? config.OverrideUid ?? (await auth.GetCurrentUser(Request)).Id;
var user = await db.Users.FindAsync(userId);
if (user == null) {
return NotFound(new { entity = "User", id = userId, message = "User not found" });
}
-
+
var member = await db.Members.SingleOrDefaultAsync(m => m.GuildId == id && m.Id == userId);
if (member is null) {
member = new Member {
@@ -105,13 +110,14 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
db.Guilds.Update(guild);
await db.SaveChangesAsync();
}
-
+
if (request.MakeOwner) {
guild.OwnerId = userId;
db.Guilds.Update(guild);
await db.SaveChangesAsync();
- } else if (request.MakeAdmin) {
- var roles = await db.Roles.Where(r => r.GuildId == id).OrderBy(x=>x.Position).ToListAsync();
+ }
+ else if (request.MakeAdmin) {
+ var roles = await db.Roles.Where(r => r.GuildId == id).OrderBy(x => x.Position).ToListAsync();
var adminRole = roles.FirstOrDefault(r => r.Permissions == "8" || r.Permissions == "9"); // Administrator
if (adminRole == null) {
adminRole = new Role {
@@ -120,7 +126,7 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
Name = "Instance administrator",
Color = 0,
Hoist = false,
- Position = roles.Max(x=>x.Position) + 1,
+ Position = roles.Max(x => x.Position) + 1,
Permissions = "8", // Administrator
Managed = false,
Mentionable = false
@@ -135,16 +141,16 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
await db.SaveChangesAsync();
}
}
-
+
// TODO: gateway events
-
+
return Ok(new { entity = "Guild", id, message = "Guild join forced" });
}
[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");
@@ -159,12 +165,6 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
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);
@@ -178,7 +178,7 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
messages_per_channel = channels.ToDictionary(c => c.ChannelId, c => messages.Count(m => m.ChannelId == c.ChannelId))
});
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) {
@@ -192,15 +192,12 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
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) {
@@ -218,22 +215,16 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
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() {
+ ChannelId = channelId,
+ Event = "MESSAGE_BULK_DELETE",
+ Payload = new {
+ ids = messageIds,
+ channel_id = channelId,
+ guild_id = guildId,
+ },
+ Origin = "Admin API (GuildController.DeleteUser)",
+ });
yield return new("BULK_DELETED", new {
channel_id = channelId,
@@ -297,27 +288,4 @@ public class GuildController(ILogger<GuildController> logger, Configuration conf
}
}
}
-
- // {
- // "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
- // }
-
}
\ No newline at end of file
|