From e073c5a5720955fe2aa2a93ae2051f878be9a874 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Sat, 30 Sep 2023 20:38:52 +0500 Subject: [PATCH] Synchronize events only on sch. event updates (#131) This PR moves all code related to synchronization of scheduled events to ScheduledEventUpdateService. Just like #130, this reduces possible points of failures, maintenance burden and reliance on gateway events --- src/Responders/GuildLoadedResponder.cs | 13 -------- .../ScheduledEventUpdatedResponder.cs | 30 ------------------- .../Update/ScheduledEventUpdateService.cs | 21 ++++++++----- 3 files changed, 14 insertions(+), 50 deletions(-) delete mode 100644 src/Responders/ScheduledEventUpdatedResponder.cs diff --git a/src/Responders/GuildLoadedResponder.cs b/src/Responders/GuildLoadedResponder.cs index cfc33fa..92fc009 100644 --- a/src/Responders/GuildLoadedResponder.cs +++ b/src/Responders/GuildLoadedResponder.cs @@ -50,19 +50,6 @@ public class GuildLoadedResponder : IResponder data.GetOrCreateMemberData(member.User.Value.ID); } - foreach (var schEvent in guild.GuildScheduledEvents) - { - if (!data.ScheduledEvents.TryGetValue(schEvent.ID.Value, out var eventData)) - { - continue; - } - - eventData.Name = schEvent.Name; - eventData.ScheduledStartTime = schEvent.ScheduledStartTime; - eventData.ScheduleOnStatusUpdated = eventData.Status != schEvent.Status; - eventData.Status = schEvent.Status; - } - if (!GuildSettings.ReceiveStartupMessages.Get(cfg)) { return Result.FromSuccess(); diff --git a/src/Responders/ScheduledEventUpdatedResponder.cs b/src/Responders/ScheduledEventUpdatedResponder.cs deleted file mode 100644 index 85162a4..0000000 --- a/src/Responders/ScheduledEventUpdatedResponder.cs +++ /dev/null @@ -1,30 +0,0 @@ -using JetBrains.Annotations; -using Octobot.Services; -using Remora.Discord.API.Abstractions.Gateway.Events; -using Remora.Discord.Gateway.Responders; -using Remora.Results; - -namespace Octobot.Responders; - -[UsedImplicitly] -public class ScheduledEventUpdatedResponder : IResponder -{ - private readonly GuildDataService _guildData; - - public ScheduledEventUpdatedResponder(GuildDataService guildData) - { - _guildData = guildData; - } - - public async Task RespondAsync(IGuildScheduledEventUpdate gatewayEvent, CancellationToken ct = default) - { - var data = await _guildData.GetData(gatewayEvent.GuildID, ct); - var eventData = data.ScheduledEvents[gatewayEvent.ID.Value]; - eventData.Name = gatewayEvent.Name; - eventData.ScheduledStartTime = gatewayEvent.ScheduledStartTime; - eventData.ScheduleOnStatusUpdated = eventData.Status != gatewayEvent.Status; - eventData.Status = gatewayEvent.Status; - - return Result.FromSuccess(); - } -} diff --git a/src/Services/Update/ScheduledEventUpdateService.cs b/src/Services/Update/ScheduledEventUpdateService.cs index 2a8beca..f83c1de 100644 --- a/src/Services/Update/ScheduledEventUpdateService.cs +++ b/src/Services/Update/ScheduledEventUpdateService.cs @@ -61,6 +61,8 @@ public sealed class ScheduledEventUpdateService : BackgroundService return Result.FromError(eventsResult); } + SyncScheduledEvents(data, events); + foreach (var storedEvent in data.ScheduledEvents.Values) { var scheduledEvent = TryGetScheduledEvent(events, storedEvent.Id); @@ -100,20 +102,25 @@ public sealed class ScheduledEventUpdateService : BackgroundService failedResults.AddIfFailed(statusUpdatedResponseResult); } - SyncScheduledEvents(data, events); - return failedResults.AggregateErrors(); } - private static void SyncScheduledEvents(GuildData data, IReadOnlyCollection events) + private static void SyncScheduledEvents(GuildData data, IEnumerable events) { - if (data.ScheduledEvents.Count < events.Count) + foreach (var @event in events) { - foreach (var @event in events.Where(@event => !data.ScheduledEvents.ContainsKey(@event.ID.Value))) + if (!data.ScheduledEvents.ContainsKey(@event.ID.Value)) { - data.ScheduledEvents.Add(@event.ID.Value, new ScheduledEventData(@event.ID.Value, - @event.Name, @event.ScheduledStartTime, @event.Status)); + data.ScheduledEvents.Add(@event.ID.Value, + new ScheduledEventData(@event.ID.Value, @event.Name, @event.ScheduledStartTime, @event.Status)); + continue; } + + var eventData = data.ScheduledEvents[@event.ID.Value]; + eventData.Name = @event.Name; + eventData.ScheduledStartTime = @event.ScheduledStartTime; + eventData.ScheduleOnStatusUpdated = eventData.Status != @event.Status; + eventData.Status = @event.Status; } }