diff --git a/extra/admin-api/DataMappings/Spacebar.DataMappings.Generic/Member.cs b/extra/admin-api/DataMappings/Spacebar.DataMappings.Generic/Member.cs index 2650fc383..2a61b75ae 100644 --- a/extra/admin-api/DataMappings/Spacebar.DataMappings.Generic/Member.cs +++ b/extra/admin-api/DataMappings/Spacebar.DataMappings.Generic/Member.cs @@ -2,9 +2,9 @@ using Spacebar.Models.Generic; namespace Spacebar.DataMappings.Generic; -public static class Member +public static class MemberExtensions { - public static Models.Generic.Member ToPublicMember(this Models.Db.Models.Member member, PartialUser? partialUser = null) + public static Member ToPublicMember(this Models.Db.Models.Member member, PartialUser? partialUser = null) { return new() { @@ -15,7 +15,8 @@ public static class Member Collectibles = member.Collectibles, DisplayNameStyles = member.DisplayNameStyles, Bio = string.IsNullOrWhiteSpace(member.Bio) ? null : member.Bio, - Nick = string.IsNullOrWhiteSpace(member.Nick) ? null : member.Nick + Nick = string.IsNullOrWhiteSpace(member.Nick) ? null : member.Nick, + Roles = member.Roles.Select(x=>x.Id).ToList() }; } } \ No newline at end of file diff --git a/extra/admin-api/Models/Spacebar.Models.Generic/Member.cs b/extra/admin-api/Models/Spacebar.Models.Generic/Member.cs index cb47dc664..9ead95595 100644 --- a/extra/admin-api/Models/Spacebar.Models.Generic/Member.cs +++ b/extra/admin-api/Models/Spacebar.Models.Generic/Member.cs @@ -28,6 +28,9 @@ public class Member { [JsonPropertyName("bio")] public string? Bio { get; set; } + + [JsonPropertyName("roles")] + public List? Roles { get; set; } } // Unsure if this is used anywhere outside of op14...? diff --git a/extra/admin-api/Spacebar.UApi/Controllers/GuildMembersController.cs b/extra/admin-api/Spacebar.UApi/Controllers/GuildMembersController.cs new file mode 100644 index 000000000..ac8c1d40c --- /dev/null +++ b/extra/admin-api/Spacebar.UApi/Controllers/GuildMembersController.cs @@ -0,0 +1,36 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Spacebar.Interop.Authentication.AspNetCore; +using Spacebar.Models.Db.Contexts; +using Spacebar.DataMappings.Generic; +using Spacebar.Models.Generic; + +namespace Spacebar.UApi.Controllers; + +[Route("/api/v{_}/guilds/{guildId}/members/")] +public class GuildMembersController(ILogger logger, SpacebarDbContext db, SpacebarAspNetAuthenticationService authService) : ControllerBase { + /// + /// Get a guild member by ID + /// + /// Guild ID + /// Member ID + /// The public member projection + /// An error has occured + [HttpGet("{memberId}")] + public async Task GetMemberAsync(string guildId, string memberId) { + var user = await authService.GetCurrentUserAsync(Request); + var cmember = db.Members.SingleOrDefault(x => x.Id == user.Id && x.GuildId == guildId); + if (cmember is null) + throw new InvalidOperationException("You are not a member of this guild."); + + var member = db.Members + .Include(x => x.IdNavigation) // User object + .Include(x => x.Roles) + .SingleOrDefault(x => x.Id == user.Id && x.GuildId == guildId); + + if (member is null) + throw new InvalidOperationException("Member not found"); + + return member.ToPublicMember(); + } +} \ No newline at end of file diff --git a/extra/admin-api/Spacebar.UApi/Program.cs b/extra/admin-api/Spacebar.UApi/Program.cs new file mode 100644 index 000000000..4695b7abc --- /dev/null +++ b/extra/admin-api/Spacebar.UApi/Program.cs @@ -0,0 +1,83 @@ +using System.Text.Json.Serialization; +using ArcaneLibs; +using Microsoft.EntityFrameworkCore; +using Spacebar.Interop.Authentication; +using Spacebar.Interop.Authentication.AspNetCore; +using Spacebar.Models.Db.Contexts; + +var builder = WebApplication.CreateBuilder(args); +if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("APPSETTINGS_PATH"))) + builder.Configuration.AddJsonFile(Environment.GetEnvironmentVariable("APPSETTINGS_PATH")!); + +// Add services to the container. + +builder.Services.AddControllers(options => { + options.MaxValidationDepth = null; + // options.MaxIAsyncEnumerableBufferLimit = 1; +}).AddJsonOptions(options => { + options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; + options.JsonSerializerOptions.WriteIndented = true; + options.JsonSerializerOptions.MaxDepth = 100; + // options.JsonSerializerOptions.DefaultBufferSize = ; +}).AddMvcOptions(o => { o.SuppressOutputFormatterBuffering = true; }); +// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi +builder.Services.AddOpenApi(); + +builder.Services.AddDbContextPool(options => { + options + .UseNpgsql(builder.Configuration.GetConnectionString("Spacebar")) + .EnableDetailedErrors(); +}); + +builder.Services.AddSingleton(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) { + app.MapOpenApi(); +} + +app.UseAuthorization(); + +app.MapControllers(); +app.Use((context, next) => { + context.Response.Headers["Access-Control-Allow-Origin"] = "*"; + context.Response.Headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS"; + context.Response.Headers["Access-Control-Allow-Headers"] = "*, Authorization"; + if (context.Request.Method == "OPTIONS") { + context.Response.StatusCode = 200; + return Task.CompletedTask; + } + + return next(); +}); + +// fallback to proxy in case we dont have a specific endpoint... +// TODO config +app.MapFallback("{*_}", async context => { + var client = new StreamingHttpClient(); + var requestMessage = new HttpRequestMessage( + new HttpMethod(context.Request.Method), + "http://api.old.server.spacebar.chat" + context.Request.Path + context.Request.QueryString + ) { + Content = new StreamContent(context.Request.Body) + }; + Console.WriteLine(requestMessage.RequestUri); + + foreach (var header in context.Request.Headers) + if (header.Key is not ("Accept-Encoding" or "Host")) + requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray()); + + var responseMessage = await client.SendUnhandledAsync(requestMessage, CancellationToken.None); + context.Response.StatusCode = (int)responseMessage.StatusCode; + + foreach (var header in responseMessage.Headers) context.Response.Headers[header.Key] = header.Value.ToArray(); + foreach (var header in responseMessage.Content.Headers) context.Response.Headers[header.Key] = header.Value.ToArray(); + + await responseMessage.Content.CopyToAsync(context.Response.Body); +}); + +app.Run(); \ No newline at end of file diff --git a/extra/admin-api/Spacebar.UApi/Properties/launchSettings.json b/extra/admin-api/Spacebar.UApi/Properties/launchSettings.json new file mode 100644 index 000000000..6871efac7 --- /dev/null +++ b/extra/admin-api/Spacebar.UApi/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5085", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/extra/admin-api/Spacebar.UApi/Spacebar.UApi.csproj b/extra/admin-api/Spacebar.UApi/Spacebar.UApi.csproj new file mode 100644 index 000000000..1fe9c7999 --- /dev/null +++ b/extra/admin-api/Spacebar.UApi/Spacebar.UApi.csproj @@ -0,0 +1,32 @@ + + + + net10.0 + enable + enable + true + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/extra/admin-api/Spacebar.UApi/Spacebar.UApi.http b/extra/admin-api/Spacebar.UApi/Spacebar.UApi.http new file mode 100644 index 000000000..aae3b7c30 --- /dev/null +++ b/extra/admin-api/Spacebar.UApi/Spacebar.UApi.http @@ -0,0 +1,6 @@ +@Spacebar.UApi_HostAddress = http://localhost:5085 + +GET {{Spacebar.UApi_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/extra/admin-api/Spacebar.UApi/appsettings.Development.json b/extra/admin-api/Spacebar.UApi/appsettings.Development.json new file mode 100644 index 000000000..0c208ae91 --- /dev/null +++ b/extra/admin-api/Spacebar.UApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/extra/admin-api/Spacebar.UApi/appsettings.json b/extra/admin-api/Spacebar.UApi/appsettings.json new file mode 100644 index 000000000..10f68b8c8 --- /dev/null +++ b/extra/admin-api/Spacebar.UApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/extra/admin-api/SpacebarAdminAPI.slnx b/extra/admin-api/SpacebarAdminAPI.slnx index d628c2d75..56b9e3320 100644 --- a/extra/admin-api/SpacebarAdminAPI.slnx +++ b/extra/admin-api/SpacebarAdminAPI.slnx @@ -36,4 +36,5 @@ +