1
0
mirror of https://github.com/chylex/Discord-History-Tracker.git synced 2024-11-25 22:42:51 +01:00

Compare commits

...

8 Commits

130 changed files with 4817 additions and 4947 deletions

View File

@ -19,7 +19,7 @@ Folder organization:
* `lib/` contains utilities required to build the project * `lib/` contains utilities required to build the project
* `web/` contains source code of the [official website](https://dht.chylex.com), which can be used as a template when making your own website * `web/` contains source code of the [official website](https://dht.chylex.com), which can be used as a template when making your own website
To start editing source code for the desktop app, install the [.NET 5 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/5.0), and then open `app/DiscordHistoryTracker.sln` in [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Rider](https://www.jetbrains.com/rider/). To start editing source code for the desktop app, install the [.NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0), and then open `app/DiscordHistoryTracker.sln` in [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Rider](https://www.jetbrains.com/rider/).
### Building ### Building
@ -50,6 +50,6 @@ Run the `app/build.sh` script, and read the [Distribution](#distribution) sectio
#### Distribution #### Distribution
The mentioned build scripts will prepare `Release` builds ready for distribution. Once the script finishes, the `app/bin` folder will contain self-contained executables for each major operating system, and a portable version that works on all other systems but requires .NET 5 to be installed. The mentioned build scripts will prepare `Release` builds ready for distribution. Once the script finishes, the `app/bin` folder will contain self-contained executables for each major operating system, and a portable version that works on all other systems but requires .NET 8 to be installed.
Note that when building on Windows, the generated `.zip` files for Linux and Mac will not have correct file permissions, so it will not be possible to run them by double-clicking `DiscordHistoryTracker`. I tried using Python to re-create the archives with correct file permissions, but found that Linux `zip` tools could not see them. The only working solution is building the Windows + portable version on Windows, and Linux + Mac version on Linux. Note that when building on Windows, the generated `.zip` files for Linux and Mac will not have correct file permissions, so it will not be possible to run them by double-clicking `DiscordHistoryTracker`. I tried using Python to re-create the archives with correct file permissions, but found that Linux `zip` tools could not see them. The only working solution is building the Windows + portable version on Windows, and Linux + Mac version on Linux.

View File

@ -6,8 +6,8 @@
<Application.Styles> <Application.Styles>
<FluentTheme Mode="Light" /> <FluentTheme />
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Default.xaml" /> <StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Simple.xaml" />
<Style Selector="Button, CheckBox, RadioButton, Expander /template/ ToggleButton#ExpanderHeader"> <Style Selector="Button, CheckBox, RadioButton, Expander /template/ ToggleButton#ExpanderHeader">
<Setter Property="Cursor" Value="Hand" /> <Setter Property="Cursor" Value="Hand" />
@ -48,68 +48,9 @@
<Setter Property="Template" Value="{StaticResource InlineDataValidationContentTemplate}" /> <Setter Property="Template" Value="{StaticResource InlineDataValidationContentTemplate}" />
</Style> </Style>
<Style Selector="Expander /template/ ToggleButton#ExpanderHeader"> <Style Selector="Expander">
<Setter Property="HorizontalContentAlignment" Value="Left" /> <Setter Property="MinHeight" Value="40" />
<Setter Property="Template"> <Setter Property="HorizontalAlignment" Value="Stretch" />
<ControlTemplate>
<Border x:Name="ToggleButtonBackground">
<Grid ColumnDefinitions="Auto,*" RowDefinitions="35">
<Border x:Name="ExpandCollapseChevronBorder"
Grid.Column="0"
Width="35"
Height="35"
Margin="2,0"
RenderTransformOrigin="50%,50%">
<Path x:Name="ExpandCollapseChevron"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RenderTransformOrigin="50%,50%"
Stretch="None"
Stroke="{DynamicResource ExpanderChevronForeground}"
StrokeThickness="1" />
<Border.RenderTransform>
<RotateTransform />
</Border.RenderTransform>
</Border>
<ContentPresenter x:Name="PART_ContentPresenter"
Grid.Column="1"
Margin="0"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="Center"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
TextBlock.Foreground="{DynamicResource ExpanderForeground}" />
</Grid>
</Border>
</ControlTemplate>
</Setter>
</Style>
<Style Selector="Expander:expanded /template/ ToggleButton#ExpanderHeader /template/ Border#ExpandCollapseChevronBorder">
<Style.Animations>
<Animation FillMode="Both" Duration="0:0:0.0625">
<KeyFrame Cue="0%">
<Setter Property="RotateTransform.Angle" Value="180" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="RotateTransform.Angle" Value="180" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style>
<Style Selector="Expander:not(:expanded) /template/ ToggleButton#ExpanderHeader /template/ Border#ExpandCollapseChevronBorder">
<Style.Animations>
<Animation FillMode="Both" Duration="0:0:0.0625">
<KeyFrame Cue="0%">
<Setter Property="RotateTransform.Angle" Value="0" />
</KeyFrame>
<KeyFrame Cue="100%">
<Setter Property="RotateTransform.Angle" Value="0" />
</KeyFrame>
</Animation>
</Style.Animations>
</Style> </Style>
</Application.Styles> </Application.Styles>
@ -168,12 +109,26 @@
<Thickness x:Key="ExpanderHeaderPadding">15,0</Thickness> <Thickness x:Key="ExpanderHeaderPadding">15,0</Thickness>
<Thickness x:Key="ExpanderContentPadding">12</Thickness> <Thickness x:Key="ExpanderContentPadding">12</Thickness>
<SolidColorBrush x:Key="ExpanderBorderBrush" Color="#697DAB" /> <SolidColorBrush x:Key="ExpanderHeaderBorderBrush" Color="#697DAB" />
<SolidColorBrush x:Key="ExpanderBackground" Color="#697DAB" /> <SolidColorBrush x:Key="ExpanderHeaderBorderBrushPointerOver" Color="#697DAB" />
<SolidColorBrush x:Key="ExpanderForeground" Color="#FFFFFF" /> <SolidColorBrush x:Key="ExpanderHeaderBorderBrushPressed" Color="#697DAB" />
<SolidColorBrush x:Key="ExpanderHeaderBorderBrushDisabled" Color="#697DAB" />
<SolidColorBrush x:Key="ExpanderHeaderBackground" Color="#697DAB" />
<SolidColorBrush x:Key="ExpanderHeaderBackgroundPointerOver" Color="#536794" />
<SolidColorBrush x:Key="ExpanderHeaderBackgroundPressed" Color="#47587F" />
<SolidColorBrush x:Key="ExpanderHeaderBackgroundDisabled" Color="#697DAB" />
<SolidColorBrush x:Key="ExpanderHeaderForeground" Color="#FFFFFF" />
<SolidColorBrush x:Key="ExpanderHeaderForegroundPointerOver" Color="#FFFFFF" />
<SolidColorBrush x:Key="ExpanderHeaderForegroundPressed" Color="#FFFFFF" />
<SolidColorBrush x:Key="ExpanderHeaderForegroundDisabled" Color="#FFFFFF" />
<SolidColorBrush x:Key="ExpanderChevronBackground" Color="Transparent" />
<SolidColorBrush x:Key="ExpanderChevronBackgroundPointerOver" Color="#536794" />
<SolidColorBrush x:Key="ExpanderChevronBackgroundPressed" Color="#47587F" />
<SolidColorBrush x:Key="ExpanderChevronBackgroundDisabled" Color="Transparent" />
<SolidColorBrush x:Key="ExpanderChevronForeground" Color="#FFFFFF" /> <SolidColorBrush x:Key="ExpanderChevronForeground" Color="#FFFFFF" />
<SolidColorBrush x:Key="ExpanderDropDownBorderBrush" Color="#697DAB" /> <SolidColorBrush x:Key="ExpanderChevronForegroundPointerOver" Color="#FFFFFF" />
<SolidColorBrush x:Key="ExpanderDropDownBackground" Color="#FFFFFF" /> <SolidColorBrush x:Key="ExpanderChevronForegroundPressed" Color="#FFFFFF" />
<SolidColorBrush x:Key="ExpanderChevronForegroundDisabled" Color="#FFFFFF" />
</Application.Resources> </Application.Resources>

View File

@ -1,20 +1,21 @@
using System;
using Avalonia; using Avalonia;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using DHT.Desktop.Main; using DHT.Desktop.Main;
namespace DHT.Desktop { namespace DHT.Desktop;
sealed class App : Application {
public override void Initialize() { sealed class App : Application {
AvaloniaXamlLoader.Load(this); public override void Initialize() {
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted() {
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
desktop.MainWindow = new MainWindow(new Arguments(desktop.Args ?? Array.Empty<string>()));
} }
public override void OnFrameworkInitializationCompleted() { base.OnFrameworkInitializationCompleted();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
desktop.MainWindow = new MainWindow(new Arguments(desktop.Args));
}
base.OnFrameworkInitializationCompleted();
}
} }
} }

View File

@ -1,64 +1,64 @@
using System; using System;
using DHT.Utils.Logging; using DHT.Utils.Logging;
namespace DHT.Desktop { namespace DHT.Desktop;
sealed class Arguments {
private static readonly Log Log = Log.ForType<Arguments>();
public static Arguments Empty => new(Array.Empty<string>()); sealed class Arguments {
private static readonly Log Log = Log.ForType<Arguments>();
public string? DatabaseFile { get; } public static Arguments Empty => new(Array.Empty<string>());
public ushort? ServerPort { get; }
public string? ServerToken { get; }
public Arguments(string[] args) { public string? DatabaseFile { get; }
for (int i = 0; i < args.Length; i++) { public ushort? ServerPort { get; }
string key = args[i]; public string? ServerToken { get; }
switch (key) { public Arguments(string[] args) {
case "-debug": for (int i = 0; i < args.Length; i++) {
Log.IsDebugEnabled = true; string key = args[i];
continue;
}
string value; switch (key) {
case "-debug":
if (i == 0 && !key.StartsWith('-')) { Log.IsDebugEnabled = true;
value = key;
key = "-db";
}
else if (i >= args.Length - 1) {
Log.Warn("Missing value for command line argument: " + key);
continue; continue;
} }
else {
value = args[++i];
}
switch (key) { string value;
case "-db":
DatabaseFile = value;
continue;
case "-port": { if (i == 0 && !key.StartsWith('-')) {
if (ushort.TryParse(value, out var port)) { value = key;
ServerPort = port; key = "-db";
} }
else { else if (i >= args.Length - 1) {
Log.Warn("Invalid port number: " + value); Log.Warn("Missing value for command line argument: " + key);
} continue;
}
else {
value = args[++i];
}
continue; switch (key) {
case "-db":
DatabaseFile = value;
continue;
case "-port": {
if (ushort.TryParse(value, out var port)) {
ServerPort = port;
}
else {
Log.Warn("Invalid port number: " + value);
} }
case "-token": continue;
ServerToken = value;
continue;
default:
Log.Warn("Unknown command line argument: " + key);
break;
} }
case "-token":
ServerToken = value;
continue;
default:
Log.Warn("Unknown command line argument: " + key);
break;
} }
} }
} }

View File

@ -2,52 +2,52 @@ using System;
using System.Globalization; using System.Globalization;
using Avalonia.Data.Converters; using Avalonia.Data.Converters;
namespace DHT.Desktop.Common { namespace DHT.Desktop.Common;
sealed class BytesValueConverter : IValueConverter {
private sealed class Unit {
private readonly string label;
private readonly string numberFormat;
public Unit(string label, int decimalPlaces) { sealed class BytesValueConverter : IValueConverter {
this.label = label; private sealed class Unit {
this.numberFormat = "{0:n" + decimalPlaces + "}"; private readonly string label;
} private readonly string numberFormat;
public string Format(double size) { public Unit(string label, int decimalPlaces) {
return string.Format(Program.Culture, numberFormat, size) + " " + label; this.label = label;
} this.numberFormat = "{0:n" + decimalPlaces + "}";
} }
private static readonly Unit[] Units = { public string Format(double size) {
new ("B", decimalPlaces: 0), return string.Format(Program.Culture, numberFormat, size) + " " + label;
new ("kB", decimalPlaces: 0),
new ("MB", decimalPlaces: 1),
new ("GB", decimalPlaces: 1),
new ("TB", decimalPlaces: 1)
};
private const int Scale = 1000;
private static string Convert(ulong size) {
int power = size == 0L ? 0 : (int) Math.Log(size, Scale);
int unit = power >= Units.Length ? Units.Length - 1 : power;
return Units[unit].Format(unit == 0 ? size : size / Math.Pow(Scale, unit));
}
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is long size and >= 0L) {
return Convert((ulong) size);
}
else if (value is ulong usize) {
return Convert(usize);
}
else {
return "-";
}
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
throw new NotSupportedException();
} }
} }
private static readonly Unit[] Units = {
new ("B", decimalPlaces: 0),
new ("kB", decimalPlaces: 0),
new ("MB", decimalPlaces: 1),
new ("GB", decimalPlaces: 1),
new ("TB", decimalPlaces: 1)
};
private const int Scale = 1000;
private static string Convert(ulong size) {
int power = size == 0L ? 0 : (int) Math.Log(size, Scale);
int unit = power >= Units.Length ? Units.Length - 1 : power;
return Units[unit].Format(unit == 0 ? size : size / Math.Pow(Scale, unit));
}
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is long size and >= 0L) {
return Convert((ulong) size);
}
else if (value is ulong usize) {
return Convert(usize);
}
else {
return "-";
}
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
throw new NotSupportedException();
}
} }

View File

@ -3,64 +3,66 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Platform.Storage;
using DHT.Desktop.Dialogs.File;
using DHT.Desktop.Dialogs.Message; using DHT.Desktop.Dialogs.Message;
using DHT.Server.Database; using DHT.Server.Database;
using DHT.Server.Database.Exceptions; using DHT.Server.Database.Exceptions;
using DHT.Server.Database.Sqlite; using DHT.Server.Database.Sqlite;
using DHT.Utils.Logging; using DHT.Utils.Logging;
namespace DHT.Desktop.Common { namespace DHT.Desktop.Common;
static class DatabaseGui {
private static readonly Log Log = Log.ForType(typeof(DatabaseGui));
private const string DatabaseFileInitialName = "archive.dht"; static class DatabaseGui {
private static readonly Log Log = Log.ForType(typeof(DatabaseGui));
private static readonly List<FileDialogFilter> DatabaseFileDialogFilter = new() { private const string DatabaseFileInitialName = "archive.dht";
new FileDialogFilter {
Name = "Discord History Tracker Database",
Extensions = { "dht" }
}
};
public static OpenFileDialog NewOpenDatabaseFileDialog() { private static readonly IReadOnlyList<FilePickerFileType> DatabaseFileDialogFilter = new List<FilePickerFileType> {
return new OpenFileDialog { FileDialogs.CreateFilter("Discord History Tracker Database", new [] { "dht" })
Title = "Open Database File", };
InitialFileName = DatabaseFileInitialName,
Filters = DatabaseFileDialogFilter public static async Task<string[]> NewOpenDatabaseFilesDialog(Window window, string? suggestedDirectory) {
}; return await window.StorageProvider.OpenFiles(new FilePickerOpenOptions {
Title = "Open Database File",
FileTypeFilter = DatabaseFileDialogFilter,
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, suggestedDirectory),
AllowMultiple = true
});
}
public static async Task<string?> NewOpenOrCreateDatabaseFileDialog(Window window, string? suggestedDirectory) {
return await window.StorageProvider.SaveFile(new FilePickerSaveOptions {
Title = "Open or Create Database File",
FileTypeChoices = DatabaseFileDialogFilter,
SuggestedFileName = DatabaseFileInitialName,
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, suggestedDirectory),
ShowOverwritePrompt = false
});
}
public static async Task<IDatabaseFile?> TryOpenOrCreateDatabaseFromPath(string path, Window window, Func<Task<bool>> checkCanUpgradeDatabase) {
IDatabaseFile? file = null;
try {
file = await SqliteDatabaseFile.OpenOrCreate(path, checkCanUpgradeDatabase);
} catch (InvalidDatabaseVersionException ex) {
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' appears to be corrupted (invalid version: " + ex.Version + ").");
} catch (DatabaseTooNewException ex) {
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' was opened in a newer version of DHT (database version " + ex.DatabaseVersion + ", app version " + ex.CurrentVersion + ").");
} catch (Exception ex) {
Log.Error(ex);
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' could not be opened:" + ex.Message);
} }
public static SaveFileDialog NewOpenOrCreateDatabaseFileDialog() { return file;
return new SaveFileDialog { }
Title = "Open or Create Database File",
InitialFileName = DatabaseFileInitialName,
Filters = DatabaseFileDialogFilter
};
}
public static async Task<IDatabaseFile?> TryOpenOrCreateDatabaseFromPath(string path, Window window, Func<Task<bool>> checkCanUpgradeDatabase) { public static async Task<DialogResult.YesNo> ShowCanUpgradeDatabaseDialog(Window window) {
IDatabaseFile? file = null; return await Dialog.ShowYesNo(window, "Database Upgrade", "This database was created with an older version of DHT. If you proceed, the database will be upgraded and will no longer open in previous versions of DHT.\n\nPlease ensure you have a backup of the database. Do you want to proceed with the upgrade?");
}
try { public static async Task<DialogResult.YesNo> ShowCanUpgradeMultipleDatabaseDialog(Window window) {
file = await SqliteDatabaseFile.OpenOrCreate(path, checkCanUpgradeDatabase); return await Dialog.ShowYesNo(window, "Database Upgrade", "One or more databases were created with an older version of DHT. If you proceed, these databases will be upgraded and will no longer open in previous versions of DHT. Otherwise, these databases will be skipped.\n\nPlease ensure you have a backup of the databases. Do you want to proceed with the upgrade?");
} catch (InvalidDatabaseVersionException ex) {
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' appears to be corrupted (invalid version: " + ex.Version + ").");
} catch (DatabaseTooNewException ex) {
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' was opened in a newer version of DHT (database version " + ex.DatabaseVersion + ", app version " + ex.CurrentVersion + ").");
} catch (Exception ex) {
Log.Error(ex);
await Dialog.ShowOk(window, "Database Error", "Database '" + Path.GetFileName(path) + "' could not be opened:" + ex.Message);
}
return file;
}
public static async Task<DialogResult.YesNo> ShowCanUpgradeDatabaseDialog(Window window) {
return await Dialog.ShowYesNo(window, "Database Upgrade", "This database was created with an older version of DHT. If you proceed, the database will be upgraded and will no longer open in previous versions of DHT.\n\nPlease ensure you have a backup of the database. Do you want to proceed with the upgrade?");
}
public static async Task<DialogResult.YesNo> ShowCanUpgradeMultipleDatabaseDialog(Window window) {
return await Dialog.ShowYesNo(window, "Database Upgrade", "One or more databases were created with an older version of DHT. If you proceed, these databases will be upgraded and will no longer open in previous versions of DHT. Otherwise, these databases will be skipped.\n\nPlease ensure you have a backup of the databases. Do you want to proceed with the upgrade?");
}
} }
} }

View File

@ -2,14 +2,14 @@ using System;
using System.Globalization; using System.Globalization;
using Avalonia.Data.Converters; using Avalonia.Data.Converters;
namespace DHT.Desktop.Common { namespace DHT.Desktop.Common;
sealed class NumberValueConverter : IValueConverter {
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
return value == null ? "-" : string.Format(Program.Culture, "{0:n0}", value);
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) { sealed class NumberValueConverter : IValueConverter {
throw new NotSupportedException(); public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
} return value == null ? "-" : string.Format(Program.Culture, "{0:n0}", value);
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
throw new NotSupportedException();
} }
} }

View File

@ -1,19 +1,19 @@
namespace DHT.Desktop.Common { namespace DHT.Desktop.Common;
static class TextFormat {
public static string Format(this int number) {
return number.ToString("N0", Program.Culture);
}
public static string Format(this long number) { static class TextFormat {
return number.ToString("N0", Program.Culture); public static string Format(this int number) {
} return number.ToString("N0", Program.Culture);
}
public static string Pluralize(this int number, string singular) { public static string Format(this long number) {
return number.Format() + "\u00A0" + (number == 1 ? singular : singular + "s"); return number.ToString("N0", Program.Culture);
} }
public static string Pluralize(this long number, string singular) { public static string Pluralize(this int number, string singular) {
return number.Format() + "\u00A0" + (number == 1 ? singular : singular + "s"); return number.Format() + "\u00A0" + (number == 1 ? singular : singular + "s");
} }
public static string Pluralize(this long number, string singular) {
return number.Format() + "\u00A0" + (number == 1 ? singular : singular + "s");
} }
} }

View File

@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AssemblyName>DiscordHistoryTracker</AssemblyName> <AssemblyName>DiscordHistoryTracker</AssemblyName>
<RootNamespace>DHT.Desktop</RootNamespace> <RootNamespace>DHT.Desktop</RootNamespace>
@ -21,10 +20,13 @@
<DebugType>none</DebugType> <DebugType>none</DebugType>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.18" /> <PackageReference Include="Avalonia" Version="11.0.0" />
<PackageReference Include="Avalonia.Controls.DataGrid" Version="0.10.18" /> <PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.0" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.18" /> <PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.0.0" />
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.18" Condition=" '$(Configuration)' == 'Debug' " /> <PackageReference Include="Avalonia.Desktop" Version="11.0.0" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.0" Condition=" '$(Configuration)' == 'Debug' " />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.0" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Server\Server.csproj" /> <ProjectReference Include="..\Server\Server.csproj" />

View File

@ -34,7 +34,7 @@
<StackPanel Margin="20"> <StackPanel Margin="20">
<ScrollViewer MaxHeight="400"> <ScrollViewer MaxHeight="400">
<ItemsRepeater Items="{Binding Items}"> <ItemsRepeater ItemsSource="{Binding Items}">
<ItemsRepeater.ItemTemplate> <ItemsRepeater.ItemTemplate>
<DataTemplate> <DataTemplate>
<CheckBox IsChecked="{Binding Checked}"> <CheckBox IsChecked="{Binding Checked}">

View File

@ -1,31 +1,21 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DHT.Desktop.Dialogs.Message; using DHT.Desktop.Dialogs.Message;
namespace DHT.Desktop.Dialogs.CheckBox { namespace DHT.Desktop.Dialogs.CheckBox;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class CheckBoxDialog : Window {
public CheckBoxDialog() {
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class CheckBoxDialog : Window {
} public CheckBoxDialog() {
InitializeComponent();
}
public void ClickOk(object? sender, RoutedEventArgs e) { public void ClickOk(object? sender, RoutedEventArgs e) {
Close(DialogResult.OkCancel.Ok); Close(DialogResult.OkCancel.Ok);
} }
public void ClickCancel(object? sender, RoutedEventArgs e) { public void ClickCancel(object? sender, RoutedEventArgs e) {
Close(DialogResult.OkCancel.Cancel); Close(DialogResult.OkCancel.Cancel);
}
} }
} }

View File

@ -4,67 +4,67 @@ using System.ComponentModel;
using System.Linq; using System.Linq;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Dialogs.CheckBox { namespace DHT.Desktop.Dialogs.CheckBox;
class CheckBoxDialogModel : BaseModel {
public string Title { get; init; } = "";
private IReadOnlyList<CheckBoxItem> items = Array.Empty<CheckBoxItem>(); class CheckBoxDialogModel : BaseModel {
public string Title { get; init; } = "";
public IReadOnlyList<CheckBoxItem> Items { private IReadOnlyList<CheckBoxItem> items = Array.Empty<CheckBoxItem>();
get => items;
protected set { public IReadOnlyList<CheckBoxItem> Items {
foreach (var item in items) { get => items;
item.PropertyChanged -= OnItemPropertyChanged;
}
items = value; protected set {
foreach (var item in items) {
foreach (var item in items) { item.PropertyChanged -= OnItemPropertyChanged;
item.PropertyChanged += OnItemPropertyChanged;
}
}
}
private bool pauseCheckEvents = false;
public bool AreAllSelected => Items.All(static item => item.Checked);
public bool AreNoneSelected => Items.All(static item => !item.Checked);
public void SelectAll() => SetAllChecked(true);
public void SelectNone() => SetAllChecked(false);
private void SetAllChecked(bool isChecked) {
pauseCheckEvents = true;
foreach (var item in Items) {
item.Checked = isChecked;
} }
pauseCheckEvents = false; items = value;
UpdateBulkButtons();
}
private void UpdateBulkButtons() { foreach (var item in items) {
OnPropertyChanged(nameof(AreAllSelected)); item.PropertyChanged += OnItemPropertyChanged;
OnPropertyChanged(nameof(AreNoneSelected));
}
private void OnItemPropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (!pauseCheckEvents && e.PropertyName == nameof(CheckBoxItem.Checked)) {
UpdateBulkButtons();
} }
} }
} }
sealed class CheckBoxDialogModel<T> : CheckBoxDialogModel { private bool pauseCheckEvents = false;
public new IReadOnlyList<CheckBoxItem<T>> Items { get; }
public IEnumerable<CheckBoxItem<T>> SelectedItems => Items.Where(static item => item.Checked); public bool AreAllSelected => Items.All(static item => item.Checked);
public bool AreNoneSelected => Items.All(static item => !item.Checked);
public CheckBoxDialogModel(IEnumerable<CheckBoxItem<T>> items) { public void SelectAll() => SetAllChecked(true);
this.Items = new List<CheckBoxItem<T>>(items); public void SelectNone() => SetAllChecked(false);
base.Items = this.Items;
private void SetAllChecked(bool isChecked) {
pauseCheckEvents = true;
foreach (var item in Items) {
item.Checked = isChecked;
}
pauseCheckEvents = false;
UpdateBulkButtons();
}
private void UpdateBulkButtons() {
OnPropertyChanged(nameof(AreAllSelected));
OnPropertyChanged(nameof(AreNoneSelected));
}
private void OnItemPropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (!pauseCheckEvents && e.PropertyName == nameof(CheckBoxItem.Checked)) {
UpdateBulkButtons();
} }
} }
} }
sealed class CheckBoxDialogModel<T> : CheckBoxDialogModel {
public new IReadOnlyList<CheckBoxItem<T>> Items { get; }
public IEnumerable<CheckBoxItem<T>> SelectedItems => Items.Where(static item => item.Checked);
public CheckBoxDialogModel(IEnumerable<CheckBoxItem<T>> items) {
this.Items = new List<CheckBoxItem<T>>(items);
base.Items = this.Items;
}
}

View File

@ -1,24 +1,24 @@
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Dialogs.CheckBox { namespace DHT.Desktop.Dialogs.CheckBox;
class CheckBoxItem : BaseModel {
public string Title { get; init; } = "";
public object? Item { get; init; } = null;
private bool isChecked = false; class CheckBoxItem : BaseModel {
public string Title { get; init; } = "";
public object? Item { get; init; } = null;
public bool Checked { private bool isChecked = false;
get => isChecked;
set => Change(ref isChecked, value);
}
}
sealed class CheckBoxItem<T> : CheckBoxItem { public bool Checked {
public new T Item { get; } get => isChecked;
set => Change(ref isChecked, value);
public CheckBoxItem(T item) { }
this.Item = item; }
base.Item = item;
} sealed class CheckBoxItem<T> : CheckBoxItem {
public new T Item { get; }
public CheckBoxItem(T item) {
this.Item = item;
base.Item = item;
} }
} }

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
namespace DHT.Desktop.Dialogs.File;
static class FileDialogs {
public static async Task<string[]> OpenFiles(this IStorageProvider storageProvider, FilePickerOpenOptions options) {
return (await storageProvider.OpenFilePickerAsync(options)).ToLocalPaths();
}
public static async Task<string?> SaveFile(this IStorageProvider storageProvider, FilePickerSaveOptions options) {
return (await storageProvider.SaveFilePickerAsync(options))?.ToLocalPath();
}
public static FilePickerFileType CreateFilter(string name, string[] extensions) {
return new FilePickerFileType(name) {
Patterns = extensions.Select(static ext => "*." + ext).ToArray()
};
}
public static Task<IStorageFolder?> GetSuggestedStartLocation(Window window, string? suggestedDirectory) {
return suggestedDirectory == null ? Task.FromResult<IStorageFolder?>(null) : window.StorageProvider.TryGetFolderFromPathAsync(suggestedDirectory);
}
private static string ToLocalPath(this IStorageFile file) {
return file.TryGetLocalPath() ?? throw new NotSupportedException("Local filesystem is not supported.");
}
private static string[] ToLocalPaths(this IReadOnlyList<IStorageFile> files) {
return files.Select(ToLocalPath).ToArray();
}
}

View File

@ -2,73 +2,73 @@ using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Threading; using Avalonia.Threading;
namespace DHT.Desktop.Dialogs.Message { namespace DHT.Desktop.Dialogs.Message;
static class Dialog {
public static async Task ShowOk(Window owner, string title, string message) {
if (!Dispatcher.UIThread.CheckAccess()) {
await Dispatcher.UIThread.InvokeAsync(() => ShowOk(owner, title, message));
return;
}
await new MessageDialog { static class Dialog {
DataContext = new MessageDialogModel { public static async Task ShowOk(Window owner, string title, string message) {
Title = title, if (!Dispatcher.UIThread.CheckAccess()) {
Message = message, await Dispatcher.UIThread.InvokeAsync(() => ShowOk(owner, title, message));
IsOkVisible = true return;
}
}.ShowDialog<DialogResult.All>(owner);
} }
public static async Task<DialogResult.OkCancel> ShowOkCancel(Window owner, string title, string message) { await new MessageDialog {
if (!Dispatcher.UIThread.CheckAccess()) { DataContext = new MessageDialogModel {
return await Dispatcher.UIThread.InvokeAsync(() => ShowOkCancel(owner, title, message)); Title = title,
Message = message,
IsOkVisible = true
} }
}.ShowDialog<DialogResult.All>(owner);
}
var result = await new MessageDialog { public static async Task<DialogResult.OkCancel> ShowOkCancel(Window owner, string title, string message) {
DataContext = new MessageDialogModel { if (!Dispatcher.UIThread.CheckAccess()) {
Title = title, return await Dispatcher.UIThread.InvokeAsync(() => ShowOkCancel(owner, title, message));
Message = message,
IsOkVisible = true,
IsCancelVisible = true
}
}.ShowDialog<DialogResult.All?>(owner);
return result.ToOkCancel();
} }
public static async Task<DialogResult.YesNo> ShowYesNo(Window owner, string title, string message) { var result = await new MessageDialog {
if (!Dispatcher.UIThread.CheckAccess()) { DataContext = new MessageDialogModel {
return await Dispatcher.UIThread.InvokeAsync(() => ShowYesNo(owner, title, message)); Title = title,
Message = message,
IsOkVisible = true,
IsCancelVisible = true
} }
}.ShowDialog<DialogResult.All?>(owner);
var result = await new MessageDialog { return result.ToOkCancel();
DataContext = new MessageDialogModel { }
Title = title,
Message = message,
IsYesVisible = true,
IsNoVisible = true
}
}.ShowDialog<DialogResult.All?>(owner);
return result.ToYesNo(); public static async Task<DialogResult.YesNo> ShowYesNo(Window owner, string title, string message) {
if (!Dispatcher.UIThread.CheckAccess()) {
return await Dispatcher.UIThread.InvokeAsync(() => ShowYesNo(owner, title, message));
} }
public static async Task<DialogResult.YesNoCancel> ShowYesNoCancel(Window owner, string title, string message) { var result = await new MessageDialog {
if (!Dispatcher.UIThread.CheckAccess()) { DataContext = new MessageDialogModel {
return await Dispatcher.UIThread.InvokeAsync(() => ShowYesNoCancel(owner, title, message)); Title = title,
Message = message,
IsYesVisible = true,
IsNoVisible = true
} }
}.ShowDialog<DialogResult.All?>(owner);
var result = await new MessageDialog { return result.ToYesNo();
DataContext = new MessageDialogModel { }
Title = title,
Message = message,
IsYesVisible = true,
IsNoVisible = true,
IsCancelVisible = true
}
}.ShowDialog<DialogResult.All?>(owner);
return result.ToYesNoCancel(); public static async Task<DialogResult.YesNoCancel> ShowYesNoCancel(Window owner, string title, string message) {
if (!Dispatcher.UIThread.CheckAccess()) {
return await Dispatcher.UIThread.InvokeAsync(() => ShowYesNoCancel(owner, title, message));
} }
var result = await new MessageDialog {
DataContext = new MessageDialogModel {
Title = title,
Message = message,
IsYesVisible = true,
IsNoVisible = true,
IsCancelVisible = true
}
}.ShowDialog<DialogResult.All?>(owner);
return result.ToYesNoCancel();
} }
} }

View File

@ -1,59 +1,59 @@
using System; using System;
namespace DHT.Desktop.Dialogs.Message { namespace DHT.Desktop.Dialogs.Message;
static class DialogResult {
public enum All {
Ok,
Yes,
No,
Cancel
}
public enum OkCancel { static class DialogResult {
Closed, public enum All {
Ok, Ok,
Cancel Yes,
} No,
Cancel
}
public enum YesNo { public enum OkCancel {
Closed, Closed,
Yes, Ok,
No Cancel
} }
public enum YesNoCancel { public enum YesNo {
Closed, Closed,
Yes, Yes,
No, No
Cancel }
}
public static OkCancel ToOkCancel(this All? result) { public enum YesNoCancel {
return result switch { Closed,
null => OkCancel.Closed, Yes,
All.Ok => OkCancel.Ok, No,
All.Cancel => OkCancel.Cancel, Cancel
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to ok/cancel.") }
};
}
public static YesNo ToYesNo(this All? result) { public static OkCancel ToOkCancel(this All? result) {
return result switch { return result switch {
null => YesNo.Closed, null => OkCancel.Closed,
All.Yes => YesNo.Yes, All.Ok => OkCancel.Ok,
All.No => YesNo.No, All.Cancel => OkCancel.Cancel,
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to yes/no.") _ => throw new ArgumentException("Cannot convert dialog result " + result + " to ok/cancel.")
}; };
} }
public static YesNoCancel ToYesNoCancel(this All? result) { public static YesNo ToYesNo(this All? result) {
return result switch { return result switch {
null => YesNoCancel.Closed, null => YesNo.Closed,
All.Yes => YesNoCancel.Yes, All.Yes => YesNo.Yes,
All.No => YesNoCancel.No, All.No => YesNo.No,
All.Cancel => YesNoCancel.Cancel, _ => throw new ArgumentException("Cannot convert dialog result " + result + " to yes/no.")
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to yes/no/cancel.") };
}; }
}
public static YesNoCancel ToYesNoCancel(this All? result) {
return result switch {
null => YesNoCancel.Closed,
All.Yes => YesNoCancel.Yes,
All.No => YesNoCancel.No,
All.Cancel => YesNoCancel.Cancel,
_ => throw new ArgumentException("Cannot convert dialog result " + result + " to yes/no/cancel.")
};
} }
} }

View File

@ -1,37 +1,28 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Dialogs.Message { namespace DHT.Desktop.Dialogs.Message;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class MessageDialog : Window {
public MessageDialog() {
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class MessageDialog : Window {
} public MessageDialog() {
InitializeComponent();
}
public void ClickOk(object? sender, RoutedEventArgs e) { public void ClickOk(object? sender, RoutedEventArgs e) {
Close(DialogResult.All.Ok); Close(DialogResult.All.Ok);
} }
public void ClickYes(object? sender, RoutedEventArgs e) { public void ClickYes(object? sender, RoutedEventArgs e) {
Close(DialogResult.All.Yes); Close(DialogResult.All.Yes);
} }
public void ClickNo(object? sender, RoutedEventArgs e) { public void ClickNo(object? sender, RoutedEventArgs e) {
Close(DialogResult.All.No); Close(DialogResult.All.No);
} }
public void ClickCancel(object? sender, RoutedEventArgs e) { public void ClickCancel(object? sender, RoutedEventArgs e) {
Close(DialogResult.All.Cancel); Close(DialogResult.All.Cancel);
}
} }
} }

View File

@ -1,11 +1,11 @@
namespace DHT.Desktop.Dialogs.Message { namespace DHT.Desktop.Dialogs.Message;
sealed class MessageDialogModel {
public string Title { get; init; } = "";
public string Message { get; init; } = "";
public bool IsOkVisible { get; init; } = false; sealed class MessageDialogModel {
public bool IsYesVisible { get; init; } = false; public string Title { get; init; } = "";
public bool IsNoVisible { get; init; } = false; public string Message { get; init; } = "";
public bool IsCancelVisible { get; init; } = false;
} public bool IsOkVisible { get; init; } = false;
public bool IsYesVisible { get; init; } = false;
public bool IsNoVisible { get; init; } = false;
public bool IsCancelVisible { get; init; } = false;
} }

View File

@ -1,7 +1,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DHT.Desktop.Dialogs.Progress { namespace DHT.Desktop.Dialogs.Progress;
interface IProgressCallback {
Task Update(string message, int finishedItems, int totalItems); interface IProgressCallback {
} Task Update(string message, int finishedItems, int totalItems);
} }

View File

@ -7,7 +7,7 @@
x:Class="DHT.Desktop.Dialogs.Progress.ProgressDialog" x:Class="DHT.Desktop.Dialogs.Progress.ProgressDialog"
Title="{Binding Title}" Title="{Binding Title}"
Icon="avares://DiscordHistoryTracker/Resources/icon.ico" Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
Opened="Loaded" Opened="OnOpened"
Closing="OnClosing" Closing="OnClosing"
Width="500" SizeToContent="Height" CanResize="False" Width="500" SizeToContent="Height" CanResize="False"
WindowStartupLocation="CenterOwner"> WindowStartupLocation="CenterOwner">

View File

@ -1,40 +1,30 @@
using System; using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Dialogs.Progress { namespace DHT.Desktop.Dialogs.Progress;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class ProgressDialog : Window {
private bool isFinished = false;
public ProgressDialog() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
InitializeComponent(); public sealed partial class ProgressDialog : Window {
#if DEBUG private bool isFinished = false;
this.AttachDevTools();
#endif
}
private void InitializeComponent() { public ProgressDialog() {
AvaloniaXamlLoader.Load(this); InitializeComponent();
} }
public void OnClosing(object? sender, CancelEventArgs e) { public void OnOpened(object? sender, EventArgs e) {
e.Cancel = !isFinished; if (DataContext is ProgressDialogModel model) {
} Task.Run(model.StartTask).ContinueWith(OnFinished, TaskScheduler.FromCurrentSynchronizationContext());
public void Loaded(object? sender, EventArgs e) {
if (DataContext is ProgressDialogModel model) {
Task.Run(model.StartTask).ContinueWith(OnFinished, TaskScheduler.FromCurrentSynchronizationContext());
}
}
private void OnFinished(Task task) {
isFinished = true;
Close();
} }
} }
public void OnClosing(object? sender, WindowClosingEventArgs e) {
e.Cancel = !isFinished;
}
private void OnFinished(Task task) {
isFinished = true;
Close();
}
} }

View File

@ -4,62 +4,62 @@ using Avalonia.Threading;
using DHT.Desktop.Common; using DHT.Desktop.Common;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Dialogs.Progress { namespace DHT.Desktop.Dialogs.Progress;
sealed class ProgressDialogModel : BaseModel {
public string Title { get; init; } = "";
private string message = ""; sealed class ProgressDialogModel : BaseModel {
public string Title { get; init; } = "";
public string Message { private string message = "";
get => message;
private set => Change(ref message, value); public string Message {
get => message;
private set => Change(ref message, value);
}
private string items = "";
public string Items {
get => items;
private set => Change(ref items, value);
}
private int progress = 0;
public int Progress {
get => progress;
private set => Change(ref progress, value);
}
private readonly TaskRunner? task;
[Obsolete("Designer")]
public ProgressDialogModel() {}
public ProgressDialogModel(TaskRunner task) {
this.task = task;
}
internal async Task StartTask() {
if (task != null) {
await task(new Callback(this));
}
}
public delegate Task TaskRunner(IProgressCallback callback);
private sealed class Callback : IProgressCallback {
private readonly ProgressDialogModel model;
public Callback(ProgressDialogModel model) {
this.model = model;
} }
private string items = ""; async Task IProgressCallback.Update(string message, int finishedItems, int totalItems) {
await Dispatcher.UIThread.InvokeAsync(() => {
public string Items { model.Message = message;
get => items; model.Items = finishedItems.Format() + " / " + totalItems.Format();
private set => Change(ref items, value); model.Progress = 100 * finishedItems / totalItems;
} });
private int progress = 0;
public int Progress {
get => progress;
private set => Change(ref progress, value);
}
private readonly TaskRunner? task;
[Obsolete("Designer")]
public ProgressDialogModel() {}
public ProgressDialogModel(TaskRunner task) {
this.task = task;
}
internal async Task StartTask() {
if (task != null) {
await task(new Callback(this));
}
}
public delegate Task TaskRunner(IProgressCallback callback);
private sealed class Callback : IProgressCallback {
private readonly ProgressDialogModel model;
public Callback(ProgressDialogModel model) {
this.model = model;
}
async Task IProgressCallback.Update(string message, int finishedItems, int totalItems) {
await Dispatcher.UIThread.InvokeAsync(() => {
model.Message = message;
model.Items = finishedItems.Format() + " / " + totalItems.Format();
model.Progress = 100 * finishedItems / totalItems;
});
}
} }
} }
} }

View File

@ -31,7 +31,7 @@
<ScrollViewer MaxHeight="400"> <ScrollViewer MaxHeight="400">
<StackPanel Spacing="10"> <StackPanel Spacing="10">
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" /> <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
<ItemsRepeater Items="{Binding Items}"> <ItemsRepeater ItemsSource="{Binding Items}">
<ItemsRepeater.ItemTemplate> <ItemsRepeater.ItemTemplate>
<DataTemplate> <DataTemplate>
<DockPanel Margin="0 5 25 0"> <DockPanel Margin="0 5 25 0">

View File

@ -1,31 +1,21 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DHT.Desktop.Dialogs.Message; using DHT.Desktop.Dialogs.Message;
namespace DHT.Desktop.Dialogs.TextBox { namespace DHT.Desktop.Dialogs.TextBox;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class TextBoxDialog : Window {
public TextBoxDialog() {
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class TextBoxDialog : Window {
} public TextBoxDialog() {
InitializeComponent();
}
public void ClickOk(object? sender, RoutedEventArgs e) { public void ClickOk(object? sender, RoutedEventArgs e) {
Close(DialogResult.OkCancel.Ok); Close(DialogResult.OkCancel.Ok);
} }
public void ClickCancel(object? sender, RoutedEventArgs e) { public void ClickCancel(object? sender, RoutedEventArgs e) {
Close(DialogResult.OkCancel.Cancel); Close(DialogResult.OkCancel.Cancel);
}
} }
} }

View File

@ -4,44 +4,44 @@ using System.ComponentModel;
using System.Linq; using System.Linq;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Dialogs.TextBox { namespace DHT.Desktop.Dialogs.TextBox;
class TextBoxDialogModel : BaseModel {
public string Title { get; init; } = "";
public string Description { get; init; } = "";
private IReadOnlyList<TextBoxItem> items = Array.Empty<TextBoxItem>(); class TextBoxDialogModel : BaseModel {
public string Title { get; init; } = "";
public string Description { get; init; } = "";
public IReadOnlyList<TextBoxItem> Items { private IReadOnlyList<TextBoxItem> items = Array.Empty<TextBoxItem>();
get => items;
protected set { public IReadOnlyList<TextBoxItem> Items {
foreach (var item in items) { get => items;
item.ErrorsChanged -= OnItemErrorsChanged;
}
items = value; protected set {
foreach (var item in items) {
item.ErrorsChanged -= OnItemErrorsChanged;
}
foreach (var item in items) { items = value;
item.ErrorsChanged += OnItemErrorsChanged;
} foreach (var item in items) {
item.ErrorsChanged += OnItemErrorsChanged;
} }
} }
public bool HasErrors => Items.Any(static item => !item.IsValid);
private void OnItemErrorsChanged(object? sender, DataErrorsChangedEventArgs e) {
OnPropertyChanged(nameof(HasErrors));
}
} }
sealed class TextBoxDialogModel<T> : TextBoxDialogModel { public bool HasErrors => Items.Any(static item => !item.IsValid);
public new IReadOnlyList<TextBoxItem<T>> Items { get; }
public IEnumerable<TextBoxItem<T>> ValidItems => Items.Where(static item => item.IsValid); private void OnItemErrorsChanged(object? sender, DataErrorsChangedEventArgs e) {
OnPropertyChanged(nameof(HasErrors));
public TextBoxDialogModel(IEnumerable<TextBoxItem<T>> items) { }
this.Items = new List<TextBoxItem<T>>(items); }
base.Items = this.Items;
} sealed class TextBoxDialogModel<T> : TextBoxDialogModel {
public new IReadOnlyList<TextBoxItem<T>> Items { get; }
public IEnumerable<TextBoxItem<T>> ValidItems => Items.Where(static item => item.IsValid);
public TextBoxDialogModel(IEnumerable<TextBoxItem<T>> items) {
this.Items = new List<TextBoxItem<T>>(items);
base.Items = this.Items;
} }
} }

View File

@ -3,40 +3,40 @@ using System.Collections;
using System.ComponentModel; using System.ComponentModel;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Dialogs.TextBox { namespace DHT.Desktop.Dialogs.TextBox;
class TextBoxItem : BaseModel, INotifyDataErrorInfo {
public string Title { get; init; } = "";
public object? Item { get; init; } = null;
public Func<string, bool> ValidityCheck { get; init; } = static _ => true; class TextBoxItem : BaseModel, INotifyDataErrorInfo {
public bool IsValid => ValidityCheck(Value); public string Title { get; init; } = "";
public object? Item { get; init; } = null;
private string value = string.Empty; public Func<string, bool> ValidityCheck { get; init; } = static _ => true;
public bool IsValid => ValidityCheck(Value);
public string Value { private string value = string.Empty;
get => this.value;
set { public string Value {
Change(ref this.value, value); get => this.value;
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Value))); set {
} Change(ref this.value, value);
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Value)));
} }
public IEnumerable GetErrors(string? propertyName) {
if (propertyName == nameof(Value) && !IsValid) {
yield return string.Empty;
}
}
public bool HasErrors => !IsValid;
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
} }
sealed class TextBoxItem<T> : TextBoxItem { public IEnumerable GetErrors(string? propertyName) {
public new T Item { get; } if (propertyName == nameof(Value) && !IsValid) {
yield return string.Empty;
public TextBoxItem(T item) {
this.Item = item;
base.Item = item;
} }
} }
public bool HasErrors => !IsValid;
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
}
sealed class TextBoxItem<T> : TextBoxItem {
public new T Item { get; }
public TextBoxItem(T item) {
this.Item = item;
base.Item = item;
}
} }

View File

@ -9,111 +9,111 @@ using DHT.Utils.Logging;
using static System.Environment.SpecialFolder; using static System.Environment.SpecialFolder;
using static System.Environment.SpecialFolderOption; using static System.Environment.SpecialFolderOption;
namespace DHT.Desktop.Discord { namespace DHT.Desktop.Discord;
static class DiscordAppSettings {
private static readonly Log Log = Log.ForType(typeof(DiscordAppSettings));
private const string JsonKeyDevTools = "DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING"; static class DiscordAppSettings {
private static readonly Log Log = Log.ForType(typeof(DiscordAppSettings));
public static string JsonFilePath { get; } private const string JsonKeyDevTools = "DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING";
private static string JsonBackupFilePath { get; }
[SuppressMessage("ReSharper", "ConvertIfStatementToConditionalTernaryExpression")] public static string JsonFilePath { get; }
static DiscordAppSettings() { private static string JsonBackupFilePath { get; }
string rootFolder;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { [SuppressMessage("ReSharper", "ConvertIfStatementToConditionalTernaryExpression")]
rootFolder = Path.Combine(Environment.GetFolderPath(ApplicationData, DoNotVerify), "Discord"); static DiscordAppSettings() {
} string rootFolder;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
rootFolder = Path.Combine(Environment.GetFolderPath(UserProfile, DoNotVerify), "Library", "Application Support", "Discord");
}
else {
rootFolder = Path.Combine(Environment.GetFolderPath(ApplicationData, DoNotVerify), "discord");
}
JsonFilePath = Path.Combine(rootFolder, "settings.json"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
JsonBackupFilePath = JsonFilePath + ".bak"; rootFolder = Path.Combine(Environment.GetFolderPath(ApplicationData, DoNotVerify), "Discord");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
rootFolder = Path.Combine(Environment.GetFolderPath(UserProfile, DoNotVerify), "Library", "Application Support", "Discord");
}
else {
rootFolder = Path.Combine(Environment.GetFolderPath(ApplicationData, DoNotVerify), "discord");
} }
public static async Task<bool?> AreDevToolsEnabled() { JsonFilePath = Path.Combine(rootFolder, "settings.json");
try { JsonBackupFilePath = JsonFilePath + ".bak";
return AreDevToolsEnabled(await ReadSettingsJson()); }
} catch (Exception) {
return null;
}
}
private static bool AreDevToolsEnabled(Dictionary<string, object?> json) { public static async Task<bool?> AreDevToolsEnabled() {
return json.TryGetValue(JsonKeyDevTools, out var value) && value is JsonElement { ValueKind: JsonValueKind.True }; try {
} return AreDevToolsEnabled(await ReadSettingsJson());
} catch (Exception) {
public static async Task<SettingsJsonResult> ConfigureDevTools(bool enable) { return null;
Dictionary<string, object?> json;
try {
json = await ReadSettingsJson();
} catch (FileNotFoundException) {
return SettingsJsonResult.FileNotFound;
} catch (JsonException) {
return SettingsJsonResult.InvalidJson;
} catch (Exception e) {
Log.Error(e);
return SettingsJsonResult.ReadError;
}
if (enable == AreDevToolsEnabled(json)) {
return SettingsJsonResult.AlreadySet;
}
if (enable) {
json[JsonKeyDevTools] = true;
}
else {
json.Remove(JsonKeyDevTools);
}
try {
if (!File.Exists(JsonBackupFilePath)) {
File.Copy(JsonFilePath, JsonBackupFilePath);
}
await WriteSettingsJson(json);
} catch (Exception e) {
Log.Error("An error occurred when writing settings file.");
Log.Error(e);
if (File.Exists(JsonBackupFilePath)) {
try {
File.Move(JsonBackupFilePath, JsonFilePath, true);
Log.Info("Restored settings file from backup.");
} catch (Exception e2) {
Log.Error("Cannot restore settings file from backup.");
Log.Error(e2);
}
}
return SettingsJsonResult.WriteError;
}
try {
File.Delete(JsonBackupFilePath);
} catch (Exception e) {
Log.Error("Cannot delete backup file.");
Log.Error(e);
}
return SettingsJsonResult.Success;
}
private static async Task<Dictionary<string, object?>> ReadSettingsJson() {
await using var stream = new FileStream(JsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
return await JsonSerializer.DeserializeAsync<Dictionary<string, object?>?>(stream) ?? throw new JsonException();
}
private static async Task WriteSettingsJson(Dictionary<string, object?> json) {
await using var stream = new FileStream(JsonFilePath, FileMode.Truncate, FileAccess.Write, FileShare.None);
await JsonSerializer.SerializeAsync(stream, json, new JsonSerializerOptions { WriteIndented = true });
} }
} }
private static bool AreDevToolsEnabled(Dictionary<string, object?> json) {
return json.TryGetValue(JsonKeyDevTools, out var value) && value is JsonElement { ValueKind: JsonValueKind.True };
}
public static async Task<SettingsJsonResult> ConfigureDevTools(bool enable) {
Dictionary<string, object?> json;
try {
json = await ReadSettingsJson();
} catch (FileNotFoundException) {
return SettingsJsonResult.FileNotFound;
} catch (JsonException) {
return SettingsJsonResult.InvalidJson;
} catch (Exception e) {
Log.Error(e);
return SettingsJsonResult.ReadError;
}
if (enable == AreDevToolsEnabled(json)) {
return SettingsJsonResult.AlreadySet;
}
if (enable) {
json[JsonKeyDevTools] = true;
}
else {
json.Remove(JsonKeyDevTools);
}
try {
if (!File.Exists(JsonBackupFilePath)) {
File.Copy(JsonFilePath, JsonBackupFilePath);
}
await WriteSettingsJson(json);
} catch (Exception e) {
Log.Error("An error occurred when writing settings file.");
Log.Error(e);
if (File.Exists(JsonBackupFilePath)) {
try {
File.Move(JsonBackupFilePath, JsonFilePath, true);
Log.Info("Restored settings file from backup.");
} catch (Exception e2) {
Log.Error("Cannot restore settings file from backup.");
Log.Error(e2);
}
}
return SettingsJsonResult.WriteError;
}
try {
File.Delete(JsonBackupFilePath);
} catch (Exception e) {
Log.Error("Cannot delete backup file.");
Log.Error(e);
}
return SettingsJsonResult.Success;
}
private static async Task<Dictionary<string, object?>> ReadSettingsJson() {
await using var stream = new FileStream(JsonFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
return await JsonSerializer.DeserializeAsync<Dictionary<string, object?>?>(stream) ?? throw new JsonException();
}
private static async Task WriteSettingsJson(Dictionary<string, object?> json) {
await using var stream = new FileStream(JsonFilePath, FileMode.Truncate, FileAccess.Write, FileShare.None);
await JsonSerializer.SerializeAsync(stream, json, new JsonSerializerOptions { WriteIndented = true });
}
} }

View File

@ -1,10 +1,10 @@
namespace DHT.Desktop.Discord { namespace DHT.Desktop.Discord;
enum SettingsJsonResult {
Success, enum SettingsJsonResult {
AlreadySet, Success,
FileNotFound, AlreadySet,
ReadError, FileNotFound,
InvalidJson, ReadError,
WriteError InvalidJson,
} WriteError
} }

View File

@ -46,7 +46,7 @@
<TextBlock Grid.Row="0" Grid.Column="1" FontWeight="Bold">License</TextBlock> <TextBlock Grid.Row="0" Grid.Column="1" FontWeight="Bold">License</TextBlock>
<TextBlock Grid.Row="0" Grid.Column="2" FontWeight="Bold">Link</TextBlock> <TextBlock Grid.Row="0" Grid.Column="2" FontWeight="Bold">Link</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0">.NET 5</TextBlock> <TextBlock Grid.Row="2" Grid.Column="0">.NET 8</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1">MIT</TextBlock> <TextBlock Grid.Row="2" Grid.Column="1">MIT</TextBlock>
<Button Grid.Row="2" Grid.Column="2" Command="{Binding ShowLibraryNetCore}">GitHub</Button> <Button Grid.Row="2" Grid.Column="2" Command="{Binding ShowLibraryNetCore}">GitHub</Button>

View File

@ -1,20 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main { namespace DHT.Desktop.Main;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class AboutWindow : Window {
public AboutWindow() {
InitializeComponent();
#if DEBUG
this.AttachDevTools();
#endif
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class AboutWindow : Window {
} public AboutWindow() {
InitializeComponent();
} }
} }

View File

@ -1,33 +1,33 @@
using System.Diagnostics; using System.Diagnostics;
namespace DHT.Desktop.Main { namespace DHT.Desktop.Main;
sealed class AboutWindowModel {
public void ShowOfficialWebsite() {
OpenUrl("https://dht.chylex.com");
}
public void ShowSourceCode() { sealed class AboutWindowModel {
OpenUrl("https://github.com/chylex/Discord-History-Tracker"); public void ShowOfficialWebsite() {
} OpenUrl("https://dht.chylex.com");
}
public void ShowLibraryAvalonia() { public void ShowSourceCode() {
OpenUrl("https://www.nuget.org/packages/Avalonia"); OpenUrl("https://github.com/chylex/Discord-History-Tracker");
} }
public void ShowLibrarySqlite() { public void ShowLibraryAvalonia() {
OpenUrl("https://www.sqlite.org"); OpenUrl("https://www.nuget.org/packages/Avalonia");
} }
public void ShowLibrarySqliteAdoNet() { public void ShowLibrarySqlite() {
OpenUrl("https://www.nuget.org/packages/Microsoft.Data.Sqlite"); OpenUrl("https://www.sqlite.org");
} }
public void ShowLibraryNetCore() { public void ShowLibrarySqliteAdoNet() {
OpenUrl("https://github.com/dotnet/core"); OpenUrl("https://www.nuget.org/packages/Microsoft.Data.Sqlite");
} }
private static void OpenUrl(string url) { public void ShowLibraryNetCore() {
Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true }); OpenUrl("https://github.com/dotnet/core");
} }
private static void OpenUrl(string url) {
Process.Start(new ProcessStartInfo { FileName = url, UseShellExecute = true });
} }
} }

View File

@ -36,7 +36,7 @@
<CheckBox IsChecked="{Binding LimitSize}">Limit Size</CheckBox> <CheckBox IsChecked="{Binding LimitSize}">Limit Size</CheckBox>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<TextBox Text="{Binding MaximumSize}" IsEnabled="{Binding LimitSize}" HorizontalContentAlignment="Right" /> <TextBox Text="{Binding MaximumSize}" IsEnabled="{Binding LimitSize}" HorizontalContentAlignment="Right" />
<ComboBox IsEnabled="{Binding LimitSize}" Items="{Binding Units}" SelectedItem="{Binding MaximumSizeUnit}"> <ComboBox IsEnabled="{Binding LimitSize}" ItemsSource="{Binding Units}" SelectedItem="{Binding MaximumSizeUnit}">
<ComboBox.ItemTemplate> <ComboBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding Name}" />

View File

@ -1,17 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Controls { namespace DHT.Desktop.Main.Controls;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class AttachmentFilterPanel : UserControl {
public AttachmentFilterPanel() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class AttachmentFilterPanel : UserControl {
} public AttachmentFilterPanel() {
InitializeComponent();
} }
} }

View File

@ -7,123 +7,123 @@ using DHT.Server.Database;
using DHT.Utils.Models; using DHT.Utils.Models;
using DHT.Utils.Tasks; using DHT.Utils.Tasks;
namespace DHT.Desktop.Main.Controls { namespace DHT.Desktop.Main.Controls;
sealed class AttachmentFilterPanelModel : BaseModel, IDisposable {
public sealed record Unit(string Name, uint Scale);
private static readonly Unit[] AllUnits = { sealed class AttachmentFilterPanelModel : BaseModel, IDisposable {
new ("B", 1), public sealed record Unit(string Name, uint Scale);
new ("kB", 1024),
new ("MB", 1024 * 1024)
};
private static readonly HashSet<string> FilterProperties = new () { private static readonly Unit[] AllUnits = {
nameof(LimitSize), new ("B", 1),
nameof(MaximumSize), new ("kB", 1024),
nameof(MaximumSizeUnit) new ("MB", 1024 * 1024)
}; };
public string FilterStatisticsText { get; private set; } = ""; private static readonly HashSet<string> FilterProperties = new () {
nameof(LimitSize),
nameof(MaximumSize),
nameof(MaximumSizeUnit)
};
private bool limitSize = false; public string FilterStatisticsText { get; private set; } = "";
private ulong maximumSize = 0L;
private Unit maximumSizeUnit = AllUnits[0];
public bool LimitSize { private bool limitSize = false;
get => limitSize; private ulong maximumSize = 0L;
set => Change(ref limitSize, value); private Unit maximumSizeUnit = AllUnits[0];
}
public ulong MaximumSize { public bool LimitSize {
get => maximumSize; get => limitSize;
set => Change(ref maximumSize, value); set => Change(ref limitSize, value);
} }
public Unit MaximumSizeUnit { public ulong MaximumSize {
get => maximumSizeUnit; get => maximumSize;
set => Change(ref maximumSizeUnit, value); set => Change(ref maximumSize, value);
} }
public IEnumerable<Unit> Units => AllUnits; public Unit MaximumSizeUnit {
get => maximumSizeUnit;
set => Change(ref maximumSizeUnit, value);
}
private readonly IDatabaseFile db; public IEnumerable<Unit> Units => AllUnits;
private readonly string verb;
private readonly AsyncValueComputer<long> matchingAttachmentCountComputer; private readonly IDatabaseFile db;
private long? matchingAttachmentCount; private readonly string verb;
private long? totalAttachmentCount;
[Obsolete("Designer")] private readonly AsyncValueComputer<long> matchingAttachmentCountComputer;
public AttachmentFilterPanelModel() : this(DummyDatabaseFile.Instance) {} private long? matchingAttachmentCount;
private long? totalAttachmentCount;
public AttachmentFilterPanelModel(IDatabaseFile db, string verb = "Matches") { [Obsolete("Designer")]
this.db = db; public AttachmentFilterPanelModel() : this(DummyDatabaseFile.Instance) {}
this.verb = verb;
this.matchingAttachmentCountComputer = AsyncValueComputer<long>.WithResultProcessor(SetAttachmentCounts).Build(); public AttachmentFilterPanelModel(IDatabaseFile db, string verb = "Matches") {
this.db = db;
this.verb = verb;
this.matchingAttachmentCountComputer = AsyncValueComputer<long>.WithResultProcessor(SetAttachmentCounts).Build();
UpdateFilterStatistics();
PropertyChanged += OnPropertyChanged;
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
}
public void Dispose() {
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
}
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName != null && FilterProperties.Contains(e.PropertyName)) {
UpdateFilterStatistics(); UpdateFilterStatistics();
PropertyChanged += OnPropertyChanged;
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
}
public void Dispose() {
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
}
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName != null && FilterProperties.Contains(e.PropertyName)) {
UpdateFilterStatistics();
}
}
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(DatabaseStatistics.TotalAttachments)) {
totalAttachmentCount = db.Statistics.TotalAttachments;
UpdateFilterStatistics();
}
}
private void UpdateFilterStatistics() {
var filter = CreateFilter();
if (filter.IsEmpty) {
matchingAttachmentCountComputer.Cancel();
matchingAttachmentCount = totalAttachmentCount;
UpdateFilterStatisticsText();
}
else {
matchingAttachmentCount = null;
UpdateFilterStatisticsText();
matchingAttachmentCountComputer.Compute(() => db.CountAttachments(filter));
}
}
private void SetAttachmentCounts(long matchingAttachmentCount) {
this.matchingAttachmentCount = matchingAttachmentCount;
UpdateFilterStatisticsText();
}
private void UpdateFilterStatisticsText() {
var matchingAttachmentCountStr = matchingAttachmentCount?.Format() ?? "(...)";
var totalAttachmentCountStr = totalAttachmentCount?.Format() ?? "(...)";
FilterStatisticsText = verb + " " + matchingAttachmentCountStr + " out of " + totalAttachmentCountStr + " attachment" + (totalAttachmentCount is null or 1 ? "." : "s.");
OnPropertyChanged(nameof(FilterStatisticsText));
}
public AttachmentFilter CreateFilter() {
AttachmentFilter filter = new();
if (LimitSize) {
try {
filter.MaxBytes = maximumSize * maximumSizeUnit.Scale;
} catch (ArithmeticException) {
// set no size limit, because the overflown size is larger than any file could possibly be
}
}
return filter;
} }
} }
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(DatabaseStatistics.TotalAttachments)) {
totalAttachmentCount = db.Statistics.TotalAttachments;
UpdateFilterStatistics();
}
}
private void UpdateFilterStatistics() {
var filter = CreateFilter();
if (filter.IsEmpty) {
matchingAttachmentCountComputer.Cancel();
matchingAttachmentCount = totalAttachmentCount;
UpdateFilterStatisticsText();
}
else {
matchingAttachmentCount = null;
UpdateFilterStatisticsText();
matchingAttachmentCountComputer.Compute(() => db.CountAttachments(filter));
}
}
private void SetAttachmentCounts(long matchingAttachmentCount) {
this.matchingAttachmentCount = matchingAttachmentCount;
UpdateFilterStatisticsText();
}
private void UpdateFilterStatisticsText() {
var matchingAttachmentCountStr = matchingAttachmentCount?.Format() ?? "(...)";
var totalAttachmentCountStr = totalAttachmentCount?.Format() ?? "(...)";
FilterStatisticsText = verb + " " + matchingAttachmentCountStr + " out of " + totalAttachmentCountStr + " attachment" + (totalAttachmentCount is null or 1 ? "." : "s.");
OnPropertyChanged(nameof(FilterStatisticsText));
}
public AttachmentFilter CreateFilter() {
AttachmentFilter filter = new();
if (LimitSize) {
try {
filter.MaxBytes = maximumSize * maximumSizeUnit.Scale;
} catch (ArithmeticException) {
// set no size limit, because the overflown size is larger than any file could possibly be
}
}
return filter;
}
} }

View File

@ -1,34 +1,26 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Controls { namespace DHT.Desktop.Main.Controls;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class MessageFilterPanel : UserControl {
private CalendarDatePicker StartDatePicker => this.FindControl<CalendarDatePicker>("StartDatePicker");
private CalendarDatePicker EndDatePicker => this.FindControl<CalendarDatePicker>("EndDatePicker");
public MessageFilterPanel() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
InitializeComponent(); public sealed partial class MessageFilterPanel : UserControl {
public MessageFilterPanel() {
InitializeComponent();
var culture = Program.Culture;
foreach (var picker in new CalendarDatePicker[] { StartDatePicker, EndDatePicker }) {
picker.FirstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek;
picker.SelectedDateFormat = CalendarDatePickerFormat.Custom;
picker.CustomDateFormatString = culture.DateTimeFormat.ShortDatePattern;
picker.Watermark = culture.DateTimeFormat.ShortDatePattern;
} }
}
private void InitializeComponent() { public void CalendarDatePicker_OnSelectedDateChanged(object? sender, SelectionChangedEventArgs e) {
AvaloniaXamlLoader.Load(this); if (DataContext is MessageFilterPanelModel model) {
model.StartDate = StartDatePicker.SelectedDate;
var culture = Program.Culture; model.EndDate = EndDatePicker.SelectedDate;
foreach (var picker in new CalendarDatePicker[] { StartDatePicker, EndDatePicker }) {
picker.FirstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek;
picker.SelectedDateFormat = CalendarDatePickerFormat.Custom;
picker.CustomDateFormatString = culture.DateTimeFormat.ShortDatePattern;
picker.Watermark = culture.DateTimeFormat.ShortDatePattern;
}
}
public void CalendarDatePicker_OnSelectedDateChanged(object? sender, SelectionChangedEventArgs e) {
if (DataContext is MessageFilterPanelModel model) {
model.StartDate = StartDatePicker.SelectedDate;
model.EndDate = EndDatePicker.SelectedDate;
}
} }
} }
} }

View File

@ -14,272 +14,272 @@ using DHT.Server.Database;
using DHT.Utils.Models; using DHT.Utils.Models;
using DHT.Utils.Tasks; using DHT.Utils.Tasks;
namespace DHT.Desktop.Main.Controls { namespace DHT.Desktop.Main.Controls;
sealed class MessageFilterPanelModel : BaseModel, IDisposable {
private static readonly HashSet<string> FilterProperties = new () {
nameof(FilterByDate),
nameof(StartDate),
nameof(EndDate),
nameof(FilterByChannel),
nameof(IncludedChannels),
nameof(FilterByUser),
nameof(IncludedUsers)
};
public string FilterStatisticsText { get; private set; } = ""; sealed class MessageFilterPanelModel : BaseModel, IDisposable {
private static readonly HashSet<string> FilterProperties = new () {
nameof(FilterByDate),
nameof(StartDate),
nameof(EndDate),
nameof(FilterByChannel),
nameof(IncludedChannels),
nameof(FilterByUser),
nameof(IncludedUsers)
};
public event PropertyChangedEventHandler? FilterPropertyChanged; public string FilterStatisticsText { get; private set; } = "";
public bool HasAnyFilters => FilterByDate || FilterByChannel || FilterByUser; public event PropertyChangedEventHandler? FilterPropertyChanged;
private bool filterByDate = false; public bool HasAnyFilters => FilterByDate || FilterByChannel || FilterByUser;
private DateTime? startDate = null;
private DateTime? endDate = null;
private bool filterByChannel = false;
private HashSet<ulong>? includedChannels = null;
private bool filterByUser = false;
private HashSet<ulong>? includedUsers = null;
public bool FilterByDate { private bool filterByDate = false;
get => filterByDate; private DateTime? startDate = null;
set => Change(ref filterByDate, value); private DateTime? endDate = null;
} private bool filterByChannel = false;
private HashSet<ulong>? includedChannels = null;
private bool filterByUser = false;
private HashSet<ulong>? includedUsers = null;
public DateTime? StartDate { public bool FilterByDate {
get => startDate; get => filterByDate;
set => Change(ref startDate, value); set => Change(ref filterByDate, value);
} }
public DateTime? EndDate { public DateTime? StartDate {
get => endDate; get => startDate;
set => Change(ref endDate, value); set => Change(ref startDate, value);
} }
public bool FilterByChannel { public DateTime? EndDate {
get => filterByChannel; get => endDate;
set => Change(ref filterByChannel, value); set => Change(ref endDate, value);
} }
public HashSet<ulong> IncludedChannels { public bool FilterByChannel {
get => includedChannels ?? db.GetAllChannels().Select(static channel => channel.Id).ToHashSet(); get => filterByChannel;
set => Change(ref includedChannels, value); set => Change(ref filterByChannel, value);
} }
public bool FilterByUser { public HashSet<ulong> IncludedChannels {
get => filterByUser; get => includedChannels ?? db.GetAllChannels().Select(static channel => channel.Id).ToHashSet();
set => Change(ref filterByUser, value); set => Change(ref includedChannels, value);
} }
public HashSet<ulong> IncludedUsers { public bool FilterByUser {
get => includedUsers ?? db.GetAllUsers().Select(static user => user.Id).ToHashSet(); get => filterByUser;
set => Change(ref includedUsers, value); set => Change(ref filterByUser, value);
} }
private string channelFilterLabel = ""; public HashSet<ulong> IncludedUsers {
get => includedUsers ?? db.GetAllUsers().Select(static user => user.Id).ToHashSet();
set => Change(ref includedUsers, value);
}
public string ChannelFilterLabel { private string channelFilterLabel = "";
get => channelFilterLabel;
set => Change(ref channelFilterLabel, value);
}
private string userFilterLabel = ""; public string ChannelFilterLabel {
get => channelFilterLabel;
set => Change(ref channelFilterLabel, value);
}
public string UserFilterLabel { private string userFilterLabel = "";
get => userFilterLabel;
set => Change(ref userFilterLabel, value);
}
private readonly Window window; public string UserFilterLabel {
private readonly IDatabaseFile db; get => userFilterLabel;
private readonly string verb; set => Change(ref userFilterLabel, value);
}
private readonly AsyncValueComputer<long> exportedMessageCountComputer; private readonly Window window;
private long? exportedMessageCount; private readonly IDatabaseFile db;
private long? totalMessageCount; private readonly string verb;
[Obsolete("Designer")] private readonly AsyncValueComputer<long> exportedMessageCountComputer;
public MessageFilterPanelModel() : this(null!, DummyDatabaseFile.Instance) {} private long? exportedMessageCount;
private long? totalMessageCount;
public MessageFilterPanelModel(Window window, IDatabaseFile db, string verb = "Matches") { [Obsolete("Designer")]
this.window = window; public MessageFilterPanelModel() : this(null!, DummyDatabaseFile.Instance) {}
this.db = db;
this.verb = verb;
this.exportedMessageCountComputer = AsyncValueComputer<long>.WithResultProcessor(SetExportedMessageCount).Build(); public MessageFilterPanelModel(Window window, IDatabaseFile db, string verb = "Matches") {
this.window = window;
this.db = db;
this.verb = verb;
this.exportedMessageCountComputer = AsyncValueComputer<long>.WithResultProcessor(SetExportedMessageCount).Build();
UpdateFilterStatistics();
UpdateChannelFilterLabel();
UpdateUserFilterLabel();
PropertyChanged += OnPropertyChanged;
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
}
public void Dispose() {
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
}
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName != null && FilterProperties.Contains(e.PropertyName)) {
UpdateFilterStatistics(); UpdateFilterStatistics();
FilterPropertyChanged?.Invoke(sender, e);
}
if (e.PropertyName is nameof(FilterByChannel) or nameof(IncludedChannels)) {
UpdateChannelFilterLabel(); UpdateChannelFilterLabel();
}
else if (e.PropertyName is nameof(FilterByUser) or nameof(IncludedUsers)) {
UpdateUserFilterLabel(); UpdateUserFilterLabel();
PropertyChanged += OnPropertyChanged;
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
}
public void Dispose() {
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
}
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName != null && FilterProperties.Contains(e.PropertyName)) {
UpdateFilterStatistics();
FilterPropertyChanged?.Invoke(sender, e);
}
if (e.PropertyName is nameof(FilterByChannel) or nameof(IncludedChannels)) {
UpdateChannelFilterLabel();
}
else if (e.PropertyName is nameof(FilterByUser) or nameof(IncludedUsers)) {
UpdateUserFilterLabel();
}
}
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) {
totalMessageCount = db.Statistics.TotalMessages;
UpdateFilterStatistics();
}
else if (e.PropertyName == nameof(DatabaseStatistics.TotalChannels)) {
UpdateChannelFilterLabel();
}
else if (e.PropertyName == nameof(DatabaseStatistics.TotalUsers)) {
UpdateUserFilterLabel();
}
}
private void UpdateFilterStatistics() {
var filter = CreateFilter();
if (filter.IsEmpty) {
exportedMessageCountComputer.Cancel();
exportedMessageCount = totalMessageCount;
UpdateFilterStatisticsText();
}
else {
exportedMessageCount = null;
UpdateFilterStatisticsText();
exportedMessageCountComputer.Compute(() => db.CountMessages(filter));
}
}
private void SetExportedMessageCount(long exportedMessageCount) {
this.exportedMessageCount = exportedMessageCount;
UpdateFilterStatisticsText();
}
private void UpdateFilterStatisticsText() {
var exportedMessageCountStr = exportedMessageCount?.Format() ?? "(...)";
var totalMessageCountStr = totalMessageCount?.Format() ?? "(...)";
FilterStatisticsText = verb + " " + exportedMessageCountStr + " out of " + totalMessageCountStr + " message" + (totalMessageCount is null or 1 ? "." : "s.");
OnPropertyChanged(nameof(FilterStatisticsText));
}
public async void OpenChannelFilterDialog() {
var servers = db.GetAllServers().ToDictionary(static server => server.Id);
var items = new List<CheckBoxItem<ulong>>();
var included = IncludedChannels;
foreach (var channel in db.GetAllChannels()) {
var channelId = channel.Id;
var channelName = channel.Name;
string title;
if (servers.TryGetValue(channel.Server, out var server)) {
var titleBuilder = new StringBuilder();
var serverType = server.Type;
titleBuilder.Append('[')
.Append(ServerTypes.ToString(serverType))
.Append("] ");
if (serverType == ServerType.DirectMessage) {
titleBuilder.Append(channelName);
}
else {
titleBuilder.Append(server.Name)
.Append(" - ")
.Append(channelName);
}
title = titleBuilder.ToString();
}
else {
title = channelName;
}
items.Add(new CheckBoxItem<ulong>(channelId) {
Title = title,
Checked = included.Contains(channelId)
});
}
var result = await OpenIdFilterDialog(window, "Included Channels", items);
if (result != null) {
IncludedChannels = result;
}
}
public async void OpenUserFilterDialog() {
var items = new List<CheckBoxItem<ulong>>();
var included = IncludedUsers;
foreach (var user in db.GetAllUsers()) {
var name = user.Name;
var discriminator = user.Discriminator;
items.Add(new CheckBoxItem<ulong>(user.Id) {
Title = discriminator == null ? name : name + " #" + discriminator,
Checked = included.Contains(user.Id)
});
}
var result = await OpenIdFilterDialog(window, "Included Users", items);
if (result != null) {
IncludedUsers = result;
}
}
private void UpdateChannelFilterLabel() {
long total = db.Statistics.TotalChannels;
long included = FilterByChannel ? IncludedChannels.Count : total;
ChannelFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("channel") + ".";
}
private void UpdateUserFilterLabel() {
long total = db.Statistics.TotalUsers;
long included = FilterByUser ? IncludedUsers.Count : total;
UserFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("user") + ".";
}
public MessageFilter CreateFilter() {
MessageFilter filter = new();
if (FilterByDate) {
filter.StartDate = StartDate;
filter.EndDate = EndDate?.AddDays(1).AddMilliseconds(-1);
}
if (FilterByChannel) {
filter.ChannelIds = new HashSet<ulong>(IncludedChannels);
}
if (FilterByUser) {
filter.UserIds = new HashSet<ulong>(IncludedUsers);
}
return filter;
}
private static async Task<HashSet<ulong>?> OpenIdFilterDialog(Window window, string title, List<CheckBoxItem<ulong>> items) {
items.Sort(static (item1, item2) => item1.Title.CompareTo(item2.Title));
var model = new CheckBoxDialogModel<ulong>(items) {
Title = title
};
var dialog = new CheckBoxDialog { DataContext = model };
var result = await dialog.ShowDialog<DialogResult.OkCancel>(window);
return result == DialogResult.OkCancel.Ok ? model.SelectedItems.Select(static item => item.Item).ToHashSet() : null;
} }
} }
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) {
totalMessageCount = db.Statistics.TotalMessages;
UpdateFilterStatistics();
}
else if (e.PropertyName == nameof(DatabaseStatistics.TotalChannels)) {
UpdateChannelFilterLabel();
}
else if (e.PropertyName == nameof(DatabaseStatistics.TotalUsers)) {
UpdateUserFilterLabel();
}
}
private void UpdateFilterStatistics() {
var filter = CreateFilter();
if (filter.IsEmpty) {
exportedMessageCountComputer.Cancel();
exportedMessageCount = totalMessageCount;
UpdateFilterStatisticsText();
}
else {
exportedMessageCount = null;
UpdateFilterStatisticsText();
exportedMessageCountComputer.Compute(() => db.CountMessages(filter));
}
}
private void SetExportedMessageCount(long exportedMessageCount) {
this.exportedMessageCount = exportedMessageCount;
UpdateFilterStatisticsText();
}
private void UpdateFilterStatisticsText() {
var exportedMessageCountStr = exportedMessageCount?.Format() ?? "(...)";
var totalMessageCountStr = totalMessageCount?.Format() ?? "(...)";
FilterStatisticsText = verb + " " + exportedMessageCountStr + " out of " + totalMessageCountStr + " message" + (totalMessageCount is null or 1 ? "." : "s.");
OnPropertyChanged(nameof(FilterStatisticsText));
}
public async void OpenChannelFilterDialog() {
var servers = db.GetAllServers().ToDictionary(static server => server.Id);
var items = new List<CheckBoxItem<ulong>>();
var included = IncludedChannels;
foreach (var channel in db.GetAllChannels()) {
var channelId = channel.Id;
var channelName = channel.Name;
string title;
if (servers.TryGetValue(channel.Server, out var server)) {
var titleBuilder = new StringBuilder();
var serverType = server.Type;
titleBuilder.Append('[')
.Append(ServerTypes.ToString(serverType))
.Append("] ");
if (serverType == ServerType.DirectMessage) {
titleBuilder.Append(channelName);
}
else {
titleBuilder.Append(server.Name)
.Append(" - ")
.Append(channelName);
}
title = titleBuilder.ToString();
}
else {
title = channelName;
}
items.Add(new CheckBoxItem<ulong>(channelId) {
Title = title,
Checked = included.Contains(channelId)
});
}
var result = await OpenIdFilterDialog(window, "Included Channels", items);
if (result != null) {
IncludedChannels = result;
}
}
public async void OpenUserFilterDialog() {
var items = new List<CheckBoxItem<ulong>>();
var included = IncludedUsers;
foreach (var user in db.GetAllUsers()) {
var name = user.Name;
var discriminator = user.Discriminator;
items.Add(new CheckBoxItem<ulong>(user.Id) {
Title = discriminator == null ? name : name + " #" + discriminator,
Checked = included.Contains(user.Id)
});
}
var result = await OpenIdFilterDialog(window, "Included Users", items);
if (result != null) {
IncludedUsers = result;
}
}
private void UpdateChannelFilterLabel() {
long total = db.Statistics.TotalChannels;
long included = FilterByChannel ? IncludedChannels.Count : total;
ChannelFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("channel") + ".";
}
private void UpdateUserFilterLabel() {
long total = db.Statistics.TotalUsers;
long included = FilterByUser ? IncludedUsers.Count : total;
UserFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("user") + ".";
}
public MessageFilter CreateFilter() {
MessageFilter filter = new();
if (FilterByDate) {
filter.StartDate = StartDate;
filter.EndDate = EndDate?.AddDays(1).AddMilliseconds(-1);
}
if (FilterByChannel) {
filter.ChannelIds = new HashSet<ulong>(IncludedChannels);
}
if (FilterByUser) {
filter.UserIds = new HashSet<ulong>(IncludedUsers);
}
return filter;
}
private static async Task<HashSet<ulong>?> OpenIdFilterDialog(Window window, string title, List<CheckBoxItem<ulong>> items) {
items.Sort(static (item1, item2) => item1.Title.CompareTo(item2.Title));
var model = new CheckBoxDialogModel<ulong>(items) {
Title = title
};
var dialog = new CheckBoxDialog { DataContext = model };
var result = await dialog.ShowDialog<DialogResult.OkCancel>(window);
return result == DialogResult.OkCancel.Ok ? model.SelectedItems.Select(static item => item.Item).ToHashSet() : null;
}
} }

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Controls { namespace DHT.Desktop.Main.Controls;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class ServerConfigurationPanel : UserControl {
public ServerConfigurationPanel() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class ServerConfigurationPanel : UserControl {
} public ServerConfigurationPanel() {
InitializeComponent();
} }
} }

View File

@ -6,111 +6,111 @@ using DHT.Server.Database;
using DHT.Server.Service; using DHT.Server.Service;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Main.Controls { namespace DHT.Desktop.Main.Controls;
sealed class ServerConfigurationPanelModel : BaseModel, IDisposable {
private string inputPort;
public string InputPort { sealed class ServerConfigurationPanelModel : BaseModel, IDisposable {
get => inputPort; private string inputPort;
set {
Change(ref inputPort, value);
OnPropertyChanged(nameof(HasMadeChanges));
}
}
private string inputToken; public string InputPort {
get => inputPort;
public string InputToken { set {
get => inputToken; Change(ref inputPort, value);
set {
Change(ref inputToken, value);
OnPropertyChanged(nameof(HasMadeChanges));
}
}
public bool HasMadeChanges => ServerManager.Port.ToString() != InputPort || ServerManager.Token != InputToken;
private bool isToggleServerButtonEnabled = true;
public bool IsToggleServerButtonEnabled {
get => isToggleServerButtonEnabled;
set => Change(ref isToggleServerButtonEnabled, value);
}
public string ToggleServerButtonText => serverManager.IsRunning ? "Stop Server" : "Start Server";
public event EventHandler<StatusBarModel.Status>? ServerStatusChanged;
private readonly Window window;
private readonly ServerManager serverManager;
[Obsolete("Designer")]
public ServerConfigurationPanelModel() : this(null!, new ServerManager(DummyDatabaseFile.Instance)) {}
public ServerConfigurationPanelModel(Window window, ServerManager serverManager) {
this.window = window;
this.serverManager = serverManager;
this.inputPort = ServerManager.Port.ToString();
this.inputToken = ServerManager.Token;
}
public void Initialize() {
ServerLauncher.ServerStatusChanged += ServerLauncherOnServerStatusChanged;
}
public void Dispose() {
ServerLauncher.ServerStatusChanged -= ServerLauncherOnServerStatusChanged;
}
private void ServerLauncherOnServerStatusChanged(object? sender, EventArgs e) {
ServerStatusChanged?.Invoke(this, serverManager.IsRunning ? StatusBarModel.Status.Ready : StatusBarModel.Status.Stopped);
OnPropertyChanged(nameof(ToggleServerButtonText));
IsToggleServerButtonEnabled = true;
}
private void BeforeServerStart() {
IsToggleServerButtonEnabled = false;
ServerStatusChanged?.Invoke(this, StatusBarModel.Status.Starting);
}
private void StartServer() {
BeforeServerStart();
serverManager.Launch();
}
private void StopServer() {
IsToggleServerButtonEnabled = false;
ServerStatusChanged?.Invoke(this, StatusBarModel.Status.Stopping);
serverManager.Stop();
}
public void OnClickToggleServerButton() {
if (serverManager.IsRunning) {
StopServer();
}
else {
StartServer();
}
}
public void OnClickRandomizeToken() {
InputToken = ServerUtils.GenerateRandomToken(20);
}
public async void OnClickApplyChanges() {
if (!ushort.TryParse(InputPort, out ushort port)) {
await Dialog.ShowOk(window, "Invalid Port", "Port must be a number between 0 and 65535.");
return;
}
BeforeServerStart();
serverManager.Relaunch(port, InputToken);
OnPropertyChanged(nameof(HasMadeChanges)); OnPropertyChanged(nameof(HasMadeChanges));
} }
}
public void OnClickCancelChanges() { private string inputToken;
InputPort = ServerManager.Port.ToString();
InputToken = ServerManager.Token; public string InputToken {
get => inputToken;
set {
Change(ref inputToken, value);
OnPropertyChanged(nameof(HasMadeChanges));
} }
} }
public bool HasMadeChanges => ServerManager.Port.ToString() != InputPort || ServerManager.Token != InputToken;
private bool isToggleServerButtonEnabled = true;
public bool IsToggleServerButtonEnabled {
get => isToggleServerButtonEnabled;
set => Change(ref isToggleServerButtonEnabled, value);
}
public string ToggleServerButtonText => serverManager.IsRunning ? "Stop Server" : "Start Server";
public event EventHandler<StatusBarModel.Status>? ServerStatusChanged;
private readonly Window window;
private readonly ServerManager serverManager;
[Obsolete("Designer")]
public ServerConfigurationPanelModel() : this(null!, new ServerManager(DummyDatabaseFile.Instance)) {}
public ServerConfigurationPanelModel(Window window, ServerManager serverManager) {
this.window = window;
this.serverManager = serverManager;
this.inputPort = ServerManager.Port.ToString();
this.inputToken = ServerManager.Token;
}
public void Initialize() {
ServerLauncher.ServerStatusChanged += ServerLauncherOnServerStatusChanged;
}
public void Dispose() {
ServerLauncher.ServerStatusChanged -= ServerLauncherOnServerStatusChanged;
}
private void ServerLauncherOnServerStatusChanged(object? sender, EventArgs e) {
ServerStatusChanged?.Invoke(this, serverManager.IsRunning ? StatusBarModel.Status.Ready : StatusBarModel.Status.Stopped);
OnPropertyChanged(nameof(ToggleServerButtonText));
IsToggleServerButtonEnabled = true;
}
private void BeforeServerStart() {
IsToggleServerButtonEnabled = false;
ServerStatusChanged?.Invoke(this, StatusBarModel.Status.Starting);
}
private void StartServer() {
BeforeServerStart();
serverManager.Launch();
}
private void StopServer() {
IsToggleServerButtonEnabled = false;
ServerStatusChanged?.Invoke(this, StatusBarModel.Status.Stopping);
serverManager.Stop();
}
public void OnClickToggleServerButton() {
if (serverManager.IsRunning) {
StopServer();
}
else {
StartServer();
}
}
public void OnClickRandomizeToken() {
InputToken = ServerUtils.GenerateRandomToken(20);
}
public async void OnClickApplyChanges() {
if (!ushort.TryParse(InputPort, out ushort port)) {
await Dialog.ShowOk(window, "Invalid Port", "Port must be a number between 0 and 65535.");
return;
}
BeforeServerStart();
serverManager.Relaunch(port, InputToken);
OnPropertyChanged(nameof(HasMadeChanges));
}
public void OnClickCancelChanges() {
InputPort = ServerManager.Port.ToString();
InputToken = ServerManager.Token;
}
} }

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Controls { namespace DHT.Desktop.Main.Controls;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class StatusBar : UserControl {
public StatusBar() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class StatusBar : UserControl {
} public StatusBar() {
InitializeComponent();
} }
} }

View File

@ -2,44 +2,44 @@ using System;
using DHT.Server.Database; using DHT.Server.Database;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Main.Controls { namespace DHT.Desktop.Main.Controls;
sealed class StatusBarModel : BaseModel {
public DatabaseStatistics DatabaseStatistics { get; }
private Status status = Status.Stopped; sealed class StatusBarModel : BaseModel {
public DatabaseStatistics DatabaseStatistics { get; }
public Status CurrentStatus { private Status status = Status.Stopped;
get => status;
set {
status = value;
OnPropertyChanged(nameof(StatusText));
}
}
public string StatusText { public Status CurrentStatus {
get { get => status;
return CurrentStatus switch { set {
Status.Starting => "STARTING", status = value;
Status.Ready => "READY", OnPropertyChanged(nameof(StatusText));
Status.Stopping => "STOPPING",
Status.Stopped => "STOPPED",
_ => ""
};
}
}
[Obsolete("Designer")]
public StatusBarModel() : this(new DatabaseStatistics()) {}
public StatusBarModel(DatabaseStatistics databaseStatistics) {
this.DatabaseStatistics = databaseStatistics;
}
public enum Status {
Starting,
Ready,
Stopping,
Stopped
} }
} }
public string StatusText {
get {
return CurrentStatus switch {
Status.Starting => "STARTING",
Status.Ready => "READY",
Status.Stopping => "STOPPING",
Status.Stopped => "STOPPED",
_ => ""
};
}
}
[Obsolete("Designer")]
public StatusBarModel() : this(new DatabaseStatistics()) {}
public StatusBarModel(DatabaseStatistics databaseStatistics) {
this.DatabaseStatistics = databaseStatistics;
}
public enum Status {
Starting,
Ready,
Stopping,
Stopped
}
} }

View File

@ -1,44 +1,35 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.IO; using System.IO;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using DHT.Desktop.Main.Pages; using DHT.Desktop.Main.Pages;
using JetBrains.Annotations; using JetBrains.Annotations;
namespace DHT.Desktop.Main { namespace DHT.Desktop.Main;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class MainWindow : Window { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
[UsedImplicitly] public sealed partial class MainWindow : Window {
public MainWindow() { [UsedImplicitly]
InitializeComponent(Arguments.Empty); public MainWindow() {
InitializeComponent();
DataContext = new MainWindowModel(this, Arguments.Empty);
}
internal MainWindow(Arguments args) {
InitializeComponent();
DataContext = new MainWindowModel(this, args);
}
public void OnClosed(object? sender, EventArgs e) {
if (DataContext is IDisposable disposable) {
disposable.Dispose();
} }
internal MainWindow(Arguments args) { foreach (var temporaryFile in ViewerPageModel.TemporaryFiles) {
InitializeComponent(args); try {
} File.Delete(temporaryFile);
} catch (Exception) {
private void InitializeComponent(Arguments args) { // ignored
AvaloniaXamlLoader.Load(this);
DataContext = new MainWindowModel(this, args);
#if DEBUG
this.AttachDevTools();
#endif
}
public void OnClosed(object? sender, EventArgs e) {
if (DataContext is IDisposable disposable) {
disposable.Dispose();
}
foreach (var temporaryFile in ViewerPageModel.TemporaryFiles) {
try {
File.Delete(temporaryFile);
} catch (Exception) {
// ignored
}
} }
} }
} }

View File

@ -10,107 +10,107 @@ using DHT.Desktop.Server;
using DHT.Server.Database; using DHT.Server.Database;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Main { namespace DHT.Desktop.Main;
sealed class MainWindowModel : BaseModel, IDisposable {
private const string DefaultTitle = "Discord History Tracker";
public string Title { get; private set; } = DefaultTitle; sealed class MainWindowModel : BaseModel, IDisposable {
private const string DefaultTitle = "Discord History Tracker";
public UserControl CurrentScreen { get; private set; } public string Title { get; private set; } = DefaultTitle;
private readonly WelcomeScreen welcomeScreen; public UserControl CurrentScreen { get; private set; }
private readonly WelcomeScreenModel welcomeScreenModel;
private MainContentScreen? mainContentScreen; private readonly WelcomeScreen welcomeScreen;
private MainContentScreenModel? mainContentScreenModel; private readonly WelcomeScreenModel welcomeScreenModel;
private readonly Window window; private MainContentScreen? mainContentScreen;
private MainContentScreenModel? mainContentScreenModel;
private IDatabaseFile? db; private readonly Window window;
[Obsolete("Designer")] private IDatabaseFile? db;
public MainWindowModel() : this(null!, Arguments.Empty) {}
public MainWindowModel(Window window, Arguments args) { [Obsolete("Designer")]
this.window = window; public MainWindowModel() : this(null!, Arguments.Empty) {}
welcomeScreenModel = new WelcomeScreenModel(window); public MainWindowModel(Window window, Arguments args) {
welcomeScreen = new WelcomeScreen { DataContext = welcomeScreenModel }; this.window = window;
CurrentScreen = welcomeScreen;
welcomeScreenModel.PropertyChanged += WelcomeScreenModelOnPropertyChanged; welcomeScreenModel = new WelcomeScreenModel(window);
welcomeScreen = new WelcomeScreen { DataContext = welcomeScreenModel };
CurrentScreen = welcomeScreen;
var dbFile = args.DatabaseFile; welcomeScreenModel.PropertyChanged += WelcomeScreenModelOnPropertyChanged;
if (!string.IsNullOrWhiteSpace(dbFile)) {
async void OnWindowOpened(object? o, EventArgs eventArgs) {
window.Opened -= OnWindowOpened;
// https://github.com/AvaloniaUI/Avalonia/issues/3071 var dbFile = args.DatabaseFile;
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (!string.IsNullOrWhiteSpace(dbFile)) {
await Task.Delay(500); async void OnWindowOpened(object? o, EventArgs eventArgs) {
} window.Opened -= OnWindowOpened;
if (File.Exists(dbFile)) { // https://github.com/AvaloniaUI/Avalonia/issues/3071
await welcomeScreenModel.OpenOrCreateDatabaseFromPath(dbFile); if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
} await Task.Delay(500);
else {
await Dialog.ShowOk(window, "Database Error", "Database file not found:\n" + dbFile);
}
} }
window.Opened += OnWindowOpened; if (File.Exists(dbFile)) {
} await welcomeScreenModel.OpenOrCreateDatabaseFromPath(dbFile);
if (args.ServerPort != null) {
ServerManager.Port = args.ServerPort.Value;
}
if (args.ServerToken != null) {
ServerManager.Token = args.ServerToken;
}
}
private async void WelcomeScreenModelOnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(welcomeScreenModel.Db)) {
if (mainContentScreenModel != null) {
mainContentScreenModel.DatabaseClosed -= MainContentScreenModelOnDatabaseClosed;
mainContentScreenModel.Dispose();
}
db?.Dispose();
db = welcomeScreenModel.Db;
if (db == null) {
Title = DefaultTitle;
mainContentScreenModel = null;
mainContentScreen = null;
CurrentScreen = welcomeScreen;
} }
else { else {
Title = Path.GetFileName(db.Path) + " - " + DefaultTitle; await Dialog.ShowOk(window, "Database Error", "Database file not found:\n" + dbFile);
mainContentScreenModel = new MainContentScreenModel(window, db);
await mainContentScreenModel.Initialize();
mainContentScreenModel.DatabaseClosed += MainContentScreenModelOnDatabaseClosed;
mainContentScreen = new MainContentScreen { DataContext = mainContentScreenModel };
CurrentScreen = mainContentScreen;
} }
OnPropertyChanged(nameof(CurrentScreen));
OnPropertyChanged(nameof(Title));
window.Focus();
} }
window.Opened += OnWindowOpened;
} }
private void MainContentScreenModelOnDatabaseClosed(object? sender, EventArgs e) { if (args.ServerPort != null) {
welcomeScreenModel.CloseDatabase(); ServerManager.Port = args.ServerPort.Value;
} }
public void Dispose() { if (args.ServerToken != null) {
welcomeScreenModel.Dispose(); ServerManager.Token = args.ServerToken;
mainContentScreenModel?.Dispose();
db?.Dispose();
db = null;
} }
} }
private async void WelcomeScreenModelOnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(welcomeScreenModel.Db)) {
if (mainContentScreenModel != null) {
mainContentScreenModel.DatabaseClosed -= MainContentScreenModelOnDatabaseClosed;
mainContentScreenModel.Dispose();
}
db?.Dispose();
db = welcomeScreenModel.Db;
if (db == null) {
Title = DefaultTitle;
mainContentScreenModel = null;
mainContentScreen = null;
CurrentScreen = welcomeScreen;
}
else {
Title = Path.GetFileName(db.Path) + " - " + DefaultTitle;
mainContentScreenModel = new MainContentScreenModel(window, db);
await mainContentScreenModel.Initialize();
mainContentScreenModel.DatabaseClosed += MainContentScreenModelOnDatabaseClosed;
mainContentScreen = new MainContentScreen { DataContext = mainContentScreenModel };
CurrentScreen = mainContentScreen;
}
OnPropertyChanged(nameof(CurrentScreen));
OnPropertyChanged(nameof(Title));
window.Focus();
}
}
private void MainContentScreenModelOnDatabaseClosed(object? sender, EventArgs e) {
welcomeScreenModel.CloseDatabase();
}
public void Dispose() {
welcomeScreenModel.Dispose();
mainContentScreenModel?.Dispose();
db?.Dispose();
db = null;
}
} }

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class AdvancedPage : UserControl {
public AdvancedPage() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class AdvancedPage : UserControl {
} public AdvancedPage() {
InitializeComponent();
} }
} }

View File

@ -6,34 +6,34 @@ using DHT.Desktop.Server;
using DHT.Server.Database; using DHT.Server.Database;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
sealed class AdvancedPageModel : BaseModel, IDisposable {
public ServerConfigurationPanelModel ServerConfigurationModel { get; }
private readonly Window window; sealed class AdvancedPageModel : BaseModel, IDisposable {
private readonly IDatabaseFile db; public ServerConfigurationPanelModel ServerConfigurationModel { get; }
[Obsolete("Designer")] private readonly Window window;
public AdvancedPageModel() : this(null!, DummyDatabaseFile.Instance, new ServerManager(DummyDatabaseFile.Instance)) {} private readonly IDatabaseFile db;
public AdvancedPageModel(Window window, IDatabaseFile db, ServerManager serverManager) { [Obsolete("Designer")]
this.window = window; public AdvancedPageModel() : this(null!, DummyDatabaseFile.Instance, new ServerManager(DummyDatabaseFile.Instance)) {}
this.db = db;
ServerConfigurationModel = new ServerConfigurationPanelModel(window, serverManager); public AdvancedPageModel(Window window, IDatabaseFile db, ServerManager serverManager) {
} this.window = window;
this.db = db;
public void Initialize() { ServerConfigurationModel = new ServerConfigurationPanelModel(window, serverManager);
ServerConfigurationModel.Initialize(); }
}
public void Dispose() { public void Initialize() {
ServerConfigurationModel.Dispose(); ServerConfigurationModel.Initialize();
} }
public async void VacuumDatabase() { public void Dispose() {
db.Vacuum(); ServerConfigurationModel.Dispose();
await Dialog.ShowOk(window, "Vacuum Database", "Done."); }
}
public async void VacuumDatabase() {
db.Vacuum();
await Dialog.ShowOk(window, "Vacuum Database", "Done.");
} }
} }

View File

@ -38,7 +38,7 @@
<controls:AttachmentFilterPanel DataContext="{Binding FilterModel}" IsEnabled="{Binding !DataContext.IsDownloading, RelativeSource={RelativeSource AncestorType=UserControl}}" /> <controls:AttachmentFilterPanel DataContext="{Binding FilterModel}" IsEnabled="{Binding !DataContext.IsDownloading, RelativeSource={RelativeSource AncestorType=UserControl}}" />
<StackPanel Orientation="Vertical" Spacing="12"> <StackPanel Orientation="Vertical" Spacing="12">
<Expander Header="Download Status" IsExpanded="True"> <Expander Header="Download Status" IsExpanded="True">
<DataGrid Items="{Binding StatisticsRows}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="True"> <DataGrid ItemsSource="{Binding StatisticsRows}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="True">
<DataGrid.Columns> <DataGrid.Columns>
<DataGridTextColumn Header="State" Binding="{Binding State}" Width="*" /> <DataGridTextColumn Header="State" Binding="{Binding State}" Width="*" />
<DataGridTextColumn Header="Attachments" Binding="{Binding Items, Converter={StaticResource NumberValueConverter}}" Width="*" CellStyleClasses="right" /> <DataGridTextColumn Header="Attachments" Binding="{Binding Items, Converter={StaticResource NumberValueConverter}}" Width="*" CellStyleClasses="right" />

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class AttachmentsPage : UserControl {
public AttachmentsPage() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class AttachmentsPage : UserControl {
} public AttachmentsPage() {
InitializeComponent();
} }
} }

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Threading;
using Avalonia.Threading;
using DHT.Desktop.Common; using DHT.Desktop.Common;
using DHT.Desktop.Main.Controls; using DHT.Desktop.Main.Controls;
using DHT.Server.Data; using DHT.Server.Data;
@ -11,193 +13,193 @@ using DHT.Server.Download;
using DHT.Utils.Models; using DHT.Utils.Models;
using DHT.Utils.Tasks; using DHT.Utils.Tasks;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
sealed class AttachmentsPageModel : BaseModel, IDisposable {
private static readonly DownloadItemFilter EnqueuedItemFilter = new() {
IncludeStatuses = new HashSet<DownloadStatus> {
DownloadStatus.Enqueued
}
};
private bool isThreadDownloadButtonEnabled = true; sealed class AttachmentsPageModel : BaseModel, IDisposable {
private static readonly DownloadItemFilter EnqueuedItemFilter = new() {
public string ToggleDownloadButtonText => downloadThread == null ? "Start Downloading" : "Stop Downloading"; IncludeStatuses = new HashSet<DownloadStatus> {
DownloadStatus.Enqueued
public bool IsToggleDownloadButtonEnabled {
get => isThreadDownloadButtonEnabled;
set => Change(ref isThreadDownloadButtonEnabled, value);
} }
};
public string DownloadMessage { get; set; } = ""; private bool isThreadDownloadButtonEnabled = true;
public double DownloadProgress => allItemsCount is null or 0 ? 0.0 : 100.0 * doneItemsCount / allItemsCount.Value;
public AttachmentFilterPanelModel FilterModel { get; } public string ToggleDownloadButtonText => downloadThread == null ? "Start Downloading" : "Stop Downloading";
private readonly StatisticsRow statisticsEnqueued = new ("Enqueued"); public bool IsToggleDownloadButtonEnabled {
private readonly StatisticsRow statisticsDownloaded = new ("Downloaded"); get => isThreadDownloadButtonEnabled;
private readonly StatisticsRow statisticsFailed = new ("Failed"); set => Change(ref isThreadDownloadButtonEnabled, value);
private readonly StatisticsRow statisticsSkipped = new ("Skipped"); }
public List<StatisticsRow> StatisticsRows { public string DownloadMessage { get; set; } = "";
get { public double DownloadProgress => allItemsCount is null or 0 ? 0.0 : 100.0 * doneItemsCount / allItemsCount.Value;
return new List<StatisticsRow> {
statisticsEnqueued,
statisticsDownloaded,
statisticsFailed,
statisticsSkipped
};
}
}
public bool IsDownloading => downloadThread != null; public AttachmentFilterPanelModel FilterModel { get; }
public bool HasFailedDownloads => statisticsFailed.Items > 0;
private readonly IDatabaseFile db; private readonly StatisticsRow statisticsEnqueued = new ("Enqueued");
private readonly AsyncValueComputer<DownloadStatusStatistics>.Single downloadStatisticsComputer; private readonly StatisticsRow statisticsDownloaded = new ("Downloaded");
private BackgroundDownloadThread? downloadThread; private readonly StatisticsRow statisticsFailed = new ("Failed");
private readonly StatisticsRow statisticsSkipped = new ("Skipped");
private int doneItemsCount; public List<StatisticsRow> StatisticsRows {
private int? allItemsCount; get {
return new List<StatisticsRow> {
public AttachmentsPageModel() : this(DummyDatabaseFile.Instance) {} statisticsEnqueued,
statisticsDownloaded,
public AttachmentsPageModel(IDatabaseFile db) { statisticsFailed,
this.db = db; statisticsSkipped
this.FilterModel = new AttachmentFilterPanelModel(db);
this.downloadStatisticsComputer = AsyncValueComputer<DownloadStatusStatistics>.WithResultProcessor(UpdateStatistics).WithOutdatedResults().BuildWithComputer(db.GetDownloadStatusStatistics);
this.downloadStatisticsComputer.Recompute();
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
}
public void Dispose() {
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
FilterModel.Dispose();
DisposeDownloadThread();
}
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(DatabaseStatistics.TotalAttachments)) {
if (IsDownloading) {
EnqueueDownloadItems();
}
else {
downloadStatisticsComputer.Recompute();
}
}
else if (e.PropertyName == nameof(DatabaseStatistics.TotalDownloads)) {
downloadStatisticsComputer.Recompute();
}
}
private void EnqueueDownloadItems() {
var filter = FilterModel.CreateFilter();
filter.DownloadItemRule = AttachmentFilter.DownloadItemRules.OnlyNotPresent;
db.EnqueueDownloadItems(filter);
downloadStatisticsComputer.Recompute();
}
private void UpdateStatistics(DownloadStatusStatistics statusStatistics) {
var hadFailedDownloads = HasFailedDownloads;
statisticsEnqueued.Items = statusStatistics.EnqueuedCount;
statisticsEnqueued.Size = statusStatistics.EnqueuedSize;
statisticsDownloaded.Items = statusStatistics.SuccessfulCount;
statisticsDownloaded.Size = statusStatistics.SuccessfulSize;
statisticsFailed.Items = statusStatistics.FailedCount;
statisticsFailed.Size = statusStatistics.FailedSize;
statisticsSkipped.Items = statusStatistics.SkippedCount;
statisticsSkipped.Size = statusStatistics.SkippedSize;
OnPropertyChanged(nameof(StatisticsRows));
if (hadFailedDownloads != HasFailedDownloads) {
OnPropertyChanged(nameof(HasFailedDownloads));
}
allItemsCount = doneItemsCount + statisticsEnqueued.Items;
UpdateDownloadMessage();
}
private void UpdateDownloadMessage() {
DownloadMessage = IsDownloading ? doneItemsCount.Format() + " / " + (allItemsCount?.Format() ?? "?") : "";
OnPropertyChanged(nameof(DownloadMessage));
OnPropertyChanged(nameof(DownloadProgress));
}
private void DownloadThreadOnOnItemFinished(object? sender, DownloadItem e) {
++doneItemsCount;
UpdateDownloadMessage();
downloadStatisticsComputer.Recompute();
}
private void DownloadThreadOnOnServerStopped(object? sender, EventArgs e) {
downloadStatisticsComputer.Recompute();
IsToggleDownloadButtonEnabled = true;
}
public void OnClickToggleDownload() {
if (downloadThread == null) {
EnqueueDownloadItems();
downloadThread = new BackgroundDownloadThread(db);
downloadThread.OnItemFinished += DownloadThreadOnOnItemFinished;
downloadThread.OnServerStopped += DownloadThreadOnOnServerStopped;
}
else {
IsToggleDownloadButtonEnabled = false;
DisposeDownloadThread();
db.RemoveDownloadItems(EnqueuedItemFilter, FilterRemovalMode.RemoveMatching);
doneItemsCount = 0;
allItemsCount = null;
UpdateDownloadMessage();
}
OnPropertyChanged(nameof(ToggleDownloadButtonText));
OnPropertyChanged(nameof(IsDownloading));
}
public void OnClickRetryFailedDownloads() {
var allExceptFailedFilter = new DownloadItemFilter {
IncludeStatuses = new HashSet<DownloadStatus> {
DownloadStatus.Enqueued,
DownloadStatus.Success
}
}; };
}
}
db.RemoveDownloadItems(allExceptFailedFilter, FilterRemovalMode.KeepMatching); public bool IsDownloading => downloadThread != null;
public bool HasFailedDownloads => statisticsFailed.Items > 0;
private readonly IDatabaseFile db;
private readonly AsyncValueComputer<DownloadStatusStatistics>.Single downloadStatisticsComputer;
private BackgroundDownloadThread? downloadThread;
private int doneItemsCount;
private int? allItemsCount;
public AttachmentsPageModel() : this(DummyDatabaseFile.Instance) {}
public AttachmentsPageModel(IDatabaseFile db) {
this.db = db;
this.FilterModel = new AttachmentFilterPanelModel(db);
this.downloadStatisticsComputer = AsyncValueComputer<DownloadStatusStatistics>.WithResultProcessor(UpdateStatistics).WithOutdatedResults().BuildWithComputer(db.GetDownloadStatusStatistics);
this.downloadStatisticsComputer.Recompute();
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
}
public void Dispose() {
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
FilterModel.Dispose();
DisposeDownloadThread();
}
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(DatabaseStatistics.TotalAttachments)) {
if (IsDownloading) { if (IsDownloading) {
EnqueueDownloadItems(); EnqueueDownloadItems();
} }
else {
downloadStatisticsComputer.Recompute();
}
}
else if (e.PropertyName == nameof(DatabaseStatistics.TotalDownloads)) {
downloadStatisticsComputer.Recompute();
}
}
private void EnqueueDownloadItems() {
var filter = FilterModel.CreateFilter();
filter.DownloadItemRule = AttachmentFilter.DownloadItemRules.OnlyNotPresent;
db.EnqueueDownloadItems(filter);
downloadStatisticsComputer.Recompute();
}
private void UpdateStatistics(DownloadStatusStatistics statusStatistics) {
var hadFailedDownloads = HasFailedDownloads;
statisticsEnqueued.Items = statusStatistics.EnqueuedCount;
statisticsEnqueued.Size = statusStatistics.EnqueuedSize;
statisticsDownloaded.Items = statusStatistics.SuccessfulCount;
statisticsDownloaded.Size = statusStatistics.SuccessfulSize;
statisticsFailed.Items = statusStatistics.FailedCount;
statisticsFailed.Size = statusStatistics.FailedSize;
statisticsSkipped.Items = statusStatistics.SkippedCount;
statisticsSkipped.Size = statusStatistics.SkippedSize;
OnPropertyChanged(nameof(StatisticsRows));
if (hadFailedDownloads != HasFailedDownloads) {
OnPropertyChanged(nameof(HasFailedDownloads));
} }
private void DisposeDownloadThread() { allItemsCount = doneItemsCount + statisticsEnqueued.Items;
if (downloadThread != null) { UpdateDownloadMessage();
downloadThread.OnItemFinished -= DownloadThreadOnOnItemFinished; }
downloadThread.StopThread();
}
downloadThread = null; private void UpdateDownloadMessage() {
DownloadMessage = IsDownloading ? doneItemsCount.Format() + " / " + (allItemsCount?.Format() ?? "?") : "";
OnPropertyChanged(nameof(DownloadMessage));
OnPropertyChanged(nameof(DownloadProgress));
}
private void DownloadThreadOnOnItemFinished(object? sender, DownloadItem e) {
Interlocked.Increment(ref doneItemsCount);
Dispatcher.UIThread.Invoke(UpdateDownloadMessage);
downloadStatisticsComputer.Recompute();
}
private void DownloadThreadOnOnServerStopped(object? sender, EventArgs e) {
downloadStatisticsComputer.Recompute();
IsToggleDownloadButtonEnabled = true;
}
public void OnClickToggleDownload() {
if (downloadThread == null) {
EnqueueDownloadItems();
downloadThread = new BackgroundDownloadThread(db);
downloadThread.OnItemFinished += DownloadThreadOnOnItemFinished;
downloadThread.OnServerStopped += DownloadThreadOnOnServerStopped;
}
else {
IsToggleDownloadButtonEnabled = false;
DisposeDownloadThread();
db.RemoveDownloadItems(EnqueuedItemFilter, FilterRemovalMode.RemoveMatching);
doneItemsCount = 0;
allItemsCount = null;
UpdateDownloadMessage();
} }
public sealed class StatisticsRow { OnPropertyChanged(nameof(ToggleDownloadButtonText));
public string State { get; } OnPropertyChanged(nameof(IsDownloading));
public int Items { get; set; } }
public ulong? Size { get; set; }
public StatisticsRow(string state) { public void OnClickRetryFailedDownloads() {
State = state; var allExceptFailedFilter = new DownloadItemFilter {
IncludeStatuses = new HashSet<DownloadStatus> {
DownloadStatus.Enqueued,
DownloadStatus.Success
} }
};
db.RemoveDownloadItems(allExceptFailedFilter, FilterRemovalMode.KeepMatching);
if (IsDownloading) {
EnqueueDownloadItems();
}
}
private void DisposeDownloadThread() {
if (downloadThread != null) {
downloadThread.OnItemFinished -= DownloadThreadOnOnItemFinished;
downloadThread.StopThread();
}
downloadThread = null;
}
public sealed class StatisticsRow {
public string State { get; }
public int Items { get; set; }
public ulong? Size { get; set; }
public StatisticsRow(string state) {
State = state;
} }
} }
} }

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class DatabasePage : UserControl {
public DatabasePage() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class DatabasePage : UserControl {
} public DatabasePage() {
InitializeComponent();
} }
} }

View File

@ -7,8 +7,10 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Avalonia.Threading; using Avalonia.Threading;
using DHT.Desktop.Common; using DHT.Desktop.Common;
using DHT.Desktop.Dialogs.File;
using DHT.Desktop.Dialogs.Message; using DHT.Desktop.Dialogs.Message;
using DHT.Desktop.Dialogs.Progress; using DHT.Desktop.Dialogs.Progress;
using DHT.Desktop.Dialogs.TextBox; using DHT.Desktop.Dialogs.TextBox;
@ -18,231 +20,226 @@ using DHT.Server.Database.Import;
using DHT.Utils.Logging; using DHT.Utils.Logging;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
sealed class DatabasePageModel : BaseModel {
private static readonly Log Log = Log.ForType<DatabasePageModel>();
public IDatabaseFile Db { get; } sealed class DatabasePageModel : BaseModel {
private static readonly Log Log = Log.ForType<DatabasePageModel>();
public event EventHandler? DatabaseClosed; public IDatabaseFile Db { get; }
private readonly Window window; public event EventHandler? DatabaseClosed;
[Obsolete("Designer")] private readonly Window window;
public DatabasePageModel() : this(null!, DummyDatabaseFile.Instance) {}
public DatabasePageModel(Window window, IDatabaseFile db) { [Obsolete("Designer")]
this.window = window; public DatabasePageModel() : this(null!, DummyDatabaseFile.Instance) {}
this.Db = db;
public DatabasePageModel(Window window, IDatabaseFile db) {
this.window = window;
this.Db = db;
}
public async void OpenDatabaseFolder() {
string file = Db.Path;
string? folder = Path.GetDirectoryName(file);
if (folder == null) {
return;
} }
public async void OpenDatabaseFolder() { switch (Environment.OSVersion.Platform) {
string file = Db.Path; case PlatformID.Win32NT:
string? folder = Path.GetDirectoryName(file); Process.Start("explorer.exe", "/select,\"" + file + "\"");
break;
if (folder == null) { case PlatformID.Unix:
return; Process.Start("xdg-open", new string[] { folder });
} break;
switch (Environment.OSVersion.Platform) { case PlatformID.MacOSX:
case PlatformID.Win32NT: Process.Start("open", new string[] { folder });
Process.Start("explorer.exe", "/select,\"" + file + "\""); break;
break;
case PlatformID.Unix: default:
Process.Start("xdg-open", new string[] { folder }); await Dialog.ShowOk(window, "Feature Not Supported", "This feature is not supported for your operating system.");
break; break;
case PlatformID.MacOSX:
Process.Start("open", new string[] { folder });
break;
default:
await Dialog.ShowOk(window, "Feature Not Supported", "This feature is not supported for your operating system.");
break;
}
}
public void CloseDatabase() {
DatabaseClosed?.Invoke(this, EventArgs.Empty);
}
public async void MergeWithDatabase() {
var fileDialog = DatabaseGui.NewOpenDatabaseFileDialog();
fileDialog.Directory = Path.GetDirectoryName(Db.Path);
fileDialog.AllowMultiple = true;
string[]? paths = await fileDialog.ShowAsync(window);
if (paths == null || paths.Length == 0) {
return;
}
ProgressDialog progressDialog = new ProgressDialog();
progressDialog.DataContext = new ProgressDialogModel(async callback => await MergeWithDatabaseFromPaths(Db, paths, progressDialog, callback)) {
Title = "Database Merge"
};
await progressDialog.ShowDialog(window);
}
private static async Task MergeWithDatabaseFromPaths(IDatabaseFile target, string[] paths, ProgressDialog dialog, IProgressCallback callback) {
int total = paths.Length;
DialogResult.YesNo? upgradeResult = null;
async Task<bool> CheckCanUpgradeDatabase() {
upgradeResult ??= total > 1
? await DatabaseGui.ShowCanUpgradeMultipleDatabaseDialog(dialog)
: await DatabaseGui.ShowCanUpgradeDatabaseDialog(dialog);
return DialogResult.YesNo.Yes == upgradeResult;
}
await PerformImport(target, paths, dialog, callback, "Database Merge", "Database Error", "database file", async path => {
SynchronizationContext? prevSyncContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(new AvaloniaSynchronizationContext());
IDatabaseFile? db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, dialog, CheckCanUpgradeDatabase);
SynchronizationContext.SetSynchronizationContext(prevSyncContext);
if (db == null) {
return false;
}
try {
target.AddFrom(db);
return true;
} finally {
db.Dispose();
}
});
}
public async void ImportLegacyArchive() {
var fileDialog = new OpenFileDialog {
Title = "Open Legacy DHT Archive",
Directory = Path.GetDirectoryName(Db.Path),
AllowMultiple = true
};
string[]? paths = await fileDialog.ShowAsync(window);
if (paths == null || paths.Length == 0) {
return;
}
ProgressDialog progressDialog = new ProgressDialog();
progressDialog.DataContext = new ProgressDialogModel(async callback => await ImportLegacyArchiveFromPaths(Db, paths, progressDialog, callback)) {
Title = "Legacy Archive Import"
};
await progressDialog.ShowDialog(window);
}
private static async Task ImportLegacyArchiveFromPaths(IDatabaseFile target, string[] paths, ProgressDialog dialog, IProgressCallback callback) {
var fakeSnowflake = new FakeSnowflake();
await PerformImport(target, paths, dialog, callback, "Legacy Archive Import", "Legacy Archive Error", "archive file", async path => {
await using var jsonStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
return await LegacyArchiveImport.Read(jsonStream, target, fakeSnowflake, async servers => {
SynchronizationContext? prevSyncContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(new AvaloniaSynchronizationContext());
Dictionary<DHT.Server.Data.Server, ulong>? result = await Dispatcher.UIThread.InvokeAsync(() => AskForServerIds(dialog, servers));
SynchronizationContext.SetSynchronizationContext(prevSyncContext);
return result;
});
});
}
private static async Task<Dictionary<DHT.Server.Data.Server, ulong>?> AskForServerIds(Window window, DHT.Server.Data.Server[] servers) {
static bool IsValidSnowflake(string value) {
return string.IsNullOrEmpty(value) || ulong.TryParse(value, out _);
}
var items = new List<TextBoxItem<DHT.Server.Data.Server>>();
foreach (var server in servers.OrderBy(static server => server.Type).ThenBy(static server => server.Name)) {
items.Add(new TextBoxItem<DHT.Server.Data.Server>(server) {
Title = server.Name + " (" + ServerTypes.ToNiceString(server.Type) + ")",
ValidityCheck = IsValidSnowflake
});
}
var model = new TextBoxDialogModel<DHT.Server.Data.Server>(items) {
Title = "Imported Server IDs",
Description = "Please fill in the IDs of servers and direct messages. First enable Developer Mode in Discord, then right-click each server or direct message, click 'Copy ID', and paste it into the input field. If a server no longer exists, leave its input field empty to use a random ID."
};
var dialog = new TextBoxDialog { DataContext = model };
var result = await dialog.ShowDialog<DialogResult.OkCancel>(window);
if (result != DialogResult.OkCancel.Ok) {
return null;
}
return model.ValidItems
.Where(static item => !string.IsNullOrEmpty(item.Value))
.ToDictionary(static item => item.Item, static item => ulong.Parse(item.Value));
}
private static async Task PerformImport(IDatabaseFile target, string[] paths, ProgressDialog dialog, IProgressCallback callback, string neutralDialogTitle, string errorDialogTitle, string itemName, Func<string, Task<bool>> performImport) {
int total = paths.Length;
var oldStatistics = target.SnapshotStatistics();
int successful = 0;
int finished = 0;
foreach (string path in paths) {
await callback.Update(Path.GetFileName(path), finished, total);
++finished;
if (!File.Exists(path)) {
await Dialog.ShowOk(dialog, errorDialogTitle, "File '" + Path.GetFileName(path) + "' no longer exists.");
continue;
}
try {
if (await performImport(path)) {
++successful;
}
} catch (Exception ex) {
Log.Error(ex);
await Dialog.ShowOk(dialog, errorDialogTitle, "File '" + Path.GetFileName(path) + "' could not be imported: " + ex.Message);
}
}
await callback.Update("Done", finished, total);
if (successful == 0) {
await Dialog.ShowOk(dialog, neutralDialogTitle, "Nothing was imported.");
return;
}
await Dialog.ShowOk(dialog, neutralDialogTitle, GetImportDialogMessage(oldStatistics, target.SnapshotStatistics(), successful, total, itemName));
}
private static string GetImportDialogMessage(DatabaseStatisticsSnapshot oldStatistics, DatabaseStatisticsSnapshot newStatistics, int successfulItems, int totalItems, string itemName) {
long newServers = newStatistics.TotalServers - oldStatistics.TotalServers;
long newChannels = newStatistics.TotalChannels - oldStatistics.TotalChannels;
long newUsers = newStatistics.TotalUsers - oldStatistics.TotalUsers;
long newMessages = newStatistics.TotalMessages - oldStatistics.TotalMessages;
StringBuilder message = new StringBuilder();
message.Append("Processed ");
if (successfulItems == totalItems) {
message.Append(successfulItems.Pluralize(itemName));
}
else {
message.Append(successfulItems.Format()).Append(" out of ").Append(totalItems.Pluralize(itemName));
}
message.Append(" and added:\n\n \u2022 ");
message.Append(newServers.Pluralize("server")).Append("\n \u2022 ");
message.Append(newChannels.Pluralize("channel")).Append("\n \u2022 ");
message.Append(newUsers.Pluralize("user")).Append("\n \u2022 ");
message.Append(newMessages.Pluralize("message"));
return message.ToString();
} }
} }
public void CloseDatabase() {
DatabaseClosed?.Invoke(this, EventArgs.Empty);
}
public async void MergeWithDatabase() {
var paths = await DatabaseGui.NewOpenDatabaseFilesDialog(window, Path.GetDirectoryName(Db.Path));
if (paths.Length == 0) {
return;
}
ProgressDialog progressDialog = new ProgressDialog();
progressDialog.DataContext = new ProgressDialogModel(async callback => await MergeWithDatabaseFromPaths(Db, paths, progressDialog, callback)) {
Title = "Database Merge"
};
await progressDialog.ShowDialog(window);
}
private static async Task MergeWithDatabaseFromPaths(IDatabaseFile target, string[] paths, ProgressDialog dialog, IProgressCallback callback) {
int total = paths.Length;
DialogResult.YesNo? upgradeResult = null;
async Task<bool> CheckCanUpgradeDatabase() {
upgradeResult ??= total > 1
? await DatabaseGui.ShowCanUpgradeMultipleDatabaseDialog(dialog)
: await DatabaseGui.ShowCanUpgradeDatabaseDialog(dialog);
return DialogResult.YesNo.Yes == upgradeResult;
}
await PerformImport(target, paths, dialog, callback, "Database Merge", "Database Error", "database file", async path => {
SynchronizationContext? prevSyncContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(new AvaloniaSynchronizationContext());
IDatabaseFile? db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, dialog, CheckCanUpgradeDatabase);
SynchronizationContext.SetSynchronizationContext(prevSyncContext);
if (db == null) {
return false;
}
try {
target.AddFrom(db);
return true;
} finally {
db.Dispose();
}
});
}
public async void ImportLegacyArchive() {
var paths = await window.StorageProvider.OpenFiles(new FilePickerOpenOptions {
Title = "Open Legacy DHT Archive",
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, Path.GetDirectoryName(Db.Path)),
AllowMultiple = true
});
if (paths.Length == 0) {
return;
}
ProgressDialog progressDialog = new ProgressDialog();
progressDialog.DataContext = new ProgressDialogModel(async callback => await ImportLegacyArchiveFromPaths(Db, paths, progressDialog, callback)) {
Title = "Legacy Archive Import"
};
await progressDialog.ShowDialog(window);
}
private static async Task ImportLegacyArchiveFromPaths(IDatabaseFile target, string[] paths, ProgressDialog dialog, IProgressCallback callback) {
var fakeSnowflake = new FakeSnowflake();
await PerformImport(target, paths, dialog, callback, "Legacy Archive Import", "Legacy Archive Error", "archive file", async path => {
await using var jsonStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
return await LegacyArchiveImport.Read(jsonStream, target, fakeSnowflake, async servers => {
SynchronizationContext? prevSyncContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(new AvaloniaSynchronizationContext());
Dictionary<DHT.Server.Data.Server, ulong>? result = await Dispatcher.UIThread.InvokeAsync(() => AskForServerIds(dialog, servers));
SynchronizationContext.SetSynchronizationContext(prevSyncContext);
return result;
});
});
}
private static async Task<Dictionary<DHT.Server.Data.Server, ulong>?> AskForServerIds(Window window, DHT.Server.Data.Server[] servers) {
static bool IsValidSnowflake(string value) {
return string.IsNullOrEmpty(value) || ulong.TryParse(value, out _);
}
var items = new List<TextBoxItem<DHT.Server.Data.Server>>();
foreach (var server in servers.OrderBy(static server => server.Type).ThenBy(static server => server.Name)) {
items.Add(new TextBoxItem<DHT.Server.Data.Server>(server) {
Title = server.Name + " (" + ServerTypes.ToNiceString(server.Type) + ")",
ValidityCheck = IsValidSnowflake
});
}
var model = new TextBoxDialogModel<DHT.Server.Data.Server>(items) {
Title = "Imported Server IDs",
Description = "Please fill in the IDs of servers and direct messages. First enable Developer Mode in Discord, then right-click each server or direct message, click 'Copy ID', and paste it into the input field. If a server no longer exists, leave its input field empty to use a random ID."
};
var dialog = new TextBoxDialog { DataContext = model };
var result = await dialog.ShowDialog<DialogResult.OkCancel>(window);
if (result != DialogResult.OkCancel.Ok) {
return null;
}
return model.ValidItems
.Where(static item => !string.IsNullOrEmpty(item.Value))
.ToDictionary(static item => item.Item, static item => ulong.Parse(item.Value));
}
private static async Task PerformImport(IDatabaseFile target, string[] paths, ProgressDialog dialog, IProgressCallback callback, string neutralDialogTitle, string errorDialogTitle, string itemName, Func<string, Task<bool>> performImport) {
int total = paths.Length;
var oldStatistics = target.SnapshotStatistics();
int successful = 0;
int finished = 0;
foreach (string path in paths) {
await callback.Update(Path.GetFileName(path), finished, total);
++finished;
if (!File.Exists(path)) {
await Dialog.ShowOk(dialog, errorDialogTitle, "File '" + Path.GetFileName(path) + "' no longer exists.");
continue;
}
try {
if (await performImport(path)) {
++successful;
}
} catch (Exception ex) {
Log.Error(ex);
await Dialog.ShowOk(dialog, errorDialogTitle, "File '" + Path.GetFileName(path) + "' could not be imported: " + ex.Message);
}
}
await callback.Update("Done", finished, total);
if (successful == 0) {
await Dialog.ShowOk(dialog, neutralDialogTitle, "Nothing was imported.");
return;
}
await Dialog.ShowOk(dialog, neutralDialogTitle, GetImportDialogMessage(oldStatistics, target.SnapshotStatistics(), successful, total, itemName));
}
private static string GetImportDialogMessage(DatabaseStatisticsSnapshot oldStatistics, DatabaseStatisticsSnapshot newStatistics, int successfulItems, int totalItems, string itemName) {
long newServers = newStatistics.TotalServers - oldStatistics.TotalServers;
long newChannels = newStatistics.TotalChannels - oldStatistics.TotalChannels;
long newUsers = newStatistics.TotalUsers - oldStatistics.TotalUsers;
long newMessages = newStatistics.TotalMessages - oldStatistics.TotalMessages;
StringBuilder message = new StringBuilder();
message.Append("Processed ");
if (successfulItems == totalItems) {
message.Append(successfulItems.Pluralize(itemName));
}
else {
message.Append(successfulItems.Format()).Append(" out of ").Append(totalItems.Pluralize(itemName));
}
message.Append(" and added:\n\n \u2022 ");
message.Append(newServers.Pluralize("server")).Append("\n \u2022 ");
message.Append(newChannels.Pluralize("channel")).Append("\n \u2022 ");
message.Append(newUsers.Pluralize("user")).Append("\n \u2022 ");
message.Append(newMessages.Pluralize("message"));
return message.ToString();
}
} }

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class DebugPage : UserControl {
public DebugPage() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class DebugPage : UserControl {
} public DebugPage() {
InitializeComponent();
} }
} }

View File

@ -63,7 +63,7 @@ namespace DHT.Desktop.Main.Pages {
var server = new DHT.Server.Data.Server { var server = new DHT.Server.Data.Server {
Id = RandomId(rand), Id = RandomId(rand),
Name = RandomName("s"), Name = RandomName("s"),
Type = ServerType.Server Type = ServerType.Server,
}; };
var channels = Enumerable.Range(0, channelCount).Select(i => new Channel { var channels = Enumerable.Range(0, channelCount).Select(i => new Channel {
@ -73,14 +73,14 @@ namespace DHT.Desktop.Main.Pages {
ParentId = null, ParentId = null,
Position = i, Position = i,
Topic = RandomText(rand, 10), Topic = RandomText(rand, 10),
Nsfw = rand.Next(4) == 0 Nsfw = rand.Next(4) == 0,
}).ToArray(); }).ToArray();
var users = Enumerable.Range(0, userCount).Select(_ => new User { var users = Enumerable.Range(0, userCount).Select(_ => new User {
Id = RandomId(rand), Id = RandomId(rand),
Name = RandomName("u"), Name = RandomName("u"),
AvatarUrl = null, AvatarUrl = null,
Discriminator = rand.Next(0, 9999).ToString() Discriminator = rand.Next(0, 9999).ToString(),
}).ToArray(); }).ToArray();
db.AddServer(server); db.AddServer(server);
@ -97,7 +97,7 @@ namespace DHT.Desktop.Main.Pages {
int hourOffset = batchIndex; int hourOffset = batchIndex;
var messages = Enumerable.Range(0, Math.Min(messageCount, BatchSize)).Select(i => { var messages = Enumerable.Range(0, Math.Min(messageCount, BatchSize)).Select(i => {
DateTimeOffset time = now.AddHours(hourOffset).AddMinutes((i * 60.0) / BatchSize); DateTimeOffset time = now.AddHours(hourOffset).AddMinutes(i * 60.0 / BatchSize);
DateTimeOffset? edit = rand.Next(100) == 0 ? time.AddSeconds(rand.Next(1, 60)) : null; DateTimeOffset? edit = rand.Next(100) == 0 ? time.AddSeconds(rand.Next(1, 60)) : null;
var timeMillis = time.ToUnixTimeMilliseconds(); var timeMillis = time.ToUnixTimeMilliseconds();
@ -113,7 +113,7 @@ namespace DHT.Desktop.Main.Pages {
RepliedToId = null, RepliedToId = null,
Attachments = ImmutableArray<Attachment>.Empty, Attachments = ImmutableArray<Attachment>.Empty,
Embeds = ImmutableArray<Embed>.Empty, Embeds = ImmutableArray<Embed>.Empty,
Reactions = ImmutableArray<Reaction>.Empty Reactions = ImmutableArray<Reaction>.Empty,
}; };
}).ToArray(); }).ToArray();
@ -161,7 +161,7 @@ namespace DHT.Desktop.Main.Pages {
"vanilla", "vanilla",
"watercress", "watermelon", "watercress", "watermelon",
"yam", "yam",
"zucchini" "zucchini",
}; };
private static string RandomText(Random rand, int maxWords) { private static string RandomText(Random rand, int maxWords) {

View File

@ -3,35 +3,29 @@ using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class TrackingPage : UserControl {
private bool isCopyingScript;
public TrackingPage() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
InitializeComponent(); public sealed partial class TrackingPage : UserControl {
} private bool isCopyingScript;
private void InitializeComponent() { public TrackingPage() {
AvaloniaXamlLoader.Load(this); InitializeComponent();
} }
public async void CopyTrackingScriptButton_OnClick(object? sender, RoutedEventArgs e) { public async void CopyTrackingScriptButton_OnClick(object? sender, RoutedEventArgs e) {
if (DataContext is TrackingPageModel model) { if (DataContext is TrackingPageModel model) {
var button = this.FindControl<Button>("CopyTrackingScript"); var originalText = CopyTrackingScript.Content;
var originalText = button.Content; CopyTrackingScript.MinWidth = CopyTrackingScript.Bounds.Width;
button.MinWidth = button.Bounds.Width;
if (await model.OnClickCopyTrackingScript() && !isCopyingScript) { if (await model.OnClickCopyTrackingScript() && !isCopyingScript) {
isCopyingScript = true; isCopyingScript = true;
button.Content = "Script Copied!"; CopyTrackingScript.Content = "Script Copied!";
await Task.Delay(TimeSpan.FromSeconds(2)); await Task.Delay(TimeSpan.FromSeconds(2));
button.Content = originalText; CopyTrackingScript.Content = originalText;
isCopyingScript = false; isCopyingScript = false;
}
} }
} }
} }

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web; using System.Web;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using DHT.Desktop.Dialogs.Message; using DHT.Desktop.Dialogs.Message;
using DHT.Desktop.Discord; using DHT.Desktop.Discord;
@ -9,109 +8,109 @@ using DHT.Desktop.Server;
using DHT.Utils.Models; using DHT.Utils.Models;
using static DHT.Desktop.Program; using static DHT.Desktop.Program;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
sealed class TrackingPageModel : BaseModel {
private bool areDevToolsEnabled;
private bool AreDevToolsEnabled { sealed class TrackingPageModel : BaseModel {
get => areDevToolsEnabled; private bool areDevToolsEnabled;
set {
Change(ref areDevToolsEnabled, value); private bool AreDevToolsEnabled {
OnPropertyChanged(nameof(ToggleAppDevToolsButtonText)); get => areDevToolsEnabled;
set {
Change(ref areDevToolsEnabled, value);
OnPropertyChanged(nameof(ToggleAppDevToolsButtonText));
}
}
public bool IsToggleAppDevToolsButtonEnabled { get; private set; } = true;
public string ToggleAppDevToolsButtonText {
get {
if (!IsToggleAppDevToolsButtonEnabled) {
return "Unavailable";
} }
return AreDevToolsEnabled ? "Disable Ctrl+Shift+I" : "Enable Ctrl+Shift+I";
}
}
private readonly Window window;
[Obsolete("Designer")]
public TrackingPageModel() : this(null!) {}
public TrackingPageModel(Window window) {
this.window = window;
}
public async Task Initialize() {
bool? devToolsEnabled = await DiscordAppSettings.AreDevToolsEnabled();
if (devToolsEnabled.HasValue) {
AreDevToolsEnabled = devToolsEnabled.Value;
}
else {
IsToggleAppDevToolsButtonEnabled = false;
OnPropertyChanged(nameof(IsToggleAppDevToolsButtonEnabled));
}
}
public async Task<bool> OnClickCopyTrackingScript() {
string bootstrap = await Resources.ReadTextAsync("Tracker/bootstrap.js");
string script = bootstrap.Replace("= 0; /*[PORT]*/", "= " + ServerManager.Port + ";")
.Replace("/*[TOKEN]*/", HttpUtility.JavaScriptStringEncode(ServerManager.Token))
.Replace("/*[IMPORTS]*/", await Resources.ReadJoinedAsync("Tracker/scripts/", '\n'))
.Replace("/*[CSS-CONTROLLER]*/", await Resources.ReadTextAsync("Tracker/styles/controller.css"))
.Replace("/*[CSS-SETTINGS]*/", await Resources.ReadTextAsync("Tracker/styles/settings.css"));
var clipboard = window.Clipboard;
if (clipboard == null) {
await Dialog.ShowOk(window, "Copy Tracking Script", "Clipboard is not available on this system.");
return false;
} }
public bool IsToggleAppDevToolsButtonEnabled { get; private set; } = true; try {
await clipboard.SetTextAsync(script);
public string ToggleAppDevToolsButtonText { return true;
get { } catch {
if (!IsToggleAppDevToolsButtonEnabled) { await Dialog.ShowOk(window, "Copy Tracking Script", "An error occurred while copying to clipboard.");
return "Unavailable"; return false;
}
return AreDevToolsEnabled ? "Disable Ctrl+Shift+I" : "Enable Ctrl+Shift+I";
}
} }
}
private readonly Window window; public async void OnClickToggleAppDevTools() {
const string DialogTitle = "Discord App Settings File";
[Obsolete("Designer")] bool oldState = AreDevToolsEnabled;
public TrackingPageModel() : this(null!) {} bool newState = !oldState;
public TrackingPageModel(Window window) { switch (await DiscordAppSettings.ConfigureDevTools(newState)) {
this.window = window; case SettingsJsonResult.Success:
} AreDevToolsEnabled = newState;
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I was " + (newState ? "enabled." : "disabled.") + " Restart the Discord app for the change to take effect.");
break;
public async Task Initialize() { case SettingsJsonResult.AlreadySet:
bool? devToolsEnabled = await DiscordAppSettings.AreDevToolsEnabled(); await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I is already " + (newState ? "enabled." : "disabled."));
if (devToolsEnabled.HasValue) { AreDevToolsEnabled = newState;
AreDevToolsEnabled = devToolsEnabled.Value; break;
}
else {
IsToggleAppDevToolsButtonEnabled = false;
OnPropertyChanged(nameof(IsToggleAppDevToolsButtonEnabled));
}
}
public async Task<bool> OnClickCopyTrackingScript() { case SettingsJsonResult.FileNotFound:
string bootstrap = await Resources.ReadTextAsync("Tracker/bootstrap.js"); await Dialog.ShowOk(window, DialogTitle, "Cannot find the settings file:\n" + DiscordAppSettings.JsonFilePath);
string script = bootstrap.Replace("= 0; /*[PORT]*/", "= " + ServerManager.Port + ";") break;
.Replace("/*[TOKEN]*/", HttpUtility.JavaScriptStringEncode(ServerManager.Token))
.Replace("/*[IMPORTS]*/", await Resources.ReadJoinedAsync("Tracker/scripts/", '\n'))
.Replace("/*[CSS-CONTROLLER]*/", await Resources.ReadTextAsync("Tracker/styles/controller.css"))
.Replace("/*[CSS-SETTINGS]*/", await Resources.ReadTextAsync("Tracker/styles/settings.css"));
var clipboard = Application.Current?.Clipboard; case SettingsJsonResult.ReadError:
if (clipboard == null) { await Dialog.ShowOk(window, DialogTitle, "Cannot read the settings file:\n" + DiscordAppSettings.JsonFilePath);
await Dialog.ShowOk(window, "Copy Tracking Script", "Clipboard is not available on this system."); break;
return false;
}
try { case SettingsJsonResult.InvalidJson:
await clipboard.SetTextAsync(script); await Dialog.ShowOk(window, DialogTitle, "Unknown format of the settings file:\n" + DiscordAppSettings.JsonFilePath);
return true; break;
} catch {
await Dialog.ShowOk(window, "Copy Tracking Script", "An error occurred while copying to clipboard.");
return false;
}
}
public async void OnClickToggleAppDevTools() { case SettingsJsonResult.WriteError:
const string DialogTitle = "Discord App Settings File"; await Dialog.ShowOk(window, DialogTitle, "Cannot save the settings file:\n" + DiscordAppSettings.JsonFilePath);
break;
bool oldState = AreDevToolsEnabled; default:
bool newState = !oldState; throw new ArgumentOutOfRangeException();
switch (await DiscordAppSettings.ConfigureDevTools(newState)) {
case SettingsJsonResult.Success:
AreDevToolsEnabled = newState;
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I was " + (newState ? "enabled." : "disabled.") + " Restart the Discord app for the change to take effect.");
break;
case SettingsJsonResult.AlreadySet:
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I is already " + (newState ? "enabled." : "disabled."));
AreDevToolsEnabled = newState;
break;
case SettingsJsonResult.FileNotFound:
await Dialog.ShowOk(window, DialogTitle, "Cannot find the settings file:\n" + DiscordAppSettings.JsonFilePath);
break;
case SettingsJsonResult.ReadError:
await Dialog.ShowOk(window, DialogTitle, "Cannot read the settings file:\n" + DiscordAppSettings.JsonFilePath);
break;
case SettingsJsonResult.InvalidJson:
await Dialog.ShowOk(window, DialogTitle, "Unknown format of the settings file:\n" + DiscordAppSettings.JsonFilePath);
break;
case SettingsJsonResult.WriteError:
await Dialog.ShowOk(window, DialogTitle, "Cannot save the settings file:\n" + DiscordAppSettings.JsonFilePath);
break;
default:
throw new ArgumentOutOfRangeException();
}
} }
} }
} }

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class ViewerPage : UserControl {
public ViewerPage() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class ViewerPage : UserControl {
} public ViewerPage() {
InitializeComponent();
} }
} }

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
@ -8,7 +7,9 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web; using System.Web;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Platform.Storage;
using DHT.Desktop.Common; using DHT.Desktop.Common;
using DHT.Desktop.Dialogs.File;
using DHT.Desktop.Dialogs.Message; using DHT.Desktop.Dialogs.Message;
using DHT.Desktop.Main.Controls; using DHT.Desktop.Main.Controls;
using DHT.Desktop.Server; using DHT.Desktop.Server;
@ -19,131 +20,129 @@ using DHT.Server.Database.Export.Strategy;
using DHT.Utils.Models; using DHT.Utils.Models;
using static DHT.Desktop.Program; using static DHT.Desktop.Program;
namespace DHT.Desktop.Main.Pages { namespace DHT.Desktop.Main.Pages;
sealed class ViewerPageModel : BaseModel, IDisposable {
public static readonly ConcurrentBag<string> TemporaryFiles = new ();
public bool DatabaseToolFilterModeKeep { get; set; } = true; sealed class ViewerPageModel : BaseModel, IDisposable {
public bool DatabaseToolFilterModeRemove { get; set; } = false; public static readonly ConcurrentBag<string> TemporaryFiles = new ();
private bool hasFilters = false; public bool DatabaseToolFilterModeKeep { get; set; } = true;
public bool DatabaseToolFilterModeRemove { get; set; } = false;
public bool HasFilters { private bool hasFilters = false;
get => hasFilters;
set => Change(ref hasFilters, value);
}
private MessageFilterPanelModel FilterModel { get; } public bool HasFilters {
get => hasFilters;
set => Change(ref hasFilters, value);
}
private readonly Window window; private MessageFilterPanelModel FilterModel { get; }
private readonly IDatabaseFile db;
[Obsolete("Designer")] private readonly Window window;
public ViewerPageModel() : this(null!, DummyDatabaseFile.Instance) {} private readonly IDatabaseFile db;
public ViewerPageModel(Window window, IDatabaseFile db) { [Obsolete("Designer")]
this.window = window; public ViewerPageModel() : this(null!, DummyDatabaseFile.Instance) {}
this.db = db;
FilterModel = new MessageFilterPanelModel(window, db, "Will export"); public ViewerPageModel(Window window, IDatabaseFile db) {
FilterModel.FilterPropertyChanged += OnFilterPropertyChanged; this.window = window;
} this.db = db;
public void Dispose() { FilterModel = new MessageFilterPanelModel(window, db, "Will export");
FilterModel.Dispose(); FilterModel.FilterPropertyChanged += OnFilterPropertyChanged;
} }
private void OnFilterPropertyChanged(object? sender, PropertyChangedEventArgs e) { public void Dispose() {
HasFilters = FilterModel.HasAnyFilters; FilterModel.Dispose();
} }
private async Task WriteViewerFile(string path, IViewerExportStrategy strategy) { private void OnFilterPropertyChanged(object? sender, PropertyChangedEventArgs e) {
const string ArchiveTag = "/*[ARCHIVE]*/"; HasFilters = FilterModel.HasAnyFilters;
}
string indexFile = await Resources.ReadTextAsync("Viewer/index.html"); private async Task WriteViewerFile(string path, IViewerExportStrategy strategy) {
string viewerTemplate = indexFile.Replace("/*[JS]*/", await Resources.ReadJoinedAsync("Viewer/scripts/", '\n')) const string ArchiveTag = "/*[ARCHIVE]*/";
.Replace("/*[CSS]*/", await Resources.ReadJoinedAsync("Viewer/styles/", '\n'));
int viewerArchiveTagStart = viewerTemplate.IndexOf(ArchiveTag); string indexFile = await Resources.ReadTextAsync("Viewer/index.html");
int viewerArchiveTagEnd = viewerArchiveTagStart + ArchiveTag.Length; string viewerTemplate = indexFile.Replace("/*[JS]*/", await Resources.ReadJoinedAsync("Viewer/scripts/", '\n'))
.Replace("/*[CSS]*/", await Resources.ReadJoinedAsync("Viewer/styles/", '\n'));
string jsonTempFile = path + ".tmp"; int viewerArchiveTagStart = viewerTemplate.IndexOf(ArchiveTag);
int viewerArchiveTagEnd = viewerArchiveTagStart + ArchiveTag.Length;
await using (var jsonStream = new FileStream(jsonTempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) { string jsonTempFile = path + ".tmp";
await ViewerJsonExport.Generate(jsonStream, strategy, db, FilterModel.CreateFilter());
char[] jsonBuffer = new char[Math.Min(32768, jsonStream.Position)]; await using (var jsonStream = new FileStream(jsonTempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) {
jsonStream.Position = 0; await ViewerJsonExport.Generate(jsonStream, strategy, db, FilterModel.CreateFilter());
await using (var outputStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read)) char[] jsonBuffer = new char[Math.Min(32768, jsonStream.Position)];
await using (var outputWriter = new StreamWriter(outputStream, Encoding.UTF8)) { jsonStream.Position = 0;
await outputWriter.WriteAsync(viewerTemplate[..viewerArchiveTagStart]);
using (var jsonReader = new StreamReader(jsonStream, Encoding.UTF8)) { await using (var outputStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
int readBytes; await using (var outputWriter = new StreamWriter(outputStream, Encoding.UTF8)) {
while ((readBytes = await jsonReader.ReadAsync(jsonBuffer, 0, jsonBuffer.Length)) > 0) { await outputWriter.WriteAsync(viewerTemplate[..viewerArchiveTagStart]);
string jsonChunk = new string(jsonBuffer, 0, readBytes);
await outputWriter.WriteAsync(HttpUtility.JavaScriptStringEncode(jsonChunk));
}
}
await outputWriter.WriteAsync(viewerTemplate[viewerArchiveTagEnd..]); using (var jsonReader = new StreamReader(jsonStream, Encoding.UTF8)) {
} int readBytes;
} while ((readBytes = await jsonReader.ReadAsync(jsonBuffer, 0, jsonBuffer.Length)) > 0) {
string jsonChunk = new string(jsonBuffer, 0, readBytes);
File.Delete(jsonTempFile); await outputWriter.WriteAsync(HttpUtility.JavaScriptStringEncode(jsonChunk));
}
public async void OnClickOpenViewer() {
string rootPath = Path.Combine(Path.GetTempPath(), "DiscordHistoryTracker");
string filenameBase = Path.GetFileNameWithoutExtension(db.Path) + "-" + DateTime.Now.ToString("yyyy-MM-dd");
string fullPath = Path.Combine(rootPath, filenameBase + ".html");
int counter = 0;
while (File.Exists(fullPath)) {
++counter;
fullPath = Path.Combine(rootPath, filenameBase + "-" + counter + ".html");
}
TemporaryFiles.Add(fullPath);
Directory.CreateDirectory(rootPath);
await WriteViewerFile(fullPath, new LiveViewerExportStrategy(ServerManager.Port, ServerManager.Token));
Process.Start(new ProcessStartInfo(fullPath) { UseShellExecute = true });
}
public async void OnClickSaveViewer() {
var dialog = new SaveFileDialog {
Title = "Save Viewer",
InitialFileName = Path.GetFileNameWithoutExtension(db.Path) + ".html",
Directory = Path.GetDirectoryName(db.Path),
Filters = new List<FileDialogFilter> {
new() {
Name = "Discord History Viewer",
Extensions = { "html" }
} }
} }
}.ShowAsync(window);
string? path = await dialog; await outputWriter.WriteAsync(viewerTemplate[viewerArchiveTagEnd..]);
if (!string.IsNullOrEmpty(path)) {
await WriteViewerFile(path, StandaloneViewerExportStrategy.Instance);
} }
} }
public async void OnClickApplyFiltersToDatabase() { File.Delete(jsonTempFile);
var filter = FilterModel.CreateFilter(); }
if (DatabaseToolFilterModeKeep) { public async void OnClickOpenViewer() {
if (DialogResult.YesNo.Yes == await Dialog.ShowYesNo(window, "Keep Matching Messages in This Database", db.CountMessages(filter).Pluralize("message") + " will be kept, and the rest will be removed from this database. This action cannot be undone. Proceed?")) { string rootPath = Path.Combine(Path.GetTempPath(), "DiscordHistoryTracker");
db.RemoveMessages(filter, FilterRemovalMode.KeepMatching); string filenameBase = Path.GetFileNameWithoutExtension(db.Path) + "-" + DateTime.Now.ToString("yyyy-MM-dd");
} string fullPath = Path.Combine(rootPath, filenameBase + ".html");
int counter = 0;
while (File.Exists(fullPath)) {
++counter;
fullPath = Path.Combine(rootPath, filenameBase + "-" + counter + ".html");
}
TemporaryFiles.Add(fullPath);
Directory.CreateDirectory(rootPath);
await WriteViewerFile(fullPath, new LiveViewerExportStrategy(ServerManager.Port, ServerManager.Token));
Process.Start(new ProcessStartInfo(fullPath) { UseShellExecute = true });
}
private static readonly FilePickerFileType[] ViewerFileTypes = {
FileDialogs.CreateFilter("Discord History Viewer", new string[] { "html" }),
};
public async void OnClickSaveViewer() {
string? path = await window.StorageProvider.SaveFile(new FilePickerSaveOptions {
Title = "Save Viewer",
FileTypeChoices = ViewerFileTypes,
SuggestedFileName = Path.GetFileNameWithoutExtension(db.Path) + ".html",
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, Path.GetDirectoryName(db.Path)),
});
if (path != null) {
await WriteViewerFile(path, StandaloneViewerExportStrategy.Instance);
}
}
public async void OnClickApplyFiltersToDatabase() {
var filter = FilterModel.CreateFilter();
if (DatabaseToolFilterModeKeep) {
if (DialogResult.YesNo.Yes == await Dialog.ShowYesNo(window, "Keep Matching Messages in This Database", db.CountMessages(filter).Pluralize("message") + " will be kept, and the rest will be removed from this database. This action cannot be undone. Proceed?")) {
db.RemoveMessages(filter, FilterRemovalMode.KeepMatching);
} }
else if (DatabaseToolFilterModeRemove) { }
if (DialogResult.YesNo.Yes == await Dialog.ShowYesNo(window, "Remove Matching Messages in This Database", db.CountMessages(filter).Pluralize("message") + " will be removed from this database. This action cannot be undone. Proceed?")) { else if (DatabaseToolFilterModeRemove) {
db.RemoveMessages(filter, FilterRemovalMode.RemoveMatching); if (DialogResult.YesNo.Yes == await Dialog.ShowYesNo(window, "Remove Matching Messages in This Database", db.CountMessages(filter).Pluralize("message") + " will be removed from this database. This action cannot be undone. Proceed?")) {
} db.RemoveMessages(filter, FilterRemovalMode.RemoveMatching);
} }
} }
} }

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Screens { namespace DHT.Desktop.Main.Screens;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class MainContentScreen : UserControl {
public MainContentScreen() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class MainContentScreen : UserControl {
} public MainContentScreen() {
InitializeComponent();
} }
} }

View File

@ -9,111 +9,111 @@ using DHT.Server.Database;
using DHT.Server.Service; using DHT.Server.Service;
using DHT.Utils.Logging; using DHT.Utils.Logging;
namespace DHT.Desktop.Main.Screens { namespace DHT.Desktop.Main.Screens;
sealed class MainContentScreenModel : IDisposable {
private static readonly Log Log = Log.ForType<MainContentScreenModel>();
public DatabasePage DatabasePage { get; } sealed class MainContentScreenModel : IDisposable {
private DatabasePageModel DatabasePageModel { get; } private static readonly Log Log = Log.ForType<MainContentScreenModel>();
public TrackingPage TrackingPage { get; } public DatabasePage DatabasePage { get; }
private TrackingPageModel TrackingPageModel { get; } private DatabasePageModel DatabasePageModel { get; }
public AttachmentsPage AttachmentsPage { get; } public TrackingPage TrackingPage { get; }
private AttachmentsPageModel AttachmentsPageModel { get; } private TrackingPageModel TrackingPageModel { get; }
public ViewerPage ViewerPage { get; } public AttachmentsPage AttachmentsPage { get; }
private ViewerPageModel ViewerPageModel { get; } private AttachmentsPageModel AttachmentsPageModel { get; }
public AdvancedPage AdvancedPage { get; } public ViewerPage ViewerPage { get; }
private AdvancedPageModel AdvancedPageModel { get; } private ViewerPageModel ViewerPageModel { get; }
public DebugPage? DebugPage { get; } public AdvancedPage AdvancedPage { get; }
private AdvancedPageModel AdvancedPageModel { get; }
#if DEBUG public DebugPage? DebugPage { get; }
public bool HasDebugPage => true;
private DebugPageModel DebugPageModel { get; } #if DEBUG
#else public bool HasDebugPage => true;
private DebugPageModel DebugPageModel { get; }
#else
public bool HasDebugPage => false; public bool HasDebugPage => false;
#endif #endif
public StatusBarModel StatusBarModel { get; } public StatusBarModel StatusBarModel { get; }
public event EventHandler? DatabaseClosed { public event EventHandler? DatabaseClosed {
add { add {
DatabasePageModel.DatabaseClosed += value; DatabasePageModel.DatabaseClosed += value;
}
remove {
DatabasePageModel.DatabaseClosed -= value;
}
} }
remove {
private readonly Window window; DatabasePageModel.DatabaseClosed -= value;
private readonly ServerManager serverManager;
[Obsolete("Designer")]
public MainContentScreenModel() : this(null!, DummyDatabaseFile.Instance) {}
public MainContentScreenModel(Window window, IDatabaseFile db) {
this.window = window;
this.serverManager = new ServerManager(db);
ServerLauncher.ServerManagementExceptionCaught += ServerLauncherOnServerManagementExceptionCaught;
DatabasePageModel = new DatabasePageModel(window, db);
DatabasePage = new DatabasePage { DataContext = DatabasePageModel };
TrackingPageModel = new TrackingPageModel(window);
TrackingPage = new TrackingPage { DataContext = TrackingPageModel };
AttachmentsPageModel = new AttachmentsPageModel(db);
AttachmentsPage = new AttachmentsPage { DataContext = AttachmentsPageModel };
ViewerPageModel = new ViewerPageModel(window, db);
ViewerPage = new ViewerPage { DataContext = ViewerPageModel };
AdvancedPageModel = new AdvancedPageModel(window, db, serverManager);
AdvancedPage = new AdvancedPage { DataContext = AdvancedPageModel };
#if DEBUG
DebugPageModel = new DebugPageModel(window, db);
DebugPage = new DebugPage { DataContext = DebugPageModel };
#else
DebugPage = null;
#endif
StatusBarModel = new StatusBarModel(db.Statistics);
AdvancedPageModel.ServerConfigurationModel.ServerStatusChanged += OnServerStatusChanged;
DatabaseClosed += OnDatabaseClosed;
StatusBarModel.CurrentStatus = serverManager.IsRunning ? StatusBarModel.Status.Ready : StatusBarModel.Status.Stopped;
}
public async Task Initialize() {
await TrackingPageModel.Initialize();
AdvancedPageModel.Initialize();
serverManager.Launch();
}
public void Dispose() {
ServerLauncher.ServerManagementExceptionCaught -= ServerLauncherOnServerManagementExceptionCaught;
AttachmentsPageModel.Dispose();
ViewerPageModel.Dispose();
serverManager.Dispose();
}
private void OnServerStatusChanged(object? sender, StatusBarModel.Status e) {
StatusBarModel.CurrentStatus = e;
}
private void OnDatabaseClosed(object? sender, EventArgs e) {
serverManager.Stop();
}
private async void ServerLauncherOnServerManagementExceptionCaught(object? sender, Exception ex) {
Log.Error(ex);
await Dialog.ShowOk(window, "Internal Server Error", ex.Message);
} }
} }
private readonly Window window;
private readonly ServerManager serverManager;
[Obsolete("Designer")]
public MainContentScreenModel() : this(null!, DummyDatabaseFile.Instance) {}
public MainContentScreenModel(Window window, IDatabaseFile db) {
this.window = window;
this.serverManager = new ServerManager(db);
ServerLauncher.ServerManagementExceptionCaught += ServerLauncherOnServerManagementExceptionCaught;
DatabasePageModel = new DatabasePageModel(window, db);
DatabasePage = new DatabasePage { DataContext = DatabasePageModel };
TrackingPageModel = new TrackingPageModel(window);
TrackingPage = new TrackingPage { DataContext = TrackingPageModel };
AttachmentsPageModel = new AttachmentsPageModel(db);
AttachmentsPage = new AttachmentsPage { DataContext = AttachmentsPageModel };
ViewerPageModel = new ViewerPageModel(window, db);
ViewerPage = new ViewerPage { DataContext = ViewerPageModel };
AdvancedPageModel = new AdvancedPageModel(window, db, serverManager);
AdvancedPage = new AdvancedPage { DataContext = AdvancedPageModel };
#if DEBUG
DebugPageModel = new DebugPageModel(window, db);
DebugPage = new DebugPage { DataContext = DebugPageModel };
#else
DebugPage = null;
#endif
StatusBarModel = new StatusBarModel(db.Statistics);
AdvancedPageModel.ServerConfigurationModel.ServerStatusChanged += OnServerStatusChanged;
DatabaseClosed += OnDatabaseClosed;
StatusBarModel.CurrentStatus = serverManager.IsRunning ? StatusBarModel.Status.Ready : StatusBarModel.Status.Stopped;
}
public async Task Initialize() {
await TrackingPageModel.Initialize();
AdvancedPageModel.Initialize();
serverManager.Launch();
}
public void Dispose() {
ServerLauncher.ServerManagementExceptionCaught -= ServerLauncherOnServerManagementExceptionCaught;
AttachmentsPageModel.Dispose();
ViewerPageModel.Dispose();
serverManager.Dispose();
}
private void OnServerStatusChanged(object? sender, StatusBarModel.Status e) {
StatusBarModel.CurrentStatus = e;
}
private void OnDatabaseClosed(object? sender, EventArgs e) {
serverManager.Stop();
}
private async void ServerLauncherOnServerManagementExceptionCaught(object? sender, Exception ex) {
Log.Error(ex);
await Dialog.ShowOk(window, "Internal Server Error", ex.Message);
}
} }

View File

@ -1,16 +1,11 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace DHT.Desktop.Main.Screens { namespace DHT.Desktop.Main.Screens;
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
public sealed class WelcomeScreen : UserControl {
public WelcomeScreen() {
InitializeComponent();
}
private void InitializeComponent() { [SuppressMessage("ReSharper", "MemberCanBeInternal")]
AvaloniaXamlLoader.Load(this); public sealed partial class WelcomeScreen : UserControl {
} public WelcomeScreen() {
InitializeComponent();
} }
} }

View File

@ -7,67 +7,64 @@ using DHT.Desktop.Dialogs.Message;
using DHT.Server.Database; using DHT.Server.Database;
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Desktop.Main.Screens { namespace DHT.Desktop.Main.Screens;
sealed class WelcomeScreenModel : BaseModel, IDisposable {
public string Version => Program.Version;
public IDatabaseFile? Db { get; private set; } sealed class WelcomeScreenModel : BaseModel, IDisposable {
public bool HasDatabase => Db != null; public string Version => Program.Version;
private readonly Window window; public IDatabaseFile? Db { get; private set; }
public bool HasDatabase => Db != null;
private string? dbFilePath; private readonly Window window;
[Obsolete("Designer")] private string? dbFilePath;
public WelcomeScreenModel() : this(null!) {}
public WelcomeScreenModel(Window window) { [Obsolete("Designer")]
this.window = window; public WelcomeScreenModel() : this(null!) {}
}
public async void OpenOrCreateDatabase() { public WelcomeScreenModel(Window window) {
var dialog = DatabaseGui.NewOpenOrCreateDatabaseFileDialog(); this.window = window;
dialog.Directory = Path.GetDirectoryName(dbFilePath); }
string? path = await dialog.ShowAsync(window); public async void OpenOrCreateDatabase() {
if (!string.IsNullOrWhiteSpace(path)) { var path = await DatabaseGui.NewOpenOrCreateDatabaseFileDialog(window, Path.GetDirectoryName(dbFilePath));
await OpenOrCreateDatabaseFromPath(path); if (path != null) {
} await OpenOrCreateDatabaseFromPath(path);
}
public async Task OpenOrCreateDatabaseFromPath(string path) {
if (Db != null) {
Db = null;
}
dbFilePath = path;
Db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, window, CheckCanUpgradeDatabase);
OnPropertyChanged(nameof(Db));
OnPropertyChanged(nameof(HasDatabase));
}
private async Task<bool> CheckCanUpgradeDatabase() {
return DialogResult.YesNo.Yes == await DatabaseGui.ShowCanUpgradeDatabaseDialog(window);
}
public void CloseDatabase() {
Dispose();
OnPropertyChanged(nameof(Db));
OnPropertyChanged(nameof(HasDatabase));
}
public async void ShowAboutDialog() {
await new AboutWindow { DataContext = new AboutWindowModel() }.ShowDialog(this.window);
}
public void Exit() {
window.Close();
}
public void Dispose() {
Db?.Dispose();
Db = null;
} }
} }
public async Task OpenOrCreateDatabaseFromPath(string path) {
if (Db != null) {
Db = null;
}
dbFilePath = path;
Db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, window, CheckCanUpgradeDatabase);
OnPropertyChanged(nameof(Db));
OnPropertyChanged(nameof(HasDatabase));
}
private async Task<bool> CheckCanUpgradeDatabase() {
return DialogResult.YesNo.Yes == await DatabaseGui.ShowCanUpgradeDatabaseDialog(window);
}
public void CloseDatabase() {
Dispose();
OnPropertyChanged(nameof(Db));
OnPropertyChanged(nameof(HasDatabase));
}
public async void ShowAboutDialog() {
await new AboutWindow { DataContext = new AboutWindowModel() }.ShowDialog(this.window);
}
public void Exit() {
window.Close();
}
public void Dispose() {
Db?.Dispose();
Db = null;
}
} }

View File

@ -3,37 +3,38 @@ using System.Reflection;
using Avalonia; using Avalonia;
using DHT.Utils.Resources; using DHT.Utils.Resources;
namespace DHT.Desktop { namespace DHT.Desktop;
static class Program {
public static string Version { get; }
public static CultureInfo Culture { get; }
public static ResourceLoader Resources { get; }
static Program() { static class Program {
var assembly = Assembly.GetExecutingAssembly(); public static string Version { get; }
public static CultureInfo Culture { get; }
public static ResourceLoader Resources { get; }
Version = assembly.GetName().Version?.ToString() ?? ""; static Program() {
while (Version.EndsWith(".0")) { var assembly = Assembly.GetExecutingAssembly();
Version = Version[..^2];
}
Culture = CultureInfo.CurrentCulture; Version = assembly.GetName().Version?.ToString() ?? "";
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; while (Version.EndsWith(".0")) {
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture; Version = Version[..^2];
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
Resources = new ResourceLoader(assembly);
} }
public static void Main(string[] args) { Culture = CultureInfo.CurrentCulture;
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
} CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
private static AppBuilder BuildAvaloniaApp() { Resources = new ResourceLoader(assembly);
return AppBuilder.Configure<App>() }
.UsePlatformDetect()
.LogToTrace(); public static void Main(string[] args) {
} BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
private static AppBuilder BuildAvaloniaApp() {
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
} }
} }

View File

@ -2,49 +2,49 @@ using System;
using DHT.Server.Database; using DHT.Server.Database;
using DHT.Server.Service; using DHT.Server.Service;
namespace DHT.Desktop.Server { namespace DHT.Desktop.Server;
sealed class ServerManager : IDisposable {
public static ushort Port { get; set; } = ServerUtils.FindAvailablePort(50000, 60000);
public static string Token { get; set; } = ServerUtils.GenerateRandomToken(20);
private static ServerManager? instance; sealed class ServerManager : IDisposable {
public static ushort Port { get; set; } = ServerUtils.FindAvailablePort(50000, 60000);
public static string Token { get; set; } = ServerUtils.GenerateRandomToken(20);
public bool IsRunning => ServerLauncher.IsRunning; private static ServerManager? instance;
private readonly IDatabaseFile db; public bool IsRunning => ServerLauncher.IsRunning;
public ServerManager(IDatabaseFile db) { private readonly IDatabaseFile db;
if (db != DummyDatabaseFile.Instance) {
if (instance != null) {
throw new InvalidOperationException("Only one instance of ServerManager can exist at the same time!");
}
instance = this; public ServerManager(IDatabaseFile db) {
if (db != DummyDatabaseFile.Instance) {
if (instance != null) {
throw new InvalidOperationException("Only one instance of ServerManager can exist at the same time!");
} }
this.db = db; instance = this;
} }
public void Launch() { this.db = db;
ServerLauncher.Relaunch(Port, Token, db); }
}
public void Relaunch(ushort port, string token) { public void Launch() {
Port = port; ServerLauncher.Relaunch(Port, Token, db);
Token = token; }
Launch();
}
public void Stop() { public void Relaunch(ushort port, string token) {
ServerLauncher.Stop(); Port = port;
} Token = token;
Launch();
}
public void Dispose() { public void Stop() {
Stop(); ServerLauncher.Stop();
}
if (instance == this) { public void Dispose() {
instance = null; Stop();
}
if (instance == this) {
instance = null;
} }
} }
} }

View File

@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>11</LangVersion>
</PropertyGroup>
</Project>

View File

@ -1,15 +1,15 @@
namespace DHT.Server.Data.Aggregations { namespace DHT.Server.Data.Aggregations;
public sealed class DownloadStatusStatistics {
public int EnqueuedCount { get; internal set; }
public ulong EnqueuedSize { get; internal set; }
public int SuccessfulCount { get; internal set; } public sealed class DownloadStatusStatistics {
public ulong SuccessfulSize { get; internal set; } public int EnqueuedCount { get; internal set; }
public ulong EnqueuedSize { get; internal set; }
public int FailedCount { get; internal set; } public int SuccessfulCount { get; internal set; }
public ulong FailedSize { get; internal set; } public ulong SuccessfulSize { get; internal set; }
public int SkippedCount { get; internal set; } public int FailedCount { get; internal set; }
public ulong SkippedSize { get; internal set; } public ulong FailedSize { get; internal set; }
}
public int SkippedCount { get; internal set; }
public ulong SkippedSize { get; internal set; }
} }

View File

@ -1,11 +1,11 @@
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct Attachment {
public ulong Id { get; internal init; } public readonly struct Attachment {
public string Name { get; internal init; } public ulong Id { get; internal init; }
public string? Type { get; internal init; } public string Name { get; internal init; }
public string Url { get; internal init; } public string? Type { get; internal init; }
public ulong Size { get; internal init; } public string Url { get; internal init; }
public int? Width { get; internal init; } public ulong Size { get; internal init; }
public int? Height { get; internal init; } public int? Width { get; internal init; }
} public int? Height { get; internal init; }
} }

View File

@ -1,11 +1,11 @@
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct Channel {
public ulong Id { get; init; } public readonly struct Channel {
public ulong Server { get; init; } public ulong Id { get; init; }
public string Name { get; init; } public ulong Server { get; init; }
public ulong? ParentId { get; init; } public string Name { get; init; }
public int? Position { get; init; } public ulong? ParentId { get; init; }
public string? Topic { get; init; } public int? Position { get; init; }
public bool? Nsfw { get; init; } public string? Topic { get; init; }
} public bool? Nsfw { get; init; }
} }

View File

@ -1,30 +1,30 @@
using System; using System;
using System.Net; using System.Net;
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct Download {
internal static Download NewSuccess(string url, byte[] data) {
return new Download(url, DownloadStatus.Success, (ulong) Math.Max(data.LongLength, 0), data);
}
internal static Download NewFailure(string url, HttpStatusCode? statusCode, ulong size) { public readonly struct Download {
return new Download(url, statusCode.HasValue ? (DownloadStatus) (int) statusCode : DownloadStatus.GenericError, size); internal static Download NewSuccess(string url, byte[] data) {
} return new Download(url, DownloadStatus.Success, (ulong) Math.Max(data.LongLength, 0), data);
}
public string Url { get; } internal static Download NewFailure(string url, HttpStatusCode? statusCode, ulong size) {
public DownloadStatus Status { get; } return new Download(url, statusCode.HasValue ? (DownloadStatus) (int) statusCode : DownloadStatus.GenericError, size);
public ulong Size { get; } }
public byte[]? Data { get; }
internal Download(string url, DownloadStatus status, ulong size, byte[]? data = null) { public string Url { get; }
Url = url; public DownloadStatus Status { get; }
Status = status; public ulong Size { get; }
Size = size; public byte[]? Data { get; }
Data = data;
}
internal Download WithData(byte[] data) { internal Download(string url, DownloadStatus status, ulong size, byte[]? data = null) {
return new Download(Url, Status, Size, data); Url = url;
} Status = status;
Size = size;
Data = data;
}
internal Download WithData(byte[] data) {
return new Download(Url, Status, Size, data);
} }
} }

View File

@ -1,12 +1,12 @@
using System.Net; using System.Net;
namespace DHT.Server.Data { namespace DHT.Server.Data;
/// <summary>
/// Extends <see cref="HttpStatusCode"/> with custom status codes in the range 0-99. /// <summary>
/// </summary> /// Extends <see cref="HttpStatusCode"/> with custom status codes in the range 0-99.
public enum DownloadStatus { /// </summary>
Enqueued = 0, public enum DownloadStatus {
GenericError = 1, Enqueued = 0,
Success = HttpStatusCode.OK GenericError = 1,
} Success = HttpStatusCode.OK
} }

View File

@ -1,6 +1,6 @@
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct DownloadedAttachment {
public string? Type { get; internal init; } public readonly struct DownloadedAttachment {
public byte[] Data { get; internal init; } public string? Type { get; internal init; }
} public byte[] Data { get; internal init; }
} }

View File

@ -1,5 +1,5 @@
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct Embed {
public string Json { get; internal init; } public readonly struct Embed {
} public string Json { get; internal init; }
} }

View File

@ -1,9 +1,9 @@
using System; using System;
namespace DHT.Server.Data { namespace DHT.Server.Data;
[Flags]
public enum EmojiFlags : ushort { [Flags]
None = 0, public enum EmojiFlags : ushort {
Animated = 0b1 None = 0,
} Animated = 0b1
} }

View File

@ -1,15 +1,15 @@
namespace DHT.Server.Data.Filters { namespace DHT.Server.Data.Filters;
public sealed class AttachmentFilter {
public ulong? MaxBytes { get; set; } = null;
public DownloadItemRules? DownloadItemRule { get; set; } = null; public sealed class AttachmentFilter {
public ulong? MaxBytes { get; set; } = null;
public bool IsEmpty => MaxBytes == null && public DownloadItemRules? DownloadItemRule { get; set; } = null;
DownloadItemRule == null;
public enum DownloadItemRules { public bool IsEmpty => MaxBytes == null &&
OnlyNotPresent, DownloadItemRule == null;
OnlyPresent
} public enum DownloadItemRules {
OnlyNotPresent,
OnlyPresent
} }
} }

View File

@ -1,10 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace DHT.Server.Data.Filters { namespace DHT.Server.Data.Filters;
public sealed class DownloadItemFilter {
public HashSet<DownloadStatus>? IncludeStatuses { get; set; } = null;
public HashSet<DownloadStatus>? ExcludeStatuses { get; set; } = null;
public bool IsEmpty => IncludeStatuses == null && ExcludeStatuses == null; public sealed class DownloadItemFilter {
} public HashSet<DownloadStatus>? IncludeStatuses { get; init; } = null;
public HashSet<DownloadStatus>? ExcludeStatuses { get; init; } = null;
public bool IsEmpty => IncludeStatuses == null && ExcludeStatuses == null;
} }

View File

@ -1,6 +1,6 @@
namespace DHT.Server.Data.Filters { namespace DHT.Server.Data.Filters;
public enum FilterRemovalMode {
KeepMatching, public enum FilterRemovalMode {
RemoveMatching KeepMatching,
} RemoveMatching
} }

View File

@ -1,19 +1,19 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace DHT.Server.Data.Filters { namespace DHT.Server.Data.Filters;
public sealed class MessageFilter {
public DateTime? StartDate { get; set; } = null;
public DateTime? EndDate { get; set; } = null;
public HashSet<ulong>? ChannelIds { get; set; } = null; public sealed class MessageFilter {
public HashSet<ulong>? UserIds { get; set; } = null; public DateTime? StartDate { get; set; } = null;
public HashSet<ulong>? MessageIds { get; set; } = null; public DateTime? EndDate { get; set; } = null;
public bool IsEmpty => StartDate == null && public HashSet<ulong>? ChannelIds { get; set; } = null;
EndDate == null && public HashSet<ulong>? UserIds { get; set; } = null;
ChannelIds == null && public HashSet<ulong>? MessageIds { get; set; } = null;
UserIds == null &&
MessageIds == null; public bool IsEmpty => StartDate == null &&
} EndDate == null &&
ChannelIds == null &&
UserIds == null &&
MessageIds == null;
} }

View File

@ -1,16 +1,16 @@
using System.Collections.Immutable; using System.Collections.Immutable;
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct Message {
public ulong Id { get; init; } public readonly struct Message {
public ulong Sender { get; init; } public ulong Id { get; init; }
public ulong Channel { get; init; } public ulong Sender { get; init; }
public string Text { get; init; } public ulong Channel { get; init; }
public long Timestamp { get; init; } public string Text { get; init; }
public long? EditTimestamp { get; init; } public long Timestamp { get; init; }
public ulong? RepliedToId { get; init; } public long? EditTimestamp { get; init; }
public ImmutableArray<Attachment> Attachments { get; init; } public ulong? RepliedToId { get; init; }
public ImmutableArray<Embed> Embeds { get; init; } public ImmutableArray<Attachment> Attachments { get; init; }
public ImmutableArray<Reaction> Reactions { get; init; } public ImmutableArray<Embed> Embeds { get; init; }
} public ImmutableArray<Reaction> Reactions { get; init; }
} }

View File

@ -1,8 +1,8 @@
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct Reaction {
public ulong? EmojiId { get; internal init; } public readonly struct Reaction {
public string? EmojiName { get; internal init; } public ulong? EmojiId { get; internal init; }
public EmojiFlags EmojiFlags { get; internal init; } public string? EmojiName { get; internal init; }
public int Count { get; internal init; } public EmojiFlags EmojiFlags { get; internal init; }
} public int Count { get; internal init; }
} }

View File

@ -1,7 +1,7 @@
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct Server {
public ulong Id { get; init; } public readonly struct Server {
public string Name { get; init; } public ulong Id { get; init; }
public ServerType? Type { get; init; } public string Name { get; init; }
} public ServerType? Type { get; init; }
} }

View File

@ -1,45 +1,45 @@
namespace DHT.Server.Data { namespace DHT.Server.Data;
public enum ServerType {
Server, public enum ServerType {
Group, Server,
DirectMessage Group,
DirectMessage
}
public static class ServerTypes {
public static ServerType? FromString(string? str) {
return str switch {
"SERVER" => ServerType.Server,
"GROUP" => ServerType.Group,
"DM" => ServerType.DirectMessage,
_ => null
};
} }
public static class ServerTypes { public static string ToString(ServerType? type) {
public static ServerType? FromString(string? str) { return type switch {
return str switch { ServerType.Server => "SERVER",
"SERVER" => ServerType.Server, ServerType.Group => "GROUP",
"GROUP" => ServerType.Group, ServerType.DirectMessage => "DM",
"DM" => ServerType.DirectMessage, _ => "UNKNOWN"
_ => null };
}; }
}
public static string ToString(ServerType? type) { public static string ToNiceString(ServerType? type) {
return type switch { return type switch {
ServerType.Server => "SERVER", ServerType.Server => "Server",
ServerType.Group => "GROUP", ServerType.Group => "Group",
ServerType.DirectMessage => "DM", ServerType.DirectMessage => "DM",
_ => "UNKNOWN" _ => "Unknown"
}; };
} }
public static string ToNiceString(ServerType? type) { internal static string ToJsonViewerString(ServerType? type) {
return type switch { return type switch {
ServerType.Server => "Server", ServerType.Server => "server",
ServerType.Group => "Group", ServerType.Group => "group",
ServerType.DirectMessage => "DM", ServerType.DirectMessage => "user",
_ => "Unknown" _ => "unknown"
}; };
}
internal static string ToJsonViewerString(ServerType? type) {
return type switch {
ServerType.Server => "server",
ServerType.Group => "group",
ServerType.DirectMessage => "user",
_ => "unknown"
};
}
} }
} }

View File

@ -1,8 +1,8 @@
namespace DHT.Server.Data { namespace DHT.Server.Data;
public readonly struct User {
public ulong Id { get; init; } public readonly struct User {
public string Name { get; init; } public ulong Id { get; init; }
public string? AvatarUrl { get; init; } public string Name { get; init; }
public string? Discriminator { get; init; } public string? AvatarUrl { get; init; }
} public string? Discriminator { get; init; }
} }

View File

@ -1,29 +1,29 @@
using System.Collections.Generic; using System.Collections.Generic;
using DHT.Server.Data; using DHT.Server.Data;
namespace DHT.Server.Database { namespace DHT.Server.Database;
public static class DatabaseExtensions {
public static void AddFrom(this IDatabaseFile target, IDatabaseFile source) {
target.AddServers(source.GetAllServers());
target.AddChannels(source.GetAllChannels());
target.AddUsers(source.GetAllUsers().ToArray());
target.AddMessages(source.GetMessages().ToArray());
foreach (var download in source.GetDownloadsWithoutData()) { public static class DatabaseExtensions {
target.AddDownload(download.Status == DownloadStatus.Success ? source.GetDownloadWithData(download) : download); public static void AddFrom(this IDatabaseFile target, IDatabaseFile source) {
} target.AddServers(source.GetAllServers());
target.AddChannels(source.GetAllChannels());
target.AddUsers(source.GetAllUsers().ToArray());
target.AddMessages(source.GetMessages().ToArray());
foreach (var download in source.GetDownloadsWithoutData()) {
target.AddDownload(download.Status == DownloadStatus.Success ? source.GetDownloadWithData(download) : download);
} }
}
internal static void AddServers(this IDatabaseFile target, IEnumerable<Data.Server> servers) { internal static void AddServers(this IDatabaseFile target, IEnumerable<Data.Server> servers) {
foreach (var server in servers) { foreach (var server in servers) {
target.AddServer(server); target.AddServer(server);
}
} }
}
internal static void AddChannels(this IDatabaseFile target, IEnumerable<Channel> channels) { internal static void AddChannels(this IDatabaseFile target, IEnumerable<Channel> channels) {
foreach (var channel in channels) { foreach (var channel in channels) {
target.AddChannel(channel); target.AddChannel(channel);
}
} }
} }
} }

View File

@ -1,46 +1,46 @@
using DHT.Utils.Models; using DHT.Utils.Models;
namespace DHT.Server.Database { namespace DHT.Server.Database;
/// <summary>
/// A live view of database statistics.
/// Some of the totals are computed asynchronously and may not reflect the most recent version of the database, or may not be available at all until computed for the first time.
/// </summary>
public sealed class DatabaseStatistics : BaseModel {
private long totalServers;
private long totalChannels;
private long totalUsers;
private long? totalMessages;
private long? totalAttachments;
private long? totalDownloads;
public long TotalServers { /// <summary>
get => totalServers; /// A live view of database statistics.
internal set => Change(ref totalServers, value); /// Some of the totals are computed asynchronously and may not reflect the most recent version of the database, or may not be available at all until computed for the first time.
} /// </summary>
public sealed class DatabaseStatistics : BaseModel {
private long totalServers;
private long totalChannels;
private long totalUsers;
private long? totalMessages;
private long? totalAttachments;
private long? totalDownloads;
public long TotalChannels { public long TotalServers {
get => totalChannels; get => totalServers;
internal set => Change(ref totalChannels, value); internal set => Change(ref totalServers, value);
} }
public long TotalUsers { public long TotalChannels {
get => totalUsers; get => totalChannels;
internal set => Change(ref totalUsers, value); internal set => Change(ref totalChannels, value);
} }
public long? TotalMessages { public long TotalUsers {
get => totalMessages; get => totalUsers;
internal set => Change(ref totalMessages, value); internal set => Change(ref totalUsers, value);
} }
public long? TotalAttachments { public long? TotalMessages {
get => totalAttachments; get => totalMessages;
internal set => Change(ref totalAttachments, value); internal set => Change(ref totalMessages, value);
} }
public long? TotalDownloads { public long? TotalAttachments {
get => totalDownloads; get => totalAttachments;
internal set => Change(ref totalDownloads, value); internal set => Change(ref totalAttachments, value);
} }
public long? TotalDownloads {
get => totalDownloads;
internal set => Change(ref totalDownloads, value);
} }
} }

View File

@ -1,11 +1,11 @@
namespace DHT.Server.Database { namespace DHT.Server.Database;
/// <summary>
/// A complete snapshot of database statistics at a particular point in time. /// <summary>
/// </summary> /// A complete snapshot of database statistics at a particular point in time.
public readonly struct DatabaseStatisticsSnapshot { /// </summary>
public long TotalServers { get; internal init; } public readonly struct DatabaseStatisticsSnapshot {
public long TotalChannels { get; internal init; } public long TotalServers { get; internal init; }
public long TotalUsers { get; internal init; } public long TotalChannels { get; internal init; }
public long TotalMessages { get; internal init; } public long TotalUsers { get; internal init; }
} public long TotalMessages { get; internal init; }
} }

View File

@ -1,88 +1,90 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using DHT.Server.Data; using DHT.Server.Data;
using DHT.Server.Data.Aggregations; using DHT.Server.Data.Aggregations;
using DHT.Server.Data.Filters; using DHT.Server.Data.Filters;
using DHT.Server.Download; using DHT.Server.Download;
namespace DHT.Server.Database { namespace DHT.Server.Database;
public sealed class DummyDatabaseFile : IDatabaseFile {
public static DummyDatabaseFile Instance { get; } = new();
public string Path => ""; [SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeNotEvident")]
public DatabaseStatistics Statistics { get; } = new(); public sealed class DummyDatabaseFile : IDatabaseFile {
public static DummyDatabaseFile Instance { get; } = new();
private DummyDatabaseFile() {} public string Path => "";
public DatabaseStatistics Statistics { get; } = new();
public DatabaseStatisticsSnapshot SnapshotStatistics() { private DummyDatabaseFile() {}
return new();
}
public void AddServer(Data.Server server) {} public DatabaseStatisticsSnapshot SnapshotStatistics() {
return new();
public List<Data.Server> GetAllServers() {
return new();
}
public void AddChannel(Channel channel) {}
public List<Channel> GetAllChannels() {
return new();
}
public void AddUsers(User[] users) {}
public List<User> GetAllUsers() {
return new();
}
public void AddMessages(Message[] messages) {}
public int CountMessages(MessageFilter? filter = null) {
return 0;
}
public List<Message> GetMessages(MessageFilter? filter = null) {
return new();
}
public HashSet<ulong> GetMessageIds(MessageFilter? filter = null) {
return new();
}
public void RemoveMessages(MessageFilter filter, FilterRemovalMode mode) {}
public int CountAttachments(AttachmentFilter? filter = null) {
return new();
}
public List<Data.Download> GetDownloadsWithoutData() {
return new();
}
public Data.Download GetDownloadWithData(Data.Download download) {
return download;
}
public DownloadedAttachment? GetDownloadedAttachment(string url) {
return null;
}
public void AddDownload(Data.Download download) {}
public void EnqueueDownloadItems(AttachmentFilter? filter = null) {}
public List<DownloadItem> GetEnqueuedDownloadItems(int count) {
return new();
}
public void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode) {}
public DownloadStatusStatistics GetDownloadStatusStatistics() {
return new();
}
public void Vacuum() {}
public void Dispose() {}
} }
public void AddServer(Data.Server server) {}
public List<Data.Server> GetAllServers() {
return new();
}
public void AddChannel(Channel channel) {}
public List<Channel> GetAllChannels() {
return new();
}
public void AddUsers(User[] users) {}
public List<User> GetAllUsers() {
return new();
}
public void AddMessages(Message[] messages) {}
public int CountMessages(MessageFilter? filter = null) {
return 0;
}
public List<Message> GetMessages(MessageFilter? filter = null) {
return new();
}
public HashSet<ulong> GetMessageIds(MessageFilter? filter = null) {
return new();
}
public void RemoveMessages(MessageFilter filter, FilterRemovalMode mode) {}
public int CountAttachments(AttachmentFilter? filter = null) {
return new();
}
public List<Data.Download> GetDownloadsWithoutData() {
return new();
}
public Data.Download GetDownloadWithData(Data.Download download) {
return download;
}
public DownloadedAttachment? GetDownloadedAttachment(string url) {
return null;
}
public void AddDownload(Data.Download download) {}
public void EnqueueDownloadItems(AttachmentFilter? filter = null) {}
public List<DownloadItem> GetEnqueuedDownloadItems(int count) {
return new();
}
public void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode) {}
public DownloadStatusStatistics GetDownloadStatusStatistics() {
return new();
}
public void Vacuum() {}
public void Dispose() {}
} }

View File

@ -1,13 +1,13 @@
using System; using System;
using DHT.Server.Database.Sqlite; using DHT.Server.Database.Sqlite;
namespace DHT.Server.Database.Exceptions { namespace DHT.Server.Database.Exceptions;
public sealed class DatabaseTooNewException : Exception {
public int DatabaseVersion { get; }
public int CurrentVersion => Schema.Version;
internal DatabaseTooNewException(int databaseVersion) : base("Database is too new: " + databaseVersion + " > " + Schema.Version) { public sealed class DatabaseTooNewException : Exception {
this.DatabaseVersion = databaseVersion; public int DatabaseVersion { get; }
} public int CurrentVersion => Schema.Version;
internal DatabaseTooNewException(int databaseVersion) : base("Database is too new: " + databaseVersion + " > " + Schema.Version) {
this.DatabaseVersion = databaseVersion;
} }
} }

View File

@ -1,11 +1,11 @@
using System; using System;
namespace DHT.Server.Database.Exceptions { namespace DHT.Server.Database.Exceptions;
public sealed class InvalidDatabaseVersionException : Exception {
public string Version { get; }
internal InvalidDatabaseVersionException(string version) : base("Invalid database version: " + version) { public sealed class InvalidDatabaseVersionException : Exception {
this.Version = version; public string Version { get; }
}
internal InvalidDatabaseVersionException(string version) : base("Invalid database version: " + version) {
this.Version = version;
} }
} }

View File

@ -1,7 +1,7 @@
using DHT.Server.Data; using DHT.Server.Data;
namespace DHT.Server.Database.Export.Strategy { namespace DHT.Server.Database.Export.Strategy;
public interface IViewerExportStrategy {
string GetAttachmentUrl(Attachment attachment); public interface IViewerExportStrategy {
} string GetAttachmentUrl(Attachment attachment);
} }

View File

@ -1,18 +1,18 @@
using System.Net; using System.Net;
using DHT.Server.Data; using DHT.Server.Data;
namespace DHT.Server.Database.Export.Strategy { namespace DHT.Server.Database.Export.Strategy;
public sealed class LiveViewerExportStrategy : IViewerExportStrategy {
private readonly string safePort;
private readonly string safeToken;
public LiveViewerExportStrategy(ushort port, string token) { public sealed class LiveViewerExportStrategy : IViewerExportStrategy {
this.safePort = port.ToString(); private readonly string safePort;
this.safeToken = WebUtility.UrlEncode(token); private readonly string safeToken;
}
public string GetAttachmentUrl(Attachment attachment) { public LiveViewerExportStrategy(ushort port, string token) {
return "http://127.0.0.1:" + safePort + "/get-attachment/" + WebUtility.UrlEncode(attachment.Url) + "?token=" + safeToken; this.safePort = port.ToString();
} this.safeToken = WebUtility.UrlEncode(token);
}
public string GetAttachmentUrl(Attachment attachment) {
return "http://127.0.0.1:" + safePort + "/get-attachment/" + WebUtility.UrlEncode(attachment.Url) + "?token=" + safeToken;
} }
} }

View File

@ -1,13 +1,13 @@
using DHT.Server.Data; using DHT.Server.Data;
namespace DHT.Server.Database.Export.Strategy { namespace DHT.Server.Database.Export.Strategy;
public sealed class StandaloneViewerExportStrategy : IViewerExportStrategy {
public static StandaloneViewerExportStrategy Instance { get; } = new ();
private StandaloneViewerExportStrategy() {} public sealed class StandaloneViewerExportStrategy : IViewerExportStrategy {
public static StandaloneViewerExportStrategy Instance { get; } = new ();
public string GetAttachmentUrl(Attachment attachment) { private StandaloneViewerExportStrategy() {}
return attachment.Url;
} public string GetAttachmentUrl(Attachment attachment) {
return attachment.Url;
} }
} }

View File

@ -9,207 +9,207 @@ using DHT.Server.Data.Filters;
using DHT.Server.Database.Export.Strategy; using DHT.Server.Database.Export.Strategy;
using DHT.Utils.Logging; using DHT.Utils.Logging;
namespace DHT.Server.Database.Export { namespace DHT.Server.Database.Export;
public static class ViewerJsonExport {
private static readonly Log Log = Log.ForType(typeof(ViewerJsonExport));
public static async Task Generate(Stream stream, IViewerExportStrategy strategy, IDatabaseFile db, MessageFilter? filter = null) { public static class ViewerJsonExport {
var perf = Log.Start(); private static readonly Log Log = Log.ForType(typeof(ViewerJsonExport));
var includedUserIds = new HashSet<ulong>(); public static async Task Generate(Stream stream, IViewerExportStrategy strategy, IDatabaseFile db, MessageFilter? filter = null) {
var includedChannelIds = new HashSet<ulong>(); var perf = Log.Start();
var includedServerIds = new HashSet<ulong>();
var includedMessages = db.GetMessages(filter); var includedUserIds = new HashSet<ulong>();
var includedChannels = new List<Channel>(); var includedChannelIds = new HashSet<ulong>();
var includedServerIds = new HashSet<ulong>();
foreach (var message in includedMessages) { var includedMessages = db.GetMessages(filter);
includedUserIds.Add(message.Sender); var includedChannels = new List<Channel>();
includedChannelIds.Add(message.Channel);
foreach (var message in includedMessages) {
includedUserIds.Add(message.Sender);
includedChannelIds.Add(message.Channel);
}
foreach (var channel in db.GetAllChannels()) {
if (includedChannelIds.Contains(channel.Id)) {
includedChannels.Add(channel);
includedServerIds.Add(channel.Server);
}
}
var users = GenerateUserList(db, includedUserIds, out var userindex, out var userIndices);
var servers = GenerateServerList(db, includedServerIds, out var serverindex);
var channels = GenerateChannelList(includedChannels, serverindex);
perf.Step("Collect database data");
var value = new {
meta = new { users, userindex, servers, channels },
data = GenerateMessageList(includedMessages, userIndices, strategy),
};
perf.Step("Generate value object");
var opts = new JsonSerializerOptions();
opts.Converters.Add(new ViewerJsonSnowflakeSerializer());
await JsonSerializer.SerializeAsync(stream, value, opts);
perf.Step("Serialize to JSON");
perf.End();
}
private static Dictionary<string, object> GenerateUserList(IDatabaseFile db, HashSet<ulong> userIds, out List<string> userindex, out Dictionary<ulong, object> userIndices) {
var users = new Dictionary<string, object>();
userindex = new List<string>();
userIndices = new Dictionary<ulong, object>();
foreach (var user in db.GetAllUsers()) {
var id = user.Id;
if (!userIds.Contains(id)) {
continue;
} }
foreach (var channel in db.GetAllChannels()) { var obj = new Dictionary<string, object> {
if (includedChannelIds.Contains(channel.Id)) { ["name"] = user.Name
includedChannels.Add(channel);
includedServerIds.Add(channel.Server);
}
}
var users = GenerateUserList(db, includedUserIds, out var userindex, out var userIndices);
var servers = GenerateServerList(db, includedServerIds, out var serverindex);
var channels = GenerateChannelList(includedChannels, serverindex);
perf.Step("Collect database data");
var value = new {
meta = new { users, userindex, servers, channels },
data = GenerateMessageList(includedMessages, userIndices, strategy)
}; };
perf.Step("Generate value object"); if (user.AvatarUrl != null) {
obj["avatar"] = user.AvatarUrl;
}
var opts = new JsonSerializerOptions(); if (user.Discriminator != null) {
opts.Converters.Add(new ViewerJsonSnowflakeSerializer()); obj["tag"] = user.Discriminator;
}
await JsonSerializer.SerializeAsync(stream, value, opts); var idStr = id.ToString();
userIndices[id] = users.Count;
perf.Step("Serialize to JSON"); userindex.Add(idStr);
perf.End(); users[idStr] = obj;
} }
private static object GenerateUserList(IDatabaseFile db, HashSet<ulong> userIds, out List<string> userindex, out Dictionary<ulong, object> userIndices) { return users;
var users = new Dictionary<string, object>(); }
userindex = new List<string>();
userIndices = new Dictionary<ulong, object>();
foreach (var user in db.GetAllUsers()) { private static List<object> GenerateServerList(IDatabaseFile db, HashSet<ulong> serverIds, out Dictionary<ulong, object> serverIndices) {
var id = user.Id; var servers = new List<object>();
if (!userIds.Contains(id)) { serverIndices = new Dictionary<ulong, object>();
continue;
}
foreach (var server in db.GetAllServers()) {
var id = server.Id;
if (!serverIds.Contains(id)) {
continue;
}
serverIndices[id] = servers.Count;
servers.Add(new Dictionary<string, object> {
["name"] = server.Name,
["type"] = ServerTypes.ToJsonViewerString(server.Type),
});
}
return servers;
}
private static Dictionary<string, object> GenerateChannelList(List<Channel> includedChannels, Dictionary<ulong, object> serverIndices) {
var channels = new Dictionary<string, object>();
foreach (var channel in includedChannels) {
var obj = new Dictionary<string, object> {
["server"] = serverIndices[channel.Server],
["name"] = channel.Name,
};
if (channel.ParentId != null) {
obj["parent"] = channel.ParentId;
}
if (channel.Position != null) {
obj["position"] = channel.Position;
}
if (channel.Topic != null) {
obj["topic"] = channel.Topic;
}
if (channel.Nsfw != null) {
obj["nsfw"] = channel.Nsfw;
}
channels[channel.Id.ToString()] = obj;
}
return channels;
}
private static Dictionary<string, Dictionary<string, object>> GenerateMessageList( List<Message> includedMessages, Dictionary<ulong, object> userIndices, IViewerExportStrategy strategy) {
var data = new Dictionary<string, Dictionary<string, object>>();
foreach (var grouping in includedMessages.GroupBy(static message => message.Channel)) {
var channel = grouping.Key.ToString();
var channelData = new Dictionary<string, object>();
foreach (var message in grouping) {
var obj = new Dictionary<string, object> { var obj = new Dictionary<string, object> {
["name"] = user.Name ["u"] = userIndices[message.Sender],
["t"] = message.Timestamp,
}; };
if (user.AvatarUrl != null) { if (!string.IsNullOrEmpty(message.Text)) {
obj["avatar"] = user.AvatarUrl; obj["m"] = message.Text;
} }
if (user.Discriminator != null) { if (message.EditTimestamp != null) {
obj["tag"] = user.Discriminator; obj["te"] = message.EditTimestamp;
} }
var idStr = id.ToString(); if (message.RepliedToId != null) {
userIndices[id] = users.Count; obj["r"] = message.RepliedToId.Value;
userindex.Add(idStr); }
users[idStr] = obj;
if (!message.Attachments.IsEmpty) {
obj["a"] = message.Attachments.Select(attachment => {
var a = new Dictionary<string, object> {
{ "url", strategy.GetAttachmentUrl(attachment) },
{ "name", Uri.TryCreate(attachment.Url, UriKind.Absolute, out var uri) ? Path.GetFileName(uri.LocalPath) : attachment.Url },
};
if (attachment is { Width: not null, Height: not null }) {
a["width"] = attachment.Width;
a["height"] = attachment.Height;
}
return a;
}).ToArray();
}
if (!message.Embeds.IsEmpty) {
obj["e"] = message.Embeds.Select(static embed => embed.Json).ToArray();
}
if (!message.Reactions.IsEmpty) {
obj["re"] = message.Reactions.Select(static reaction => {
var r = new Dictionary<string, object>();
if (reaction.EmojiId != null) {
r["id"] = reaction.EmojiId.Value;
}
if (reaction.EmojiName != null) {
r["n"] = reaction.EmojiName;
}
r["a"] = reaction.EmojiFlags.HasFlag(EmojiFlags.Animated);
r["c"] = reaction.Count;
return r;
}).ToArray();
}
channelData[message.Id.ToString()] = obj;
} }
return users; data[channel] = channelData;
} }
private static object GenerateServerList(IDatabaseFile db, HashSet<ulong> serverIds, out Dictionary<ulong, object> serverIndices) { return data;
var servers = new List<object>();
serverIndices = new Dictionary<ulong, object>();
foreach (var server in db.GetAllServers()) {
var id = server.Id;
if (!serverIds.Contains(id)) {
continue;
}
serverIndices[id] = servers.Count;
servers.Add(new Dictionary<string, object> {
["name"] = server.Name,
["type"] = ServerTypes.ToJsonViewerString(server.Type)
});
}
return servers;
}
private static object GenerateChannelList(List<Channel> includedChannels, Dictionary<ulong, object> serverIndices) {
var channels = new Dictionary<string, object>();
foreach (var channel in includedChannels) {
var obj = new Dictionary<string, object> {
["server"] = serverIndices[channel.Server],
["name"] = channel.Name
};
if (channel.ParentId != null) {
obj["parent"] = channel.ParentId;
}
if (channel.Position != null) {
obj["position"] = channel.Position;
}
if (channel.Topic != null) {
obj["topic"] = channel.Topic;
}
if (channel.Nsfw != null) {
obj["nsfw"] = channel.Nsfw;
}
channels[channel.Id.ToString()] = obj;
}
return channels;
}
private static object GenerateMessageList( List<Message> includedMessages, Dictionary<ulong, object> userIndices, IViewerExportStrategy strategy) {
var data = new Dictionary<string, Dictionary<string, object>>();
foreach (var grouping in includedMessages.GroupBy(static message => message.Channel)) {
var channel = grouping.Key.ToString();
var channelData = new Dictionary<string, object>();
foreach (var message in grouping) {
var obj = new Dictionary<string, object> {
["u"] = userIndices[message.Sender],
["t"] = message.Timestamp
};
if (!string.IsNullOrEmpty(message.Text)) {
obj["m"] = message.Text;
}
if (message.EditTimestamp != null) {
obj["te"] = message.EditTimestamp;
}
if (message.RepliedToId != null) {
obj["r"] = message.RepliedToId.Value;
}
if (!message.Attachments.IsEmpty) {
obj["a"] = message.Attachments.Select(attachment => {
var a = new Dictionary<string, object> {
{ "url", strategy.GetAttachmentUrl(attachment) },
{ "name", Uri.TryCreate(attachment.Url, UriKind.Absolute, out var uri) ? Path.GetFileName(uri.LocalPath) : attachment.Url }
};
if (attachment.Width != null && attachment.Height != null) {
a["width"] = attachment.Width;
a["height"] = attachment.Height;
}
return a;
}).ToArray();
}
if (!message.Embeds.IsEmpty) {
obj["e"] = message.Embeds.Select(static embed => embed.Json).ToArray();
}
if (!message.Reactions.IsEmpty) {
obj["re"] = message.Reactions.Select(static reaction => {
var r = new Dictionary<string, object>();
if (reaction.EmojiId != null) {
r["id"] = reaction.EmojiId.Value;
}
if (reaction.EmojiName != null) {
r["n"] = reaction.EmojiName;
}
r["a"] = reaction.EmojiFlags.HasFlag(EmojiFlags.Animated);
r["c"] = reaction.Count;
return r;
}).ToArray();
}
channelData[message.Id.ToString()] = obj;
}
data[channel] = channelData;
}
return data;
}
} }
} }

View File

@ -2,14 +2,14 @@ using System;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
namespace DHT.Server.Database.Export { namespace DHT.Server.Database.Export;
sealed class ViewerJsonSnowflakeSerializer : JsonConverter<ulong> {
public override ulong Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
return ulong.Parse(reader.GetString()!);
}
public override void Write(Utf8JsonWriter writer, ulong value, JsonSerializerOptions options) { sealed class ViewerJsonSnowflakeSerializer : JsonConverter<ulong> {
writer.WriteStringValue(value.ToString()); public override ulong Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
} return ulong.Parse(reader.GetString()!);
}
public override void Write(Utf8JsonWriter writer, ulong value, JsonSerializerOptions options) {
writer.WriteStringValue(value.ToString());
} }
} }

View File

@ -5,39 +5,39 @@ using DHT.Server.Data.Aggregations;
using DHT.Server.Data.Filters; using DHT.Server.Data.Filters;
using DHT.Server.Download; using DHT.Server.Download;
namespace DHT.Server.Database { namespace DHT.Server.Database;
public interface IDatabaseFile : IDisposable {
string Path { get; }
DatabaseStatistics Statistics { get; }
DatabaseStatisticsSnapshot SnapshotStatistics();
void AddServer(Data.Server server); public interface IDatabaseFile : IDisposable {
List<Data.Server> GetAllServers(); string Path { get; }
DatabaseStatistics Statistics { get; }
DatabaseStatisticsSnapshot SnapshotStatistics();
void AddChannel(Channel channel); void AddServer(Data.Server server);
List<Channel> GetAllChannels(); List<Data.Server> GetAllServers();
void AddUsers(User[] users); void AddChannel(Channel channel);
List<User> GetAllUsers(); List<Channel> GetAllChannels();
void AddMessages(Message[] messages); void AddUsers(User[] users);
int CountMessages(MessageFilter? filter = null); List<User> GetAllUsers();
List<Message> GetMessages(MessageFilter? filter = null);
HashSet<ulong> GetMessageIds(MessageFilter? filter = null);
void RemoveMessages(MessageFilter filter, FilterRemovalMode mode);
int CountAttachments(AttachmentFilter? filter = null); void AddMessages(Message[] messages);
int CountMessages(MessageFilter? filter = null);
List<Message> GetMessages(MessageFilter? filter = null);
HashSet<ulong> GetMessageIds(MessageFilter? filter = null);
void RemoveMessages(MessageFilter filter, FilterRemovalMode mode);
void AddDownload(Data.Download download); int CountAttachments(AttachmentFilter? filter = null);
List<Data.Download> GetDownloadsWithoutData();
Data.Download GetDownloadWithData(Data.Download download);
DownloadedAttachment? GetDownloadedAttachment(string url);
void EnqueueDownloadItems(AttachmentFilter? filter = null); void AddDownload(Data.Download download);
List<DownloadItem> GetEnqueuedDownloadItems(int count); List<Data.Download> GetDownloadsWithoutData();
void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode); Data.Download GetDownloadWithData(Data.Download download);
DownloadStatusStatistics GetDownloadStatusStatistics(); DownloadedAttachment? GetDownloadedAttachment(string url);
void Vacuum(); void EnqueueDownloadItems(AttachmentFilter? filter = null);
} List<DownloadItem> GetEnqueuedDownloadItems(int count);
void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode);
DownloadStatusStatistics GetDownloadStatusStatistics();
void Vacuum();
} }

View File

@ -1,21 +1,21 @@
using System; using System;
namespace DHT.Server.Database.Import { namespace DHT.Server.Database.Import;
/// <summary>
/// https://discord.com/developers/docs/reference#snowflakes
/// </summary>
public sealed class FakeSnowflake {
private const ulong DiscordEpoch = 1420070400000UL;
private ulong id; /// <summary>
/// https://discord.com/developers/docs/reference#snowflakes
/// </summary>
public sealed class FakeSnowflake {
private const ulong DiscordEpoch = 1420070400000UL;
public FakeSnowflake() { private ulong id;
var unixMillis = (ulong) (DateTime.UtcNow.Subtract(DateTime.UnixEpoch).Ticks / TimeSpan.TicksPerMillisecond);
this.id = (unixMillis - DiscordEpoch) << 22;
}
internal ulong Next() { public FakeSnowflake() {
return id++; var unixMillis = (ulong) (DateTime.UtcNow.Subtract(DateTime.UnixEpoch).Ticks / TimeSpan.TicksPerMillisecond);
} this.id = (unixMillis - DiscordEpoch) << 22;
}
internal ulong Next() {
return id++;
} }
} }

View File

@ -12,252 +12,248 @@ using DHT.Utils.Http;
using DHT.Utils.Logging; using DHT.Utils.Logging;
using Microsoft.AspNetCore.StaticFiles; using Microsoft.AspNetCore.StaticFiles;
namespace DHT.Server.Database.Import { namespace DHT.Server.Database.Import;
public static class LegacyArchiveImport {
private static readonly Log Log = Log.ForType(typeof(LegacyArchiveImport));
private static readonly FileExtensionContentTypeProvider ContentTypeProvider = new (); public static class LegacyArchiveImport {
private static readonly Log Log = Log.ForType(typeof(LegacyArchiveImport));
public static async Task<bool> Read(Stream stream, IDatabaseFile db, FakeSnowflake fakeSnowflake, Func<Data.Server[], Task<Dictionary<Data.Server, ulong>?>> askForServerIds) { private static readonly FileExtensionContentTypeProvider ContentTypeProvider = new ();
var perf = Log.Start();
var root = await JsonSerializer.DeserializeAsync<JsonElement>(stream);
try { public static async Task<bool> Read(Stream stream, IDatabaseFile db, FakeSnowflake fakeSnowflake, Func<Data.Server[], Task<Dictionary<Data.Server, ulong>?>> askForServerIds) {
var meta = root.RequireObject("meta"); var perf = Log.Start();
var data = root.RequireObject("data"); var root = await JsonSerializer.DeserializeAsync<JsonElement>(stream);
perf.Step("Deserialize JSON"); try {
var meta = root.RequireObject("meta");
var data = root.RequireObject("data");
var users = ReadUserList(meta); perf.Step("Deserialize JSON");
var servers = ReadServerList(meta, fakeSnowflake);
var newServersOnly = new HashSet<Data.Server>(servers); var users = ReadUserList(meta);
var oldServersById = db.GetAllServers().ToDictionary(static server => server.Id, static server => server); var servers = ReadServerList(meta, fakeSnowflake);
var oldChannels = db.GetAllChannels(); var newServersOnly = new HashSet<Data.Server>(servers);
var oldChannelsById = oldChannels.ToDictionary(static channel => channel.Id, static channel => channel); var oldServersById = db.GetAllServers().ToDictionary(static server => server.Id, static server => server);
foreach (var (channelId, serverIndex) in ReadChannelToServerIndexMapping(meta, servers)) { var oldChannels = db.GetAllChannels();
if (oldChannelsById.TryGetValue(channelId, out var oldChannel) && oldServersById.TryGetValue(oldChannel.Server, out var oldServer) && newServersOnly.Remove(servers[serverIndex])) { var oldChannelsById = oldChannels.ToDictionary(static channel => channel.Id, static channel => channel);
servers[serverIndex] = oldServer;
} foreach (var (channelId, serverIndex) in ReadChannelToServerIndexMapping(meta, servers)) {
if (oldChannelsById.TryGetValue(channelId, out var oldChannel) && oldServersById.TryGetValue(oldChannel.Server, out var oldServer) && newServersOnly.Remove(servers[serverIndex])) {
servers[serverIndex] = oldServer;
}
}
perf.Step("Read server and user list");
if (newServersOnly.Count > 0) {
var askedServerIds = await askForServerIds(newServersOnly.ToArray());
if (askedServerIds == null) {
return false;
} }
perf.Step("Read server and user list"); perf.Step("Ask for server IDs");
if (newServersOnly.Count > 0) { for (var i = 0; i < servers.Length; i++) {
var askedServerIds = await askForServerIds(newServersOnly.ToArray()); var server = servers[i];
if (askedServerIds == null) { if (askedServerIds.TryGetValue(server, out var serverId)) {
return false; servers[i] = server with { Id = serverId };
}
perf.Step("Ask for server IDs");
for (var i = 0; i < servers.Length; i++) {
var server = servers[i];
if (askedServerIds.TryGetValue(server, out var serverId)) {
servers[i] = new Data.Server {
Id = serverId,
Name = server.Name,
Type = server.Type
};
}
} }
} }
var channels = ReadChannelList(meta, servers);
perf.Step("Read channel list");
var oldMessageIds = db.GetMessageIds();
var newMessages = channels.SelectMany(channel => ReadMessages(data, channel, users, fakeSnowflake))
.Where(message => !oldMessageIds.Contains(message.Id))
.ToArray();
perf.Step("Read messages");
db.AddUsers(users);
db.AddServers(servers);
db.AddChannels(channels);
db.AddMessages(newMessages);
perf.Step("Import into database");
} catch (HttpException e) {
throw new JsonException(e.Message);
} }
perf.End(); var channels = ReadChannelList(meta, servers);
return true;
perf.Step("Read channel list");
var oldMessageIds = db.GetMessageIds();
var newMessages = channels.SelectMany(channel => ReadMessages(data, channel, users, fakeSnowflake))
.Where(message => !oldMessageIds.Contains(message.Id))
.ToArray();
perf.Step("Read messages");
db.AddUsers(users);
db.AddServers(servers);
db.AddChannels(channels);
db.AddMessages(newMessages);
perf.Step("Import into database");
} catch (HttpException e) {
throw new JsonException(e.Message);
} }
private static User[] ReadUserList(JsonElement meta) { perf.End();
const string UsersPath = "meta.users[]"; return true;
}
static ulong ParseUserIndex(JsonElement element, int index) { private static User[] ReadUserList(JsonElement meta) {
return ulong.Parse(element.GetString() ?? throw new JsonException("Expected key 'meta.userindex[" + index + "]' to be a string.")); const string UsersPath = "meta.users[]";
}
var userindex = meta.RequireArray("userindex", "meta") static ulong ParseUserIndex(JsonElement element, int index) {
.Select(static (item, index) => (ParseUserIndex(item, index), index)) return ulong.Parse(element.GetString() ?? throw new JsonException("Expected key 'meta.userindex[" + index + "]' to be a string."));
.ToDictionary();
var users = new User[userindex.Count];
foreach (var item in meta.RequireObject("users", "meta").EnumerateObject()) {
var path = UsersPath + "." + item.Name;
var userId = ulong.Parse(item.Name);
var userObj = item.Value;
users[userindex[userId]] = new User {
Id = userId,
Name = userObj.RequireString("name", path),
AvatarUrl = userObj.HasKey("avatar") ? userObj.RequireString("avatar", path) : null,
Discriminator = userObj.HasKey("tag") ? userObj.RequireString("tag", path) : null
};
}
return users;
} }
private static Data.Server[] ReadServerList(JsonElement meta, FakeSnowflake fakeSnowflake) { var userindex = meta.RequireArray("userindex", "meta")
const string ServersPath = "meta.servers[]"; .Select(static (item, index) => (ParseUserIndex(item, index), index))
.ToDictionary();
return meta.RequireArray("servers", "meta").Select(serverObj => new Data.Server { var users = new User[userindex.Count];
foreach (var item in meta.RequireObject("users", "meta").EnumerateObject()) {
var path = UsersPath + "." + item.Name;
var userId = ulong.Parse(item.Name);
var userObj = item.Value;
users[userindex[userId]] = new User {
Id = userId,
Name = userObj.RequireString("name", path),
AvatarUrl = userObj.HasKey("avatar") ? userObj.RequireString("avatar", path) : null,
Discriminator = userObj.HasKey("tag") ? userObj.RequireString("tag", path) : null,
};
}
return users;
}
private static Data.Server[] ReadServerList(JsonElement meta, FakeSnowflake fakeSnowflake) {
const string ServersPath = "meta.servers[]";
return meta.RequireArray("servers", "meta").Select(serverObj => new Data.Server {
Id = fakeSnowflake.Next(),
Name = serverObj.RequireString("name", ServersPath),
Type = ServerTypes.FromString(serverObj.RequireString("type", ServersPath)),
}).ToArray();
}
private const string ChannelsPath = "meta.channels";
private static Dictionary<ulong, int> ReadChannelToServerIndexMapping(JsonElement meta, Data.Server[] servers) {
return meta.RequireObject("channels", "meta").EnumerateObject().Select(item => {
var path = ChannelsPath + "." + item.Name;
var channelId = ulong.Parse(item.Name);
var channelObj = item.Value;
return (channelId, channelObj.RequireInt("server", path, min: 0, max: servers.Length - 1));
}).ToDictionary();
}
private static Channel[] ReadChannelList(JsonElement meta, Data.Server[] servers) {
return meta.RequireObject("channels", "meta").EnumerateObject().Select(item => {
var path = ChannelsPath + "." + item.Name;
var channelId = ulong.Parse(item.Name);
var channelObj = item.Value;
return new Channel {
Id = channelId,
Server = servers[channelObj.RequireInt("server", path, min: 0, max: servers.Length - 1)].Id,
Name = channelObj.RequireString("name", path),
Position = channelObj.HasKey("position") ? channelObj.RequireInt("position", path, min: 0) : null,
Topic = channelObj.HasKey("topic") ? channelObj.RequireString("topic", path) : null,
Nsfw = channelObj.HasKey("nsfw") ? channelObj.RequireBool("nsfw", path) : null,
};
}).ToArray();
}
private static Message[] ReadMessages(JsonElement data, Channel channel, User[] users, FakeSnowflake fakeSnowflake) {
const string DataPath = "data";
var channelId = channel.Id;
var channelIdStr = channelId.ToString();
var messagesObj = data.HasKey(channelIdStr) ? data.RequireObject(channelIdStr, DataPath) : (JsonElement?) null;
if (messagesObj == null) {
return Array.Empty<Message>();
}
return messagesObj.Value.EnumerateObject().Select(item => {
var path = DataPath + "." + item.Name;
var messageId = ulong.Parse(item.Name);
var messageObj = item.Value;
return new Message {
Id = messageId,
Sender = users[messageObj.RequireInt("u", path, min: 0, max: users.Length - 1)].Id,
Channel = channelId,
Text = messageObj.HasKey("m") ? messageObj.RequireString("m", path) : string.Empty,
Timestamp = messageObj.RequireLong("t", path),
EditTimestamp = messageObj.HasKey("te") ? messageObj.RequireLong("te", path) : null,
RepliedToId = messageObj.HasKey("r") ? messageObj.RequireSnowflake("r", path) : null,
Attachments = messageObj.HasKey("a") ? ReadMessageAttachments(messageObj.RequireArray("a", path), fakeSnowflake, path + ".a[]").ToImmutableArray() : ImmutableArray<Attachment>.Empty,
Embeds = messageObj.HasKey("e") ? ReadMessageEmbeds(messageObj.RequireArray("e", path), path + ".e[]").ToImmutableArray() : ImmutableArray<Embed>.Empty,
Reactions = messageObj.HasKey("re") ? ReadMessageReactions(messageObj.RequireArray("re", path), path + ".re[]").ToImmutableArray() : ImmutableArray<Reaction>.Empty,
};
}).ToArray();
}
[SuppressMessage("ReSharper", "ConvertToLambdaExpression")]
private static IEnumerable<Attachment> ReadMessageAttachments(JsonElement.ArrayEnumerator attachmentsArray, FakeSnowflake fakeSnowflake, string path) {
return attachmentsArray.Select(attachmentObj => {
string url = attachmentObj.RequireString("url", path);
string name = url[(url.LastIndexOf('/') + 1)..];
string? type = ContentTypeProvider.TryGetContentType(name, out var contentType) ? contentType : null;
return new Attachment {
Id = fakeSnowflake.Next(), Id = fakeSnowflake.Next(),
Name = serverObj.RequireString("name", ServersPath), Name = name,
Type = ServerTypes.FromString(serverObj.RequireString("type", ServersPath)) Type = type,
}).ToArray(); Url = url,
} Size = 0, // unknown size
};
}).DistinctByKeyStable(static attachment => {
// Some Discord messages have duplicate attachments with the same id for unknown reasons.
return attachment.Id;
});
}
private const string ChannelsPath = "meta.channels"; private static IEnumerable<Embed> ReadMessageEmbeds(JsonElement.ArrayEnumerator embedsArray, string path) {
// Some rich embeds are missing URLs which causes a missing 'url' key.
return embedsArray.Where(static embedObj => embedObj.HasKey("url")).Select(embedObj => {
string url = embedObj.RequireString("url", path);
string type = embedObj.RequireString("type", path);
private static Dictionary<ulong, int> ReadChannelToServerIndexMapping(JsonElement meta, Data.Server[] servers) { var embedJson = new Dictionary<string, object> {
return meta.RequireObject("channels", "meta").EnumerateObject().Select(item => { { "url", url },
var path = ChannelsPath + "." + item.Name; { "type", type },
var channelId = ulong.Parse(item.Name); { "dht_legacy", true },
var channelObj = item.Value; };
return (channelId, channelObj.RequireInt("server", path, min: 0, max: servers.Length - 1)); if (type == "image") {
}).ToDictionary(); embedJson["image"] = new Dictionary<string, string> {
} { "url", url }
private static Channel[] ReadChannelList(JsonElement meta, Data.Server[] servers) {
return meta.RequireObject("channels", "meta").EnumerateObject().Select(item => {
var path = ChannelsPath + "." + item.Name;
var channelId = ulong.Parse(item.Name);
var channelObj = item.Value;
return new Channel {
Id = channelId,
Server = servers[channelObj.RequireInt("server", path, min: 0, max: servers.Length - 1)].Id,
Name = channelObj.RequireString("name", path),
Position = channelObj.HasKey("position") ? channelObj.RequireInt("position", path, min: 0) : null,
Topic = channelObj.HasKey("topic") ? channelObj.RequireString("topic", path) : null,
Nsfw = channelObj.HasKey("nsfw") ? channelObj.RequireBool("nsfw", path) : null
}; };
}).ToArray(); }
} else if (type == "rich") {
if (embedObj.HasKey("t")) {
embedJson["title"] = embedObj.RequireString("t", path);
}
private static Message[] ReadMessages(JsonElement data, Channel channel, User[] users, FakeSnowflake fakeSnowflake) { if (embedObj.HasKey("d")) {
const string DataPath = "data"; embedJson["description"] = embedObj.RequireString("d", path);
}
var channelId = channel.Id;
var channelIdStr = channelId.ToString();
var messagesObj = data.HasKey(channelIdStr) ? data.RequireObject(channelIdStr, DataPath) : (JsonElement?) null;
if (messagesObj == null) {
return Array.Empty<Message>();
} }
return messagesObj.Value.EnumerateObject().Select(item => { return new Embed {
var path = DataPath + "." + item.Name; Json = JsonSerializer.Serialize(embedJson)
var messageId = ulong.Parse(item.Name); };
var messageObj = item.Value; });
}
return new Message { private static IEnumerable<Reaction> ReadMessageReactions(JsonElement.ArrayEnumerator reactionsArray, string path) {
Id = messageId, return reactionsArray.Select(reactionObj => {
Sender = users[messageObj.RequireInt("u", path, min: 0, max: users.Length - 1)].Id, var id = reactionObj.HasKey("id") ? reactionObj.RequireSnowflake("id", path) : (ulong?) null;
Channel = channelId, var name = reactionObj.HasKey("n") ? reactionObj.RequireString("n", path) : null;
Text = messageObj.HasKey("m") ? messageObj.RequireString("m", path) : string.Empty,
Timestamp = messageObj.RequireLong("t", path),
EditTimestamp = messageObj.HasKey("te") ? messageObj.RequireLong("te", path) : null,
RepliedToId = messageObj.HasKey("r") ? messageObj.RequireSnowflake("r", path) : null,
Attachments = messageObj.HasKey("a") ? ReadMessageAttachments(messageObj.RequireArray("a", path), fakeSnowflake, path + ".a[]").ToImmutableArray() : ImmutableArray<Attachment>.Empty,
Embeds = messageObj.HasKey("e") ? ReadMessageEmbeds(messageObj.RequireArray("e", path), path + ".e[]").ToImmutableArray() : ImmutableArray<Embed>.Empty,
Reactions = messageObj.HasKey("re") ? ReadMessageReactions(messageObj.RequireArray("re", path), path + ".re[]").ToImmutableArray() : ImmutableArray<Reaction>.Empty
};
}).ToArray();
}
[SuppressMessage("ReSharper", "ConvertToLambdaExpression")] if (id == null && name == null) {
private static IEnumerable<Attachment> ReadMessageAttachments(JsonElement.ArrayEnumerator attachmentsArray, FakeSnowflake fakeSnowflake, string path) { throw new JsonException("Expected key '" + path + ".id' and/or '" + path + ".n' to be present.");
return attachmentsArray.Select(attachmentObj => { }
string url = attachmentObj.RequireString("url", path);
string name = url[(url.LastIndexOf('/') + 1)..];
string? type = ContentTypeProvider.TryGetContentType(name, out var contentType) ? contentType : null;
return new Attachment { return new Reaction {
Id = fakeSnowflake.Next(), EmojiId = id,
Name = name, EmojiName = name,
Type = type, EmojiFlags = reactionObj.HasKey("an") && reactionObj.RequireBool("an", path) ? EmojiFlags.Animated : EmojiFlags.None,
Url = url, Count = reactionObj.RequireInt("c", path, min: 0),
Size = 0 // unknown size };
}; });
}).DistinctByKeyStable(static attachment => {
// Some Discord messages have duplicate attachments with the same id for unknown reasons.
return attachment.Id;
});
}
private static IEnumerable<Embed> ReadMessageEmbeds(JsonElement.ArrayEnumerator embedsArray, string path) {
// Some rich embeds are missing URLs which causes a missing 'url' key.
return embedsArray.Where(static embedObj => embedObj.HasKey("url")).Select(embedObj => {
string url = embedObj.RequireString("url", path);
string type = embedObj.RequireString("type", path);
var embedJson = new Dictionary<string, object> {
{ "url", url },
{ "type", type },
{ "dht_legacy", true }
};
if (type == "image") {
embedJson["image"] = new Dictionary<string, string> {
{ "url", url }
};
}
else if (type == "rich") {
if (embedObj.HasKey("t")) {
embedJson["title"] = embedObj.RequireString("t", path);
}
if (embedObj.HasKey("d")) {
embedJson["description"] = embedObj.RequireString("d", path);
}
}
return new Embed {
Json = JsonSerializer.Serialize(embedJson)
};
});
}
private static IEnumerable<Reaction> ReadMessageReactions(JsonElement.ArrayEnumerator reactionsArray, string path) {
return reactionsArray.Select(reactionObj => {
var id = reactionObj.HasKey("id") ? reactionObj.RequireSnowflake("id", path) : (ulong?) null;
var name = reactionObj.HasKey("n") ? reactionObj.RequireString("n", path) : null;
if (id == null && name == null) {
throw new JsonException("Expected key '" + path + ".id' and/or '" + path + ".n' to be present.");
}
return new Reaction {
EmojiId = id,
EmojiName = name,
EmojiFlags = reactionObj.HasKey("an") && reactionObj.RequireBool("an", path) ? EmojiFlags.Animated : EmojiFlags.None,
Count = reactionObj.RequireInt("c", path, min: 0)
};
});
}
} }
} }

View File

@ -4,60 +4,61 @@ using DHT.Server.Database.Exceptions;
using DHT.Server.Database.Sqlite.Utils; using DHT.Server.Database.Sqlite.Utils;
using DHT.Utils.Logging; using DHT.Utils.Logging;
namespace DHT.Server.Database.Sqlite { namespace DHT.Server.Database.Sqlite;
sealed class Schema {
internal const int Version = 5;
private static readonly Log Log = Log.ForType<Schema>(); sealed class Schema {
internal const int Version = 5;
private readonly ISqliteConnection conn; private static readonly Log Log = Log.ForType<Schema>();
public Schema(ISqliteConnection conn) { private readonly ISqliteConnection conn;
this.conn = conn;
public Schema(ISqliteConnection conn) {
this.conn = conn;
}
private void Execute(string sql) {
conn.Command(sql).ExecuteNonQuery();
}
public async Task<bool> Setup(Func<Task<bool>> checkCanUpgradeSchemas) {
Execute(@"CREATE TABLE IF NOT EXISTS metadata (key TEXT PRIMARY KEY, value TEXT)");
var dbVersionStr = conn.SelectScalar("SELECT value FROM metadata WHERE key = 'version'");
if (dbVersionStr == null) {
InitializeSchemas();
}
else if (!int.TryParse(dbVersionStr.ToString(), out int dbVersion) || dbVersion < 1) {
throw new InvalidDatabaseVersionException(dbVersionStr.ToString() ?? "<null>");
}
else if (dbVersion > Version) {
throw new DatabaseTooNewException(dbVersion);
}
else if (dbVersion < Version) {
var proceed = await checkCanUpgradeSchemas();
if (!proceed) {
return false;
}
UpgradeSchemas(dbVersion);
} }
private void Execute(string sql) { return true;
conn.Command(sql).ExecuteNonQuery(); }
}
public async Task<bool> Setup(Func<Task<bool>> checkCanUpgradeSchemas) { private void InitializeSchemas() {
Execute(@"CREATE TABLE IF NOT EXISTS metadata (key TEXT PRIMARY KEY, value TEXT)"); Execute(@"CREATE TABLE users (
var dbVersionStr = conn.SelectScalar("SELECT value FROM metadata WHERE key = 'version'");
if (dbVersionStr == null) {
InitializeSchemas();
}
else if (!int.TryParse(dbVersionStr.ToString(), out int dbVersion) || dbVersion < 1) {
throw new InvalidDatabaseVersionException(dbVersionStr.ToString() ?? "<null>");
}
else if (dbVersion > Version) {
throw new DatabaseTooNewException(dbVersion);
}
else if (dbVersion < Version) {
var proceed = await checkCanUpgradeSchemas();
if (!proceed) {
return false;
}
UpgradeSchemas(dbVersion);
}
return true;
}
private void InitializeSchemas() {
Execute(@"CREATE TABLE users (
id INTEGER PRIMARY KEY NOT NULL, id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
avatar_url TEXT, avatar_url TEXT,
discriminator TEXT)"); discriminator TEXT)");
Execute(@"CREATE TABLE servers ( Execute(@"CREATE TABLE servers (
id INTEGER PRIMARY KEY NOT NULL, id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
type TEXT NOT NULL)"); type TEXT NOT NULL)");
Execute(@"CREATE TABLE channels ( Execute(@"CREATE TABLE channels (
id INTEGER PRIMARY KEY NOT NULL, id INTEGER PRIMARY KEY NOT NULL,
server INTEGER NOT NULL, server INTEGER NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
@ -66,14 +67,14 @@ namespace DHT.Server.Database.Sqlite {
topic TEXT, topic TEXT,
nsfw INTEGER)"); nsfw INTEGER)");
Execute(@"CREATE TABLE messages ( Execute(@"CREATE TABLE messages (
message_id INTEGER PRIMARY KEY NOT NULL, message_id INTEGER PRIMARY KEY NOT NULL,
sender_id INTEGER NOT NULL, sender_id INTEGER NOT NULL,
channel_id INTEGER NOT NULL, channel_id INTEGER NOT NULL,
text TEXT NOT NULL, text TEXT NOT NULL,
timestamp INTEGER NOT NULL)"); timestamp INTEGER NOT NULL)");
Execute(@"CREATE TABLE attachments ( Execute(@"CREATE TABLE attachments (
message_id INTEGER NOT NULL, message_id INTEGER NOT NULL,
attachment_id INTEGER NOT NULL PRIMARY KEY NOT NULL, attachment_id INTEGER NOT NULL PRIMARY KEY NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
@ -83,91 +84,90 @@ namespace DHT.Server.Database.Sqlite {
width INTEGER, width INTEGER,
height INTEGER)"); height INTEGER)");
Execute(@"CREATE TABLE embeds ( Execute(@"CREATE TABLE embeds (
message_id INTEGER NOT NULL, message_id INTEGER NOT NULL,
json TEXT NOT NULL)"); json TEXT NOT NULL)");
Execute(@"CREATE TABLE reactions ( Execute(@"CREATE TABLE reactions (
message_id INTEGER NOT NULL, message_id INTEGER NOT NULL,
emoji_id INTEGER, emoji_id INTEGER,
emoji_name TEXT, emoji_name TEXT,
emoji_flags INTEGER NOT NULL, emoji_flags INTEGER NOT NULL,
count INTEGER NOT NULL)"); count INTEGER NOT NULL)");
CreateMessageEditTimestampTable(); CreateMessageEditTimestampTable();
CreateMessageRepliedToTable(); CreateMessageRepliedToTable();
CreateDownloadsTable(); CreateDownloadsTable();
Execute("CREATE INDEX attachments_message_ix ON attachments(message_id)"); Execute("CREATE INDEX attachments_message_ix ON attachments(message_id)");
Execute("CREATE INDEX embeds_message_ix ON embeds(message_id)"); Execute("CREATE INDEX embeds_message_ix ON embeds(message_id)");
Execute("CREATE INDEX reactions_message_ix ON reactions(message_id)"); Execute("CREATE INDEX reactions_message_ix ON reactions(message_id)");
Execute("INSERT INTO metadata (key, value) VALUES ('version', " + Version + ")"); Execute("INSERT INTO metadata (key, value) VALUES ('version', " + Version + ")");
} }
private void CreateMessageEditTimestampTable() { private void CreateMessageEditTimestampTable() {
Execute(@"CREATE TABLE edit_timestamps ( Execute(@"CREATE TABLE edit_timestamps (
message_id INTEGER PRIMARY KEY NOT NULL, message_id INTEGER PRIMARY KEY NOT NULL,
edit_timestamp INTEGER NOT NULL)"); edit_timestamp INTEGER NOT NULL)");
} }
private void CreateMessageRepliedToTable() { private void CreateMessageRepliedToTable() {
Execute(@"CREATE TABLE replied_to ( Execute(@"CREATE TABLE replied_to (
message_id INTEGER PRIMARY KEY NOT NULL, message_id INTEGER PRIMARY KEY NOT NULL,
replied_to_id INTEGER NOT NULL)"); replied_to_id INTEGER NOT NULL)");
} }
private void CreateDownloadsTable() { private void CreateDownloadsTable() {
Execute(@"CREATE TABLE downloads ( Execute(@"CREATE TABLE downloads (
url TEXT NOT NULL PRIMARY KEY, url TEXT NOT NULL PRIMARY KEY,
status INTEGER NOT NULL, status INTEGER NOT NULL,
size INTEGER NOT NULL, size INTEGER NOT NULL,
blob BLOB)"); blob BLOB)");
}
private void UpgradeSchemas(int dbVersion) {
var perf = Log.Start("from version " + dbVersion);
Execute("UPDATE metadata SET value = " + Version + " WHERE key = 'version'");
if (dbVersion <= 1) {
Execute("ALTER TABLE channels ADD parent_id INTEGER");
perf.Step("Upgrade to version 2");
} }
private void UpgradeSchemas(int dbVersion) { if (dbVersion <= 2) {
var perf = Log.Start("from version " + dbVersion); CreateMessageEditTimestampTable();
CreateMessageRepliedToTable();
Execute("UPDATE metadata SET value = " + Version + " WHERE key = 'version'"); Execute(@"INSERT INTO edit_timestamps (message_id, edit_timestamp)
if (dbVersion <= 1) {
Execute("ALTER TABLE channels ADD parent_id INTEGER");
perf.Step("Upgrade to version 2");
}
if (dbVersion <= 2) {
CreateMessageEditTimestampTable();
CreateMessageRepliedToTable();
Execute(@"INSERT INTO edit_timestamps (message_id, edit_timestamp)
SELECT message_id, edit_timestamp FROM messages SELECT message_id, edit_timestamp FROM messages
WHERE edit_timestamp IS NOT NULL"); WHERE edit_timestamp IS NOT NULL");
Execute(@"INSERT INTO replied_to (message_id, replied_to_id) Execute(@"INSERT INTO replied_to (message_id, replied_to_id)
SELECT message_id, replied_to_id FROM messages SELECT message_id, replied_to_id FROM messages
WHERE replied_to_id IS NOT NULL"); WHERE replied_to_id IS NOT NULL");
Execute("ALTER TABLE messages DROP COLUMN replied_to_id"); Execute("ALTER TABLE messages DROP COLUMN replied_to_id");
Execute("ALTER TABLE messages DROP COLUMN edit_timestamp"); Execute("ALTER TABLE messages DROP COLUMN edit_timestamp");
perf.Step("Upgrade to version 3"); perf.Step("Upgrade to version 3");
Execute("VACUUM"); Execute("VACUUM");
perf.Step("Vacuum"); perf.Step("Vacuum");
}
if (dbVersion <= 3) {
CreateDownloadsTable();
perf.Step("Upgrade to version 4");
}
if (dbVersion <= 4) {
Execute("ALTER TABLE attachments ADD width INTEGER");
Execute("ALTER TABLE attachments ADD height INTEGER");
perf.Step("Upgrade to version 5");
}
perf.End();
} }
if (dbVersion <= 3) {
CreateDownloadsTable();
perf.Step("Upgrade to version 4");
}
if (dbVersion <= 4) {
Execute("ALTER TABLE attachments ADD width INTEGER");
Execute("ALTER TABLE attachments ADD height INTEGER");
perf.Step("Upgrade to version 5");
}
perf.End();
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -5,79 +5,79 @@ using DHT.Server.Data;
using DHT.Server.Data.Filters; using DHT.Server.Data.Filters;
using DHT.Server.Database.Sqlite.Utils; using DHT.Server.Database.Sqlite.Utils;
namespace DHT.Server.Database.Sqlite { namespace DHT.Server.Database.Sqlite;
static class SqliteFilters {
public static string GenerateWhereClause(this MessageFilter? filter, string? tableAlias = null, bool invert = false) {
if (filter == null) {
return "";
}
var where = new SqliteWhereGenerator(tableAlias, invert); static class SqliteFilters {
public static string GenerateWhereClause(this MessageFilter? filter, string? tableAlias = null, bool invert = false) {
if (filter.StartDate != null) { if (filter == null) {
where.AddCondition("timestamp >= " + new DateTimeOffset(filter.StartDate.Value).ToUnixTimeMilliseconds()); return "";
}
if (filter.EndDate != null) {
where.AddCondition("timestamp <= " + new DateTimeOffset(filter.EndDate.Value).ToUnixTimeMilliseconds());
}
if (filter.ChannelIds != null) {
where.AddCondition("channel_id IN (" + string.Join(",", filter.ChannelIds) + ")");
}
if (filter.UserIds != null) {
where.AddCondition("sender_id IN (" + string.Join(",", filter.UserIds) + ")");
}
if (filter.MessageIds != null) {
where.AddCondition("message_id IN (" + string.Join(",", filter.MessageIds) + ")");
}
return where.Generate();
} }
public static string GenerateWhereClause(this AttachmentFilter? filter, string? tableAlias = null, bool invert = false) { var where = new SqliteWhereGenerator(tableAlias, invert);
if (filter == null) {
return "";
}
var where = new SqliteWhereGenerator(tableAlias, invert); if (filter.StartDate != null) {
where.AddCondition("timestamp >= " + new DateTimeOffset(filter.StartDate.Value).ToUnixTimeMilliseconds());
if (filter.MaxBytes != null) {
where.AddCondition("size <= " + filter.MaxBytes);
}
if (filter.DownloadItemRule == AttachmentFilter.DownloadItemRules.OnlyNotPresent) {
where.AddCondition("url NOT IN (SELECT url FROM downloads)");
}
else if (filter.DownloadItemRule == AttachmentFilter.DownloadItemRules.OnlyPresent) {
where.AddCondition("url IN (SELECT url FROM downloads)");
}
return where.Generate();
} }
public static string GenerateWhereClause(this DownloadItemFilter? filter, string? tableAlias = null, bool invert = false) { if (filter.EndDate != null) {
if (filter == null) { where.AddCondition("timestamp <= " + new DateTimeOffset(filter.EndDate.Value).ToUnixTimeMilliseconds());
return "";
}
var where = new SqliteWhereGenerator(tableAlias, invert);
if (filter.IncludeStatuses != null) {
where.AddCondition("status IN (" + filter.IncludeStatuses.In() + ")");
}
if (filter.ExcludeStatuses != null) {
where.AddCondition("status NOT IN (" + filter.ExcludeStatuses.In() + ")");
}
return where.Generate();
} }
private static string In(this ISet<DownloadStatus> statuses) { if (filter.ChannelIds != null) {
return string.Join(",", statuses.Select(static status => (int) status)); where.AddCondition("channel_id IN (" + string.Join(",", filter.ChannelIds) + ")");
} }
if (filter.UserIds != null) {
where.AddCondition("sender_id IN (" + string.Join(",", filter.UserIds) + ")");
}
if (filter.MessageIds != null) {
where.AddCondition("message_id IN (" + string.Join(",", filter.MessageIds) + ")");
}
return where.Generate();
}
public static string GenerateWhereClause(this AttachmentFilter? filter, string? tableAlias = null, bool invert = false) {
if (filter == null) {
return "";
}
var where = new SqliteWhereGenerator(tableAlias, invert);
if (filter.MaxBytes != null) {
where.AddCondition("size <= " + filter.MaxBytes);
}
if (filter.DownloadItemRule == AttachmentFilter.DownloadItemRules.OnlyNotPresent) {
where.AddCondition("url NOT IN (SELECT url FROM downloads)");
}
else if (filter.DownloadItemRule == AttachmentFilter.DownloadItemRules.OnlyPresent) {
where.AddCondition("url IN (SELECT url FROM downloads)");
}
return where.Generate();
}
public static string GenerateWhereClause(this DownloadItemFilter? filter, string? tableAlias = null, bool invert = false) {
if (filter == null) {
return "";
}
var where = new SqliteWhereGenerator(tableAlias, invert);
if (filter.IncludeStatuses != null) {
where.AddCondition("status IN (" + filter.IncludeStatuses.In() + ")");
}
if (filter.ExcludeStatuses != null) {
where.AddCondition("status NOT IN (" + filter.ExcludeStatuses.In() + ")");
}
return where.Generate();
}
private static string In(this ISet<DownloadStatus> statuses) {
return string.Join(",", statuses.Select(static status => (int) status));
} }
} }

View File

@ -1,8 +1,8 @@
using System; using System;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
namespace DHT.Server.Database.Sqlite.Utils { namespace DHT.Server.Database.Sqlite.Utils;
interface ISqliteConnection : IDisposable {
SqliteConnection InnerConnection { get; } interface ISqliteConnection : IDisposable {
} SqliteConnection InnerConnection { get; }
} }

View File

@ -5,112 +5,110 @@ using System.Threading;
using DHT.Utils.Logging; using DHT.Utils.Logging;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
namespace DHT.Server.Database.Sqlite.Utils { namespace DHT.Server.Database.Sqlite.Utils;
sealed class SqliteConnectionPool : IDisposable {
private static string GetConnectionString(SqliteConnectionStringBuilder connectionStringBuilder) {
connectionStringBuilder.Pooling = false;
return connectionStringBuilder.ToString();
}
private readonly object monitor = new (); sealed class SqliteConnectionPool : IDisposable {
private readonly Random rand = new (); private static string GetConnectionString(SqliteConnectionStringBuilder connectionStringBuilder) {
private volatile bool isDisposed; connectionStringBuilder.Pooling = false;
return connectionStringBuilder.ToString();
}
private readonly BlockingCollection<PooledConnection> free = new (new ConcurrentStack<PooledConnection>()); private readonly object monitor = new ();
private readonly List<PooledConnection> used; private readonly Random rand = new ();
private volatile bool isDisposed;
public SqliteConnectionPool(SqliteConnectionStringBuilder connectionStringBuilder, int poolSize) { private readonly BlockingCollection<PooledConnection> free = new (new ConcurrentStack<PooledConnection>());
var connectionString = GetConnectionString(connectionStringBuilder); private readonly List<PooledConnection> used;
for (int i = 0; i < poolSize; i++) { public SqliteConnectionPool(SqliteConnectionStringBuilder connectionStringBuilder, int poolSize) {
var conn = new SqliteConnection(connectionString); var connectionString = GetConnectionString(connectionStringBuilder);
conn.Open();
var pooledConn = new PooledConnection(this, conn); for (int i = 0; i < poolSize; i++) {
var conn = new SqliteConnection(connectionString);
conn.Open();
using (var cmd = pooledConn.Command("PRAGMA journal_mode=WAL")) { var pooledConn = new PooledConnection(this, conn);
cmd.ExecuteNonQuery();
}
free.Add(pooledConn); using (var cmd = pooledConn.Command("PRAGMA journal_mode=WAL")) {
cmd.ExecuteNonQuery();
} }
used = new List<PooledConnection>(poolSize); free.Add(pooledConn);
} }
private void ThrowIfDisposed() { used = new List<PooledConnection>(poolSize);
if (isDisposed) { }
throw new ObjectDisposedException(nameof(SqliteConnectionPool));
}
}
public ISqliteConnection Take() { private void ThrowIfDisposed() {
while (true) { ObjectDisposedException.ThrowIf(isDisposed, nameof(SqliteConnectionPool));
ThrowIfDisposed(); }
lock (monitor) { public ISqliteConnection Take() {
if (free.TryTake(out var conn)) { while (true) {
used.Add(conn);
return conn;
}
else {
Log.ForType<SqliteConnectionPool>().Warn("Thread " + Thread.CurrentThread.ManagedThreadId + " is starving for connections.");
}
}
Thread.Sleep(TimeSpan.FromMilliseconds(rand.Next(100, 200)));
}
}
private void Return(PooledConnection conn) {
ThrowIfDisposed(); ThrowIfDisposed();
lock (monitor) { lock (monitor) {
if (used.Remove(conn)) { if (free.TryTake(out var conn)) {
free.Add(conn); used.Add(conn);
return conn;
}
else {
Log.ForType<SqliteConnectionPool>().Warn("Thread " + Environment.CurrentManagedThreadId + " is starving for connections.");
} }
} }
Thread.Sleep(TimeSpan.FromMilliseconds(rand.Next(100, 200)));
} }
}
public void Dispose() { private void Return(PooledConnection conn) {
if (isDisposed) { ThrowIfDisposed();
return;
}
isDisposed = true; lock (monitor) {
if (used.Remove(conn)) {
lock (monitor) { free.Add(conn);
while (free.TryTake(out var conn)) {
Close(conn.InnerConnection);
}
foreach (var conn in used) {
Close(conn.InnerConnection);
}
free.Dispose();
used.Clear();
}
}
private static void Close(SqliteConnection conn) {
conn.Close();
conn.Dispose();
}
private sealed class PooledConnection : ISqliteConnection {
public SqliteConnection InnerConnection { get; }
private readonly SqliteConnectionPool pool;
public PooledConnection(SqliteConnectionPool pool, SqliteConnection conn) {
this.pool = pool;
this.InnerConnection = conn;
}
void IDisposable.Dispose() {
pool.Return(this);
} }
} }
} }
public void Dispose() {
if (isDisposed) {
return;
}
isDisposed = true;
lock (monitor) {
while (free.TryTake(out var conn)) {
Close(conn.InnerConnection);
}
foreach (var conn in used) {
Close(conn.InnerConnection);
}
free.Dispose();
used.Clear();
}
}
private static void Close(SqliteConnection conn) {
conn.Close();
conn.Dispose();
}
private sealed class PooledConnection : ISqliteConnection {
public SqliteConnection InnerConnection { get; }
private readonly SqliteConnectionPool pool;
public PooledConnection(SqliteConnectionPool pool, SqliteConnection conn) {
this.pool = pool;
this.InnerConnection = conn;
}
void IDisposable.Dispose() {
pool.Return(this);
}
}
} }

View File

@ -2,70 +2,70 @@ using System;
using System.Linq; using System.Linq;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
namespace DHT.Server.Database.Sqlite.Utils { namespace DHT.Server.Database.Sqlite.Utils;
static class SqliteExtensions {
public static SqliteCommand Command(this ISqliteConnection conn, string sql) {
var cmd = conn.InnerConnection.CreateCommand();
cmd.CommandText = sql;
return cmd;
}
public static SqliteTransaction BeginTransaction(this ISqliteConnection conn) { static class SqliteExtensions {
return conn.InnerConnection.BeginTransaction(); public static SqliteCommand Command(this ISqliteConnection conn, string sql) {
} var cmd = conn.InnerConnection.CreateCommand();
cmd.CommandText = sql;
return cmd;
}
public static object? SelectScalar(this ISqliteConnection conn, string sql) { public static SqliteTransaction BeginTransaction(this ISqliteConnection conn) {
using var cmd = conn.Command(sql); return conn.InnerConnection.BeginTransaction();
return cmd.ExecuteScalar(); }
}
public static SqliteCommand Insert(this ISqliteConnection conn, string tableName, (string Name, SqliteType Type)[] columns) { public static object? SelectScalar(this ISqliteConnection conn, string sql) {
string columnNames = string.Join(',', columns.Select(static c => c.Name)); using var cmd = conn.Command(sql);
string columnParams = string.Join(',', columns.Select(static c => ':' + c.Name)); return cmd.ExecuteScalar();
}
var cmd = conn.Command("INSERT INTO " + tableName + " (" + columnNames + ")" + public static SqliteCommand Insert(this ISqliteConnection conn, string tableName, (string Name, SqliteType Type)[] columns) {
"VALUES (" + columnParams + ")"); string columnNames = string.Join(',', columns.Select(static c => c.Name));
string columnParams = string.Join(',', columns.Select(static c => ':' + c.Name));
CreateParameters(cmd, columns); var cmd = conn.Command("INSERT INTO " + tableName + " (" + columnNames + ")" +
return cmd; "VALUES (" + columnParams + ")");
}
public static SqliteCommand Upsert(this ISqliteConnection conn, string tableName, (string Name, SqliteType Type)[] columns) { CreateParameters(cmd, columns);
string columnNames = string.Join(',', columns.Select(static c => c.Name)); return cmd;
string columnParams = string.Join(',', columns.Select(static c => ':' + c.Name)); }
string columnUpdates = string.Join(',', columns.Skip(1).Select(static c => c.Name + " = excluded." + c.Name));
var cmd = conn.Command("INSERT INTO " + tableName + " (" + columnNames + ")" + public static SqliteCommand Upsert(this ISqliteConnection conn, string tableName, (string Name, SqliteType Type)[] columns) {
"VALUES (" + columnParams + ")" + string columnNames = string.Join(',', columns.Select(static c => c.Name));
"ON CONFLICT (" + columns[0].Name + ")" + string columnParams = string.Join(',', columns.Select(static c => ':' + c.Name));
"DO UPDATE SET " + columnUpdates); string columnUpdates = string.Join(',', columns.Skip(1).Select(static c => c.Name + " = excluded." + c.Name));
CreateParameters(cmd, columns); var cmd = conn.Command("INSERT INTO " + tableName + " (" + columnNames + ")" +
return cmd; "VALUES (" + columnParams + ")" +
} "ON CONFLICT (" + columns[0].Name + ")" +
"DO UPDATE SET " + columnUpdates);
public static SqliteCommand Delete(this ISqliteConnection conn, string tableName, (string Name, SqliteType Type) column) { CreateParameters(cmd, columns);
var cmd = conn.Command("DELETE FROM " + tableName + " WHERE " + column.Name + " = :" + column.Name); return cmd;
CreateParameters(cmd, new [] { column }); }
return cmd;
}
private static void CreateParameters(SqliteCommand cmd, (string Name, SqliteType Type)[] columns) { public static SqliteCommand Delete(this ISqliteConnection conn, string tableName, (string Name, SqliteType Type) column) {
foreach (var (name, type) in columns) { var cmd = conn.Command("DELETE FROM " + tableName + " WHERE " + column.Name + " = :" + column.Name);
cmd.Parameters.Add(":" + name, type); CreateParameters(cmd, new [] { column });
} return cmd;
} }
public static void AddAndSet(this SqliteCommand cmd, string key, SqliteType type, object? value) { private static void CreateParameters(SqliteCommand cmd, (string Name, SqliteType Type)[] columns) {
cmd.Parameters.Add(key, type).Value = value ?? DBNull.Value; foreach (var (name, type) in columns) {
} cmd.Parameters.Add(":" + name, type);
public static void Set(this SqliteCommand cmd, string key, object? value) {
cmd.Parameters[key].Value = value ?? DBNull.Value;
}
public static ulong GetUint64(this SqliteDataReader reader, int ordinal) {
return (ulong) reader.GetInt64(ordinal);
} }
} }
public static void AddAndSet(this SqliteCommand cmd, string key, SqliteType type, object? value) {
cmd.Parameters.Add(key, type).Value = value ?? DBNull.Value;
}
public static void Set(this SqliteCommand cmd, string key, object? value) {
cmd.Parameters[key].Value = value ?? DBNull.Value;
}
public static ulong GetUint64(this SqliteDataReader reader, int ordinal) {
return (ulong) reader.GetInt64(ordinal);
}
} }

View File

@ -1,31 +1,31 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace DHT.Server.Database.Sqlite.Utils { namespace DHT.Server.Database.Sqlite.Utils;
sealed class SqliteWhereGenerator {
private readonly string? tableAlias;
private readonly bool invert;
private readonly List<string> conditions = new ();
public SqliteWhereGenerator(string? tableAlias, bool invert) { sealed class SqliteWhereGenerator {
this.tableAlias = tableAlias; private readonly string? tableAlias;
this.invert = invert; private readonly bool invert;
private readonly List<string> conditions = new ();
public SqliteWhereGenerator(string? tableAlias, bool invert) {
this.tableAlias = tableAlias;
this.invert = invert;
}
public void AddCondition(string condition) {
conditions.Add(tableAlias == null ? condition : tableAlias + '.' + condition);
}
public string Generate() {
if (conditions.Count == 0) {
return "";
} }
public void AddCondition(string condition) { if (invert) {
conditions.Add(tableAlias == null ? condition : tableAlias + '.' + condition); return " WHERE NOT (" + string.Join(" AND ", conditions) + ")";
} }
else {
public string Generate() { return " WHERE " + string.Join(" AND ", conditions);
if (conditions.Count == 0) {
return "";
}
if (invert) {
return " WHERE NOT (" + string.Join(" AND ", conditions) + ")";
}
else {
return " WHERE " + string.Join(" AND ", conditions);
}
} }
} }
} }

Some files were not shown because too many files have changed in this diff Show More