From 25221c33e0489b6a987f2d1db3ca6b88ce2ed18a Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sun, 11 Apr 2021 18:49:12 -0400 Subject: [PATCH] user removal functionnal --- src/BSLManager/App.cs | 58 ++++++++++++++++++++++---------- src/BSLManager/BSLManager.csproj | 6 ++++ src/BSLManager/Bootstrapper.cs | 18 ++++++---- src/BSLManager/Program.cs | 19 ++++------- 4 files changed, 65 insertions(+), 36 deletions(-) diff --git a/src/BSLManager/App.cs b/src/BSLManager/App.cs index 3979ab6..12b2970 100644 --- a/src/BSLManager/App.cs +++ b/src/BSLManager/App.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Reflection; using System.Threading.Tasks; using BirdsiteLive.DAL.Contracts; +using BirdsiteLive.DAL.Models; using BirdsiteLive.Moderation.Actions; using Terminal.Gui; @@ -13,6 +14,9 @@ namespace BSLManager private readonly IFollowersDal _followersDal; private readonly IRemoveFollowerAction _removeFollowerAction; + private List _displayableUserList = new List(); + private List _sourceUserList = new List(); + #region Ctor public App(IFollowersDal followersDal, IRemoveFollowerAction removeFollowerAction) { @@ -62,22 +66,10 @@ namespace BSLManager var n = MessageBox.Query(50, 7, "Quit BSL Manager", "Are you sure you want to quit?", "Yes", "No"); return n == 0; } + + RetrieveUserList(); - var listData = new List(); - - Application.MainLoop.Invoke(async () => { - var followers = await _followersDal.GetAllFollowersAsync(); - var orderedFollowers = followers.OrderByDescending(x => x.Followings.Count).ToList(); - - foreach (var follower in orderedFollowers) - { - listData.Add($"@{follower.Acct}@{follower.Host} {follower.Followings.Count}"); - } - - RefreshUI(); - }); - - var list = new ListView(listData) + var list = new ListView(_displayableUserList) { X = 1, Y = 2, @@ -104,7 +96,7 @@ namespace BSLManager var dialog = new Dialog("Delete", 60, 18, cancel, ok); - var name = new Label($"User: {listData[el]}") + var name = new Label($"User: {_displayableUserList[el]}") { X = 1, Y = 1, @@ -124,8 +116,7 @@ namespace BSLManager if (okpressed) { - listData.RemoveAt(el); - RefreshUI(); + DeleteAndRemoveUser(el); } } }; @@ -139,6 +130,37 @@ namespace BSLManager Application.Run(); } + private void DeleteAndRemoveUser(int el) + { + Application.MainLoop.Invoke(async () => + { + var userToDelete = _sourceUserList[el]; + + await _removeFollowerAction.ProcessAsync(userToDelete); + + _sourceUserList.RemoveAt(el); + _displayableUserList.RemoveAt(el); + RefreshUI(); + }); + } + + private void RetrieveUserList() + { + Application.MainLoop.Invoke(async () => + { + var followers = await _followersDal.GetAllFollowersAsync(); + _sourceUserList = followers.OrderByDescending(x => x.Followings.Count).ToList(); + + _displayableUserList.Clear(); + foreach (var follower in _sourceUserList) + { + _displayableUserList.Add($"@{follower.Acct}@{follower.Host} {follower.Followings.Count}"); + } + + RefreshUI(); + }); + } + private void RefreshUI() { typeof(Application).GetMethod("TerminalResized", BindingFlags.Static | BindingFlags.NonPublic) diff --git a/src/BSLManager/BSLManager.csproj b/src/BSLManager/BSLManager.csproj index 8a3ef97..52e5cde 100644 --- a/src/BSLManager/BSLManager.csproj +++ b/src/BSLManager/BSLManager.csproj @@ -19,4 +19,10 @@ + + + PreserveNewest + + + diff --git a/src/BSLManager/Bootstrapper.cs b/src/BSLManager/Bootstrapper.cs index 1698d9a..7375cd6 100644 --- a/src/BSLManager/Bootstrapper.cs +++ b/src/BSLManager/Bootstrapper.cs @@ -14,12 +14,14 @@ namespace BSLManager { public class Bootstrapper { - private readonly DbSettings _settings; + private readonly DbSettings _dbSettings; + private readonly InstanceSettings _instanceSettings; #region Ctor - public Bootstrapper(DbSettings settings) + public Bootstrapper(DbSettings dbSettings, InstanceSettings instanceSettings) { - _settings = settings; + _dbSettings = dbSettings; + _instanceSettings = instanceSettings; } #endregion @@ -27,9 +29,13 @@ namespace BSLManager { var container = new Container(x => { - if (string.Equals(_settings.Type, DbTypes.Postgres, StringComparison.OrdinalIgnoreCase)) + x.For().Use(x => _dbSettings); + + x.For().Use(x => _instanceSettings); + + if (string.Equals(_dbSettings.Type, DbTypes.Postgres, StringComparison.OrdinalIgnoreCase)) { - var connString = $"Host={_settings.Host};Username={_settings.User};Password={_settings.Password};Database={_settings.Name}"; + var connString = $"Host={_dbSettings.Host};Username={_dbSettings.User};Password={_dbSettings.Password};Database={_dbSettings.Name}"; var postgresSettings = new PostgresSettings { ConnString = connString @@ -42,7 +48,7 @@ namespace BSLManager } else { - throw new NotImplementedException($"{_settings.Type} is not supported"); + throw new NotImplementedException($"{_dbSettings.Type} is not supported"); } var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider(); diff --git a/src/BSLManager/Program.cs b/src/BSLManager/Program.cs index 9fc3c04..ea2569c 100644 --- a/src/BSLManager/Program.cs +++ b/src/BSLManager/Program.cs @@ -18,23 +18,18 @@ namespace BSLManager { Console.OutputEncoding = Encoding.Default; - var settings = GetSettings(); - - var bootstrapper = new Bootstrapper(settings); - var container = bootstrapper.Init(); - - var app = container.GetInstance(); - app.Run(); - } - - private static DbSettings GetSettings() - { var builder = new ConfigurationBuilder() .AddEnvironmentVariables(); var configuration = builder.Build(); var dbSettings = configuration.GetSection("Db").Get(); - return dbSettings; + var instanceSettings = configuration.GetSection("Instance").Get(); + + var bootstrapper = new Bootstrapper(dbSettings, instanceSettings); + var container = bootstrapper.Init(); + + var app = container.GetInstance(); + app.Run(); } } }