From 53179abfbb7e4967cff570924eb3a816568e86c4 Mon Sep 17 00:00:00 2001
From: Toastie <toastie@toastiet0ast.com>
Date: Wed, 29 Jan 2025 23:35:43 +1300
Subject: [PATCH] done some todos

---
 .../Administration/Mute/MuteService.cs        |  4 +-
 .../Protection/ProtectionService.cs           | 26 ++++++++++--
 .../Modules/Gambling/Shop/ShopService.cs      | 25 ++++++-----
 .../StreamNotificationService.cs              | 41 +++++++++----------
 4 files changed, 58 insertions(+), 38 deletions(-)

diff --git a/src/EllieBot/Modules/Administration/Mute/MuteService.cs b/src/EllieBot/Modules/Administration/Mute/MuteService.cs
index d061be5..e328691 100644
--- a/src/EllieBot/Modules/Administration/Mute/MuteService.cs
+++ b/src/EllieBot/Modules/Administration/Mute/MuteService.cs
@@ -316,7 +316,7 @@ public class MuteService : IEService, IReadyExecutor
         StartUn_Timer(guild.Id, userId, after, TimerType.Ban); // start the timer
     }
 
-    // todo unrole timers -> temprole
+    // todo UN* unrole timers -> temprole
 
     public void StartUn_Timer(
         ulong guildId,
@@ -410,7 +410,7 @@ public class MuteService : IEService, IReadyExecutor
     }
 
 
-    // todo update to new way of tracking expiries
+    // todo UN* update to new way of tracking expiries
     public async Task OnReadyAsync()
     {
         await using var uow = _db.GetDbContext();
diff --git a/src/EllieBot/Modules/Administration/Protection/ProtectionService.cs b/src/EllieBot/Modules/Administration/Protection/ProtectionService.cs
index 4a9890a..f83ba30 100644
--- a/src/EllieBot/Modules/Administration/Protection/ProtectionService.cs
+++ b/src/EllieBot/Modules/Administration/Protection/ProtectionService.cs
@@ -274,7 +274,25 @@ public class ProtectionService : IReadyExecutor, IEService
 
         await using var uow = _db.GetDbContext();
 
-        // todo finish this
+        await uow.GetTable<AntiRaidSetting>()
+           .InsertOrUpdateAsync(() => new()
+           {
+               GuildId = guildId,
+               Action = action,
+               Seconds = seconds,
+               UserThreshold = userThreshold,
+               PunishDuration = minutesDuration
+           }, _ => new()
+           {
+               Action = action,
+               Seconds = seconds,
+               UserThreshold = userThreshold,
+               PunishDuration = minutesDuration
+           }, () => new()
+           {
+               GuildId = guildId
+           });
+
 
         return stats;
     }
@@ -333,7 +351,7 @@ public class ProtectionService : IReadyExecutor, IEService
             }
         };
 
-        stats = _antiSpamGuilds.AddOrUpdate(guildId,
+        _antiSpamGuilds.AddOrUpdate(guildId,
             stats,
             (_, old) =>
             {
@@ -371,16 +389,18 @@ public class ProtectionService : IReadyExecutor, IEService
         {
             ChannelId = channelId
         };
+
         await using var uow = _db.GetDbContext();
         var spam = await uow.GetTable<AntiSpamSetting>()
             .Include(x => x.IgnoredChannels)
             .Where(x => x.GuildId == guildId)
             .FirstOrDefaultAsyncEF();
+
         if (spam is null)
             return null;
 
         var added = false;
-        if (!spam.IgnoredChannels.Any(x => x.ChannelId == channelId))
+        if (spam.IgnoredChannels.All(x => x.ChannelId != channelId))
         {
             if (_antiSpamGuilds.TryGetValue(guildId, out var temp))
                 temp.AntiSpamSettings.IgnoredChannels.Add(obj); // add to local cache
diff --git a/src/EllieBot/Modules/Gambling/Shop/ShopService.cs b/src/EllieBot/Modules/Gambling/Shop/ShopService.cs
index b936897..e36afcd 100644
--- a/src/EllieBot/Modules/Gambling/Shop/ShopService.cs
+++ b/src/EllieBot/Modules/Gambling/Shop/ShopService.cs
@@ -15,9 +15,9 @@ public class ShopService : IShopService, IEService
 
     private async Task<IndexedCollection<ShopEntry>> GetEntriesInternal(DbContext uow, ulong guildId)
     {
-        var items = await uow.GetTable<ShopEntry>()
+        var items = await uow.Set<ShopEntry>()
                              .Where(x => x.GuildId == guildId)
-                             .ToListAsyncLinqToDB();
+                             .ToListAsyncEF();
 
         return items.ToIndexed();
     }
@@ -46,6 +46,8 @@ public class ShopService : IShopService, IEService
         if (string.IsNullOrWhiteSpace(newName))
             throw new ArgumentNullException(nameof(newName));
 
+        newName = newName?.TrimTo(100);
+
         await using var uow = _db.GetDbContext();
 
         var changed = await uow.GetTable<ShopEntry>()
@@ -71,13 +73,7 @@ public class ShopService : IShopService, IEService
         entries[index1].Index = index2;
         entries[index2].Index = index1;
 
-        // todo fix swap
-        await uow.GetTable<ShopEntry>()
-                 .Where(x => x.GuildId == guildId)
-                 .UpdateAsync(x => new ShopEntry()
-                 {
-                     Index = x.Index == index1 ? index2 : x.Index == index2 ? index1 : x.Index,
-                 });
+        await uow.SaveChangesAsync();
 
         return true;
     }
@@ -88,9 +84,16 @@ public class ShopService : IShopService, IEService
         ArgumentOutOfRangeException.ThrowIfNegative(toIndex);
 
         await using var uow = _db.GetDbContext();
-        var entries = GetEntriesInternal(uow, guildId);
+        var entries = await GetEntriesInternal(uow, guildId);
 
-        // todo move
+        if (fromIndex >= entries.Count || toIndex >= entries.Count || fromIndex == toIndex)
+            return false;
+
+        var entry = entries[fromIndex];
+        entries.RemoveAt(fromIndex);
+        entries.Insert(toIndex, entry);
+
+        await uow.SaveChangesAsync();
         return true;
     }
 
diff --git a/src/EllieBot/Modules/Searches/StreamNotification/StreamNotificationService.cs b/src/EllieBot/Modules/Searches/StreamNotification/StreamNotificationService.cs
index d8d074d..f4a3c59 100644
--- a/src/EllieBot/Modules/Searches/StreamNotification/StreamNotificationService.cs
+++ b/src/EllieBot/Modules/Searches/StreamNotification/StreamNotificationService.cs
@@ -94,7 +94,6 @@ public sealed class StreamNotificationService : IEService, IReadyExecutor
     private async Task InitStateAsync()
     {
         await using var uow = _db.GetDbContext();
-        // todo check all guilds on shard for correct param order
         var notifyOffline = await uow.GetTable<GuildConfig>()
                                      .Where(gc => gc.NotifyStreamOffline)
                                      .Select(x => x.GuildId)
@@ -350,28 +349,26 @@ public sealed class StreamNotificationService : IEService, IReadyExecutor
 
     private async Task ClientOnJoinedGuild(SocketGuild guild)
     {
-        await using (var uow = _db.GetDbContext())
+        await using var uow = _db.GetDbContext();
+        var fs = await uow.Set<FollowedStream>()
+                          .Where(x => x.GuildId == guild.Id)
+                          .ToListAsyncLinqToDB();
+
+        var notifyOffline = await uow.GetTable<GuildConfig>()
+                                     .Where(x => x.GuildId == guild.Id)
+                                     .Select(x => x.NotifyStreamOffline)
+                                     .FirstOrDefaultAsyncLinqToDB();
+
+        // todo hashset
+        if (notifyOffline)
+            _offlineNotificationServers.Add(guild.Id);
+
+        foreach (var followedStream in fs)
         {
-            var fs = await uow.Set<FollowedStream>()
-                              .Where(x => x.GuildId == guild.Id)
-                              .ToListAsyncLinqToDB();
-
-            var notifyOffline = await uow.GetTable<GuildConfig>()
-                                         .Where(x => x.GuildId == guild.Id)
-                                         .Select(x => x.NotifyStreamOffline)
-                                         .FirstOrDefaultAsyncLinqToDB();
-
-            // todo hashset
-            if (notifyOffline)
-                _offlineNotificationServers.Add(guild.Id);
-
-            foreach (var followedStream in fs)
-            {
-                var key = followedStream.CreateKey();
-                var streams = GetLocalGuildStreams(key, guild.Id);
-                streams.Add(followedStream);
-                PublishFollowStream(followedStream);
-            }
+            var key = followedStream.CreateKey();
+            var streams = GetLocalGuildStreams(key, guild.Id);
+            streams.Add(followedStream);
+            PublishFollowStream(followedStream);
         }
     }