Skip to content

Commit

Permalink
Merge pull request #8 from eclipserporg/feature/update_presence_when_…
Browse files Browse the repository at this point in the history
…server_down

Update server presence when server down
  • Loading branch information
pauliusdotpro authored Oct 29, 2023
2 parents d88f7dd + d584479 commit e8d88eb
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 305 deletions.
153 changes: 76 additions & 77 deletions Controllers/DiscordController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,91 @@
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace app.Controllers
namespace app.Controllers;

[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class DiscordController : ControllerBase
{
[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class DiscordController : ControllerBase
private readonly DiscordService _discordService;

public DiscordController(DiscordService discordService)
{
private readonly DiscordService _discordService;
_discordService = discordService;
}

public DiscordController(DiscordService discordService)
{
_discordService = discordService;
}
[HttpGet(Name = "hasContentCreator")]
public async Task<bool> GetHasContentCreator(ulong id)
{
Log.Information("Get hasContentCreator");
var member = await _discordService.Guild.GetMemberAsync(id);

[HttpGet(Name = "hasContentCreator")]
public async Task<bool> GetHasContentCreator(ulong id)
{
Log.Information("Get hasContentCreator");
var member = await _discordService.Guild.GetMemberAsync(id);
if(member == null)
return false;

if(member == null)
return false;
return member.Roles.Any(x => x.Id == _discordService.CreatorRole.Id);
}

return member.Roles.Any(x => x.Id == _discordService.CreatorRole.Id);
}
[HttpGet(Name = "user")]
public async Task<DiscordUserDto?> GetUser(ulong id)
{
Log.Information("Get user");
var member = await _discordService.Guild.GetMemberAsync(id);

[HttpGet(Name = "user")]
public async Task<DiscordUserDto?> GetUser(ulong id)
if (member == null)
return null;

return new DiscordUserDto()
{
Log.Information("Get user");
var member = await _discordService.Guild.GetMemberAsync(id);

if (member == null)
return null;

return new DiscordUserDto()
{
Id = member.Id.ToString(),
Username = member.Username,
Discriminator = member.Discriminator,
AvatarUrl = member.AvatarUrl
};
}

[HttpPost(Name = "grantRole")]
public async Task PostGrantRole(ulong id, string role)
Id = member.Id.ToString(),
Username = member.Username,
Discriminator = member.Discriminator,
AvatarUrl = member.AvatarUrl
};
}

[HttpPost(Name = "grantRole")]
public async Task PostGrantRole(ulong id, string role)
{
Log.Information("Post grantRole");
var member = await _discordService.Guild.GetMemberAsync(id);

if (member == null)
return;

var discordRole = role switch
{
Log.Information("Post grantRole");
var member = await _discordService.Guild.GetMemberAsync(id);

if (member == null)
return;

var discordRole = role switch
{
nameof(_discordService.MemberRole) => _discordService.MemberRole,
nameof(_discordService.CreatorRole) => _discordService.CreatorRole,
nameof(_discordService.BannedRole) => _discordService.BannedRole,
nameof(_discordService.ReadOnlyRole) => _discordService.ReadOnlyRole,
nameof(_discordService.DonatorRole) => _discordService.DonatorRole,
_ => throw new Exception("Invalid role name")
};

await member.GrantRoleAsync(discordRole);
}

[HttpPost(Name = "revokeRole")]
public async Task PostRevokeRole(ulong id, string role)
nameof(_discordService.MemberRole) => _discordService.MemberRole,
nameof(_discordService.CreatorRole) => _discordService.CreatorRole,
nameof(_discordService.BannedRole) => _discordService.BannedRole,
nameof(_discordService.ReadOnlyRole) => _discordService.ReadOnlyRole,
nameof(_discordService.DonatorRole) => _discordService.DonatorRole,
_ => throw new Exception("Invalid role name")
};

await member.GrantRoleAsync(discordRole);
}

[HttpPost(Name = "revokeRole")]
public async Task PostRevokeRole(ulong id, string role)
{
Log.Information("Post revokeRole");
var member = await _discordService.Guild.GetMemberAsync(id);

if (member == null)
return;

var discordRole = role switch
{
Log.Information("Post revokeRole");
var member = await _discordService.Guild.GetMemberAsync(id);

if (member == null)
return;

var discordRole = role switch
{
nameof(_discordService.MemberRole) => _discordService.MemberRole,
nameof(_discordService.CreatorRole) => _discordService.CreatorRole,
nameof(_discordService.BannedRole) => _discordService.BannedRole,
nameof(_discordService.ReadOnlyRole) => _discordService.ReadOnlyRole,
nameof(_discordService.DonatorRole) => _discordService.DonatorRole,
_ => throw new Exception("Invalid role name")
};

await member.RevokeRoleAsync(discordRole);
}
nameof(_discordService.MemberRole) => _discordService.MemberRole,
nameof(_discordService.CreatorRole) => _discordService.CreatorRole,
nameof(_discordService.BannedRole) => _discordService.BannedRole,
nameof(_discordService.ReadOnlyRole) => _discordService.ReadOnlyRole,
nameof(_discordService.DonatorRole) => _discordService.DonatorRole,
_ => throw new Exception("Invalid role name")
};

await member.RevokeRoleAsync(discordRole);
}
}
65 changes: 32 additions & 33 deletions Controllers/FeedsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,42 @@
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace app.Controllers
namespace app.Controllers;

[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class FeedsController : ControllerBase
{
[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class FeedsController : ControllerBase
private readonly DiscordService _discordService;
public FeedsController(DiscordService discordService)
{
private readonly DiscordService _discordService;
public FeedsController(DiscordService discordService)
{
_discordService = discordService;
}
_discordService = discordService;
}

[HttpPost(Name = "send")]
public async Task PostSend(string channel, string message)
[HttpPost(Name = "send")]
public async Task PostSend(string channel, string message)
{
Log.Information("Post send");
var discordChannel = channel switch
{
Log.Information("Post send");
var discordChannel = channel switch
{
nameof (_discordService.HelpVerifyChannel) => _discordService.HelpVerifyChannel,
nameof (_discordService.VerificationChannel) => _discordService.VerificationChannel,
nameof (_discordService.CommandsChannel) => _discordService.CommandsChannel,
nameof (_discordService.GeneralChannel) => _discordService.GeneralChannel,
nameof (_discordService.SurveilanceChannel) => _discordService.SurveilanceChannel,
nameof (_discordService.CheatChannel) => _discordService.CheatChannel,
nameof (_discordService.VerifyLogsChannel) => _discordService.VerifyLogsChannel,
nameof (_discordService.LinkedAccountsChannel) => _discordService.LinkedAccountsChannel,
nameof (_discordService.LinkedAccountsCompactChannel) => _discordService.LinkedAccountsCompactChannel,
nameof (_discordService.DiscordBotLogsChannel) => _discordService.DiscordBotLogsChannel,
nameof (_discordService.WeazelFeedChannel) => _discordService.WeazelFeedChannel,
nameof (_discordService.QuizUpdatesChannel) => _discordService.QuizUpdatesChannel,
nameof (_discordService.AccountCreationChannel) => _discordService.AccountCreationChannel,
nameof (_discordService.BanNotificationsChannel) => _discordService.BanNotificationsChannel,
_ => throw new Exception("Invalid channel name")
};
nameof (_discordService.HelpVerifyChannel) => _discordService.HelpVerifyChannel,
nameof (_discordService.VerificationChannel) => _discordService.VerificationChannel,
nameof (_discordService.CommandsChannel) => _discordService.CommandsChannel,
nameof (_discordService.GeneralChannel) => _discordService.GeneralChannel,
nameof (_discordService.SurveilanceChannel) => _discordService.SurveilanceChannel,
nameof (_discordService.CheatChannel) => _discordService.CheatChannel,
nameof (_discordService.VerifyLogsChannel) => _discordService.VerifyLogsChannel,
nameof (_discordService.LinkedAccountsChannel) => _discordService.LinkedAccountsChannel,
nameof (_discordService.LinkedAccountsCompactChannel) => _discordService.LinkedAccountsCompactChannel,
nameof (_discordService.DiscordBotLogsChannel) => _discordService.DiscordBotLogsChannel,
nameof (_discordService.WeazelFeedChannel) => _discordService.WeazelFeedChannel,
nameof (_discordService.QuizUpdatesChannel) => _discordService.QuizUpdatesChannel,
nameof (_discordService.AccountCreationChannel) => _discordService.AccountCreationChannel,
nameof (_discordService.BanNotificationsChannel) => _discordService.BanNotificationsChannel,
_ => throw new Exception("Invalid channel name")
};

await _discordService.Client.SendMessageAsync(discordChannel, message);
}
await _discordService.Client.SendMessageAsync(discordChannel, message);
}
}
33 changes: 16 additions & 17 deletions Controllers/PresenceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace app.Controllers
namespace app.Controllers;

[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class PresenceController : ControllerBase
{
[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class PresenceController : ControllerBase
{
private readonly DiscordService _discordService;
private readonly DiscordService _discordService;

public PresenceController(DiscordService discordService)
{
_discordService = discordService;
}
public PresenceController(DiscordService discordService)
{
_discordService = discordService;
}

[HttpPost(Name = "players")]
public void PostPlayers(int playerCount)
{
Log.Information("Post players");
_discordService.Client.UpdateStatusAsync(new DiscordActivity($"with {playerCount} players!", ActivityType.Playing));
}
[HttpPost(Name = "players")]
public async Task PostPlayers(int playerCount)
{
Log.Information("Post players");
await _discordService.Client.UpdateStatusAsync(new DiscordActivity($"with {playerCount} players!", ActivityType.Playing));
}
}
68 changes: 33 additions & 35 deletions Middlewares/BasicAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,47 @@
using System.Text;
using System.Text.Encodings.Web;

namespace app.Middlewares
namespace app.Middlewares;

public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
private readonly IConfiguration _configuration;
private readonly Credentials _credentials;

public BasicAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
IOptions<Credentials> credentials,
IConfiguration configuration) : base(options, logger, encoder, clock)
{
private readonly IConfiguration _configuration;
private readonly Credentials _credentials;
_configuration = configuration;
_credentials = credentials.Value;
}

public BasicAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
IOptions<Credentials> credentials,
IConfiguration configuration) : base(options, logger, encoder, clock)
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
string authHeader = Request.Headers["Authorization"];
if (authHeader != null && authHeader.StartsWith("Basic"))
{
_configuration = configuration;
_credentials = credentials.Value;
}

protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
string authHeader = Request.Headers["Authorization"];
if (authHeader != null && authHeader.StartsWith("Basic"))
{

var authHeaderValue = authHeader.Replace("Basic ", "");
var decodedAuthHeaderValue = Encoding.UTF8.GetString(Convert.FromBase64String(authHeaderValue));
var userPassArray = decodedAuthHeaderValue.Split(":");
var extractedUsername = userPassArray[0];
var extractedPassword = userPassArray[1];
var authHeaderValue = authHeader.Replace("Basic ", "");
var decodedAuthHeaderValue = Encoding.UTF8.GetString(Convert.FromBase64String(authHeaderValue));
var userPassArray = decodedAuthHeaderValue.Split(":");
var extractedUsername = userPassArray[0];
var extractedPassword = userPassArray[1];

if (string.Equals(_credentials.Username, extractedUsername) && string.Equals(extractedPassword, _credentials.Password))
{
var claims = new[] { new Claim(ClaimTypes.Name, _credentials.Username) };
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
if (string.Equals(_credentials.Username, extractedUsername) && string.Equals(extractedPassword, _credentials.Password))
{
var claims = new[] { new Claim(ClaimTypes.Name, _credentials.Username) };
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);

return AuthenticateResult.Success(ticket);
}
return AuthenticateResult.Success(ticket);
}
return AuthenticateResult.Fail("Failed to authenticate");
}
return AuthenticateResult.Fail("Failed to authenticate");
}

}
11 changes: 5 additions & 6 deletions Models/Credentials.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
namespace app.Models
namespace app.Models;

public class Credentials
{
public class Credentials
{
public string Username { get; set; }
public string Password { get; set; }
}
public string Username { get; set; }
public string Password { get; set; }
}
Loading

0 comments on commit e8d88eb

Please sign in to comment.