Skip to content

Commit

Permalink
chore: tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Apr 14, 2024
1 parent 83c45bf commit 82d6243
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/GZCTF/Controllers/TeamController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ public async Task<IActionResult> DeleteTeam(int id, CancellationToken token)

await teamRepository.DeleteTeam(team, token);

logger.Log(Program.StaticLocalizer[nameof(Resources.Program.Team_Deleted), team.Name], user, TaskStatus.Success);
logger.Log(Program.StaticLocalizer[nameof(Resources.Program.Team_Deleted), team.Name], user,
TaskStatus.Success);

return Ok();
}
Expand Down
9 changes: 6 additions & 3 deletions src/GZCTF/Services/Interface/IMailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public interface IMailSender
/// <param name="email">用户新注册的Email</param>
/// <param name="confirmLink">确认链接</param>
/// <param name="localizer">本地化</param>
public bool SendConfirmEmailUrl(string? userName, string? email, string? confirmLink, IStringLocalizer<Program> localizer);
public bool SendConfirmEmailUrl(string? userName, string? email, string? confirmLink,
IStringLocalizer<Program> localizer);

/// <summary>
/// 发送邮箱重置邮件
Expand All @@ -35,7 +36,8 @@ public interface IMailSender
/// <param name="email">用户的电子邮件</param>
/// <param name="resetLink">重置链接</param>
/// <param name="localizer">本地化</param>
public bool SendChangeEmailUrl(string? userName, string? email, string? resetLink, IStringLocalizer<Program> localizer);
public bool SendChangeEmailUrl(string? userName, string? email, string? resetLink,
IStringLocalizer<Program> localizer);

/// <summary>
/// 发送密码重置邮件
Expand All @@ -44,5 +46,6 @@ public interface IMailSender
/// <param name="email">用户的电子邮件</param>
/// <param name="resetLink">重置链接</param>
/// <param name="localizer">本地化</param>
public bool SendResetPasswordUrl(string? userName, string? email, string? resetLink, IStringLocalizer<Program> localizer);
public bool SendResetPasswordUrl(string? userName, string? email, string? resetLink,
IStringLocalizer<Program> localizer);
}
25 changes: 16 additions & 9 deletions src/GZCTF/Services/MailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class MailSender : IMailSender, IDisposable
{
private readonly ConcurrentQueue<MailContent> _mailQueue = new();
private readonly EmailConfig? _options;
private readonly IOptionsSnapshot<GlobalConfig> _globalConfig;
private readonly IOptions<GlobalConfig> _globalConfig;
private readonly ILogger<MailSender> _logger;
private readonly SmtpClient? _smtpClient;
private readonly CancellationTokenSource _cancellationTokenSource = new();
Expand All @@ -24,7 +24,7 @@ public sealed class MailSender : IMailSender, IDisposable

public MailSender(
IOptions<EmailConfig> options,
IOptionsSnapshot<GlobalConfig> globalConfig,
IOptions<GlobalConfig> globalConfig,
ILogger<MailSender> logger)
{
_globalConfig = globalConfig;
Expand All @@ -36,7 +36,8 @@ public MailSender(
{
_smtpClient = new();
_smtpClient.AuthenticationMechanisms.Remove("XOAUTH2");
Task.Factory.StartNew(MailSenderWorker, _cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);
Task.Factory.StartNew(MailSenderWorker, _cancellationToken, TaskCreationOptions.LongRunning,
TaskScheduler.Default);
}
}

Expand Down Expand Up @@ -103,12 +104,14 @@ private async Task MailSenderWorker()
{
if (!_smtpClient.IsConnected)
{
await _smtpClient.ConnectAsync(_options!.Smtp!.Host, _options.Smtp.Port!.Value, cancellationToken: _cancellationToken);
await _smtpClient.ConnectAsync(_options!.Smtp!.Host, _options.Smtp.Port!.Value,
cancellationToken: _cancellationToken);
}

if (!_smtpClient.IsAuthenticated)
{
await _smtpClient.AuthenticateAsync(_options!.UserName, _options.Password, cancellationToken: _cancellationToken);
await _smtpClient.AuthenticateAsync(_options!.UserName, _options.Password,
cancellationToken: _cancellationToken);
}

while (_mailQueue.TryDequeue(out var content))
Expand All @@ -130,16 +133,20 @@ private async Task MailSenderWorker()
}
}

public bool SendConfirmEmailUrl(string? userName, string? email, string? confirmLink, IStringLocalizer<Program> localizer) =>
public bool SendConfirmEmailUrl(string? userName, string? email, string? confirmLink,
IStringLocalizer<Program> localizer) =>
SendUrlIfPossible(userName, email, confirmLink, MailType.ConfirmEmail, localizer);

public bool SendChangeEmailUrl(string? userName, string? email, string? resetLink, IStringLocalizer<Program> localizer) =>
public bool SendChangeEmailUrl(string? userName, string? email, string? resetLink,
IStringLocalizer<Program> localizer) =>
SendUrlIfPossible(userName, email, resetLink, MailType.ChangeEmail, localizer);

public bool SendResetPasswordUrl(string? userName, string? email, string? resetLink, IStringLocalizer<Program> localizer) =>
public bool SendResetPasswordUrl(string? userName, string? email, string? resetLink,
IStringLocalizer<Program> localizer) =>
SendUrlIfPossible(userName, email, resetLink, MailType.ResetPassword, localizer);

bool SendUrlIfPossible(string? userName, string? email, string? resetLink, MailType type, IStringLocalizer<Program> localizer)
bool SendUrlIfPossible(string? userName, string? email, string? resetLink, MailType type,
IStringLocalizer<Program> localizer)
{
if (_smtpClient is null)
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/GZCTF/Utils/AsyncManualResetEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ public void Reset()
return;
}
}
}
}

0 comments on commit 82d6243

Please sign in to comment.