mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-08-17 10:31:41 +02:00
Compare commits
28 Commits
v35.3
...
v36.1.beta
Author | SHA1 | Date | |
---|---|---|---|
c9bb46c8c7
|
|||
73f4c70325
|
|||
de5a8b690b
|
|||
daa2feb445
|
|||
4e94e788bc
|
|||
133ec532d2
|
|||
3d435d0165
|
|||
3e8151e1f3
|
|||
9f98eba9c1
|
|||
6b54a80be1
|
|||
1e6e5c6f92
|
|||
2459c8ee1a
|
|||
d129a60d1c
|
|||
65ecb0177c
|
|||
d51dcb0a84
|
|||
b13b85dedd
|
|||
15e8b9da63
|
|||
9572f0f002
|
|||
2f3b8b974c
|
|||
bff86b09c7
|
|||
5ca7cf09e8
|
|||
a1c93232d0
|
|||
db5f9d65db
|
|||
4cbf387e2a
|
|||
64cf3c9fbb
|
|||
a4ebd5eed6
|
|||
06716330d6
|
|||
1a6346677e
|
@@ -7,12 +7,15 @@
|
||||
<entry key="Desktop/Dialogs/CheckBox/CheckBoxDialog.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Dialogs/Message/MessageDialog.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Dialogs/Progress/ProgressDialog.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Dialogs/TextBox/TextBoxDialog.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/AboutWindow.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Controls/FilterPanel.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Controls/AttachmentFilterPanel.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Controls/MessageFilterPanel.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Controls/ServerConfigurationPanel.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Controls/StatusBar.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/MainWindow.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Pages/AdvancedPage.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Pages/AttachmentsPage.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Pages/DatabasePage.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Pages/DebugPage.axaml" value="Desktop/Desktop.csproj" />
|
||||
<entry key="Desktop/Main/Pages/TrackingPage.axaml" value="Desktop/Desktop.csproj" />
|
||||
|
@@ -7,6 +7,7 @@
|
||||
<Application.Styles>
|
||||
|
||||
<FluentTheme Mode="Light" />
|
||||
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Default.xaml" />
|
||||
|
||||
<Style Selector="Button, CheckBox, RadioButton, Expander /template/ ToggleButton#ExpanderHeader">
|
||||
<Setter Property="Cursor" Value="Hand" />
|
||||
@@ -35,6 +36,20 @@
|
||||
<Style Selector="TextBox:disabled"><!-- TODO bug in Avalonia (https://github.com/AvaloniaUI/Avalonia/pull/7792) -->
|
||||
<Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
|
||||
</Style>
|
||||
<Style Selector="TextBox:error DataValidationErrors">
|
||||
<Style.Resources>
|
||||
<ControlTemplate x:Key="InlineDataValidationContentTemplate" TargetType="DataValidationErrors">
|
||||
<ContentPresenter Name="PART_ContentPresenter"
|
||||
Padding="{TemplateBinding Padding}"
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
CornerRadius="{TemplateBinding CornerRadius}"
|
||||
Content="{TemplateBinding Content}"
|
||||
ContentTemplate="{TemplateBinding ContentTemplate}" />
|
||||
</ControlTemplate>
|
||||
</Style.Resources>
|
||||
<Setter Property="Template" Value="{StaticResource InlineDataValidationContentTemplate}" />
|
||||
</Style>
|
||||
|
||||
<Style Selector="Expander /template/ ToggleButton#ExpanderHeader">
|
||||
<Setter Property="HorizontalContentAlignment" Value="Left" />
|
||||
@@ -105,6 +120,7 @@
|
||||
<Application.Resources>
|
||||
|
||||
<common:NumberValueConverter x:Key="NumberValueConverter" />
|
||||
<common:BytesValueConverter x:Key="BytesValueConverter" />
|
||||
|
||||
<system:Double x:Key="ControlContentThemeFontSize">14</system:Double>
|
||||
<CornerRadius x:Key="ControlCornerRadius">0</CornerRadius>
|
||||
@@ -154,7 +170,7 @@
|
||||
<SolidColorBrush x:Key="TextControlPlaceholderForegroundDisabled" Color="#AAAAAA" />
|
||||
|
||||
<Thickness x:Key="ExpanderHeaderPadding">15,0</Thickness>
|
||||
<Thickness x:Key="ExpanderContentPadding">15</Thickness>
|
||||
<Thickness x:Key="ExpanderContentPadding">12</Thickness>
|
||||
<SolidColorBrush x:Key="ExpanderBorderBrush" Color="#697DAB" />
|
||||
<SolidColorBrush x:Key="ExpanderBackground" Color="#697DAB" />
|
||||
<SolidColorBrush x:Key="ExpanderForeground" Color="#FFFFFF" />
|
||||
|
45
app/Desktop/Common/BytesValueConverter.cs
Normal file
45
app/Desktop/Common/BytesValueConverter.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Avalonia.Data.Converters;
|
||||
|
||||
namespace DHT.Desktop.Common {
|
||||
sealed class BytesValueConverter : IValueConverter {
|
||||
private static readonly string[] Units = {
|
||||
"B",
|
||||
"kB",
|
||||
"MB",
|
||||
"GB",
|
||||
"TB"
|
||||
};
|
||||
|
||||
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;
|
||||
if (unit == 0) {
|
||||
return string.Format(Program.Culture, "{0:n0}", size) + " " + Units[unit];
|
||||
}
|
||||
else {
|
||||
double humanReadableSize = size / Math.Pow(Scale, unit);
|
||||
return string.Format(Program.Culture, "{0:n0}", humanReadableSize) + " " + Units[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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -21,15 +21,20 @@
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="0.10.13" />
|
||||
<PackageReference Include="Avalonia.Desktop" Version="0.10.13" />
|
||||
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.13" Condition=" '$(Configuration)' == 'Debug' " />
|
||||
<PackageReference Include="Avalonia" Version="0.10.14" />
|
||||
<PackageReference Include="Avalonia.Controls.DataGrid" Version="0.10.14" />
|
||||
<PackageReference Include="Avalonia.Desktop" Version="0.10.14" />
|
||||
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.14" Condition=" '$(Configuration)' == 'Debug' " />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Server\Server.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\Version.cs" Link="Version.cs" />
|
||||
<Compile Update="Dialogs\TextBox\TextBoxDialog.axaml.cs">
|
||||
<DependentUpon>CheckBoxDialog.axaml</DependentUpon>
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<AvaloniaResource Include="Resources/icon.ico" />
|
||||
|
@@ -34,8 +34,8 @@
|
||||
|
||||
<StackPanel Margin="20">
|
||||
<ScrollViewer MaxHeight="400">
|
||||
<ItemsControl Items="{Binding Items}">
|
||||
<ItemsControl.ItemTemplate>
|
||||
<ItemsRepeater Items="{Binding Items}">
|
||||
<ItemsRepeater.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<CheckBox IsChecked="{Binding Checked}">
|
||||
<Label>
|
||||
@@ -43,8 +43,8 @@
|
||||
</Label>
|
||||
</CheckBox>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
</ItemsRepeater.ItemTemplate>
|
||||
</ItemsRepeater>
|
||||
</ScrollViewer>
|
||||
<Panel Classes="buttons">
|
||||
<WrapPanel Classes="left">
|
||||
|
56
app/Desktop/Dialogs/TextBox/TextBoxDialog.axaml
Normal file
56
app/Desktop/Dialogs/TextBox/TextBoxDialog.axaml
Normal file
@@ -0,0 +1,56 @@
|
||||
<Window xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:namespace="clr-namespace:DHT.Desktop.Dialogs.TextBox"
|
||||
mc:Ignorable="d" d:DesignWidth="500"
|
||||
x:Class="DHT.Desktop.Dialogs.TextBox.TextBoxDialog"
|
||||
Title="{Binding Title}"
|
||||
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||
Width="500" SizeToContent="Height" CanResize="False"
|
||||
WindowStartupLocation="CenterOwner">
|
||||
|
||||
<Window.DataContext>
|
||||
<namespace:TextBoxDialogModel />
|
||||
</Window.DataContext>
|
||||
|
||||
<Window.Styles>
|
||||
<Style Selector="Panel.buttons">
|
||||
<Setter Property="Margin" Value="0 20 0 0" />
|
||||
</Style>
|
||||
<Style Selector="Panel.buttons > WrapPanel.right">
|
||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||
</Style>
|
||||
<Style Selector="Panel.buttons Button">
|
||||
<Setter Property="MinWidth" Value="80" />
|
||||
<Setter Property="Margin" Value="8 0 0 0" />
|
||||
</Style>
|
||||
</Window.Styles>
|
||||
|
||||
<StackPanel Margin="20">
|
||||
<ScrollViewer MaxHeight="400">
|
||||
<StackPanel Spacing="10">
|
||||
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
|
||||
<ItemsRepeater Items="{Binding Items}">
|
||||
<ItemsRepeater.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<DockPanel Margin="0 5 25 0">
|
||||
<TextBox Name="Input" Text="{Binding Value}" Width="180" VerticalAlignment="Top" DockPanel.Dock="Right" />
|
||||
<Label Target="Input" VerticalAlignment="Center" DockPanel.Dock="Left">
|
||||
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" />
|
||||
</Label>
|
||||
</DockPanel>
|
||||
</DataTemplate>
|
||||
</ItemsRepeater.ItemTemplate>
|
||||
</ItemsRepeater>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
<Panel Classes="buttons">
|
||||
<WrapPanel Classes="right">
|
||||
<Button Click="ClickOk" IsEnabled="{Binding !HasErrors}">OK</Button>
|
||||
<Button Click="ClickCancel">Cancel</Button>
|
||||
</WrapPanel>
|
||||
</Panel>
|
||||
</StackPanel>
|
||||
|
||||
</Window>
|
31
app/Desktop/Dialogs/TextBox/TextBoxDialog.axaml.cs
Normal file
31
app/Desktop/Dialogs/TextBox/TextBoxDialog.axaml.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using DHT.Desktop.Dialogs.Message;
|
||||
|
||||
namespace DHT.Desktop.Dialogs.TextBox {
|
||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||
public sealed class TextBoxDialog : Window {
|
||||
public TextBoxDialog() {
|
||||
InitializeComponent();
|
||||
#if DEBUG
|
||||
this.AttachDevTools();
|
||||
#endif
|
||||
}
|
||||
|
||||
private void InitializeComponent() {
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
}
|
||||
|
||||
public void ClickOk(object? sender, RoutedEventArgs e) {
|
||||
Close(DialogResult.OkCancel.Ok);
|
||||
}
|
||||
|
||||
public void ClickCancel(object? sender, RoutedEventArgs e) {
|
||||
Close(DialogResult.OkCancel.Cancel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
47
app/Desktop/Dialogs/TextBox/TextBoxDialogModel.cs
Normal file
47
app/Desktop/Dialogs/TextBox/TextBoxDialogModel.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using DHT.Utils.Models;
|
||||
|
||||
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>();
|
||||
|
||||
public IReadOnlyList<TextBoxItem> Items {
|
||||
get => items;
|
||||
|
||||
protected set {
|
||||
foreach (var item in items) {
|
||||
item.ErrorsChanged -= OnItemErrorsChanged;
|
||||
}
|
||||
|
||||
items = value;
|
||||
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
}
|
42
app/Desktop/Dialogs/TextBox/TextBoxItem.cs
Normal file
42
app/Desktop/Dialogs/TextBox/TextBoxItem.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using DHT.Utils.Models;
|
||||
|
||||
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;
|
||||
public bool IsValid => ValidityCheck(Value);
|
||||
|
||||
private string value = string.Empty;
|
||||
|
||||
public string Value {
|
||||
get => this.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 new T Item { get; }
|
||||
|
||||
public TextBoxItem(T item) {
|
||||
this.Item = item;
|
||||
base.Item = item;
|
||||
}
|
||||
}
|
||||
}
|
51
app/Desktop/Main/Controls/AttachmentFilterPanel.axaml
Normal file
51
app/Desktop/Main/Controls/AttachmentFilterPanel.axaml
Normal file
@@ -0,0 +1,51 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
||||
mc:Ignorable="d"
|
||||
x:Class="DHT.Desktop.Main.Controls.AttachmentFilterPanel">
|
||||
|
||||
<Design.DataContext>
|
||||
<controls:AttachmentFilterPanelModel />
|
||||
</Design.DataContext>
|
||||
|
||||
<UserControl.Styles>
|
||||
<Style Selector="WrapPanel > StackPanel">
|
||||
<Setter Property="Margin" Value="0 20 40 0" />
|
||||
<Setter Property="Spacing" Value="4" />
|
||||
</Style>
|
||||
<Style Selector="WrapPanel > StackPanel:nth-last-child(1)">
|
||||
<Setter Property="Margin" Value="0 20 0 0" />
|
||||
</Style>
|
||||
<Style Selector="Grid > Label">
|
||||
<Setter Property="VerticalAlignment" Value="Center" />
|
||||
</Style>
|
||||
<Style Selector="ComboBox">
|
||||
<Setter Property="Margin" Value="8 0 0 0" />
|
||||
</Style>
|
||||
<Style Selector="Button">
|
||||
<Setter Property="Margin" Value="0 0 0 8" />
|
||||
</Style>
|
||||
</UserControl.Styles>
|
||||
|
||||
<StackPanel>
|
||||
<TextBlock Text="{Binding FilterStatisticsText}" />
|
||||
<WrapPanel>
|
||||
<StackPanel>
|
||||
<CheckBox IsChecked="{Binding LimitSize}">Limit Size</CheckBox>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBox Text="{Binding MaximumSize}" IsEnabled="{Binding LimitSize}" HorizontalContentAlignment="Right" />
|
||||
<ComboBox IsEnabled="{Binding LimitSize}" Items="{Binding Units}" SelectedItem="{Binding MaximumSizeUnit}">
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Name}" />
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</WrapPanel>
|
||||
</StackPanel>
|
||||
|
||||
</UserControl>
|
17
app/Desktop/Main/Controls/AttachmentFilterPanel.axaml.cs
Normal file
17
app/Desktop/Main/Controls/AttachmentFilterPanel.axaml.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
|
||||
namespace DHT.Desktop.Main.Controls {
|
||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||
public sealed class AttachmentFilterPanel : UserControl {
|
||||
public AttachmentFilterPanel() {
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void InitializeComponent() {
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
125
app/Desktop/Main/Controls/AttachmentFilterPanelModel.cs
Normal file
125
app/Desktop/Main/Controls/AttachmentFilterPanelModel.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using DHT.Desktop.Common;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Server.Database;
|
||||
using DHT.Utils.Models;
|
||||
using DHT.Utils.Tasks;
|
||||
|
||||
namespace DHT.Desktop.Main.Controls {
|
||||
sealed class AttachmentFilterPanelModel : BaseModel, IDisposable {
|
||||
public sealed record Unit(string Name, int Scale);
|
||||
|
||||
private static readonly Unit[] AllUnits = {
|
||||
new ("B", 1),
|
||||
new ("kB", 1024),
|
||||
new ("MB", 1024 * 1024)
|
||||
};
|
||||
|
||||
private static readonly HashSet<string> FilterProperties = new () {
|
||||
nameof(LimitSize),
|
||||
nameof(MaximumSize),
|
||||
nameof(MaximumSizeUnit)
|
||||
};
|
||||
|
||||
public string FilterStatisticsText { get; private set; } = "";
|
||||
|
||||
private bool limitSize = false;
|
||||
private int maximumSize = 0;
|
||||
private Unit maximumSizeUnit = AllUnits[0];
|
||||
|
||||
public bool LimitSize {
|
||||
get => limitSize;
|
||||
set => Change(ref limitSize, value);
|
||||
}
|
||||
|
||||
public int MaximumSize {
|
||||
get => maximumSize;
|
||||
set => Change(ref maximumSize, value);
|
||||
}
|
||||
|
||||
public Unit MaximumSizeUnit {
|
||||
get => maximumSizeUnit;
|
||||
set => Change(ref maximumSizeUnit, value);
|
||||
}
|
||||
|
||||
public IEnumerable<Unit> Units => AllUnits;
|
||||
|
||||
private readonly IDatabaseFile db;
|
||||
private readonly string verb;
|
||||
|
||||
private readonly AsyncValueComputer<long> matchingAttachmentCountComputer;
|
||||
private long? matchingAttachmentCount;
|
||||
private long? totalAttachmentCount;
|
||||
|
||||
[Obsolete("Designer")]
|
||||
public AttachmentFilterPanelModel() : this(DummyDatabaseFile.Instance) {}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
filter.MaxBytes = maximumSize * maximumSizeUnit.Scale;
|
||||
}
|
||||
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,60 +0,0 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
||||
mc:Ignorable="d"
|
||||
x:Class="DHT.Desktop.Main.Controls.FilterPanel">
|
||||
|
||||
<Design.DataContext>
|
||||
<controls:FilterPanelModel />
|
||||
</Design.DataContext>
|
||||
|
||||
<UserControl.Styles>
|
||||
<Style Selector="WrapPanel > StackPanel">
|
||||
<Setter Property="Margin" Value="0 20 40 0" />
|
||||
<Setter Property="Spacing" Value="4" />
|
||||
</Style>
|
||||
<Style Selector="WrapPanel > StackPanel:nth-last-child(1)">
|
||||
<Setter Property="Margin" Value="0 20 0 0" />
|
||||
</Style>
|
||||
<Style Selector="Grid > Label">
|
||||
<Setter Property="VerticalAlignment" Value="Center" />
|
||||
</Style>
|
||||
<Style Selector="Grid > CalendarDatePicker">
|
||||
<Setter Property="CornerRadius" Value="0" />
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||
<Setter Property="HorizontalContentAlignment" Value="Center" />
|
||||
<Setter Property="VerticalAlignment" Value="Center" />
|
||||
<Setter Property="VerticalContentAlignment" Value="Center" />
|
||||
<Setter Property="IsTodayHighlighted" Value="True" />
|
||||
<Setter Property="SelectedDateFormat" Value="Short" />
|
||||
</Style>
|
||||
<Style Selector="Button">
|
||||
<Setter Property="Margin" Value="0 0 0 8" />
|
||||
</Style>
|
||||
</UserControl.Styles>
|
||||
|
||||
<WrapPanel>
|
||||
<StackPanel>
|
||||
<CheckBox IsChecked="{Binding FilterByDate}">Filter by Date</CheckBox>
|
||||
<Grid ColumnDefinitions="Auto, 4, 125" RowDefinitions="Auto, 4, Auto" Margin="4 0">
|
||||
<Label Grid.Row="0" Grid.Column="0">From:</Label>
|
||||
<CalendarDatePicker Grid.Row="0" Grid.Column="2" x:Name="StartDatePicker" IsEnabled="{Binding FilterByDate}" SelectedDateChanged="CalendarDatePicker_OnSelectedDateChanged" />
|
||||
<Label Grid.Row="2" Grid.Column="0">To:</Label>
|
||||
<CalendarDatePicker Grid.Row="2" Grid.Column="2" x:Name="EndDatePicker" IsEnabled="{Binding FilterByDate}" SelectedDateChanged="CalendarDatePicker_OnSelectedDateChanged" />
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
<StackPanel>
|
||||
<CheckBox IsChecked="{Binding FilterByChannel}">Filter by Channel</CheckBox>
|
||||
<Button Command="{Binding OpenChannelFilterDialog}" IsEnabled="{Binding FilterByChannel}">Select Channels...</Button>
|
||||
<TextBlock Text="{Binding ChannelFilterLabel}" />
|
||||
</StackPanel>
|
||||
<StackPanel>
|
||||
<CheckBox IsChecked="{Binding FilterByUser}">Filter by User</CheckBox>
|
||||
<Button Command="{Binding OpenUserFilterDialog}" IsEnabled="{Binding FilterByUser}">Select Users...</Button>
|
||||
<TextBlock Text="{Binding UserFilterLabel}" />
|
||||
</StackPanel>
|
||||
</WrapPanel>
|
||||
|
||||
</UserControl>
|
63
app/Desktop/Main/Controls/MessageFilterPanel.axaml
Normal file
63
app/Desktop/Main/Controls/MessageFilterPanel.axaml
Normal file
@@ -0,0 +1,63 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
||||
mc:Ignorable="d"
|
||||
x:Class="DHT.Desktop.Main.Controls.MessageFilterPanel">
|
||||
|
||||
<Design.DataContext>
|
||||
<controls:MessageFilterPanelModel />
|
||||
</Design.DataContext>
|
||||
|
||||
<UserControl.Styles>
|
||||
<Style Selector="WrapPanel > StackPanel">
|
||||
<Setter Property="Margin" Value="0 20 40 0" />
|
||||
<Setter Property="Spacing" Value="4" />
|
||||
</Style>
|
||||
<Style Selector="WrapPanel > StackPanel:nth-last-child(1)">
|
||||
<Setter Property="Margin" Value="0 20 0 0" />
|
||||
</Style>
|
||||
<Style Selector="Grid > Label">
|
||||
<Setter Property="VerticalAlignment" Value="Center" />
|
||||
</Style>
|
||||
<Style Selector="Grid > CalendarDatePicker">
|
||||
<Setter Property="CornerRadius" Value="0" />
|
||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||
<Setter Property="HorizontalContentAlignment" Value="Center" />
|
||||
<Setter Property="VerticalAlignment" Value="Center" />
|
||||
<Setter Property="VerticalContentAlignment" Value="Center" />
|
||||
<Setter Property="IsTodayHighlighted" Value="True" />
|
||||
<Setter Property="SelectedDateFormat" Value="Short" />
|
||||
</Style>
|
||||
<Style Selector="Button">
|
||||
<Setter Property="Margin" Value="0 0 0 8" />
|
||||
</Style>
|
||||
</UserControl.Styles>
|
||||
|
||||
<StackPanel>
|
||||
<TextBlock Text="{Binding FilterStatisticsText}" />
|
||||
<WrapPanel>
|
||||
<StackPanel>
|
||||
<CheckBox IsChecked="{Binding FilterByDate}">Filter by Date</CheckBox>
|
||||
<Grid ColumnDefinitions="Auto, 4, 125" RowDefinitions="Auto, 4, Auto" Margin="4 0">
|
||||
<Label Grid.Row="0" Grid.Column="0">From:</Label>
|
||||
<CalendarDatePicker Grid.Row="0" Grid.Column="2" x:Name="StartDatePicker" IsEnabled="{Binding FilterByDate}" SelectedDateChanged="CalendarDatePicker_OnSelectedDateChanged" />
|
||||
<Label Grid.Row="2" Grid.Column="0">To:</Label>
|
||||
<CalendarDatePicker Grid.Row="2" Grid.Column="2" x:Name="EndDatePicker" IsEnabled="{Binding FilterByDate}" SelectedDateChanged="CalendarDatePicker_OnSelectedDateChanged" />
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
<StackPanel>
|
||||
<CheckBox IsChecked="{Binding FilterByChannel}">Filter by Channel</CheckBox>
|
||||
<Button Command="{Binding OpenChannelFilterDialog}" IsEnabled="{Binding FilterByChannel}">Select Channels...</Button>
|
||||
<TextBlock Text="{Binding ChannelFilterLabel}" />
|
||||
</StackPanel>
|
||||
<StackPanel>
|
||||
<CheckBox IsChecked="{Binding FilterByUser}">Filter by User</CheckBox>
|
||||
<Button Command="{Binding OpenUserFilterDialog}" IsEnabled="{Binding FilterByUser}">Select Users...</Button>
|
||||
<TextBlock Text="{Binding UserFilterLabel}" />
|
||||
</StackPanel>
|
||||
</WrapPanel>
|
||||
</StackPanel>
|
||||
|
||||
</UserControl>
|
@@ -4,11 +4,11 @@ using Avalonia.Markup.Xaml;
|
||||
|
||||
namespace DHT.Desktop.Main.Controls {
|
||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||
public sealed class FilterPanel : UserControl {
|
||||
public sealed class MessageFilterPanel : UserControl {
|
||||
private CalendarDatePicker StartDatePicker => this.FindControl<CalendarDatePicker>("StartDatePicker");
|
||||
private CalendarDatePicker EndDatePicker => this.FindControl<CalendarDatePicker>("EndDatePicker");
|
||||
|
||||
public FilterPanel() {
|
||||
public MessageFilterPanel() {
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace DHT.Desktop.Main.Controls {
|
||||
}
|
||||
|
||||
public void CalendarDatePicker_OnSelectedDateChanged(object? sender, SelectionChangedEventArgs e) {
|
||||
if (DataContext is FilterPanelModel model) {
|
||||
if (DataContext is MessageFilterPanelModel model) {
|
||||
model.StartDate = StartDatePicker.SelectedDate;
|
||||
model.EndDate = EndDatePicker.SelectedDate;
|
||||
}
|
@@ -12,9 +12,10 @@ using DHT.Server.Data;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Server.Database;
|
||||
using DHT.Utils.Models;
|
||||
using DHT.Utils.Tasks;
|
||||
|
||||
namespace DHT.Desktop.Main.Controls {
|
||||
sealed class FilterPanelModel : BaseModel, IDisposable {
|
||||
sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
||||
private static readonly HashSet<string> FilterProperties = new () {
|
||||
nameof(FilterByDate),
|
||||
nameof(StartDate),
|
||||
@@ -25,6 +26,8 @@ namespace DHT.Desktop.Main.Controls {
|
||||
nameof(IncludedUsers)
|
||||
};
|
||||
|
||||
public string FilterStatisticsText { get; private set; } = "";
|
||||
|
||||
public event PropertyChangedEventHandler? FilterPropertyChanged;
|
||||
|
||||
public bool HasAnyFilters => FilterByDate || FilterByChannel || FilterByUser;
|
||||
@@ -88,14 +91,23 @@ namespace DHT.Desktop.Main.Controls {
|
||||
|
||||
private readonly Window window;
|
||||
private readonly IDatabaseFile db;
|
||||
private readonly string verb;
|
||||
|
||||
private readonly AsyncValueComputer<long> exportedMessageCountComputer;
|
||||
private long? exportedMessageCount;
|
||||
private long? totalMessageCount;
|
||||
|
||||
[Obsolete("Designer")]
|
||||
public FilterPanelModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||
public MessageFilterPanelModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||
|
||||
public FilterPanelModel(Window window, IDatabaseFile db) {
|
||||
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();
|
||||
|
||||
@@ -109,6 +121,7 @@ namespace DHT.Desktop.Main.Controls {
|
||||
|
||||
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
||||
if (e.PropertyName != null && FilterProperties.Contains(e.PropertyName)) {
|
||||
UpdateFilterStatistics();
|
||||
FilterPropertyChanged?.Invoke(sender, e);
|
||||
}
|
||||
|
||||
@@ -121,7 +134,11 @@ namespace DHT.Desktop.Main.Controls {
|
||||
}
|
||||
|
||||
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
||||
if (e.PropertyName == nameof(DatabaseStatistics.TotalChannels)) {
|
||||
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)) {
|
||||
@@ -129,6 +146,33 @@ namespace DHT.Desktop.Main.Controls {
|
||||
}
|
||||
}
|
||||
|
||||
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 0 ? "." : "s.");
|
||||
OnPropertyChanged(nameof(FilterStatisticsText));
|
||||
}
|
||||
|
||||
public async void OpenChannelFilterDialog() {
|
||||
var servers = db.GetAllServers().ToDictionary(static server => server.Id);
|
||||
var items = new List<CheckBoxItem<ulong>>();
|
@@ -24,7 +24,7 @@
|
||||
<StackPanel>
|
||||
<Button Command="{Binding OnClickToggleServerButton}" Content="{Binding ToggleServerButtonText}" IsEnabled="{Binding IsToggleServerButtonEnabled}" />
|
||||
<TextBlock TextWrapping="Wrap" Margin="0 15">
|
||||
The following settings determine how the tracking script communicates with this application. If you change them, you will have to copy and apply the tracking script again.
|
||||
The following settings determine how the tracking script communicates with this application. If you change them, you will have to copy/paste the tracking script again.
|
||||
</TextBlock>
|
||||
<WrapPanel>
|
||||
<StackPanel>
|
||||
|
@@ -8,7 +8,7 @@
|
||||
Title="{Binding Title}"
|
||||
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||
Width="800" Height="500"
|
||||
MinWidth="500" MinHeight="275"
|
||||
MinWidth="520" MinHeight="300"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Closed="OnClosed">
|
||||
|
||||
|
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using DHT.Desktop.Main.Pages;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace DHT.Desktop.Main {
|
||||
@@ -30,6 +32,14 @@ namespace DHT.Desktop.Main {
|
||||
if (DataContext is IDisposable disposable) {
|
||||
disposable.Dispose();
|
||||
}
|
||||
|
||||
foreach (var temporaryFile in ViewerPageModel.TemporaryFiles) {
|
||||
try {
|
||||
File.Delete(temporaryFile);
|
||||
} catch (Exception) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
54
app/Desktop/Main/Pages/AttachmentsPage.axaml
Normal file
54
app/Desktop/Main/Pages/AttachmentsPage.axaml
Normal file
@@ -0,0 +1,54 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages"
|
||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="DHT.Desktop.Main.Pages.AttachmentsPage">
|
||||
|
||||
<Design.DataContext>
|
||||
<pages:AttachmentsPageModel />
|
||||
</Design.DataContext>
|
||||
|
||||
<UserControl.Styles>
|
||||
<Style Selector="Expander">
|
||||
<Setter Property="Margin" Value="0 5 0 0" />
|
||||
</Style>
|
||||
<Style Selector="DataGridColumnHeader">
|
||||
<Setter Property="FontWeight" Value="Medium" />
|
||||
</Style>
|
||||
<Style Selector="DataGridColumnHeader:nth-child(2)">
|
||||
<Setter Property="HorizontalContentAlignment" Value="Right" />
|
||||
</Style>
|
||||
<Style Selector="DataGridColumnHeader:nth-child(3)">
|
||||
<Setter Property="HorizontalContentAlignment" Value="Right" />
|
||||
</Style>
|
||||
<Style Selector="DataGridCell.right">
|
||||
<Setter Property="HorizontalContentAlignment" Value="Right" />
|
||||
</Style>
|
||||
</UserControl.Styles>
|
||||
|
||||
<StackPanel Orientation="Vertical" Spacing="20">
|
||||
<DockPanel>
|
||||
<Button Command="{Binding OnClickToggleDownload}" Content="{Binding ToggleDownloadButtonText}" IsEnabled="{Binding IsToggleDownloadButtonEnabled}" DockPanel.Dock="Left" />
|
||||
<TextBlock Text="{Binding DownloadMessage}" Margin="10 0 0 0" VerticalAlignment="Center" DockPanel.Dock="Left" />
|
||||
<ProgressBar Value="{Binding DownloadProgress}" IsVisible="{Binding IsDownloading}" Margin="15 0" VerticalAlignment="Center" DockPanel.Dock="Right" />
|
||||
</DockPanel>
|
||||
<controls:AttachmentFilterPanel DataContext="{Binding FilterModel}" IsEnabled="{Binding !DataContext.IsDownloading, RelativeSource={RelativeSource AncestorType=UserControl}}" />
|
||||
<StackPanel Orientation="Vertical" Spacing="12">
|
||||
<Expander Header="Download Status" IsExpanded="True">
|
||||
<DataGrid Items="{Binding StatisticsRows}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="True">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="State" Binding="{Binding State}" Width="*" />
|
||||
<DataGridTextColumn Header="Attachments" Binding="{Binding Items, Converter={StaticResource NumberValueConverter}}" Width="*" CellStyleClasses="right" />
|
||||
<DataGridTextColumn Header="Size" Binding="{Binding Size, Converter={StaticResource BytesValueConverter}}" Width="*" CellStyleClasses="right" />
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</Expander>
|
||||
<StackPanel Orientation="Horizontal" Spacing="10">
|
||||
<Button Command="{Binding OnClickRetryFailedDownloads}" IsEnabled="{Binding HasFailedDownloads}">Retry Failed Downloads</Button>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</UserControl>
|
16
app/Desktop/Main/Pages/AttachmentsPage.axaml.cs
Normal file
16
app/Desktop/Main/Pages/AttachmentsPage.axaml.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
|
||||
namespace DHT.Desktop.Main.Pages {
|
||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||
public sealed class AttachmentsPage : UserControl {
|
||||
public AttachmentsPage() {
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void InitializeComponent() {
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
}
|
||||
}
|
||||
}
|
204
app/Desktop/Main/Pages/AttachmentsPageModel.cs
Normal file
204
app/Desktop/Main/Pages/AttachmentsPageModel.cs
Normal file
@@ -0,0 +1,204 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using DHT.Desktop.Common;
|
||||
using DHT.Desktop.Main.Controls;
|
||||
using DHT.Server.Data;
|
||||
using DHT.Server.Data.Aggregations;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Server.Database;
|
||||
using DHT.Server.Download;
|
||||
using DHT.Utils.Models;
|
||||
using DHT.Utils.Tasks;
|
||||
|
||||
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;
|
||||
|
||||
public string ToggleDownloadButtonText => downloadThread == null ? "Start Downloading" : "Stop Downloading";
|
||||
|
||||
public bool IsToggleDownloadButtonEnabled {
|
||||
get => isThreadDownloadButtonEnabled;
|
||||
set => Change(ref isThreadDownloadButtonEnabled, value);
|
||||
}
|
||||
|
||||
public string DownloadMessage { get; set; } = "";
|
||||
public double DownloadProgress => allItemsCount is null or 0 ? 0.0 : 100.0 * doneItemsCount / allItemsCount.Value;
|
||||
|
||||
public AttachmentFilterPanelModel FilterModel { get; }
|
||||
|
||||
private readonly StatisticsRow statisticsEnqueued = new ("Enqueued");
|
||||
private readonly StatisticsRow statisticsDownloaded = new ("Downloaded");
|
||||
private readonly StatisticsRow statisticsFailed = new ("Failed");
|
||||
private readonly StatisticsRow statisticsSkipped = new ("Skipped");
|
||||
|
||||
public List<StatisticsRow> StatisticsRows {
|
||||
get {
|
||||
return new List<StatisticsRow> {
|
||||
statisticsEnqueued,
|
||||
statisticsDownloaded,
|
||||
statisticsFailed,
|
||||
statisticsSkipped
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
downloadStatisticsComputer.Recompute();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -24,6 +24,7 @@
|
||||
<WrapPanel>
|
||||
<Button Command="{Binding OpenDatabaseFolder}">Open Database Folder</Button>
|
||||
<Button Command="{Binding MergeWithDatabase}">Merge with Database(s)...</Button>
|
||||
<Button Command="{Binding ImportLegacyArchive}">Import Legacy Archive(s)...</Button>
|
||||
</WrapPanel>
|
||||
</StackPanel>
|
||||
|
||||
|
@@ -1,13 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Threading;
|
||||
using DHT.Desktop.Common;
|
||||
using DHT.Desktop.Dialogs.Message;
|
||||
using DHT.Desktop.Dialogs.Progress;
|
||||
using DHT.Desktop.Dialogs.TextBox;
|
||||
using DHT.Server.Data;
|
||||
using DHT.Server.Database;
|
||||
using DHT.Server.Database.Import;
|
||||
using DHT.Utils.Logging;
|
||||
using DHT.Utils.Models;
|
||||
|
||||
@@ -56,6 +63,10 @@ namespace DHT.Desktop.Main.Pages {
|
||||
}
|
||||
}
|
||||
|
||||
public void CloseDatabase() {
|
||||
DatabaseClosed?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public async void MergeWithDatabase() {
|
||||
var fileDialog = DatabaseGui.NewOpenDatabaseFileDialog();
|
||||
fileDialog.Directory = Path.GetDirectoryName(Db.Path);
|
||||
@@ -74,10 +85,6 @@ namespace DHT.Desktop.Main.Pages {
|
||||
await progressDialog.ShowDialog(window);
|
||||
}
|
||||
|
||||
public void CloseDatabase() {
|
||||
DatabaseClosed?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private static async Task MergeWithDatabaseFromPaths(IDatabaseFile target, string[] paths, ProgressDialog dialog, IProgressCallback callback) {
|
||||
int total = paths.Length;
|
||||
|
||||
@@ -91,8 +98,95 @@ namespace DHT.Desktop.Main.Pages {
|
||||
return DialogResult.YesNo.Yes == upgradeResult;
|
||||
}
|
||||
|
||||
var oldStatistics = target.Statistics.Clone();
|
||||
var oldMessageCount = target.CountMessages();
|
||||
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;
|
||||
@@ -102,56 +196,53 @@ namespace DHT.Desktop.Main.Pages {
|
||||
++finished;
|
||||
|
||||
if (!File.Exists(path)) {
|
||||
await Dialog.ShowOk(dialog, "Database Error", "Database '" + Path.GetFileName(path) + "' no longer exists.");
|
||||
continue;
|
||||
}
|
||||
|
||||
IDatabaseFile? db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, dialog, CheckCanUpgradeDatabase);
|
||||
if (db == null) {
|
||||
await Dialog.ShowOk(dialog, errorDialogTitle, "File '" + Path.GetFileName(path) + "' no longer exists.");
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
target.AddFrom(db);
|
||||
if (await performImport(path)) {
|
||||
++successful;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Log.Error(ex);
|
||||
await Dialog.ShowOk(dialog, "Database Error", "Database '" + Path.GetFileName(path) + "' could not be merged: " + ex.Message);
|
||||
continue;
|
||||
} finally {
|
||||
db.Dispose();
|
||||
await Dialog.ShowOk(dialog, errorDialogTitle, "File '" + Path.GetFileName(path) + "' could not be imported: " + ex.Message);
|
||||
}
|
||||
|
||||
++successful;
|
||||
}
|
||||
|
||||
await callback.Update("Done", finished, total);
|
||||
|
||||
if (successful == 0) {
|
||||
await Dialog.ShowOk(dialog, "Database Merge", "Nothing was merged.");
|
||||
await Dialog.ShowOk(dialog, neutralDialogTitle, "Nothing was imported.");
|
||||
return;
|
||||
}
|
||||
|
||||
var newStatistics = target.Statistics;
|
||||
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 newMessages = target.CountMessages() - oldMessageCount;
|
||||
long newUsers = newStatistics.TotalUsers - oldStatistics.TotalUsers;
|
||||
long newMessages = newStatistics.TotalMessages - oldStatistics.TotalMessages;
|
||||
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.Append("Processed ");
|
||||
|
||||
if (successful == total) {
|
||||
message.Append(successful.Pluralize("database file"));
|
||||
if (successfulItems == totalItems) {
|
||||
message.Append(successfulItems.Pluralize(itemName));
|
||||
}
|
||||
else {
|
||||
message.Append(successful.Format()).Append(" out of ").Append(total.Pluralize("database file"));
|
||||
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"));
|
||||
|
||||
await Dialog.ShowOk(dialog, "Database Merge", message.ToString());
|
||||
return message.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,9 +5,7 @@
|
||||
xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages"
|
||||
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="DHT.Desktop.Main.Pages.ViewerPage"
|
||||
AttachedToVisualTree="OnAttachedToVisualTree"
|
||||
DetachedFromVisualTree="OnDetachedFromVisualTree">
|
||||
x:Class="DHT.Desktop.Main.Pages.ViewerPage">
|
||||
|
||||
<Design.DataContext>
|
||||
<pages:ViewerPageModel />
|
||||
@@ -15,17 +13,16 @@
|
||||
|
||||
<UserControl.Styles>
|
||||
<Style Selector="Expander">
|
||||
<Setter Property="Margin" Value="0 25 0 0" />
|
||||
<Setter Property="Margin" Value="0 5 0 0" />
|
||||
</Style>
|
||||
</UserControl.Styles>
|
||||
|
||||
<StackPanel Orientation="Vertical">
|
||||
<StackPanel Orientation="Vertical" Spacing="20">
|
||||
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
|
||||
<Button Command="{Binding OnClickOpenViewer}" Margin="0 0 5 0">Open Viewer</Button>
|
||||
<Button Command="{Binding OnClickSaveViewer}" Margin="5 0 0 0">Save Viewer</Button>
|
||||
</StackPanel>
|
||||
<TextBlock Text="{Binding ExportedMessageText}" Margin="0 20 0 0" />
|
||||
<controls:FilterPanel DataContext="{Binding FilterModel}" />
|
||||
<controls:MessageFilterPanel DataContext="{Binding FilterModel}" />
|
||||
<Expander Header="Database Tools">
|
||||
<StackPanel Orientation="Vertical" Spacing="10">
|
||||
<StackPanel Orientation="Vertical" Spacing="4">
|
||||
|
@@ -1,5 +1,4 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
|
||||
@@ -13,17 +12,5 @@ namespace DHT.Desktop.Main.Pages {
|
||||
private void InitializeComponent() {
|
||||
AvaloniaXamlLoader.Load(this);
|
||||
}
|
||||
|
||||
public void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) {
|
||||
if (DataContext is ViewerPageModel model) {
|
||||
model.SetPageVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) {
|
||||
if (DataContext is ViewerPageModel model) {
|
||||
model.SetPageVisible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Avalonia.Controls;
|
||||
@@ -17,8 +19,8 @@ using static DHT.Desktop.Program;
|
||||
|
||||
namespace DHT.Desktop.Main.Pages {
|
||||
sealed class ViewerPageModel : BaseModel, IDisposable {
|
||||
public string ExportedMessageText { get; private set; } = "";
|
||||
|
||||
public static readonly ConcurrentBag<string> TemporaryFiles = new ();
|
||||
|
||||
public bool DatabaseToolFilterModeKeep { get; set; } = true;
|
||||
public bool DatabaseToolFilterModeRemove { get; set; } = false;
|
||||
|
||||
@@ -29,13 +31,11 @@ namespace DHT.Desktop.Main.Pages {
|
||||
set => Change(ref hasFilters, value);
|
||||
}
|
||||
|
||||
private FilterPanelModel FilterModel { get; }
|
||||
private MessageFilterPanelModel FilterModel { get; }
|
||||
|
||||
private readonly Window window;
|
||||
private readonly IDatabaseFile db;
|
||||
|
||||
private bool isPageVisible = false;
|
||||
|
||||
[Obsolete("Designer")]
|
||||
public ViewerPageModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||
|
||||
@@ -43,51 +43,53 @@ namespace DHT.Desktop.Main.Pages {
|
||||
this.window = window;
|
||||
this.db = db;
|
||||
|
||||
FilterModel = new FilterPanelModel(window, db);
|
||||
FilterModel = new MessageFilterPanelModel(window, db, "Will export");
|
||||
FilterModel.FilterPropertyChanged += OnFilterPropertyChanged;
|
||||
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
||||
FilterModel.Dispose();
|
||||
}
|
||||
|
||||
public void SetPageVisible(bool isPageVisible) {
|
||||
this.isPageVisible = isPageVisible;
|
||||
if (isPageVisible) {
|
||||
UpdateStatistics();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFilterPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
||||
UpdateStatistics();
|
||||
HasFilters = FilterModel.HasAnyFilters;
|
||||
}
|
||||
|
||||
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
||||
if (isPageVisible && e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) {
|
||||
UpdateStatistics();
|
||||
private async Task WriteViewerFile(string path) {
|
||||
const string ArchiveTag = "/*[ARCHIVE]*/";
|
||||
|
||||
string indexFile = await Resources.ReadTextAsync("Viewer/index.html");
|
||||
string viewerTemplate = indexFile.Replace("/*[JS]*/", await Resources.ReadJoinedAsync("Viewer/scripts/", '\n'))
|
||||
.Replace("/*[CSS]*/", await Resources.ReadJoinedAsync("Viewer/styles/", '\n'));
|
||||
|
||||
int viewerArchiveTagStart = viewerTemplate.IndexOf(ArchiveTag);
|
||||
int viewerArchiveTagEnd = viewerArchiveTagStart + ArchiveTag.Length;
|
||||
|
||||
string jsonTempFile = path + ".tmp";
|
||||
|
||||
await using (var jsonStream = new FileStream(jsonTempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) {
|
||||
await ViewerJsonExport.Generate(jsonStream, db, FilterModel.CreateFilter());
|
||||
|
||||
char[] jsonBuffer = new char[Math.Min(32768, jsonStream.Position)];
|
||||
jsonStream.Position = 0;
|
||||
|
||||
await using (var outputStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
|
||||
await using (var outputWriter = new StreamWriter(outputStream, Encoding.UTF8)) {
|
||||
await outputWriter.WriteAsync(viewerTemplate[..viewerArchiveTagStart]);
|
||||
|
||||
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);
|
||||
await outputWriter.WriteAsync(HttpUtility.JavaScriptStringEncode(jsonChunk));
|
||||
}
|
||||
}
|
||||
|
||||
await outputWriter.WriteAsync(viewerTemplate[viewerArchiveTagEnd..]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateStatistics() {
|
||||
var filter = FilterModel.CreateFilter();
|
||||
var allMessagesCount = db.Statistics.TotalMessages?.Format() ?? "?";
|
||||
var filteredMessagesCount = filter.IsEmpty ? allMessagesCount : db.CountMessages(filter).Format();
|
||||
|
||||
ExportedMessageText = "Will export " + filteredMessagesCount + " out of " + allMessagesCount + " message(s).";
|
||||
OnPropertyChanged(nameof(ExportedMessageText));
|
||||
}
|
||||
|
||||
private async Task<string> GenerateViewerContents() {
|
||||
string json = ViewerJsonExport.Generate(db, FilterModel.CreateFilter());
|
||||
|
||||
string index = await Resources.ReadTextAsync("Viewer/index.html");
|
||||
string viewer = index.Replace("/*[JS]*/", await Resources.ReadJoinedAsync("Viewer/scripts/", '\n'))
|
||||
.Replace("/*[CSS]*/", await Resources.ReadJoinedAsync("Viewer/styles/", '\n'))
|
||||
.Replace("/*[ARCHIVE]*/", HttpUtility.JavaScriptStringEncode(json));
|
||||
return viewer;
|
||||
File.Delete(jsonTempFile);
|
||||
}
|
||||
|
||||
public async void OnClickOpenViewer() {
|
||||
@@ -101,8 +103,10 @@ namespace DHT.Desktop.Main.Pages {
|
||||
fullPath = Path.Combine(rootPath, filenameBase + "-" + counter + ".html");
|
||||
}
|
||||
|
||||
TemporaryFiles.Add(fullPath);
|
||||
|
||||
Directory.CreateDirectory(rootPath);
|
||||
await File.WriteAllTextAsync(fullPath, await GenerateViewerContents());
|
||||
await WriteViewerFile(fullPath);
|
||||
|
||||
Process.Start(new ProcessStartInfo(fullPath) { UseShellExecute = true });
|
||||
}
|
||||
@@ -110,7 +114,7 @@ namespace DHT.Desktop.Main.Pages {
|
||||
public async void OnClickSaveViewer() {
|
||||
var dialog = new SaveFileDialog {
|
||||
Title = "Save Viewer",
|
||||
InitialFileName = "archive.html",
|
||||
InitialFileName = Path.GetFileNameWithoutExtension(db.Path) + ".html",
|
||||
Directory = Path.GetDirectoryName(db.Path),
|
||||
Filters = new List<FileDialogFilter> {
|
||||
new() {
|
||||
@@ -122,7 +126,7 @@ namespace DHT.Desktop.Main.Pages {
|
||||
|
||||
string? path = await dialog;
|
||||
if (!string.IsNullOrEmpty(path)) {
|
||||
await File.WriteAllTextAsync(path, await GenerateViewerContents());
|
||||
await WriteViewerFile(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,12 +135,12 @@ namespace DHT.Desktop.Main.Pages {
|
||||
|
||||
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, MessageFilterRemovalMode.KeepMatching);
|
||||
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?")) {
|
||||
db.RemoveMessages(filter, MessageFilterRemovalMode.RemoveMatching);
|
||||
db.RemoveMessages(filter, FilterRemovalMode.RemoveMatching);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -73,14 +73,14 @@
|
||||
<DockPanel>
|
||||
<Border Classes="statusBar" DockPanel.Dock="Bottom">
|
||||
<DockPanel>
|
||||
<TextBlock Classes="invisibleTabItem" DockPanel.Dock="Left">Advanced</TextBlock>
|
||||
<TextBlock Classes="invisibleTabItem" DockPanel.Dock="Left">Attachments</TextBlock>
|
||||
<controls:StatusBar DataContext="{Binding StatusBarModel}" DockPanel.Dock="Right" />
|
||||
</DockPanel>
|
||||
</Border>
|
||||
<TabControl x:Name="TabControl" TabStripPlacement="Left" DockPanel.Dock="Top">
|
||||
<TabControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<Grid ColumnDefinitions="Auto" RowDefinitions="Auto,Auto,Auto,*,Auto,Auto" />
|
||||
<Grid ColumnDefinitions="Auto" RowDefinitions="Auto,Auto,Auto,Auto,*,Auto,Auto" />
|
||||
</ItemsPanelTemplate>
|
||||
</TabControl.ItemsPanel>
|
||||
<TabItem x:Name="TabDatabase" Header="Database" Classes="first" Grid.Row="0">
|
||||
@@ -93,17 +93,22 @@
|
||||
<ContentPresenter Content="{Binding TrackingPage}" Classes="page" />
|
||||
</ScrollViewer>
|
||||
</TabItem>
|
||||
<TabItem x:Name="TabViewer" Header="Viewer" Grid.Row="2">
|
||||
<TabItem x:Name="TabAttachments" Header="Attachments" Grid.Row="2">
|
||||
<ScrollViewer>
|
||||
<ContentPresenter Content="{Binding AttachmentsPage}" Classes="page" />
|
||||
</ScrollViewer>
|
||||
</TabItem>
|
||||
<TabItem x:Name="TabViewer" Header="Viewer" Grid.Row="3">
|
||||
<ScrollViewer>
|
||||
<ContentPresenter Content="{Binding ViewerPage}" Classes="page" />
|
||||
</ScrollViewer>
|
||||
</TabItem>
|
||||
<TabItem x:Name="TabAdvanced" Header="Advanced" Grid.Row="4">
|
||||
<TabItem x:Name="TabAdvanced" Header="Advanced" Grid.Row="5">
|
||||
<ScrollViewer>
|
||||
<ContentPresenter Content="{Binding AdvancedPage}" Classes="page" />
|
||||
</ScrollViewer>
|
||||
</TabItem>
|
||||
<TabItem x:Name="TabDebug" Header="Debug" Grid.Row="5" IsVisible="{Binding HasDebugPage}">
|
||||
<TabItem x:Name="TabDebug" Header="Debug" Grid.Row="6" IsVisible="{Binding HasDebugPage}">
|
||||
<ScrollViewer>
|
||||
<ContentPresenter Content="{Binding DebugPage}" Classes="page" />
|
||||
</ScrollViewer>
|
||||
|
@@ -19,6 +19,9 @@ namespace DHT.Desktop.Main.Screens {
|
||||
public TrackingPage TrackingPage { get; }
|
||||
private TrackingPageModel TrackingPageModel { get; }
|
||||
|
||||
public AttachmentsPage AttachmentsPage { get; }
|
||||
private AttachmentsPageModel AttachmentsPageModel { get; }
|
||||
|
||||
public ViewerPage ViewerPage { get; }
|
||||
private ViewerPageModel ViewerPageModel { get; }
|
||||
|
||||
@@ -63,6 +66,9 @@ namespace DHT.Desktop.Main.Screens {
|
||||
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 };
|
||||
|
||||
@@ -92,6 +98,7 @@ namespace DHT.Desktop.Main.Screens {
|
||||
|
||||
public void Dispose() {
|
||||
ServerLauncher.ServerManagementExceptionCaught -= ServerLauncherOnServerManagementExceptionCaught;
|
||||
AttachmentsPageModel.Dispose();
|
||||
ViewerPageModel.Dispose();
|
||||
serverManager.Dispose();
|
||||
}
|
||||
|
@@ -2,6 +2,10 @@
|
||||
margin-bottom: 48px !important;
|
||||
}
|
||||
|
||||
#app-mount div[class*="app-"] > div[class*="app-"] {
|
||||
margin-bottom: 0 !important;
|
||||
}
|
||||
|
||||
#dht-ctrl {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
|
15
app/Server/Data/Aggregations/DownloadStatusStatistics.cs
Normal file
15
app/Server/Data/Aggregations/DownloadStatusStatistics.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
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 ulong SuccessfulSize { get; internal set; }
|
||||
|
||||
public int FailedCount { get; internal set; }
|
||||
public ulong FailedSize { get; internal set; }
|
||||
|
||||
public int SkippedCount { get; internal set; }
|
||||
public ulong SkippedSize { get; internal set; }
|
||||
}
|
||||
}
|
30
app/Server/Data/Download.cs
Normal file
30
app/Server/Data/Download.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
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) {
|
||||
return new Download(url, statusCode.HasValue ? (DownloadStatus) (int) statusCode : DownloadStatus.GenericError, size);
|
||||
}
|
||||
|
||||
public string Url { get; }
|
||||
public DownloadStatus Status { get; }
|
||||
public ulong Size { get; }
|
||||
public byte[]? Data { get; }
|
||||
|
||||
internal Download(string url, DownloadStatus status, ulong size, byte[]? data = null) {
|
||||
Url = url;
|
||||
Status = status;
|
||||
Size = size;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
internal Download WithData(byte[] data) {
|
||||
return new Download(Url, Status, Size, data);
|
||||
}
|
||||
}
|
||||
}
|
12
app/Server/Data/DownloadStatus.cs
Normal file
12
app/Server/Data/DownloadStatus.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Net;
|
||||
|
||||
namespace DHT.Server.Data {
|
||||
/// <summary>
|
||||
/// Extends <see cref="HttpStatusCode"/> with custom status codes in the range 0-99.
|
||||
/// </summary>
|
||||
public enum DownloadStatus {
|
||||
Enqueued = 0,
|
||||
GenericError = 1,
|
||||
Success = HttpStatusCode.OK
|
||||
}
|
||||
}
|
15
app/Server/Data/Filters/AttachmentFilter.cs
Normal file
15
app/Server/Data/Filters/AttachmentFilter.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace DHT.Server.Data.Filters {
|
||||
public sealed class AttachmentFilter {
|
||||
public long? MaxBytes { get; set; } = null;
|
||||
|
||||
public DownloadItemRules? DownloadItemRule { get; set; } = null;
|
||||
|
||||
public bool IsEmpty => MaxBytes == null &&
|
||||
DownloadItemRule == null;
|
||||
|
||||
public enum DownloadItemRules {
|
||||
OnlyNotPresent,
|
||||
OnlyPresent
|
||||
}
|
||||
}
|
||||
}
|
10
app/Server/Data/Filters/DownloadItemFilter.cs
Normal file
10
app/Server/Data/Filters/DownloadItemFilter.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
namespace DHT.Server.Data.Filters {
|
||||
public enum MessageFilterRemovalMode {
|
||||
public enum FilterRemovalMode {
|
||||
KeepMatching,
|
||||
RemoveMatching
|
||||
}
|
@@ -24,6 +24,15 @@ namespace DHT.Server.Data {
|
||||
};
|
||||
}
|
||||
|
||||
public static string ToNiceString(ServerType? type) {
|
||||
return type switch {
|
||||
ServerType.Server => "Server",
|
||||
ServerType.Group => "Group",
|
||||
ServerType.DirectMessage => "DM",
|
||||
_ => "Unknown"
|
||||
};
|
||||
}
|
||||
|
||||
internal static string ToJsonViewerString(ServerType? type) {
|
||||
return type switch {
|
||||
ServerType.Server => "server",
|
||||
|
@@ -1,16 +1,29 @@
|
||||
using System.Collections.Generic;
|
||||
using DHT.Server.Data;
|
||||
|
||||
namespace DHT.Server.Database {
|
||||
public static class DatabaseExtensions {
|
||||
public static void AddFrom(this IDatabaseFile target, IDatabaseFile source) {
|
||||
foreach (var server in source.GetAllServers()) {
|
||||
target.AddServer(server);
|
||||
}
|
||||
|
||||
foreach (var channel in source.GetAllChannels()) {
|
||||
target.AddChannel(channel);
|
||||
}
|
||||
|
||||
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) {
|
||||
foreach (var server in servers) {
|
||||
target.AddServer(server);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void AddChannels(this IDatabaseFile target, IEnumerable<Channel> channels) {
|
||||
foreach (var channel in channels) {
|
||||
target.AddChannel(channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,11 +1,17 @@
|
||||
using DHT.Utils.Models;
|
||||
|
||||
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 {
|
||||
get => totalServers;
|
||||
@@ -27,13 +33,14 @@ namespace DHT.Server.Database {
|
||||
internal set => Change(ref totalMessages, value);
|
||||
}
|
||||
|
||||
public DatabaseStatistics Clone() {
|
||||
return new DatabaseStatistics {
|
||||
totalServers = totalServers,
|
||||
totalChannels = totalChannels,
|
||||
totalUsers = TotalUsers,
|
||||
totalMessages = totalMessages
|
||||
};
|
||||
public long? TotalAttachments {
|
||||
get => totalAttachments;
|
||||
internal set => Change(ref totalAttachments, value);
|
||||
}
|
||||
|
||||
public long? TotalDownloads {
|
||||
get => totalDownloads;
|
||||
internal set => Change(ref totalDownloads, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
11
app/Server/Database/DatabaseStatisticsSnapshot.cs
Normal file
11
app/Server/Database/DatabaseStatisticsSnapshot.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace DHT.Server.Database {
|
||||
/// <summary>
|
||||
/// A complete snapshot of database statistics at a particular point in time.
|
||||
/// </summary>
|
||||
public readonly struct DatabaseStatisticsSnapshot {
|
||||
public long TotalServers { get; internal init; }
|
||||
public long TotalChannels { get; internal init; }
|
||||
public long TotalUsers { get; internal init; }
|
||||
public long TotalMessages { get; internal init; }
|
||||
}
|
||||
}
|
@@ -1,6 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using DHT.Server.Data;
|
||||
using DHT.Server.Data.Aggregations;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Server.Download;
|
||||
|
||||
namespace DHT.Server.Database {
|
||||
public sealed class DummyDatabaseFile : IDatabaseFile {
|
||||
@@ -11,6 +13,10 @@ namespace DHT.Server.Database {
|
||||
|
||||
private DummyDatabaseFile() {}
|
||||
|
||||
public DatabaseStatisticsSnapshot SnapshotStatistics() {
|
||||
return new();
|
||||
}
|
||||
|
||||
public void AddServer(Data.Server server) {}
|
||||
|
||||
public List<Data.Server> GetAllServers() {
|
||||
@@ -39,7 +45,37 @@ namespace DHT.Server.Database {
|
||||
return new();
|
||||
}
|
||||
|
||||
public void RemoveMessages(MessageFilter filter, MessageFilterRemovalMode mode) {}
|
||||
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 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() {}
|
||||
|
||||
|
@@ -1,6 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using DHT.Server.Data;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Utils.Logging;
|
||||
@@ -9,7 +11,7 @@ namespace DHT.Server.Database.Export {
|
||||
public static class ViewerJsonExport {
|
||||
private static readonly Log Log = Log.ForType(typeof(ViewerJsonExport));
|
||||
|
||||
public static string Generate(IDatabaseFile db, MessageFilter? filter = null) {
|
||||
public static async Task Generate(Stream stream, IDatabaseFile db, MessageFilter? filter = null) {
|
||||
var perf = Log.Start();
|
||||
|
||||
var includedUserIds = new HashSet<ulong>();
|
||||
@@ -37,17 +39,20 @@ namespace DHT.Server.Database.Export {
|
||||
|
||||
perf.Step("Collect database data");
|
||||
|
||||
var value = new {
|
||||
meta = new { users, userindex, servers, channels },
|
||||
data = GenerateMessageList(includedMessages, userIndices)
|
||||
};
|
||||
|
||||
perf.Step("Generate value object");
|
||||
|
||||
var opts = new JsonSerializerOptions();
|
||||
opts.Converters.Add(new ViewerJsonSnowflakeSerializer());
|
||||
|
||||
var json = JsonSerializer.Serialize(new {
|
||||
meta = new { users, userindex, servers, channels },
|
||||
data = GenerateMessageList(includedMessages, userIndices)
|
||||
}, opts);
|
||||
await JsonSerializer.SerializeAsync(stream, value, opts);
|
||||
|
||||
perf.Step("Serialize to JSON");
|
||||
perf.End();
|
||||
return json;
|
||||
}
|
||||
|
||||
private static object GenerateUserList(IDatabaseFile db, HashSet<ulong> userIds, out List<string> userindex, out Dictionary<ulong, object> userIndices) {
|
||||
@@ -159,8 +164,8 @@ namespace DHT.Server.Database.Export {
|
||||
}
|
||||
|
||||
if (!message.Attachments.IsEmpty) {
|
||||
obj["a"] = message.Attachments.Select(static attachment => new {
|
||||
url = attachment.Url
|
||||
obj["a"] = message.Attachments.Select(static attachment => new Dictionary<string, object> {
|
||||
{ "url", attachment.Url }
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
|
@@ -1,12 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DHT.Server.Data;
|
||||
using DHT.Server.Data.Aggregations;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Server.Download;
|
||||
|
||||
namespace DHT.Server.Database {
|
||||
public interface IDatabaseFile : IDisposable {
|
||||
string Path { get; }
|
||||
DatabaseStatistics Statistics { get; }
|
||||
DatabaseStatisticsSnapshot SnapshotStatistics();
|
||||
|
||||
void AddServer(Data.Server server);
|
||||
List<Data.Server> GetAllServers();
|
||||
@@ -20,7 +23,19 @@ namespace DHT.Server.Database {
|
||||
void AddMessages(Message[] messages);
|
||||
int CountMessages(MessageFilter? filter = null);
|
||||
List<Message> GetMessages(MessageFilter? filter = null);
|
||||
void RemoveMessages(MessageFilter filter, MessageFilterRemovalMode mode);
|
||||
HashSet<ulong> GetMessageIds(MessageFilter? filter = null);
|
||||
void RemoveMessages(MessageFilter filter, FilterRemovalMode mode);
|
||||
|
||||
int CountAttachments(AttachmentFilter? filter = null);
|
||||
|
||||
void AddDownload(Data.Download download);
|
||||
List<Data.Download> GetDownloadsWithoutData();
|
||||
Data.Download GetDownloadWithData(Data.Download download);
|
||||
|
||||
void EnqueueDownloadItems(AttachmentFilter? filter = null);
|
||||
List<DownloadItem> GetEnqueuedDownloadItems(int count);
|
||||
void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode);
|
||||
DownloadStatusStatistics GetDownloadStatusStatistics();
|
||||
|
||||
void Vacuum();
|
||||
}
|
||||
|
21
app/Server/Database/Import/FakeSnowflake.cs
Normal file
21
app/Server/Database/Import/FakeSnowflake.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
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;
|
||||
|
||||
public FakeSnowflake() {
|
||||
var unixMillis = (ulong) (DateTime.UtcNow.Subtract(DateTime.UnixEpoch).Ticks / TimeSpan.TicksPerMillisecond);
|
||||
this.id = (unixMillis - DiscordEpoch) << 22;
|
||||
}
|
||||
|
||||
internal ulong Next() {
|
||||
return id++;
|
||||
}
|
||||
}
|
||||
}
|
263
app/Server/Database/Import/LegacyArchiveImport.cs
Normal file
263
app/Server/Database/Import/LegacyArchiveImport.cs
Normal file
@@ -0,0 +1,263 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using DHT.Server.Data;
|
||||
using DHT.Utils.Collections;
|
||||
using DHT.Utils.Http;
|
||||
using DHT.Utils.Logging;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
|
||||
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 async Task<bool> Read(Stream stream, IDatabaseFile db, FakeSnowflake fakeSnowflake, Func<Data.Server[], Task<Dictionary<Data.Server, ulong>?>> askForServerIds) {
|
||||
var perf = Log.Start();
|
||||
var root = await JsonSerializer.DeserializeAsync<JsonElement>(stream);
|
||||
|
||||
try {
|
||||
var meta = root.RequireObject("meta");
|
||||
var data = root.RequireObject("data");
|
||||
|
||||
perf.Step("Deserialize JSON");
|
||||
|
||||
var users = ReadUserList(meta);
|
||||
var servers = ReadServerList(meta, fakeSnowflake);
|
||||
|
||||
var newServersOnly = new HashSet<Data.Server>(servers);
|
||||
var oldServersById = db.GetAllServers().ToDictionary(static server => server.Id, static server => server);
|
||||
|
||||
var oldChannels = db.GetAllChannels();
|
||||
var oldChannelsById = oldChannels.ToDictionary(static channel => channel.Id, static channel => channel);
|
||||
|
||||
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("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();
|
||||
return true;
|
||||
}
|
||||
|
||||
private static User[] ReadUserList(JsonElement meta) {
|
||||
const string UsersPath = "meta.users[]";
|
||||
|
||||
static ulong ParseUserIndex(JsonElement element, int index) {
|
||||
return ulong.Parse(element.GetString() ?? throw new JsonException("Expected key 'meta.userindex[" + index + "]' to be a string."));
|
||||
}
|
||||
|
||||
var userindex = meta.RequireArray("userindex", "meta")
|
||||
.Select(static (item, index) => (ParseUserIndex(item, index), index))
|
||||
.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) {
|
||||
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(),
|
||||
Name = name,
|
||||
Type = type,
|
||||
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 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)
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -6,7 +6,7 @@ using DHT.Utils.Logging;
|
||||
|
||||
namespace DHT.Server.Database.Sqlite {
|
||||
sealed class Schema {
|
||||
internal const int Version = 3;
|
||||
internal const int Version = 4;
|
||||
|
||||
private static readonly Log Log = Log.ForType<Schema>();
|
||||
|
||||
@@ -94,6 +94,7 @@ namespace DHT.Server.Database.Sqlite {
|
||||
|
||||
CreateMessageEditTimestampTable();
|
||||
CreateMessageRepliedToTable();
|
||||
CreateDownloadsTable();
|
||||
|
||||
Execute("CREATE INDEX attachments_message_ix ON attachments(message_id)");
|
||||
Execute("CREATE INDEX embeds_message_ix ON embeds(message_id)");
|
||||
@@ -114,6 +115,14 @@ namespace DHT.Server.Database.Sqlite {
|
||||
replied_to_id INTEGER NOT NULL)");
|
||||
}
|
||||
|
||||
private void CreateDownloadsTable() {
|
||||
Execute(@"CREATE TABLE downloads (
|
||||
url TEXT NOT NULL PRIMARY KEY,
|
||||
status INTEGER NOT NULL,
|
||||
size INTEGER NOT NULL,
|
||||
blob BLOB)");
|
||||
}
|
||||
|
||||
private void UpgradeSchemas(int dbVersion) {
|
||||
var perf = Log.Start("from version " + dbVersion);
|
||||
|
||||
@@ -145,6 +154,11 @@ namespace DHT.Server.Database.Sqlite {
|
||||
perf.Step("Vacuum");
|
||||
}
|
||||
|
||||
if (dbVersion <= 3) {
|
||||
CreateDownloadsTable();
|
||||
perf.Step("Upgrade to version 4");
|
||||
}
|
||||
|
||||
perf.End();
|
||||
}
|
||||
}
|
||||
|
@@ -4,10 +4,13 @@ using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DHT.Server.Data;
|
||||
using DHT.Server.Data.Aggregations;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Server.Database.Sqlite.Utils;
|
||||
using DHT.Server.Download;
|
||||
using DHT.Utils.Collections;
|
||||
using DHT.Utils.Logging;
|
||||
using DHT.Utils.Tasks;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace DHT.Server.Database.Sqlite {
|
||||
@@ -21,14 +24,19 @@ namespace DHT.Server.Database.Sqlite {
|
||||
};
|
||||
|
||||
var pool = new SqliteConnectionPool(connectionString, DefaultPoolSize);
|
||||
bool wasOpened;
|
||||
|
||||
using (var conn = pool.Take()) {
|
||||
if (!await new Schema(conn).Setup(checkCanUpgradeSchemas)) {
|
||||
return null;
|
||||
}
|
||||
wasOpened = await new Schema(conn).Setup(checkCanUpgradeSchemas);
|
||||
}
|
||||
|
||||
return new SqliteDatabaseFile(path, pool);
|
||||
if (wasOpened) {
|
||||
return new SqliteDatabaseFile(path, pool);
|
||||
}
|
||||
else {
|
||||
pool.Dispose();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string Path { get; }
|
||||
@@ -36,12 +44,17 @@ namespace DHT.Server.Database.Sqlite {
|
||||
|
||||
private readonly Log log;
|
||||
private readonly SqliteConnectionPool pool;
|
||||
private readonly SqliteMessageStatisticsThread messageStatisticsThread;
|
||||
private readonly AsyncValueComputer<long>.Single totalMessagesComputer;
|
||||
private readonly AsyncValueComputer<long>.Single totalAttachmentsComputer;
|
||||
private readonly AsyncValueComputer<long>.Single totalDownloadsComputer;
|
||||
|
||||
private SqliteDatabaseFile(string path, SqliteConnectionPool pool) {
|
||||
this.log = Log.ForType(typeof(SqliteDatabaseFile), System.IO.Path.GetFileName(path));
|
||||
this.pool = pool;
|
||||
this.messageStatisticsThread = new SqliteMessageStatisticsThread(pool, UpdateMessageStatistics);
|
||||
|
||||
this.totalMessagesComputer = AsyncValueComputer<long>.WithResultProcessor(UpdateMessageStatistics).WithOutdatedResults().BuildWithComputer(ComputeMessageStatistics);
|
||||
this.totalAttachmentsComputer = AsyncValueComputer<long>.WithResultProcessor(UpdateAttachmentStatistics).WithOutdatedResults().BuildWithComputer(ComputeAttachmentStatistics);
|
||||
this.totalDownloadsComputer = AsyncValueComputer<long>.WithResultProcessor(UpdateDownloadStatistics).WithOutdatedResults().BuildWithComputer(ComputeDownloadStatistics);
|
||||
|
||||
this.Path = path;
|
||||
this.Statistics = new DatabaseStatistics();
|
||||
@@ -52,14 +65,24 @@ namespace DHT.Server.Database.Sqlite {
|
||||
UpdateUserStatistics(conn);
|
||||
}
|
||||
|
||||
messageStatisticsThread.RequestUpdate();
|
||||
totalMessagesComputer.Recompute();
|
||||
totalAttachmentsComputer.Recompute();
|
||||
totalDownloadsComputer.Recompute();
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
messageStatisticsThread.Dispose();
|
||||
pool.Dispose();
|
||||
}
|
||||
|
||||
public DatabaseStatisticsSnapshot SnapshotStatistics() {
|
||||
return new DatabaseStatisticsSnapshot {
|
||||
TotalServers = Statistics.TotalServers,
|
||||
TotalChannels = Statistics.TotalChannels,
|
||||
TotalUsers = Statistics.TotalUsers,
|
||||
TotalMessages = ComputeMessageStatistics()
|
||||
};
|
||||
}
|
||||
|
||||
public void AddServer(Data.Server server) {
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Upsert("servers", new[] {
|
||||
@@ -192,120 +215,130 @@ namespace DHT.Server.Database.Sqlite {
|
||||
cmd.Set(":message_id", id);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
bool addedAttachments = false;
|
||||
|
||||
using var conn = pool.Take();
|
||||
using var tx = conn.BeginTransaction();
|
||||
using (var conn = pool.Take()) {
|
||||
using var tx = conn.BeginTransaction();
|
||||
|
||||
using var messageCmd = conn.Upsert("messages", new[] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("sender_id", SqliteType.Integer),
|
||||
("channel_id", SqliteType.Integer),
|
||||
("text", SqliteType.Text),
|
||||
("timestamp", SqliteType.Integer)
|
||||
});
|
||||
using var messageCmd = conn.Upsert("messages", new[] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("sender_id", SqliteType.Integer),
|
||||
("channel_id", SqliteType.Integer),
|
||||
("text", SqliteType.Text),
|
||||
("timestamp", SqliteType.Integer)
|
||||
});
|
||||
|
||||
using var deleteEditTimestampCmd = DeleteByMessageId(conn, "edit_timestamps");
|
||||
using var deleteRepliedToCmd = DeleteByMessageId(conn, "replied_to");
|
||||
using var deleteEditTimestampCmd = DeleteByMessageId(conn, "edit_timestamps");
|
||||
using var deleteRepliedToCmd = DeleteByMessageId(conn, "replied_to");
|
||||
|
||||
using var deleteAttachmentsCmd = DeleteByMessageId(conn, "attachments");
|
||||
using var deleteEmbedsCmd = DeleteByMessageId(conn, "embeds");
|
||||
using var deleteReactionsCmd = DeleteByMessageId(conn, "reactions");
|
||||
using var deleteAttachmentsCmd = DeleteByMessageId(conn, "attachments");
|
||||
using var deleteEmbedsCmd = DeleteByMessageId(conn, "embeds");
|
||||
using var deleteReactionsCmd = DeleteByMessageId(conn, "reactions");
|
||||
|
||||
using var editTimestampCmd = conn.Insert("edit_timestamps", new [] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("edit_timestamp", SqliteType.Integer)
|
||||
});
|
||||
using var editTimestampCmd = conn.Insert("edit_timestamps", new [] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("edit_timestamp", SqliteType.Integer)
|
||||
});
|
||||
|
||||
using var repliedToCmd = conn.Insert("replied_to", new [] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("replied_to_id", SqliteType.Integer)
|
||||
});
|
||||
using var repliedToCmd = conn.Insert("replied_to", new [] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("replied_to_id", SqliteType.Integer)
|
||||
});
|
||||
|
||||
using var attachmentCmd = conn.Insert("attachments", new[] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("attachment_id", SqliteType.Integer),
|
||||
("name", SqliteType.Text),
|
||||
("type", SqliteType.Text),
|
||||
("url", SqliteType.Text),
|
||||
("size", SqliteType.Integer)
|
||||
});
|
||||
using var attachmentCmd = conn.Insert("attachments", new[] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("attachment_id", SqliteType.Integer),
|
||||
("name", SqliteType.Text),
|
||||
("type", SqliteType.Text),
|
||||
("url", SqliteType.Text),
|
||||
("size", SqliteType.Integer)
|
||||
});
|
||||
|
||||
using var embedCmd = conn.Insert("embeds", new[] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("json", SqliteType.Text)
|
||||
});
|
||||
using var embedCmd = conn.Insert("embeds", new[] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("json", SqliteType.Text)
|
||||
});
|
||||
|
||||
using var reactionCmd = conn.Insert("reactions", new[] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("emoji_id", SqliteType.Integer),
|
||||
("emoji_name", SqliteType.Text),
|
||||
("emoji_flags", SqliteType.Integer),
|
||||
("count", SqliteType.Integer)
|
||||
});
|
||||
using var reactionCmd = conn.Insert("reactions", new[] {
|
||||
("message_id", SqliteType.Integer),
|
||||
("emoji_id", SqliteType.Integer),
|
||||
("emoji_name", SqliteType.Text),
|
||||
("emoji_flags", SqliteType.Integer),
|
||||
("count", SqliteType.Integer)
|
||||
});
|
||||
|
||||
foreach (var message in messages) {
|
||||
object messageId = message.Id;
|
||||
foreach (var message in messages) {
|
||||
object messageId = message.Id;
|
||||
|
||||
messageCmd.Set(":message_id", messageId);
|
||||
messageCmd.Set(":sender_id", message.Sender);
|
||||
messageCmd.Set(":channel_id", message.Channel);
|
||||
messageCmd.Set(":text", message.Text);
|
||||
messageCmd.Set(":timestamp", message.Timestamp);
|
||||
messageCmd.ExecuteNonQuery();
|
||||
messageCmd.Set(":message_id", messageId);
|
||||
messageCmd.Set(":sender_id", message.Sender);
|
||||
messageCmd.Set(":channel_id", message.Channel);
|
||||
messageCmd.Set(":text", message.Text);
|
||||
messageCmd.Set(":timestamp", message.Timestamp);
|
||||
messageCmd.ExecuteNonQuery();
|
||||
|
||||
ExecuteDeleteByMessageId(deleteEditTimestampCmd, messageId);
|
||||
ExecuteDeleteByMessageId(deleteRepliedToCmd, messageId);
|
||||
ExecuteDeleteByMessageId(deleteEditTimestampCmd, messageId);
|
||||
ExecuteDeleteByMessageId(deleteRepliedToCmd, messageId);
|
||||
|
||||
ExecuteDeleteByMessageId(deleteAttachmentsCmd, messageId);
|
||||
ExecuteDeleteByMessageId(deleteEmbedsCmd, messageId);
|
||||
ExecuteDeleteByMessageId(deleteReactionsCmd, messageId);
|
||||
ExecuteDeleteByMessageId(deleteAttachmentsCmd, messageId);
|
||||
ExecuteDeleteByMessageId(deleteEmbedsCmd, messageId);
|
||||
ExecuteDeleteByMessageId(deleteReactionsCmd, messageId);
|
||||
|
||||
if (message.EditTimestamp is {} timestamp) {
|
||||
editTimestampCmd.Set(":message_id", messageId);
|
||||
editTimestampCmd.Set(":edit_timestamp", timestamp);
|
||||
editTimestampCmd.ExecuteNonQuery();
|
||||
}
|
||||
if (message.EditTimestamp is {} timestamp) {
|
||||
editTimestampCmd.Set(":message_id", messageId);
|
||||
editTimestampCmd.Set(":edit_timestamp", timestamp);
|
||||
editTimestampCmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
if (message.RepliedToId is {} repliedToId) {
|
||||
repliedToCmd.Set(":message_id", messageId);
|
||||
repliedToCmd.Set(":replied_to_id", repliedToId);
|
||||
repliedToCmd.ExecuteNonQuery();
|
||||
}
|
||||
if (message.RepliedToId is {} repliedToId) {
|
||||
repliedToCmd.Set(":message_id", messageId);
|
||||
repliedToCmd.Set(":replied_to_id", repliedToId);
|
||||
repliedToCmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
if (!message.Attachments.IsEmpty) {
|
||||
foreach (var attachment in message.Attachments) {
|
||||
attachmentCmd.Set(":message_id", messageId);
|
||||
attachmentCmd.Set(":attachment_id", attachment.Id);
|
||||
attachmentCmd.Set(":name", attachment.Name);
|
||||
attachmentCmd.Set(":type", attachment.Type);
|
||||
attachmentCmd.Set(":url", attachment.Url);
|
||||
attachmentCmd.Set(":size", attachment.Size);
|
||||
attachmentCmd.ExecuteNonQuery();
|
||||
if (!message.Attachments.IsEmpty) {
|
||||
addedAttachments = true;
|
||||
|
||||
foreach (var attachment in message.Attachments) {
|
||||
attachmentCmd.Set(":message_id", messageId);
|
||||
attachmentCmd.Set(":attachment_id", attachment.Id);
|
||||
attachmentCmd.Set(":name", attachment.Name);
|
||||
attachmentCmd.Set(":type", attachment.Type);
|
||||
attachmentCmd.Set(":url", attachment.Url);
|
||||
attachmentCmd.Set(":size", attachment.Size);
|
||||
attachmentCmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
if (!message.Embeds.IsEmpty) {
|
||||
foreach (var embed in message.Embeds) {
|
||||
embedCmd.Set(":message_id", messageId);
|
||||
embedCmd.Set(":json", embed.Json);
|
||||
embedCmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
if (!message.Reactions.IsEmpty) {
|
||||
foreach (var reaction in message.Reactions) {
|
||||
reactionCmd.Set(":message_id", messageId);
|
||||
reactionCmd.Set(":emoji_id", reaction.EmojiId);
|
||||
reactionCmd.Set(":emoji_name", reaction.EmojiName);
|
||||
reactionCmd.Set(":emoji_flags", (int) reaction.EmojiFlags);
|
||||
reactionCmd.Set(":count", reaction.Count);
|
||||
reactionCmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!message.Embeds.IsEmpty) {
|
||||
foreach (var embed in message.Embeds) {
|
||||
embedCmd.Set(":message_id", messageId);
|
||||
embedCmd.Set(":json", embed.Json);
|
||||
embedCmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
if (!message.Reactions.IsEmpty) {
|
||||
foreach (var reaction in message.Reactions) {
|
||||
reactionCmd.Set(":message_id", messageId);
|
||||
reactionCmd.Set(":emoji_id", reaction.EmojiId);
|
||||
reactionCmd.Set(":emoji_name", reaction.EmojiName);
|
||||
reactionCmd.Set(":emoji_flags", (int) reaction.EmojiFlags);
|
||||
reactionCmd.Set(":count", reaction.Count);
|
||||
reactionCmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
tx.Commit();
|
||||
}
|
||||
|
||||
tx.Commit();
|
||||
messageStatisticsThread.RequestUpdate();
|
||||
totalMessagesComputer.Recompute();
|
||||
|
||||
if (addedAttachments) {
|
||||
totalAttachmentsComputer.Recompute();
|
||||
}
|
||||
}
|
||||
|
||||
public int CountMessages(MessageFilter? filter = null) {
|
||||
@@ -353,26 +386,170 @@ LEFT JOIN replied_to rt ON m.message_id = rt.message_id" + filter.GenerateWhereC
|
||||
return list;
|
||||
}
|
||||
|
||||
public void RemoveMessages(MessageFilter filter, MessageFilterRemovalMode mode) {
|
||||
var whereClause = filter.GenerateWhereClause(invert: mode == MessageFilterRemovalMode.KeepMatching);
|
||||
if (string.IsNullOrEmpty(whereClause)) {
|
||||
return;
|
||||
}
|
||||
|
||||
public HashSet<ulong> GetMessageIds(MessageFilter? filter = null) {
|
||||
var perf = log.Start();
|
||||
|
||||
// Rider is being stupid...
|
||||
StringBuilder build = new StringBuilder()
|
||||
.Append("DELETE ")
|
||||
.Append("FROM messages")
|
||||
.Append(whereClause);
|
||||
var ids = new HashSet<ulong>();
|
||||
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Command(build.ToString());
|
||||
cmd.ExecuteNonQuery();
|
||||
using var cmd = conn.Command("SELECT message_id FROM messages" + filter.GenerateWhereClause());
|
||||
using var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read()) {
|
||||
ids.Add(reader.GetUint64(0));
|
||||
}
|
||||
|
||||
UpdateMessageStatistics(conn);
|
||||
perf.End();
|
||||
return ids;
|
||||
}
|
||||
|
||||
public void RemoveMessages(MessageFilter filter, FilterRemovalMode mode) {
|
||||
var whereClause = filter.GenerateWhereClause(invert: mode == FilterRemovalMode.KeepMatching);
|
||||
|
||||
if (!string.IsNullOrEmpty(whereClause)) {
|
||||
var perf = log.Start();
|
||||
|
||||
DeleteFromTable("messages", whereClause);
|
||||
totalMessagesComputer.Recompute();
|
||||
|
||||
perf.End();
|
||||
}
|
||||
}
|
||||
|
||||
public int CountAttachments(AttachmentFilter? filter = null) {
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Command("SELECT COUNT(*) FROM attachments a" + filter.GenerateWhereClause("a"));
|
||||
using var reader = cmd.ExecuteReader();
|
||||
|
||||
return reader.Read() ? reader.GetInt32(0) : 0;
|
||||
}
|
||||
|
||||
public void AddDownload(Data.Download download) {
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Upsert("downloads", new[] {
|
||||
("url", SqliteType.Text),
|
||||
("status", SqliteType.Integer),
|
||||
("size", SqliteType.Integer),
|
||||
("blob", SqliteType.Blob)
|
||||
});
|
||||
|
||||
cmd.Set(":url", download.Url);
|
||||
cmd.Set(":status", (int) download.Status);
|
||||
cmd.Set(":size", download.Size);
|
||||
cmd.Set(":blob", download.Data);
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
totalDownloadsComputer.Recompute();
|
||||
}
|
||||
|
||||
public List<Data.Download> GetDownloadsWithoutData() {
|
||||
var list = new List<Data.Download>();
|
||||
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Command("SELECT url, status, size FROM downloads");
|
||||
using var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read()) {
|
||||
string url = reader.GetString(0);
|
||||
var status = (DownloadStatus) reader.GetInt32(1);
|
||||
ulong size = reader.GetUint64(2);
|
||||
|
||||
list.Add(new Data.Download(url, status, size));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public Data.Download GetDownloadWithData(Data.Download download) {
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Command("SELECT blob FROM downloads WHERE url = :url");
|
||||
cmd.AddAndSet(":url", SqliteType.Text, download.Url);
|
||||
|
||||
using var reader = cmd.ExecuteReader();
|
||||
|
||||
if (reader.Read() && !reader.IsDBNull(0)) {
|
||||
return download.WithData((byte[]) reader["blob"]);
|
||||
}
|
||||
else {
|
||||
return download;
|
||||
}
|
||||
}
|
||||
|
||||
public void EnqueueDownloadItems(AttachmentFilter? filter = null) {
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Command("INSERT INTO downloads (url, status, size) SELECT a.url, :enqueued, a.size FROM attachments a" + filter.GenerateWhereClause("a"));
|
||||
cmd.AddAndSet(":enqueued", SqliteType.Integer, (int) DownloadStatus.Enqueued);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public List<DownloadItem> GetEnqueuedDownloadItems(int count) {
|
||||
var list = new List<DownloadItem>();
|
||||
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Command("SELECT url, size FROM downloads WHERE status = :enqueued LIMIT :limit");
|
||||
cmd.AddAndSet(":enqueued", SqliteType.Integer, (int) DownloadStatus.Enqueued);
|
||||
cmd.AddAndSet(":limit", SqliteType.Integer, Math.Max(0, count));
|
||||
|
||||
using var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read()) {
|
||||
list.Add(new DownloadItem {
|
||||
Url = reader.GetString(0),
|
||||
Size = reader.GetUint64(1)
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode) {
|
||||
var whereClause = filter.GenerateWhereClause(invert: mode == FilterRemovalMode.KeepMatching);
|
||||
|
||||
if (!string.IsNullOrEmpty(whereClause)) {
|
||||
DeleteFromTable("downloads", whereClause);
|
||||
}
|
||||
}
|
||||
|
||||
public DownloadStatusStatistics GetDownloadStatusStatistics() {
|
||||
static void LoadUndownloadedStatistics(ISqliteConnection conn, DownloadStatusStatistics result) {
|
||||
using var cmd = conn.Command("SELECT IFNULL(COUNT(filtered.size), 0), IFNULL(SUM(filtered.size), 0) FROM (SELECT DISTINCT a.url, a.size FROM attachments a WHERE a.url NOT IN (SELECT d.url FROM downloads d)) filtered");
|
||||
using var reader = cmd.ExecuteReader();
|
||||
|
||||
if (reader.Read()) {
|
||||
result.SkippedCount = reader.GetInt32(0);
|
||||
result.SkippedSize = reader.GetUint64(1);
|
||||
}
|
||||
}
|
||||
|
||||
static void LoadSuccessStatistics(ISqliteConnection conn, DownloadStatusStatistics result) {
|
||||
using var cmd = conn.Command(@"SELECT
|
||||
IFNULL(SUM(CASE WHEN status = :enqueued THEN 1 ELSE 0 END), 0),
|
||||
IFNULL(SUM(CASE WHEN status = :enqueued THEN size ELSE 0 END), 0),
|
||||
IFNULL(SUM(CASE WHEN status = :success THEN 1 ELSE 0 END), 0),
|
||||
IFNULL(SUM(CASE WHEN status = :success THEN size ELSE 0 END), 0),
|
||||
IFNULL(SUM(CASE WHEN status != :enqueued AND status != :success THEN 1 ELSE 0 END), 0),
|
||||
IFNULL(SUM(CASE WHEN status != :enqueued AND status != :success THEN size ELSE 0 END), 0)
|
||||
FROM downloads");
|
||||
cmd.AddAndSet(":enqueued", SqliteType.Integer, (int) DownloadStatus.Enqueued);
|
||||
cmd.AddAndSet(":success", SqliteType.Integer, (int) DownloadStatus.Success);
|
||||
|
||||
using var reader = cmd.ExecuteReader();
|
||||
|
||||
if (reader.Read()) {
|
||||
result.EnqueuedCount = reader.GetInt32(0);
|
||||
result.EnqueuedSize = reader.GetUint64(1);
|
||||
result.SuccessfulCount = reader.GetInt32(2);
|
||||
result.SuccessfulSize = reader.GetUint64(3);
|
||||
result.FailedCount = reader.GetInt32(4);
|
||||
result.FailedSize = reader.GetUint64(5);
|
||||
}
|
||||
}
|
||||
|
||||
var result = new DownloadStatusStatistics();
|
||||
|
||||
using var conn = pool.Take();
|
||||
LoadUndownloadedStatistics(conn, result);
|
||||
LoadSuccessStatistics(conn, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private MultiDictionary<ulong, Attachment> GetAllAttachments() {
|
||||
@@ -436,6 +613,19 @@ LEFT JOIN replied_to rt ON m.message_id = rt.message_id" + filter.GenerateWhereC
|
||||
return dict;
|
||||
}
|
||||
|
||||
private void DeleteFromTable(string table, string whereClause) {
|
||||
// Rider is being stupid...
|
||||
StringBuilder build = new StringBuilder()
|
||||
.Append("DELETE ")
|
||||
.Append("FROM ")
|
||||
.Append(table)
|
||||
.Append(whereClause);
|
||||
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Command(build.ToString());
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void Vacuum() {
|
||||
using var conn = pool.Take();
|
||||
using var cmd = conn.Command("VACUUM");
|
||||
@@ -454,8 +644,31 @@ LEFT JOIN replied_to rt ON m.message_id = rt.message_id" + filter.GenerateWhereC
|
||||
Statistics.TotalUsers = conn.SelectScalar("SELECT COUNT(*) FROM users") as long? ?? 0;
|
||||
}
|
||||
|
||||
private void UpdateMessageStatistics(ISqliteConnection conn) {
|
||||
Statistics.TotalMessages = conn.SelectScalar("SELECT COUNT(*) FROM messages") as long? ?? 0L;
|
||||
private long ComputeMessageStatistics() {
|
||||
using var conn = pool.Take();
|
||||
return conn.SelectScalar("SELECT COUNT(*) FROM messages") as long? ?? 0L;
|
||||
}
|
||||
|
||||
private void UpdateMessageStatistics(long totalMessages) {
|
||||
Statistics.TotalMessages = totalMessages;
|
||||
}
|
||||
|
||||
private long ComputeAttachmentStatistics() {
|
||||
using var conn = pool.Take();
|
||||
return conn.SelectScalar("SELECT COUNT(*) FROM attachments") as long? ?? 0L;
|
||||
}
|
||||
|
||||
private void UpdateAttachmentStatistics(long totalAttachments) {
|
||||
Statistics.TotalAttachments = totalAttachments;
|
||||
}
|
||||
|
||||
private long ComputeDownloadStatistics() {
|
||||
using var conn = pool.Take();
|
||||
return conn.SelectScalar("SELECT COUNT(*) FROM downloads") as long? ?? 0L;
|
||||
}
|
||||
|
||||
private void UpdateDownloadStatistics(long totalDownloads) {
|
||||
Statistics.TotalDownloads = totalDownloads;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
83
app/Server/Database/Sqlite/SqliteFilters.cs
Normal file
83
app/Server/Database/Sqlite/SqliteFilters.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DHT.Server.Data;
|
||||
using DHT.Server.Data.Filters;
|
||||
using DHT.Server.Database.Sqlite.Utils;
|
||||
|
||||
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);
|
||||
|
||||
if (filter.StartDate != null) {
|
||||
where.AddCondition("timestamp >= " + new DateTimeOffset(filter.StartDate.Value).ToUnixTimeMilliseconds());
|
||||
}
|
||||
|
||||
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) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,50 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DHT.Server.Data.Filters;
|
||||
|
||||
namespace DHT.Server.Database.Sqlite {
|
||||
static class SqliteMessageFilter {
|
||||
public static string GenerateWhereClause(this MessageFilter? filter, string? tableAlias = null, bool invert = false) {
|
||||
if (filter == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (tableAlias != null) {
|
||||
tableAlias += ".";
|
||||
}
|
||||
|
||||
List<string> conditions = new();
|
||||
|
||||
if (filter.StartDate != null) {
|
||||
conditions.Add(tableAlias + "timestamp >= " + new DateTimeOffset(filter.StartDate.Value).ToUnixTimeMilliseconds());
|
||||
}
|
||||
|
||||
if (filter.EndDate != null) {
|
||||
conditions.Add(tableAlias + "timestamp <= " + new DateTimeOffset(filter.EndDate.Value).ToUnixTimeMilliseconds());
|
||||
}
|
||||
|
||||
if (filter.ChannelIds != null) {
|
||||
conditions.Add(tableAlias + "channel_id IN (" + string.Join(",", filter.ChannelIds) + ")");
|
||||
}
|
||||
|
||||
if (filter.UserIds != null) {
|
||||
conditions.Add(tableAlias + "sender_id IN (" + string.Join(",", filter.UserIds) + ")");
|
||||
}
|
||||
|
||||
if (filter.MessageIds != null) {
|
||||
conditions.Add(tableAlias + "message_id IN (" + string.Join(",", filter.MessageIds) + ")");
|
||||
}
|
||||
|
||||
if (conditions.Count == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (invert) {
|
||||
return " WHERE NOT (" + string.Join(" AND ", conditions) + ")";
|
||||
}
|
||||
else {
|
||||
return " WHERE " + string.Join(" AND ", conditions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using DHT.Server.Database.Sqlite.Utils;
|
||||
|
||||
namespace DHT.Server.Database.Sqlite {
|
||||
sealed class SqliteMessageStatisticsThread : IDisposable {
|
||||
private readonly SqliteConnectionPool pool;
|
||||
private readonly Action<ISqliteConnection> action;
|
||||
|
||||
private readonly CancellationTokenSource cancellationTokenSource = new();
|
||||
private readonly CancellationToken cancellationToken;
|
||||
|
||||
private readonly AutoResetEvent requestEvent = new (false);
|
||||
|
||||
public SqliteMessageStatisticsThread(SqliteConnectionPool pool, Action<ISqliteConnection> action) {
|
||||
this.pool = pool;
|
||||
this.action = action;
|
||||
|
||||
this.cancellationToken = cancellationTokenSource.Token;
|
||||
|
||||
var thread = new Thread(RunThread) {
|
||||
Name = "DHT message statistics thread",
|
||||
IsBackground = true
|
||||
};
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
try {
|
||||
cancellationTokenSource.Cancel();
|
||||
} catch (ObjectDisposedException) {}
|
||||
}
|
||||
|
||||
public void RequestUpdate() {
|
||||
try {
|
||||
requestEvent.Set();
|
||||
} catch (ObjectDisposedException) {}
|
||||
}
|
||||
|
||||
private void RunThread() {
|
||||
try {
|
||||
while (!cancellationToken.IsCancellationRequested) {
|
||||
if (requestEvent.WaitOne(TimeSpan.FromMilliseconds(100))) {
|
||||
using var conn = pool.Take();
|
||||
action(conn);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
cancellationTokenSource.Dispose();
|
||||
requestEvent.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -13,6 +13,7 @@ namespace DHT.Server.Database.Sqlite.Utils {
|
||||
}
|
||||
|
||||
private readonly object monitor = new ();
|
||||
private readonly Random rand = new ();
|
||||
private volatile bool isDisposed;
|
||||
|
||||
private readonly BlockingCollection<PooledConnection> free = new (new ConcurrentStack<PooledConnection>());
|
||||
@@ -49,7 +50,7 @@ namespace DHT.Server.Database.Sqlite.Utils {
|
||||
while (conn == null) {
|
||||
ThrowIfDisposed();
|
||||
lock (monitor) {
|
||||
if (free.TryTake(out conn, TimeSpan.FromMilliseconds(100))) {
|
||||
if (free.TryTake(out conn, TimeSpan.FromMilliseconds(rand.Next(100, 200)))) {
|
||||
used.Add(conn);
|
||||
break;
|
||||
}
|
||||
|
@@ -56,6 +56,10 @@ namespace DHT.Server.Database.Sqlite.Utils {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
31
app/Server/Database/Sqlite/Utils/SqliteWhereGenerator.cs
Normal file
31
app/Server/Database/Sqlite/Utils/SqliteWhereGenerator.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
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) {
|
||||
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 "";
|
||||
}
|
||||
|
||||
if (invert) {
|
||||
return " WHERE NOT (" + string.Join(" AND ", conditions) + ")";
|
||||
}
|
||||
else {
|
||||
return " WHERE " + string.Join(" AND ", conditions);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
129
app/Server/Download/BackgroundDownloadThread.cs
Normal file
129
app/Server/Download/BackgroundDownloadThread.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using DHT.Server.Database;
|
||||
using DHT.Utils.Logging;
|
||||
using DHT.Utils.Models;
|
||||
|
||||
namespace DHT.Server.Download {
|
||||
public sealed class BackgroundDownloadThread : BaseModel {
|
||||
private static readonly Log Log = Log.ForType<BackgroundDownloadThread>();
|
||||
|
||||
public event EventHandler<DownloadItem>? OnItemFinished {
|
||||
add => parameters.OnItemFinished += value;
|
||||
remove => parameters.OnItemFinished -= value;
|
||||
}
|
||||
|
||||
public event EventHandler? OnServerStopped {
|
||||
add => parameters.OnServerStopped += value;
|
||||
remove => parameters.OnServerStopped -= value;
|
||||
}
|
||||
|
||||
private readonly CancellationTokenSource cancellationTokenSource;
|
||||
private readonly ThreadInstance.Parameters parameters;
|
||||
|
||||
public BackgroundDownloadThread(IDatabaseFile db) {
|
||||
this.cancellationTokenSource = new CancellationTokenSource();
|
||||
this.parameters = new ThreadInstance.Parameters(db, cancellationTokenSource);
|
||||
|
||||
var thread = new Thread(new ThreadInstance().Work) {
|
||||
Name = "DHT download thread"
|
||||
};
|
||||
|
||||
thread.Start(parameters);
|
||||
}
|
||||
|
||||
public void StopThread() {
|
||||
try {
|
||||
cancellationTokenSource.Cancel();
|
||||
} catch (ObjectDisposedException) {
|
||||
Log.Warn("Attempted to stop background download thread after the cancellation token has been disposed.");
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ThreadInstance {
|
||||
private const int QueueSize = 32;
|
||||
|
||||
public sealed class Parameters {
|
||||
public event EventHandler<DownloadItem>? OnItemFinished;
|
||||
public event EventHandler? OnServerStopped;
|
||||
|
||||
public IDatabaseFile Db { get; }
|
||||
public CancellationTokenSource CancellationTokenSource { get; }
|
||||
|
||||
public Parameters(IDatabaseFile db, CancellationTokenSource cancellationTokenSource) {
|
||||
Db = db;
|
||||
CancellationTokenSource = cancellationTokenSource;
|
||||
}
|
||||
|
||||
public void FireOnItemFinished(DownloadItem item) {
|
||||
OnItemFinished?.Invoke(null, item);
|
||||
}
|
||||
|
||||
public void FireOnServerStopped() {
|
||||
OnServerStopped?.Invoke(null, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly WebClient client = new ();
|
||||
|
||||
public ThreadInstance() {
|
||||
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36";
|
||||
}
|
||||
|
||||
public void Work(object? obj) {
|
||||
var parameters = (Parameters) obj!;
|
||||
|
||||
var cancellationTokenSource = parameters.CancellationTokenSource;
|
||||
var cancellationToken = cancellationTokenSource.Token;
|
||||
|
||||
var db = parameters.Db;
|
||||
var queue = new ConcurrentQueue<DownloadItem>();
|
||||
|
||||
cancellationToken.Register(client.CancelAsync);
|
||||
|
||||
try {
|
||||
while (!cancellationToken.IsCancellationRequested) {
|
||||
FillQueue(db, queue, cancellationToken);
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested && queue.TryDequeue(out var item)) {
|
||||
var url = item.Url;
|
||||
Log.Debug("Downloading " + url + "...");
|
||||
|
||||
try {
|
||||
db.AddDownload(Data.Download.NewSuccess(url, client.DownloadData(url)));
|
||||
} catch (WebException e) {
|
||||
db.AddDownload(Data.Download.NewFailure(url, e.Response is HttpWebResponse response ? response.StatusCode : null, item.Size));
|
||||
Log.Error(e);
|
||||
} finally {
|
||||
parameters.FireOnItemFinished(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (OperationCanceledException) {
|
||||
//
|
||||
} catch (ObjectDisposedException) {
|
||||
//
|
||||
} finally {
|
||||
cancellationTokenSource.Dispose();
|
||||
parameters.FireOnServerStopped();
|
||||
}
|
||||
}
|
||||
|
||||
private static void FillQueue(IDatabaseFile db, ConcurrentQueue<DownloadItem> queue, CancellationToken cancellationToken) {
|
||||
while (!cancellationToken.IsCancellationRequested && queue.IsEmpty) {
|
||||
var newItems = db.GetEnqueuedDownloadItems(QueueSize);
|
||||
if (newItems.Count == 0) {
|
||||
Thread.Sleep(TimeSpan.FromMilliseconds(50));
|
||||
}
|
||||
else {
|
||||
foreach (var item in newItems) {
|
||||
queue.Enqueue(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
6
app/Server/Download/DownloadItem.cs
Normal file
6
app/Server/Download/DownloadItem.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace DHT.Server.Download {
|
||||
public readonly struct DownloadItem {
|
||||
public string Url { get; init; }
|
||||
public ulong Size { get; init; }
|
||||
}
|
||||
}
|
@@ -20,7 +20,7 @@
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.5" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Utils\Utils.csproj" />
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace DHT.Utils.Collections {
|
||||
public static class LinqExtensions {
|
||||
@@ -14,5 +15,9 @@ namespace DHT.Utils.Collections {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<(TKey, TValue)> collection) where TKey : notnull {
|
||||
return collection.ToDictionary(static item => item.Item1, static item => item.Item2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
126
app/Utils/Tasks/AsyncValueComputer.cs
Normal file
126
app/Utils/Tasks/AsyncValueComputer.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DHT.Utils.Tasks {
|
||||
public sealed class AsyncValueComputer<TValue> {
|
||||
private readonly Action<TValue> resultProcessor;
|
||||
private readonly TaskScheduler resultTaskScheduler;
|
||||
private readonly bool processOutdatedResults;
|
||||
|
||||
private readonly object stateLock = new ();
|
||||
|
||||
private SoftHardCancellationToken? currentCancellationTokenSource;
|
||||
private bool wasHardCancelled = false;
|
||||
|
||||
private Func<TValue>? currentComputeFunction;
|
||||
private bool hasComputeFunctionChanged = false;
|
||||
|
||||
private AsyncValueComputer(Action<TValue> resultProcessor, TaskScheduler resultTaskScheduler, bool processOutdatedResults) {
|
||||
this.resultProcessor = resultProcessor;
|
||||
this.resultTaskScheduler = resultTaskScheduler;
|
||||
this.processOutdatedResults = processOutdatedResults;
|
||||
}
|
||||
|
||||
public void Cancel() {
|
||||
lock (stateLock) {
|
||||
wasHardCancelled = true;
|
||||
currentCancellationTokenSource?.RequestHardCancellation();
|
||||
}
|
||||
}
|
||||
|
||||
public void Compute(Func<TValue> func) {
|
||||
lock (stateLock) {
|
||||
wasHardCancelled = false;
|
||||
|
||||
if (currentComputeFunction != null) {
|
||||
currentComputeFunction = func;
|
||||
hasComputeFunctionChanged = true;
|
||||
currentCancellationTokenSource?.RequestSoftCancellation();
|
||||
}
|
||||
else {
|
||||
EnqueueComputation(func);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "MethodSupportsCancellation")]
|
||||
private void EnqueueComputation(Func<TValue> func) {
|
||||
var cancellationTokenSource = new SoftHardCancellationToken();
|
||||
|
||||
currentCancellationTokenSource?.RequestSoftCancellation();
|
||||
currentCancellationTokenSource = cancellationTokenSource;
|
||||
currentComputeFunction = func;
|
||||
hasComputeFunctionChanged = false;
|
||||
|
||||
var task = Task.Run(func);
|
||||
|
||||
task.ContinueWith(t => {
|
||||
if (!cancellationTokenSource.IsCancelled(processOutdatedResults)) {
|
||||
resultProcessor(t.Result);
|
||||
}
|
||||
}, CancellationToken.None, TaskContinuationOptions.NotOnFaulted, resultTaskScheduler);
|
||||
|
||||
task.ContinueWith(_ => {
|
||||
lock (stateLock) {
|
||||
cancellationTokenSource.Dispose();
|
||||
|
||||
if (currentCancellationTokenSource == cancellationTokenSource) {
|
||||
currentCancellationTokenSource = null;
|
||||
}
|
||||
|
||||
if (hasComputeFunctionChanged && !wasHardCancelled) {
|
||||
EnqueueComputation(currentComputeFunction);
|
||||
}
|
||||
else {
|
||||
currentComputeFunction = null;
|
||||
hasComputeFunctionChanged = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public sealed class Single {
|
||||
private readonly AsyncValueComputer<TValue> baseComputer;
|
||||
private readonly Func<TValue> resultComputer;
|
||||
|
||||
internal Single(AsyncValueComputer<TValue> baseComputer, Func<TValue> resultComputer) {
|
||||
this.baseComputer = baseComputer;
|
||||
this.resultComputer = resultComputer;
|
||||
}
|
||||
|
||||
public void Recompute() {
|
||||
baseComputer.Compute(resultComputer);
|
||||
}
|
||||
}
|
||||
|
||||
public static Builder WithResultProcessor(Action<TValue> resultProcessor, TaskScheduler? scheduler = null) {
|
||||
return new Builder(resultProcessor, scheduler ?? TaskScheduler.FromCurrentSynchronizationContext());
|
||||
}
|
||||
|
||||
public sealed class Builder {
|
||||
private readonly Action<TValue> resultProcessor;
|
||||
private readonly TaskScheduler resultTaskScheduler;
|
||||
private bool processOutdatedResults;
|
||||
|
||||
internal Builder(Action<TValue> resultProcessor, TaskScheduler resultTaskScheduler) {
|
||||
this.resultProcessor = resultProcessor;
|
||||
this.resultTaskScheduler = resultTaskScheduler;
|
||||
}
|
||||
|
||||
public Builder WithOutdatedResults() {
|
||||
this.processOutdatedResults = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AsyncValueComputer<TValue> Build() {
|
||||
return new AsyncValueComputer<TValue>(resultProcessor, resultTaskScheduler, processOutdatedResults);
|
||||
}
|
||||
|
||||
public Single BuildWithComputer(Func<TValue> resultComputer) {
|
||||
return new Single(Build(), resultComputer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
39
app/Utils/Tasks/SoftHardCancellationToken.cs
Normal file
39
app/Utils/Tasks/SoftHardCancellationToken.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace DHT.Utils.Tasks {
|
||||
/// <summary>
|
||||
/// Manages a pair of cancellation tokens that follow these rules:
|
||||
/// <list type="number">
|
||||
/// <item><description>If the soft token is cancelled, the hard token remains uncancelled.</description></item>
|
||||
/// <item><description>If the hard token is cancelled, the soft token is also cancelled.</description></item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
sealed class SoftHardCancellationToken : IDisposable {
|
||||
private readonly CancellationTokenSource soft;
|
||||
private readonly CancellationTokenSource hard;
|
||||
|
||||
public SoftHardCancellationToken() {
|
||||
this.soft = new CancellationTokenSource();
|
||||
this.hard = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
public bool IsCancelled(bool onlyHardCancellation) {
|
||||
return (onlyHardCancellation ? hard : soft).IsCancellationRequested;
|
||||
}
|
||||
|
||||
public void RequestSoftCancellation() {
|
||||
soft.Cancel();
|
||||
}
|
||||
|
||||
public void RequestHardCancellation() {
|
||||
soft.Cancel();
|
||||
hard.Cancel();
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
soft.Dispose();
|
||||
hard.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,6 +7,6 @@ using DHT.Utils;
|
||||
|
||||
namespace DHT.Utils {
|
||||
static class Version {
|
||||
public const string Tag = "35.3.0.0";
|
||||
public const string Tag = "36.1.0.0";
|
||||
}
|
||||
}
|
||||
|
BIN
app/empty.dht
BIN
app/empty.dht
Binary file not shown.
@@ -53,11 +53,11 @@ define('LATEST_VERSION', $version === false ? '_' : $version);
|
||||
<svg class="icon">
|
||||
<use href="#icon-globe" />
|
||||
</svg>
|
||||
<span class="platform">Portable</span>
|
||||
<span class="platform">Other</span>
|
||||
</a>
|
||||
</div>
|
||||
<p>To launch the three OS-specific versions, extract the <strong>DiscordHistoryTracker</strong> executable, and double-click it.</p>
|
||||
<p>To launch the <strong>Portable</strong> version, which works on other operating systems including 32-bit versions, you must install <a href="https://dotnet.microsoft.com/download/dotnet/5.0/runtime" rel="nofollow noopener">.NET 5</a>. Then extract the downloaded archive into a folder, open the folder in a terminal, and type: <code>dotnet DiscordHistoryTracker.dll</code></p>
|
||||
<p>To launch the <strong>Other</strong> version, which works on other operating systems including 32-bit versions, you must install <a href="https://dotnet.microsoft.com/download/dotnet/5.0" rel="nofollow noopener">ASP.NET Core Runtime 5</a>. Then extract the downloaded archive into a folder, open the folder in a terminal, and type: <code>dotnet DiscordHistoryTracker.dll</code></p>
|
||||
|
||||
<h3>How to Track Messages</h3>
|
||||
<p>The app saves messages into a database file stored on your computer. When you open the app, you are given the option to create a new database file, or open an existing one.</p>
|
||||
|
Reference in New Issue
Block a user