refactorization + fix boostrapper
This commit is contained in:
parent
8d0a612238
commit
f1a7146c67
4 changed files with 182 additions and 122 deletions
148
src/BSLManager/App.cs
Normal file
148
src/BSLManager/App.cs
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BirdsiteLive.DAL.Contracts;
|
||||||
|
using BirdsiteLive.Moderation.Actions;
|
||||||
|
using Terminal.Gui;
|
||||||
|
|
||||||
|
namespace BSLManager
|
||||||
|
{
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
private readonly IFollowersDal _followersDal;
|
||||||
|
private readonly IRemoveFollowerAction _removeFollowerAction;
|
||||||
|
|
||||||
|
#region Ctor
|
||||||
|
public App(IFollowersDal followersDal, IRemoveFollowerAction removeFollowerAction)
|
||||||
|
{
|
||||||
|
_followersDal = followersDal;
|
||||||
|
_removeFollowerAction = removeFollowerAction;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public void Run()
|
||||||
|
{
|
||||||
|
Application.Init();
|
||||||
|
var top = Application.Top;
|
||||||
|
|
||||||
|
// Creates the top-level window to show
|
||||||
|
var win = new Window("BSL Manager")
|
||||||
|
{
|
||||||
|
X = 0,
|
||||||
|
Y = 1, // Leave one row for the toplevel menu
|
||||||
|
|
||||||
|
// By using Dim.Fill(), it will automatically resize without manual intervention
|
||||||
|
Width = Dim.Fill(),
|
||||||
|
Height = Dim.Fill()
|
||||||
|
};
|
||||||
|
|
||||||
|
top.Add(win);
|
||||||
|
|
||||||
|
// Creates a menubar, the item "New" has a help menu.
|
||||||
|
var menu = new MenuBar(new MenuBarItem[]
|
||||||
|
{
|
||||||
|
new MenuBarItem("_File", new MenuItem[]
|
||||||
|
{
|
||||||
|
new MenuItem("_Quit", "", () =>
|
||||||
|
{
|
||||||
|
if (Quit()) top.Running = false;
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
//new MenuBarItem ("_Edit", new MenuItem [] {
|
||||||
|
// new MenuItem ("_Copy", "", null),
|
||||||
|
// new MenuItem ("C_ut", "", null),
|
||||||
|
// new MenuItem ("_Paste", "", null)
|
||||||
|
//})
|
||||||
|
});
|
||||||
|
top.Add(menu);
|
||||||
|
|
||||||
|
static bool Quit()
|
||||||
|
{
|
||||||
|
var n = MessageBox.Query(50, 7, "Quit BSL Manager", "Are you sure you want to quit?", "Yes", "No");
|
||||||
|
return n == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var listData = new List<string>();
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
X = 1,
|
||||||
|
Y = 2,
|
||||||
|
Width = Dim.Fill(),
|
||||||
|
Height = Dim.Fill()
|
||||||
|
};
|
||||||
|
|
||||||
|
list.KeyDown += _ =>
|
||||||
|
{
|
||||||
|
if (_.KeyEvent.Key == Key.Enter)
|
||||||
|
{
|
||||||
|
var el = list.SelectedItem;
|
||||||
|
|
||||||
|
bool okpressed = false;
|
||||||
|
var ok = new Button(10, 14, "Yes");
|
||||||
|
ok.Clicked += () =>
|
||||||
|
{
|
||||||
|
Application.RequestStop();
|
||||||
|
okpressed = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
var cancel = new Button(3, 14, "No");
|
||||||
|
cancel.Clicked += () => Application.RequestStop();
|
||||||
|
|
||||||
|
var dialog = new Dialog("Delete", 60, 18, cancel, ok);
|
||||||
|
|
||||||
|
var name = new Label($"User: {listData[el]}")
|
||||||
|
{
|
||||||
|
X = 1,
|
||||||
|
Y = 1,
|
||||||
|
Width = Dim.Fill(),
|
||||||
|
Height = 1
|
||||||
|
};
|
||||||
|
var entry = new Label("Delete user and remove all their followings?")
|
||||||
|
{
|
||||||
|
X = 1,
|
||||||
|
Y = 3,
|
||||||
|
Width = Dim.Fill(),
|
||||||
|
Height = 1
|
||||||
|
};
|
||||||
|
dialog.Add(name);
|
||||||
|
dialog.Add(entry);
|
||||||
|
Application.Run(dialog);
|
||||||
|
|
||||||
|
if (okpressed)
|
||||||
|
{
|
||||||
|
listData.RemoveAt(el);
|
||||||
|
RefreshUI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add some controls,
|
||||||
|
win.Add(
|
||||||
|
new Label(1, 0, "Listing followers"),
|
||||||
|
list
|
||||||
|
);
|
||||||
|
|
||||||
|
Application.Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RefreshUI()
|
||||||
|
{
|
||||||
|
typeof(Application).GetMethod("TerminalResized", BindingFlags.Static | BindingFlags.NonPublic)
|
||||||
|
.Invoke(null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\BirdsiteLive.Common\BirdsiteLive.Common.csproj" />
|
<ProjectReference Include="..\BirdsiteLive.Common\BirdsiteLive.Common.csproj" />
|
||||||
|
<ProjectReference Include="..\BirdsiteLive.Moderation\BirdsiteLive.Moderation.csproj" />
|
||||||
<ProjectReference Include="..\DataAccessLayers\BirdsiteLive.DAL.Postgres\BirdsiteLive.DAL.Postgres.csproj" />
|
<ProjectReference Include="..\DataAccessLayers\BirdsiteLive.DAL.Postgres\BirdsiteLive.DAL.Postgres.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
using BirdsiteLive.Common.Settings;
|
using BirdsiteLive.Common.Settings;
|
||||||
using BirdsiteLive.Common.Structs;
|
using BirdsiteLive.Common.Structs;
|
||||||
using BirdsiteLive.DAL.Contracts;
|
using BirdsiteLive.DAL.Contracts;
|
||||||
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
||||||
using BirdsiteLive.DAL.Postgres.Settings;
|
using BirdsiteLive.DAL.Postgres.Settings;
|
||||||
using Lamar;
|
using Lamar;
|
||||||
|
using Lamar.Scanning.Conventions;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace BSLManager
|
namespace BSLManager
|
||||||
{
|
{
|
||||||
|
@ -41,14 +45,19 @@ namespace BSLManager
|
||||||
throw new NotImplementedException($"{_settings.Type} is not supported");
|
throw new NotImplementedException($"{_settings.Type} is not supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
|
||||||
|
x.For<IHttpClientFactory>().Use(_ => serviceProvider.GetService<IHttpClientFactory>());
|
||||||
|
|
||||||
|
x.For(typeof(ILogger<>)).Use(typeof(DummyLogger<>));
|
||||||
|
|
||||||
x.Scan(_ =>
|
x.Scan(_ =>
|
||||||
{
|
{
|
||||||
//_.Assembly("BirdsiteLive.Twitter");
|
_.Assembly("BirdsiteLive.Twitter");
|
||||||
//_.Assembly("BirdsiteLive.Domain");
|
_.Assembly("BirdsiteLive.Domain");
|
||||||
_.Assembly("BirdsiteLive.DAL");
|
_.Assembly("BirdsiteLive.DAL");
|
||||||
_.Assembly("BirdsiteLive.DAL.Postgres");
|
_.Assembly("BirdsiteLive.DAL.Postgres");
|
||||||
//_.Assembly("BirdsiteLive.Moderation");
|
_.Assembly("BirdsiteLive.Moderation");
|
||||||
//_.Assembly("BirdsiteLive.Pipeline");
|
|
||||||
_.TheCallingAssembly();
|
_.TheCallingAssembly();
|
||||||
|
|
||||||
_.WithDefaultConventions();
|
_.WithDefaultConventions();
|
||||||
|
@ -58,5 +67,22 @@ namespace BSLManager
|
||||||
});
|
});
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class DummyLogger<T> : ILogger<T>
|
||||||
|
{
|
||||||
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsEnabled(LogLevel logLevel)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IDisposable BeginScope<TState>(TState state)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -23,9 +23,8 @@ namespace BSLManager
|
||||||
var bootstrapper = new Bootstrapper(settings);
|
var bootstrapper = new Bootstrapper(settings);
|
||||||
var container = bootstrapper.Init();
|
var container = bootstrapper.Init();
|
||||||
|
|
||||||
var followersDal = container.GetInstance<IFollowersDal>();
|
var app = container.GetInstance<App>();
|
||||||
|
app.Run();
|
||||||
await LaunchAppAsync(followersDal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DbSettings GetSettings()
|
private static DbSettings GetSettings()
|
||||||
|
@ -37,119 +36,5 @@ namespace BSLManager
|
||||||
var dbSettings = configuration.GetSection("Db").Get<DbSettings>();
|
var dbSettings = configuration.GetSection("Db").Get<DbSettings>();
|
||||||
return dbSettings;
|
return dbSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task LaunchAppAsync(IFollowersDal followersDal)
|
|
||||||
{
|
|
||||||
var followers = await followersDal.GetAllFollowersAsync();
|
|
||||||
var orderedFollowers = followers.OrderByDescending(x => x.Followings.Count).ToList();
|
|
||||||
|
|
||||||
Application.Init();
|
|
||||||
var top = Application.Top;
|
|
||||||
|
|
||||||
// Creates the top-level window to show
|
|
||||||
var win = new Window("BSL Manager")
|
|
||||||
{
|
|
||||||
X = 0,
|
|
||||||
Y = 1, // Leave one row for the toplevel menu
|
|
||||||
|
|
||||||
// By using Dim.Fill(), it will automatically resize without manual intervention
|
|
||||||
Width = Dim.Fill(),
|
|
||||||
Height = Dim.Fill()
|
|
||||||
};
|
|
||||||
|
|
||||||
top.Add(win);
|
|
||||||
|
|
||||||
// Creates a menubar, the item "New" has a help menu.
|
|
||||||
var menu = new MenuBar(new MenuBarItem[]
|
|
||||||
{
|
|
||||||
new MenuBarItem("_File", new MenuItem[]
|
|
||||||
{
|
|
||||||
new MenuItem("_Quit", "", () =>
|
|
||||||
{
|
|
||||||
if (Quit()) top.Running = false;
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
//new MenuBarItem ("_Edit", new MenuItem [] {
|
|
||||||
// new MenuItem ("_Copy", "", null),
|
|
||||||
// new MenuItem ("C_ut", "", null),
|
|
||||||
// new MenuItem ("_Paste", "", null)
|
|
||||||
//})
|
|
||||||
});
|
|
||||||
top.Add(menu);
|
|
||||||
|
|
||||||
static bool Quit()
|
|
||||||
{
|
|
||||||
var n = MessageBox.Query(50, 7, "Quit BSL Manager", "Are you sure you want to quit?", "Yes", "No");
|
|
||||||
return n == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
var listData = new List<string>();
|
|
||||||
foreach (var follower in orderedFollowers)
|
|
||||||
{
|
|
||||||
listData.Add($"@{follower.Acct}@{follower.Host} {follower.Followings.Count}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var list = new ListView(listData)
|
|
||||||
{
|
|
||||||
X = 1,
|
|
||||||
Y = 2,
|
|
||||||
Width = Dim.Fill(),
|
|
||||||
Height = Dim.Fill()
|
|
||||||
};
|
|
||||||
|
|
||||||
list.KeyDown += _ =>
|
|
||||||
{
|
|
||||||
if (_.KeyEvent.Key == Key.Enter)
|
|
||||||
{
|
|
||||||
var el = list.SelectedItem;
|
|
||||||
|
|
||||||
bool okpressed = false;
|
|
||||||
var ok = new Button(10, 14, "Yes");
|
|
||||||
ok.Clicked += () =>
|
|
||||||
{
|
|
||||||
Application.RequestStop();
|
|
||||||
okpressed = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
var cancel = new Button(3, 14, "No");
|
|
||||||
cancel.Clicked += () => Application.RequestStop();
|
|
||||||
|
|
||||||
var dialog = new Dialog("Delete", 60, 18, cancel, ok);
|
|
||||||
|
|
||||||
var name = new Label($"User: {listData[el]}")
|
|
||||||
{
|
|
||||||
X = 1,
|
|
||||||
Y = 1,
|
|
||||||
Width = Dim.Fill(),
|
|
||||||
Height = 1
|
|
||||||
};
|
|
||||||
var entry = new Label("Delete user and remove all their followings?")
|
|
||||||
{
|
|
||||||
X = 1,
|
|
||||||
Y = 3,
|
|
||||||
Width = Dim.Fill(),
|
|
||||||
Height = 1
|
|
||||||
};
|
|
||||||
dialog.Add(name);
|
|
||||||
dialog.Add(entry);
|
|
||||||
Application.Run(dialog);
|
|
||||||
|
|
||||||
if (okpressed)
|
|
||||||
{
|
|
||||||
listData.RemoveAt(el);
|
|
||||||
typeof(Application).GetMethod("TerminalResized", BindingFlags.Static | BindingFlags.NonPublic)
|
|
||||||
.Invoke(null, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add some controls,
|
|
||||||
win.Add(
|
|
||||||
new Label(1, 0, "Listing followers"),
|
|
||||||
list
|
|
||||||
);
|
|
||||||
|
|
||||||
Application.Run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue