Skip to content

Commit

Permalink
Merge pull request #3 from Transcom-DT/develop
Browse files Browse the repository at this point in the history
2.1 - Bugfixes and Improvements
  • Loading branch information
SakuraIsayeki authored Mar 24, 2021
2 parents 4609780 + 3936f92 commit 6176c8e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
24 changes: 21 additions & 3 deletions Transcom.SocialGuard.Api/Controllers/EmitterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Transcom.SocialGuard.Api.Controllers
{
[ApiController, Route("api/[controller]"), Authorize(Roles = UserRole.Emitter)]
[ApiController, Route("api/[controller]"), Authorize]
public class EmitterController : ControllerBase
{
private readonly EmitterService emitterService;
Expand All @@ -29,8 +29,26 @@ public EmitterController(EmitterService emitterService)
public async Task<IActionResult> GetEmitterProfile()
{
Emitter emitter = await emitterService.GetEmitterAsync(HttpContext);
return emitter is null
? StatusCode(204)

return emitter is null
? StatusCode(204)
: StatusCode(200, emitter);
}

/// <summary>
/// Fetches specified user's Emitter profile.
/// </summary>
/// <response code="200">Returns Emitter profile</response>
/// <response code="404">If user's Emitter profile is not found.</response>
/// <returns>Emitter profile</returns>
[HttpGet("{id}"), AllowAnonymous]
[ProducesResponseType(typeof(Emitter), 200), ProducesResponseType(404)]
public async Task<IActionResult> GetEmitterProfile(string id)
{
Emitter emitter = await emitterService.GetEmitterAsync(id);

return emitter is null
? StatusCode(404)
: StatusCode(200, emitter);
}

Expand Down
7 changes: 2 additions & 5 deletions Transcom.SocialGuard.Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public async Task<IActionResult> FetchUser(ulong id)
[ProducesResponseType(201), ProducesResponseType(409)]
public async Task<IActionResult> InsertUserRecord([FromBody] TrustlistUser userRecord)
{
Emitter emitter = await GetEmitterAsync();
Emitter emitter = await emitterService.GetEmitterAsync(HttpContext);

if (emitter is null)
{
Expand Down Expand Up @@ -93,7 +93,7 @@ public async Task<IActionResult> InsertUserRecord([FromBody] TrustlistUser userR
[ProducesResponseType(202), ProducesResponseType(404)]
public async Task<IActionResult> EscalateUserRecord([FromBody] TrustlistUser userRecord)
{
Emitter emitter = await GetEmitterAsync();
Emitter emitter = await emitterService.GetEmitterAsync(HttpContext);

if (emitter is null)
{
Expand Down Expand Up @@ -124,8 +124,5 @@ public async Task<IActionResult> DeleteUserRecord(ulong id)
await trustlistService.DeleteUserRecordAsync(id);
return StatusCode(200);
}


private async Task<Emitter> GetEmitterAsync() => await emitterService.GetEmitterAsync(HttpContext);
}
}
14 changes: 13 additions & 1 deletion Transcom.SocialGuard.Api/Services/EmitterService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using MongoDB.Driver;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Transcom.SocialGuard.Api.Data.Models;

Expand All @@ -18,9 +19,20 @@ public EmitterService(IMongoDatabase database)

public async Task<Emitter> GetEmitterAsync(HttpContext context) => (await emitters.FindAsync(e => e.Login == context.User.Identity.Name)).FirstOrDefault();

public async Task<Emitter> GetEmitterAsync(string login) => (await emitters.FindAsync(e => e.Login == login)).FirstOrDefault();

public async Task CreateOrUpdateEmitterSelfAsync(Emitter emitter, HttpContext context)
{
await emitters.InsertOneAsync(emitter with { Login = context.User.Identity.Name });
emitter = emitter with { Login = context.User.Identity.Name };

if ((await emitters.FindAsync(e => e.Login == emitter.Login)).Any())
{
await emitters.ReplaceOneAsync(e => e.Login == emitter.Login, emitter);
}
else
{
await emitters.InsertOneAsync(emitter);
}
}

public async Task DeleteEmitterAsync(string emitterLogin) => await emitters.FindOneAndDeleteAsync(e => e.Login == emitterLogin);
Expand Down
26 changes: 23 additions & 3 deletions Transcom.SocialGuard.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.OpenApi.Extensions;

namespace Transcom.SocialGuard.Api
{
Expand All @@ -27,15 +30,23 @@ public Startup(IConfiguration configuration)
}

public IConfiguration Configuration { get; }
public static string Version { get; } = typeof(Startup).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;


// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{

services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("2.0", new OpenApiInfo { Title = "Natsecure SocialGuard", Version = "2.0" });
c.SwaggerDoc(Version, new OpenApiInfo
{
Title = "SocialGuard",
Version = Version,
Description = "Centralised Discord Trustlist to keep servers safe from known blacklisted users.",
Contact = new() { Name = "Transcom-DT", Url = new("https://github.com/Transcom-DT/SocialGuard") },
License = new() { Name = "GNU GPLv3", Url = new("https://www.gnu.org/licenses/gpl-3.0.html") },
});

// Set the comments path for the Swagger JSON and UI.
string xmlFile = $"{typeof(Startup).Assembly.GetName().Name}.xml";
Expand Down Expand Up @@ -122,7 +133,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseStaticFiles();

app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/2.0/swagger.json", "Natsecure SocialGuard 2.0"));
app.UseSwaggerUI(c => c.SwaggerEndpoint($"/swagger/{Version}/swagger.json", $"SocialGuard {Version}"));

app.UseHttpsRedirection();

Expand All @@ -141,6 +152,15 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseEndpoints(endpoints =>
{
/*
* Remove once proper website is built.
*/
endpoints.MapGet("/", context =>
{
context.Response.Redirect("swagger/index.html");
return Task.CompletedTask;
});

endpoints.MapControllers();
});
}
Expand Down
7 changes: 7 additions & 0 deletions Transcom.SocialGuard.Api/Transcom.SocialGuard.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
<Authors>Sakura Isayeki</Authors>
<Company>Transcom-DT</Company>
<Product>SocialGuard</Product>
<Version>2.1.0</Version>
<RepositoryUrl>https://github.com/Transcom-DT/SocialGuard</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<Copyright>GNU GPLv3</Copyright>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 6176c8e

Please sign in to comment.