diff --git a/src/GrillBot.App/Helpers/RemindHelper.cs b/src/GrillBot.App/Helpers/RemindHelper.cs index 74563cd8..fa7c8c12 100644 --- a/src/GrillBot.App/Helpers/RemindHelper.cs +++ b/src/GrillBot.App/Helpers/RemindHelper.cs @@ -22,15 +22,17 @@ public RemindHelper(IDiscordClient discordClient, ITextsManager texts) public async Task ProcessRemindAsync(Database.Entity.RemindMessage remind, bool force) { - var embed = await CreateRemindEmbedAsync(remind, force); + var embed = CreateRemindEmbed(remind, force); - var destination = await DiscordClient.FindUserAsync(remind.ToUserId.ToUlong()); - if (destination == null) return NotSentRemind; + var targetUser = await DiscordClient.FindUserAsync(remind.ToUserId.ToUlong()); + if (targetUser is null) + return NotSentRemind; try { var postponeComponents = CreatePostponeComponents(force); - var msg = await destination.SendMessageAsync(embed: embed.Build(), components: postponeComponents); + var msg = await targetUser.SendMessageAsync(embed: embed.Build(), components: postponeComponents); + return msg.Id.ToString(); } catch (HttpException ex) when (ex.DiscordCode == DiscordErrorCode.CannotSendMessageToUser) @@ -58,7 +60,7 @@ public async Task ProcessRemindAsync(Database.Entity.RemindMessage remin return ComponentsHelper.CreateWrappedComponents(components); } - private async Task CreateRemindEmbedAsync(Database.Entity.RemindMessage remind, bool force = false) + private EmbedBuilder CreateRemindEmbed(Database.Entity.RemindMessage remind, bool force = false) { const string localeBase = "RemindModule/NotifyMessage/"; @@ -70,19 +72,13 @@ private async Task CreateRemindEmbedAsync(Database.Entity.RemindMe .WithTitle(Texts[force ? localeBase + "ForceTitle" : localeBase + "Title", locale]) .AddField(Texts[localeBase + "Fields/Id", locale], remind.Id, true); - if (remind.FromUserId != remind.ToUserId) - { - var fromUser = await DiscordClient.FindUserAsync(remind.FromUserId.ToUlong()); - - if (fromUser != null) - embed.AddField(Texts[localeBase + "Fields/From", locale], fromUser.GetFullName(), true); - } + if (remind.FromUserId != remind.ToUserId && remind.FromUser is not null) + embed.AddField(Texts[localeBase + "Fields/From", locale], remind.FromUser.GetDisplayName(), true); if (remind.Postpone > 0) embed.AddField(Texts[localeBase + "Fields/Attention", locale], Texts[localeBase + "Postponed", locale].FormatWith(remind.Postpone)); - embed - .AddField(Texts[localeBase + "Fields/Message", locale], remind.Message); + embed.AddField(Texts[localeBase + "Fields/Message", locale], remind.Message); if (!force) embed.AddField(Texts[localeBase + "Fields/Options", locale], Texts[localeBase + "Options", locale]); diff --git a/src/GrillBot.App/Jobs/RemindCronJob.cs b/src/GrillBot.App/Jobs/RemindCronJob.cs index 3c2e7f25..6ef6e72b 100644 --- a/src/GrillBot.App/Jobs/RemindCronJob.cs +++ b/src/GrillBot.App/Jobs/RemindCronJob.cs @@ -21,10 +21,12 @@ public RemindCronJob(FinishRemind finishRemind, GrillBotDatabaseBuilder database protected override async Task RunAsync(IJobExecutionContext context) { - var reminders = await GetIdsForProcessAsync(); + await using var repository = DatabaseBuilder.CreateRepository(); + var id = await repository.Remind.GetFirstIdForProcessAsync(); var result = new Dictionary(); - foreach (var id in reminders) + + while (id != 0) { try { @@ -37,23 +39,21 @@ protected override async Task RunAsync(IJobExecutionContext context) result.Add(id, CreateReportMessage(ex)); await LoggingManager.ErrorAsync(nameof(RemindCronJob), $"An error occured while processing remind #{id}", ex); } + finally + { + id = await repository.Remind.GetFirstIdForProcessAsync(); + } } if (result.Count > 0) { - var resultBuilder = new StringBuilder($"Processed reminders ({reminders.Count}):").AppendLine() + var resultBuilder = new StringBuilder($"Processed reminders ({result.Count}):").AppendLine() .AppendJoin("\n", result.Select(o => $"{o.Key}: {o.Value}")); context.Result = resultBuilder.ToString(); } } - private async Task> GetIdsForProcessAsync() - { - await using var repository = DatabaseBuilder.CreateRepository(); - return await repository.Remind.GetRemindIdsForProcessAsync(); - } - private string CreateReportMessage(Exception? exception = null) { var result = new List(); diff --git a/src/GrillBot.Database/Services/Repository/RemindRepository.cs b/src/GrillBot.Database/Services/Repository/RemindRepository.cs index 9664313c..50389f72 100644 --- a/src/GrillBot.Database/Services/Repository/RemindRepository.cs +++ b/src/GrillBot.Database/Services/Repository/RemindRepository.cs @@ -18,14 +18,16 @@ public RemindRepository(GrillBotContext context, ICounterManager counter) : base { } - public async Task> GetRemindIdsForProcessAsync() + public async Task GetFirstIdForProcessAsync() { using (CreateCounter()) { - return await Context.Reminders.AsNoTracking() + var query = Context.Reminders.AsNoTracking() .Where(o => o.RemindMessageId == null && o.At <= DateTime.Now) - .Select(o => o.Id) - .ToListAsync(); + .OrderBy(o => o.At) + .Select(o => o.Id); + + return await query.FirstOrDefaultAsync(); } }