diff --git a/Cajetan.OF-DL/Services/CajetanDbService.cs b/Cajetan.OF-DL/Services/CajetanDbService.cs index 8800c85..7ef65a5 100644 --- a/Cajetan.OF-DL/Services/CajetanDbService.cs +++ b/Cajetan.OF-DL/Services/CajetanDbService.cs @@ -21,6 +21,10 @@ public class CajetanDbService(IConfigService configService) await cmdInfo.ExecuteNonQueryAsync(); } + using (SqliteCommand cmdNonNude = new("CREATE TABLE IF NOT EXISTS user_non_nude (user_id INTEGER NOT NULL, name VARCHAR NOT NULL, PRIMARY KEY(user_id)", connection)) + { + await cmdNonNude.ExecuteNonQueryAsync(); + } } public async Task> GetUsersAsync() @@ -91,7 +95,7 @@ public class CajetanDbService(IConfigService configService) cmdInfo.Parameters.AddWithValue("@userId", userInfo.Id); cmdInfo.Parameters.AddWithValue("@name", userInfo.Name ?? userInfo.Username); - cmdInfo.Parameters.AddWithValue("@blob", Newtonsoft.Json.JsonConvert.SerializeObject(userInfo)); + cmdInfo.Parameters.AddWithValue("@blob", JsonConvert.SerializeObject(userInfo)); try { @@ -104,4 +108,36 @@ public class CajetanDbService(IConfigService configService) } } } + + public async Task UpdateNonNudeCollectionAsync(Dictionary usersInNonNudeLists) + { + if (usersInNonNudeLists.Count == 0) + return; + + await using SqliteConnection connection = new($"Data Source={Directory.GetCurrentDirectory()}/users.db"); + + Log.Debug("Database data source: " + connection.DataSource); + + foreach ((string username, long userId) in usersInNonNudeLists) + { + using SqliteCommand cmdInfo = new( + "INSERT OR REPLACE INTO user_non_nude (user_id, name) " + + "VALUES (@userId, @name);", + connection + ); + + cmdInfo.Parameters.AddWithValue("@userId", userId); + cmdInfo.Parameters.AddWithValue("@name", username); + + try + { + await cmdInfo.ExecuteNonQueryAsync(); + Log.Debug("Updating Non-Nude collection with: {Username:l}", username); + } + catch (Exception ex) + { + Log.Warning(ex, "Failed to update Non-Nude collection with: {Username:l}", username); + } + } + } } diff --git a/Cajetan.OF-DL/Services/ICajetanDbService.cs b/Cajetan.OF-DL/Services/ICajetanDbService.cs index 12e204e..d1a7c84 100644 --- a/Cajetan.OF-DL/Services/ICajetanDbService.cs +++ b/Cajetan.OF-DL/Services/ICajetanDbService.cs @@ -6,4 +6,5 @@ public interface ICajetanDbService : IDbService Task> GetUsersAsync(); Task UpdateUserInfoAsync(UserEntities.UserInfo? userInfo); + Task UpdateNonNudeCollectionAsync(Dictionary usersInNonNudeLists); } diff --git a/Cajetan.OF-DL/Worker.cs b/Cajetan.OF-DL/Worker.cs index e044d7e..3bae0fa 100644 --- a/Cajetan.OF-DL/Worker.cs +++ b/Cajetan.OF-DL/Worker.cs @@ -364,6 +364,8 @@ internal class Worker(IServiceProvider serviceProvider) await _dbService.CreateUsersDb(result.Users); await _dbService.InitializeUserInfoTablesAsync(); + await UpdateNonNudeListAsync(); + return result; async Task FetchUsersAsync() @@ -390,6 +392,28 @@ internal class Worker(IServiceProvider serviceProvider) result.Lists = await _apiService.GetLists("/lists") ?? []; } + async Task UpdateNonNudeListAsync() + { + const string LIST_NAME = "NonNude"; + const long LIST_ID = 1220021758; + + HashSet listNames = new(StringComparer.OrdinalIgnoreCase); + + if (result.Lists.ContainsKey(LIST_NAME)) + listNames.Add(LIST_NAME); + + string? nameById = result.Lists.FirstOrDefault(l => l.Value == LIST_ID).Key; + if (!string.IsNullOrWhiteSpace(nameById)) + listNames.Add(nameById); + + Dictionary usersInNonNudeLists = await GetUsersFromSpecificListsAsync(result, [.. listNames]); + + AnsiConsole.Markup($"[green]Updating Non-Nude collection with {usersInNonNudeLists.Count} Users[/]"); + await _dbService.UpdateNonNudeCollectionAsync(usersInNonNudeLists); + + AnsiConsole.WriteLine(); + } + void AddToResult(Dictionary? subscriptions) { foreach ((string username, long userId) in subscriptions ?? [])