Split dangerous commands into cleanup commands for each module they belong to

This commit is contained in:
Kwoth
2023-07-15 13:39:46 +00:00
parent 842a8a2f71
commit 8f62a46016
91 changed files with 616 additions and 501 deletions

View File

@@ -30,14 +30,15 @@ public class FeedsService : INService
using (var uow = db.GetDbContext())
{
var guildConfigIds = bot.AllGuildConfigs.Select(x => x.Id).ToList();
_subs = uow.GuildConfigs.AsQueryable()
.Where(x => guildConfigIds.Contains(x.Id))
.Include(x => x.FeedSubs)
.ToList()
.SelectMany(x => x.FeedSubs)
.GroupBy(x => x.Url.ToLower())
.ToDictionary(x => x.Key, x => x.ToList())
.ToConcurrent();
_subs = uow.Set<GuildConfig>()
.AsQueryable()
.Where(x => guildConfigIds.Contains(x.Id))
.Include(x => x.FeedSubs)
.ToList()
.SelectMany(x => x.FeedSubs)
.GroupBy(x => x.Url.ToLower())
.ToDictionary(x => x.Key, x => x.ToList())
.ToConcurrent();
}
_client = client;
@@ -60,8 +61,8 @@ public class FeedsService : INService
// remove from db
await using var ctx = _db.GetDbContext();
await ctx.GetTable<FeedSub>()
.DeleteAsync(x => ids.Contains(x.Id));
.DeleteAsync(x => ids.Contains(x.Id));
// remove from the local cache
_subs.TryRemove(url, out _);
@@ -94,14 +95,14 @@ public class FeedsService : INService
var feed = await FeedReader.ReadAsync(rssUrl);
var items = feed
.Items.Select(item => (Item: item,
LastUpdate: item.PublishingDate?.ToUniversalTime()
?? (item.SpecificItem as AtomFeedItem)?.UpdatedDate?.ToUniversalTime()))
.Where(data => data.LastUpdate is not null)
.Select(data => (data.Item, LastUpdate: (DateTime)data.LastUpdate))
.OrderByDescending(data => data.LastUpdate)
.Reverse() // start from the oldest
.ToList();
.Items.Select(item => (Item: item,
LastUpdate: item.PublishingDate?.ToUniversalTime()
?? (item.SpecificItem as AtomFeedItem)?.UpdatedDate?.ToUniversalTime()))
.Where(data => data.LastUpdate is not null)
.Select(data => (data.Item, LastUpdate: (DateTime)data.LastUpdate))
.OrderByDescending(data => data.LastUpdate)
.Reverse() // start from the oldest
.ToList();
if (!_lastPosts.TryGetValue(kvp.Key, out var lastFeedUpdate))
{
@@ -140,12 +141,12 @@ public class FeedsService : INService
if (!gotImage && feedItem.SpecificItem is AtomFeedItem afi)
{
var previewElement = afi.Element.Elements()
.FirstOrDefault(x => x.Name.LocalName == "preview");
.FirstOrDefault(x => x.Name.LocalName == "preview");
if (previewElement is null)
{
previewElement = afi.Element.Elements()
.FirstOrDefault(x => x.Name.LocalName == "thumbnail");
.FirstOrDefault(x => x.Name.LocalName == "thumbnail");
}
if (previewElement is not null)
@@ -184,7 +185,7 @@ public class FeedsService : INService
catch (Exception ex)
{
var errorCount = await AddError(rssUrl, kvp.Value.Select(x => x.Id).ToList());
Log.Warning("An error occured while getting rss stream ({ErrorCount} / 100) {RssFeed}"
+ "\n {Message}",
errorCount,
@@ -201,8 +202,8 @@ public class FeedsService : INService
{
using var uow = _db.GetDbContext();
return uow.GuildConfigsForId(guildId, set => set.Include(x => x.FeedSubs))
.FeedSubs.OrderBy(x => x.Id)
.ToList();
.FeedSubs.OrderBy(x => x.Id)
.ToList();
}
public FeedAddResult AddFeed(ulong guildId, ulong channelId, string rssFeed, string message)
@@ -250,8 +251,8 @@ public class FeedsService : INService
using var uow = _db.GetDbContext();
var items = uow.GuildConfigsForId(guildId, set => set.Include(x => x.FeedSubs))
.FeedSubs.OrderBy(x => x.Id)
.ToList();
.FeedSubs.OrderBy(x => x.Id)
.ToList();
if (items.Count <= index)
return false;

View File

@@ -321,7 +321,7 @@ public sealed class StreamNotificationService : INService, IReadyExecutor
{
using (var uow = _db.GetDbContext())
{
var gc = uow.GuildConfigs.AsQueryable()
var gc = uow.Set<GuildConfig>().AsQueryable()
.Include(x => x.FollowedStreams)
.FirstOrDefault(x => x.GuildId == guildConfig.GuildId);

View File

@@ -36,7 +36,7 @@ public sealed class TranslateService : ITranslateService, IExecNoCommand, IReady
await using (var ctx = _db.GetDbContext())
{
var guilds = _bot.AllGuildConfigs.Select(x => x.GuildId).ToList();
cs = await ctx.AutoTranslateChannels.Include(x => x.Users)
cs = await ctx.Set<AutoTranslateChannel>().Include(x => x.Users)
.Where(x => guilds.Contains(x.GuildId))
.ToListAsyncEF();
}
@@ -108,12 +108,12 @@ public sealed class TranslateService : ITranslateService, IExecNoCommand, IReady
{
await using var ctx = _db.GetDbContext();
var old = await ctx.AutoTranslateChannels.ToLinqToDBTable()
var old = await ctx.Set<AutoTranslateChannel>().ToLinqToDBTable()
.FirstOrDefaultAsyncLinqToDB(x => x.ChannelId == channelId);
if (old is null)
{
ctx.AutoTranslateChannels.Add(new()
ctx.Set<AutoTranslateChannel>().Add(new()
{
GuildId = guildId,
ChannelId = channelId,
@@ -138,7 +138,7 @@ public sealed class TranslateService : ITranslateService, IExecNoCommand, IReady
return true;
}
await ctx.AutoTranslateChannels.ToLinqToDBTable().DeleteAsync(x => x.ChannelId == channelId);
await ctx.Set<AutoTranslateChannel>().ToLinqToDBTable().DeleteAsync(x => x.ChannelId == channelId);
await ctx.SaveChangesAsync();
_atcs.TryRemove(channelId, out _);
@@ -168,7 +168,7 @@ public sealed class TranslateService : ITranslateService, IExecNoCommand, IReady
return null;
await using var ctx = _db.GetDbContext();
var ch = await ctx.AutoTranslateChannels.GetByChannelId(channelId);
var ch = await ctx.Set<AutoTranslateChannel>().GetByChannelId(channelId);
if (ch is null)
return null;
@@ -210,7 +210,7 @@ public sealed class TranslateService : ITranslateService, IExecNoCommand, IReady
public async Task<bool> UnregisterUser(ulong channelId, ulong userId)
{
await using var ctx = _db.GetDbContext();
var rows = await ctx.AutoTranslateUsers.ToLinqToDBTable()
var rows = await ctx.Set<AutoTranslateUser>().ToLinqToDBTable()
.DeleteAsync(x => x.UserId == userId && x.Channel.ChannelId == channelId);
if (_users.TryGetValue(channelId, out var inner))