Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #113

Merged
merged 14 commits into from
Sep 30, 2024
2 changes: 1 addition & 1 deletion API/Controller/Admin/OnlineDevicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.EntityFrameworkCore;
using OpenShock.Common.Models;
using OpenShock.Common.Redis;
using OpenShock.ServicesCommon.Utils;
using OpenShock.Common.Utils;
using Semver;
using System.Text.Json.Serialization;
using OpenShock.Common.JsonSerialization;
Expand Down
31 changes: 7 additions & 24 deletions API/Controller/Device/AssignLCG.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Response;
using OpenShock.Common.Utils;
using OpenShock.ServicesCommon.Geo;
using System.Net;
using System.Text;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Services.LCGNodeProvisioner;
using System.Net;

namespace OpenShock.API.Controller.Device;

Expand All @@ -19,51 +18,35 @@ public sealed partial class DeviceController
[HttpGet("assignLCG")]
[ProducesSuccess<LcgNodeResponse>]
[ProducesProblem(HttpStatusCode.ServiceUnavailable, "NoLcgNodesAvailable")]
public async Task<IActionResult> GetLiveControlGateway([FromServices] IGeoLocation geoLocation,
public async Task<IActionResult> GetLiveControlGateway([FromServices] ILCGNodeProvisioner geoLocation,
[FromServices] IWebHostEnvironment env)
{
var messageBuilder = new StringBuilder();
var countryCode = CountryCodeMapper.CountryInfo.Alpha2CountryCode.DefaultAlphaCode;
var countryCode = CountryCodeMapper.Alpha2CountryCode.UnknownCountryCode;
if (HttpContext.Request.Headers.TryGetValue("CF-IPCountry", out var countryHeader) &&
!string.IsNullOrEmpty(countryHeader))
{
var countryHeaderString = countryHeader.ToString();
if (CountryCodeMapper.CountryInfo.Alpha2CountryCode.TryParseAndValidate(countryHeaderString, out var code))
if (CountryCodeMapper.Alpha2CountryCode.TryParseAndValidate(countryHeaderString, out var code))
{
countryCode = code;
}
else
{
_logger.LogWarning("Country alpha2 code could not be parsed [{CountryHeader}]", countryHeaderString);
messageBuilder.AppendLine("Invalid alpha2 country code, default country used.");
}
}
else
{
_logger.LogWarning("CF-IPCountry header could not be parsed");
messageBuilder.AppendLine("No CF-IPCountry header found, default country used.");
}

if (CountryCodeMapper.CountryCodeToCountryInfo.TryGetValue(countryCode, out var country))
LucHeart marked this conversation as resolved.
Show resolved Hide resolved
{
if (_logger.IsEnabled(LogLevel.Debug))
_logger.LogDebug("Client country identified as [{@CountryInfo}]", country);
}
else
{
country = CountryCodeMapper.DefaultCountry;
_logger.LogWarning("Country not found in mapping [{Alpha2Code}]", countryCode);
messageBuilder.AppendLine("Country not found in mapping, default country used.");
}

var closestNode = await geoLocation.GetClosestNode(country, env.EnvironmentName);

var closestNode = await geoLocation.GetOptimalNode(countryCode, env.EnvironmentName);
if (closestNode == null) return Problem(AssignLcgError.NoLcgNodesAvailable);

return RespondSuccess(new LcgNodeResponse
{
Fqdn = closestNode.Fqdn,
Country = closestNode.Country
}, messageBuilder.ToString());
});
}
}
2 changes: 1 addition & 1 deletion API/Controller/Public/GetStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<BaseResponse<StatsResponse>> GetOnlineDevicesStatistics(
}
}

public class StatsResponse
public sealed class StatsResponse
{
public required int DevicesOnline { get; set; }
}
2 changes: 1 addition & 1 deletion API/Controller/Public/PublicShareController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using OpenShock.API.Models.Response;
using OpenShock.API.Utils;
using OpenShock.Common.Models;
using OpenShock.Common.Utils;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Utils;

namespace OpenShock.API.Controller.Public;

Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Shockers/ControlLogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using Microsoft.EntityFrameworkCore;
using OpenShock.API.Models.Response;
using OpenShock.Common.Models;
using OpenShock.Common.Utils;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Utils;

namespace OpenShock.API.Controller.Shockers;

Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Shockers/ControlShockerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using OpenShock.Common.Models;
using OpenShock.Common.Utils;
using OpenShock.ServicesCommon.Authentication.Attributes;
using OpenShock.ServicesCommon.DeviceControl;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Hubs;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Services.RedisPubSub;
using OpenShock.ServicesCommon.Utils;

namespace OpenShock.API.Controller.Shockers;

Expand Down
4 changes: 2 additions & 2 deletions API/Controller/Shockers/ShareShockerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
using OpenShock.API.Services;
using OpenShock.Common.Models;
using OpenShock.Common.OpenShockDb;
using OpenShock.ServicesCommon.Utils;
using System.Net;
using OpenShock.ServicesCommon.Authentication.Attributes;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using OpenShock.Common.Utils;

namespace OpenShock.API.Controller.Shockers;

Expand Down Expand Up @@ -85,7 +85,7 @@ public async Task<IActionResult> ShockerShareCodeList([FromRoute] Guid shockerId
return RespondSuccess(shares);
}

public class ShareCodeInfo
public sealed class ShareCodeInfo
{
public required Guid Id { get; set; }
public required DateTime CreatedOn { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions API/Controller/Shockers/SharedShockersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using Microsoft.EntityFrameworkCore;
using OpenShock.API.Models.Response;
using OpenShock.Common.Models;
using OpenShock.Common.Utils;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Utils;

namespace OpenShock.API.Controller.Shockers;

public partial class ShockerController
public sealed partial class ShockerController
{
/// <summary>
/// List all shockers shared with the authenticated user.
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/DeviceSelfResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Models.Response;

public class DeviceSelfResponse
public sealed class DeviceSelfResponse
{
public required Guid Id { get; set; }
public required string Name { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/LcgNodeResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Models.Response;

public class LcgNodeResponse
public sealed class LcgNodeResponse
{
public required string Fqdn { get; set; }
public required string Country { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OpenShock.API.Models.Response;

public class LogEntry
public sealed class LogEntry
{
public required Guid Id { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions API/Models/Response/OwnerShockerResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace OpenShock.API.Models.Response;

public class OwnerShockerResponse : GenericIni
public sealed class OwnerShockerResponse : GenericIni
{
public IList<SharedDevice> Devices { get; set; } = new List<SharedDevice>();

public class SharedDevice
public sealed class SharedDevice
{
public required Guid Id { get; set; }
public required string Name { get; set; }
// ReSharper disable once CollectionNeverQueried.Global
public IList<SharedShocker> Shockers { get; set; } = new List<SharedShocker>();

public class SharedShocker
public sealed class SharedShocker
{
public required Guid Id { get; set; }
public required string Name { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/PublicShareLinkResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OpenShock.API.Models.Response;

public class PublicShareLinkResponse
public sealed class PublicShareLinkResponse
{
public required Guid Id { get; set; }
public required string Name { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/ResponseDeviceWithShockers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Models.Response;

public class ResponseDeviceWithShockers : ResponseDevice
public sealed class ResponseDeviceWithShockers : ResponseDevice
{
public required IEnumerable<ShockerResponse> Shockers { get; set; }
}
2 changes: 1 addition & 1 deletion API/Models/Response/ResponseDeviceWithToken.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Models.Response;

public class ResponseDeviceWithToken : ResponseDevice
public sealed class ResponseDeviceWithToken : ResponseDevice
{
public required string? Token { get; set; }
}
2 changes: 1 addition & 1 deletion API/Models/Response/ShareInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OpenShock.API.Models.Response;

public class ShareInfo
public sealed class ShareInfo
{
public required GenericIni SharedWith { get; set; }
public required DateTime CreatedOn { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/ShareLinkDevice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Models.Response;

public class ShareLinkDevice
public sealed class ShareLinkDevice
{
public required Guid Id { get; set; }
public required string Name { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/ShareLinkResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OpenShock.API.Models.Response;

public class ShareLinkResponse
public sealed class ShareLinkResponse
{
public required Guid Id { get; set; }
public required string Name { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/ShareLinkShocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OpenShock.API.Models.Response;

public class ShareLinkShocker
public sealed class ShareLinkShocker
{
public required Guid Id { get; set; }
public required string Name { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/ShockerLimits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace OpenShock.API.Models.Response;

public class ShockerLimits
public sealed class ShockerLimits
{
[Range(Constants.MinControlIntensity, Constants.MaxControlIntensity)]
public required byte? Intensity { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/ShockerPermissions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Models.Response;

public class ShockerPermissions
public sealed class ShockerPermissions
{
public required bool Vibrate { get; set; }
public required bool Sound { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion API/Models/Response/ShockerWithDevice.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Models.Response;

public class ShockerWithDevice : ShockerResponse
public sealed class ShockerWithDevice : ShockerResponse
{
public required Guid Device { get; set; }
}
2 changes: 1 addition & 1 deletion API/Realtime/RedisSubscriberService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace OpenShock.API.Realtime;
/// <summary>
/// Redis subscription service, which handles listening to pub sub on redis
/// </summary>
public class RedisSubscriberService : IHostedService, IAsyncDisposable
public sealed class RedisSubscriberService : IHostedService, IAsyncDisposable
{
private readonly IHubContext<UserHub, IUserHub> _hubContext;
private readonly IDbContextFactory<OpenShockContext> _dbContextFactory;
Expand Down
6 changes: 3 additions & 3 deletions API/Services/Account/IAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ public interface IAccountService
public Task<OneOf<Success, NotFound, SecretInvalid>> PasswordResetComplete(Guid passwordResetId, string secret, string newPassword);
}

public struct AccountWithEmailOrUsernameExists;
public struct TooManyPasswordResets;
public struct SecretInvalid;
public readonly struct AccountWithEmailOrUsernameExists;
public readonly struct TooManyPasswordResets;
public readonly struct SecretInvalid;
6 changes: 1 addition & 5 deletions API/Services/Account/LoginContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
namespace OpenShock.API.Services.Account;

public struct LoginContext
{
public required string UserAgent { get; init; }
public required string Ip { get; init; }
}
public readonly record struct LoginContext(string UserAgent, string Ip);
2 changes: 1 addition & 1 deletion API/Services/DeviceUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OpenShock.API.Services;

public class DeviceUpdateService : IDeviceUpdateService
public sealed class DeviceUpdateService : IDeviceUpdateService
{
private readonly IRedisPubService _redisPubService;
private readonly IHubContext<UserHub, IUserHub> _hubContext;
Expand Down
2 changes: 1 addition & 1 deletion API/Services/Email/Mailjet/Mail/MailsWrap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Services.Email.Mailjet.Mail;

public class MailsWrap
public sealed class MailsWrap
{
public required IEnumerable<Mail.MailBase> Messages { get; set; }
}
2 changes: 1 addition & 1 deletion API/Services/Email/Mailjet/Mail/TemplateMail.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace OpenShock.API.Services.Email.Mailjet.Mail;

public class TemplateMail : MailBase
public sealed class TemplateMail : MailBase
{
public bool TemplateLanguage { get; set; } = true;
public required ulong TemplateId { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
using OpenShock.ServicesCommon.Authentication.Services;
using OpenShock.ServicesCommon.DataAnnotations;
using OpenShock.ServicesCommon.ExceptionHandle;
using OpenShock.ServicesCommon.Geo;
using OpenShock.ServicesCommon.Hubs;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Services.BatchUpdate;
using OpenShock.ServicesCommon.Services.Device;
using OpenShock.ServicesCommon.Services.LCGNodeProvisioner;
using OpenShock.ServicesCommon.Services.Ota;
using OpenShock.ServicesCommon.Services.RedisPubSub;
using OpenShock.ServicesCommon.Services.Turnstile;
Expand All @@ -47,7 +47,7 @@

namespace OpenShock.API;

public class Startup
public sealed class Startup
{
private readonly ForwardedHeadersOptions _forwardedSettings = new()
{
Expand Down Expand Up @@ -129,7 +129,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<IClientAuthService<Device>, ClientAuthService<Device>>();
services.AddScoped<ITokenReferenceService<ApiToken>, TokenReferenceService<ApiToken>>();

services.AddSingleton<IGeoLocation, GeoLocation>();
services.AddSingleton<ILCGNodeProvisioner, LCGNodeProvisioner>();


services.AddSingleton(x =>
Expand Down
2 changes: 1 addition & 1 deletion API/Utils/OneWayPolymorphicJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace OpenShock.API.Utils;

public class OneWayPolymorphicJsonConverter<T> : JsonConverter<T>
public sealed class OneWayPolymorphicJsonConverter<T> : JsonConverter<T>
{
public override bool CanConvert(Type typeToConvert)
{
Expand Down
Loading
Loading