Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
austins committed Nov 15, 2023
1 parent c5f4d6a commit 6e5b24a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public async Task Execute_NoSourceMessage_Succeeds()
public async Task Execute_InvalidData_Throws()
{
// Arrange
_sut.GuildId = null;
_sut.ChannelId = null;
_sut.ReplyMessageId = null;
_sut.GuildId = null!;
_sut.ChannelId = "abc";
_sut.ReplyMessageId = null!;
_sut.ReactionEmoteName = "Not an emoji.";
_sut.ReactionUserId = null;
_sut.SourceMessageId = "5abc";
_sut.ReactionUserId = null!;
_sut.SourceMessageId = "5xyz";

// Act & Assert
await _sut.Invoking(x => x.Execute(_context)).Should().ThrowAsync<ValidationException>();
Expand Down
9 changes: 9 additions & 0 deletions DiscordTranslationBot/Extensions/ValidationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

namespace DiscordTranslationBot.Extensions;

/// <summary>
/// Extensions for Fluent Validation.
/// </summary>
public static class ValidationExtensions
{
/// <summary>
/// Validates that a string is a positive ulong.
/// </summary>
/// <param name="ruleBuilder">Fluent Validation rule builder.</param>
/// <typeparam name="T">Type of object being validated</typeparam>
/// <returns></returns>
public static IRuleBuilderOptions<T, string> PositiveUInt64<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.NotEmpty().Must(x => ulong.TryParse(x, out var num) && num > 0);
Expand Down
2 changes: 1 addition & 1 deletion DiscordTranslationBot/Handlers/FlagEmojiReactionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public partial class FlagEmojiReactionHandler : INotificationHandler<ReactionAdd
/// <param name="client">Discord client to use.</param>
/// <param name="translationProviders">Translation providers to use.</param>
/// <param name="countryService">Country service to use.</param>
/// <param name="scheduler">Scheduler to use.</param>
/// <param name="schedulerFactory">Scheduler factory to use.</param>
/// <param name="logger">Logger to use.</param>
public FlagEmojiReactionHandler(
IDiscordClient client,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using System.Globalization;
using Discord;
using Discord.WebSocket;
using DiscordTranslationBot.Extensions;
using DiscordTranslationBot.Models.Discord;
using FluentValidation;
using Quartz;

namespace DiscordTranslationBot.Jobs;

/// <summary>
/// Job that deletes a temporary reply message and clears the associated reaction when a user reacts to a message with a flag emoji.
/// </summary>
public sealed class DeleteTempReplyForFlagEmojiReactionJob : IJob
{
private readonly IDiscordClient _client;
private readonly IValidator<DeleteTempReplyForFlagEmojiReactionJob> _validator;

/// <summary>
/// Initializes a new instance of the <see cref="DeleteTempReplyForFlagEmojiReactionJob"/> class.
/// </summary>
/// <param name="client">Discord client to use.</param>
/// <param name="validator">Validator to use.</param>
public DeleteTempReplyForFlagEmojiReactionJob(
IDiscordClient client,
IValidator<DeleteTempReplyForFlagEmojiReactionJob> validator
Expand All @@ -22,18 +29,40 @@ IValidator<DeleteTempReplyForFlagEmojiReactionJob> validator
_validator = validator;
}

public string GuildId { get; set; }

public string ChannelId { get; set; }

public string ReplyMessageId { get; set; }

public string ReactionEmoteName { get; set; }

public string ReactionUserId { get; set; }

public string SourceMessageId { get; set; }

/// <summary>
/// The Guild ID.
/// </summary>
public string GuildId { get; set; } = null!;

/// <summary>
/// The channel ID.
/// </summary>
public string ChannelId { get; set; } = null!;

/// <summary>
/// The reply message ID.
/// </summary>
public string ReplyMessageId { get; set; } = null!;

/// <summary>
/// The reaction emoji.
/// </summary>
public string ReactionEmoteName { get; set; } = null!;

/// <summary>
/// The user ID of the user who made the reaction.
/// </summary>
public string ReactionUserId { get; set; } = null!;

/// <summary>
/// The source message ID.
/// </summary>
public string SourceMessageId { get; set; } = null!;

/// <summary>
/// Handles the job.
/// </summary>
/// <param name="context">The job execution context.</param>
public async Task Execute(IJobExecutionContext context)
{
await _validator.ValidateAndThrowAsync(this, context.CancellationToken);
Expand Down Expand Up @@ -70,6 +99,13 @@ await channel.DeleteMessageAsync(
);
}

/// <summary>
/// Creates a job detail to be used by the scheduler.
/// </summary>
/// <param name="reply">The reply message.</param>
/// <param name="reaction">The reaction.</param>
/// <param name="sourceMessage">The source message.</param>
/// <returns>A job detail.</returns>
public static IJobDetail Create(IMessage reply, Reaction reaction, IMessage sourceMessage)
{
return JobBuilder
Expand All @@ -87,9 +123,15 @@ public static IJobDetail Create(IMessage reply, Reaction reaction, IMessage sour
}
}

/// <summary>
/// Validator for <see cref="DeleteTempReplyForFlagEmojiReactionJob"/>.
/// </summary>
public sealed class DeleteTempReplyForFlagEmojiReactionJobValidator
: AbstractValidator<DeleteTempReplyForFlagEmojiReactionJob>
{
/// <summary>
/// Initializes a new instance of the <see cref="DeleteTempReplyForFlagEmojiReactionJobValidator"/> class.
/// </summary>
public DeleteTempReplyForFlagEmojiReactionJobValidator()
{
RuleFor(x => x.GuildId).PositiveUInt64();
Expand Down

0 comments on commit 6e5b24a

Please sign in to comment.