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
8 changes: 4 additions & 4 deletions API/ApiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;
using OpenShock.API.Services.Email.Mailjet.Mail;
using OpenShock.API.Services.Email.Mailjet.Mail;
using OpenShock.ServicesCommon.Config;
using System.ComponentModel.DataAnnotations;

namespace OpenShock.API;

Expand All @@ -9,7 +9,7 @@ public sealed class ApiConfig : BaseConfig
[Required] public required FrontendConfig Frontend { get; init; }
[Required] public required MailConfig Mail { get; init; }
[Required] public required TurnstileConfig Turnstile { get; init; }

public sealed class TurnstileConfig
{
[Required] public required bool Enabled { get; init; }
Expand Down Expand Up @@ -58,5 +58,5 @@ public sealed class TemplateConfig
}



}
10 changes: 5 additions & 5 deletions API/Controller/Account/Login.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Requests;
using System.Net;
using Asp.Versioning;
using OpenShock.API.Services.Account;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using System.Net;

namespace OpenShock.API.Controller.Account;

Expand All @@ -28,15 +28,15 @@ public async Task<IActionResult> Login(
{
var cookieDomainToUse = apiConfig.Frontend.CookieDomain.Split(',').FirstOrDefault(domain => Request.Headers.Host.ToString().EndsWith(domain, StringComparison.OrdinalIgnoreCase));
if (cookieDomainToUse == null) return Problem(LoginError.InvalidDomain);

var loginAction = await accountService.Login(body.Email, body.Password, new LoginContext
{
Ip = HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString() ?? string.Empty,
UserAgent = HttpContext.Request.Headers.UserAgent.ToString()
}, cancellationToken);

if (loginAction.IsT1) return Problem(LoginError.InvalidCredentials);


HttpContext.Response.Cookies.Append("openShockSession", loginAction.AsT0.Value, new CookieOptions
{
Expand Down
12 changes: 6 additions & 6 deletions API/Controller/Account/LoginV2.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Requests;
using System.Net;
using Asp.Versioning;
using OpenShock.API.Services.Account;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Services.Turnstile;
using System.Net;

namespace OpenShock.API.Controller.Account;

Expand Down Expand Up @@ -33,16 +33,16 @@ public async Task<IActionResult> LoginV2(

var turnStile = await turnstileService.VerifyUserResponseToken(body.TurnstileResponse, HttpContext.Connection.RemoteIpAddress, cancellationToken);
if (!turnStile.IsT0) return Problem(TurnstileError.InvalidTurnstile);

var loginAction = await accountService.Login(body.Email, body.Password, new LoginContext
{
Ip = HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString() ?? string.Empty,
UserAgent = HttpContext.Request.Headers.UserAgent.ToString()
}, cancellationToken);

if (loginAction.IsT1) return Problem(LoginError.InvalidCredentials);


HttpContext.Response.Cookies.Append("openShockSession", loginAction.AsT0.Value, new CookieOptions
{
Expires = new DateTimeOffset(DateTime.UtcNow.Add(accountService.SessionLifetime)),
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/PasswordResetCheckValid.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using System.Net;
using Asp.Versioning;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Services.Account;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using System.Net;

namespace OpenShock.API.Controller.Account;

Expand Down
8 changes: 4 additions & 4 deletions API/Controller/Account/PasswordResetComplete.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using System.Net;
using Asp.Versioning;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Services.Account;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using System.Net;

namespace OpenShock.API.Controller.Account;

Expand Down Expand Up @@ -33,7 +33,7 @@ public async Task<IActionResult> PasswordResetComplete([FromRoute] Guid password
notFound => Problem(PasswordResetError.PasswordResetNotFound),
invalid => Problem(PasswordResetError.PasswordResetNotFound));
}


public sealed class PasswordResetProcessData
{
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/PasswordResetInitiate.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using OpenShock.Common.Models;
using Asp.Versioning;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Services.Account;
using OpenShock.Common.Models;
using OpenShock.ServicesCommon.Problems;

namespace OpenShock.API.Controller.Account;
Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/Signup.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.AspNetCore.Mvc;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Requests;
using System.Net;
using Asp.Versioning;
using OpenShock.API.Services.Account;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using System.Net;

namespace OpenShock.API.Controller.Account;

Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Account/SignupV2.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Requests;
using System.Net;
using Asp.Versioning;
using OpenShock.API.Services.Account;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Services.Turnstile;
using System.Net;

namespace OpenShock.API.Controller.Account;

Expand Down
4 changes: 2 additions & 2 deletions API/Controller/Admin/OnlineDevicesController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.Common.JsonSerialization;
using OpenShock.Common.Models;
using OpenShock.Common.Redis;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Utils;
using Semver;
using System.Text.Json.Serialization;
using OpenShock.Common.JsonSerialization;
using OpenShock.ServicesCommon.Problems;

namespace OpenShock.API.Controller.Admin;

Expand Down
22 changes: 5 additions & 17 deletions API/Controller/Device/AssignLCG.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using OpenShock.API.Models.Response;
using OpenShock.Common.Utils;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Geo;
using OpenShock.ServicesCommon.Problems;
using System.Net;
using System.Text;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;

namespace OpenShock.API.Controller.Device;

Expand All @@ -23,12 +23,12 @@ public async Task<IActionResult> GetLiveControlGateway([FromServices] IGeoLocati
[FromServices] IWebHostEnvironment env)
{
var messageBuilder = new StringBuilder();
var countryCode = CountryCodeMapper.CountryInfo.Alpha2CountryCode.DefaultAlphaCode;
var countryCode = CountryCodeMapper.Alpha2CountryCode.DefaultAlphaCode;
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;
}
Expand All @@ -44,19 +44,7 @@ public async Task<IActionResult> GetLiveControlGateway([FromServices] IGeoLocati
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.GetClosestNode(countryCode, env.EnvironmentName);

if (closestNode == null) return Problem(AssignLcgError.NoLcgNodesAvailable);

Expand Down
4 changes: 2 additions & 2 deletions API/Controller/Device/Pair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.Common.Redis;
using Redis.OM;
using System.Net;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using Redis.OM;
using System.Net;

namespace OpenShock.API.Controller.Device;

Expand Down
6 changes: 3 additions & 3 deletions API/Controller/Devices/DeviceOtaController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.Common.Models.Services.Ota;
using OpenShock.ServicesCommon.Authentication.Attributes;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Services.Ota;
using System.Net;

namespace OpenShock.API.Controller.Devices;

Expand All @@ -31,5 +31,5 @@ public async Task<IActionResult> GetOtaUpdateHistory([FromRoute] Guid deviceId,

return RespondSuccess(await otaService.GetUpdates(deviceId));
}

}
16 changes: 8 additions & 8 deletions API/Controller/Devices/DevicesController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.API.Models.Requests;
using OpenShock.API.Services;
Expand All @@ -9,6 +8,7 @@
using OpenShock.ServicesCommon.Authentication.Attributes;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using System.Net;

namespace OpenShock.API.Controller.Devices;

Expand Down Expand Up @@ -46,8 +46,8 @@ public sealed partial class DevicesController
public async Task<IActionResult> GetDeviceById([FromRoute] Guid deviceId)
{
var hasAuthPerms = IsAllowed(PermissionType.Devices_Auth);


var device = await _db.Devices.Where(x => x.Owner == CurrentUser.DbUser.Id && x.Id == deviceId)
.Select(x => new Models.Response.ResponseDeviceWithToken
{
Expand Down Expand Up @@ -128,9 +128,9 @@ public async Task<IActionResult> RemoveDevice([FromRoute] Guid deviceId, [FromSe
var affected = await _db.Devices.Where(x => x.Owner == CurrentUser.DbUser.Id && x.Id == deviceId)
.ExecuteDeleteAsync();
if (affected <= 0) return Problem(DeviceError.DeviceNotFound);

await updateService.UpdateDeviceForAllShared(CurrentUser.DbUser.Id, deviceId, DeviceUpdateType.Deleted);

return RespondSuccessSimple("Successfully deleted device");
}

Expand All @@ -152,9 +152,9 @@ public async Task<BaseResponse<Guid>> CreateDevice([FromServices] IDeviceUpdateS
};
_db.Devices.Add(device);
await _db.SaveChangesAsync();

await updateService.UpdateDevice(CurrentUser.DbUser.Id, device.Id, DeviceUpdateType.Created);

Response.StatusCode = (int)HttpStatusCode.Created;
return new BaseResponse<Guid>
{
Expand Down
2 changes: 1 addition & 1 deletion API/Controller/Devices/ShockersController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.API.Models.Response;
using System.Net;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using System.Net;

namespace OpenShock.API.Controller.Devices;

Expand Down
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; }
}
8 changes: 4 additions & 4 deletions API/Controller/Public/PublicShareController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.API.Models.Response;
using OpenShock.API.Utils;
using OpenShock.Common.Models;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using OpenShock.ServicesCommon.Utils;
using System.Net;

namespace OpenShock.API.Controller.Public;

Expand Down Expand Up @@ -61,8 +61,8 @@ public async Task<IActionResult> GetShareLink([FromRoute] Guid shareLinkId)
}).SingleOrDefaultAsync();

if (shareLink == null) return RespondSuccess(ShareLinkError.ShareLinkNotFound);


var final = new PublicShareLinkResponse
{
Id = shareLink.Id,
Expand Down
4 changes: 2 additions & 2 deletions API/Controller/Shares/DeleteShareCode.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.ServicesCommon.Errors;
using OpenShock.ServicesCommon.Problems;
using System.Net;

namespace OpenShock.API.Controller.Shares;

Expand Down
Loading