mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-09-16 13:24:47 +02:00
Compare commits
1 Commits
3bf5acfa65
...
wip-viewer
Author | SHA1 | Date | |
---|---|---|---|
b660af4be0
|
@@ -1,6 +1,6 @@
|
|||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.Progress;
|
namespace DHT.Desktop.Dialogs.Progress;
|
||||||
|
|
||||||
sealed class ProgressItem : BaseModel {
|
sealed class ProgressItem : BaseModel {
|
||||||
private bool isVisible = false;
|
private bool isVisible = false;
|
||||||
|
@@ -3,7 +3,7 @@ using Avalonia.Controls;
|
|||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.TextBox;
|
namespace DHT.Desktop.Dialogs.TextBox;
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
public sealed partial class TextBoxDialog : Window {
|
public sealed partial class TextBoxDialog : Window {
|
||||||
|
@@ -4,7 +4,7 @@ using System.ComponentModel;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.TextBox;
|
namespace DHT.Desktop.Dialogs.TextBox;
|
||||||
|
|
||||||
class TextBoxDialogModel : BaseModel {
|
class TextBoxDialogModel : BaseModel {
|
||||||
public string Title { get; init; } = "";
|
public string Title { get; init; } = "";
|
||||||
|
@@ -3,7 +3,7 @@ using System.Collections;
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
namespace DHT.Desktop.Dialogs.TextBox;
|
namespace DHT.Desktop.Dialogs.TextBox;
|
||||||
|
|
||||||
class TextBoxItem : BaseModel, INotifyDataErrorInfo {
|
class TextBoxItem : BaseModel, INotifyDataErrorInfo {
|
||||||
public string Title { get; init; } = "";
|
public string Title { get; init; } = "";
|
||||||
|
@@ -2,7 +2,6 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using DHT.Desktop.Common;
|
using DHT.Desktop.Common;
|
||||||
using DHT.Server;
|
|
||||||
using DHT.Server.Data.Filters;
|
using DHT.Server.Data.Filters;
|
||||||
using DHT.Server.Database;
|
using DHT.Server.Database;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
@@ -48,7 +47,7 @@ sealed class AttachmentFilterPanelModel : BaseModel, IDisposable {
|
|||||||
|
|
||||||
public IEnumerable<Unit> Units => AllUnits;
|
public IEnumerable<Unit> Units => AllUnits;
|
||||||
|
|
||||||
private readonly State state;
|
private readonly IDatabaseFile db;
|
||||||
private readonly string verb;
|
private readonly string verb;
|
||||||
|
|
||||||
private readonly AsyncValueComputer<long> matchingAttachmentCountComputer;
|
private readonly AsyncValueComputer<long> matchingAttachmentCountComputer;
|
||||||
@@ -56,10 +55,10 @@ sealed class AttachmentFilterPanelModel : BaseModel, IDisposable {
|
|||||||
private long? totalAttachmentCount;
|
private long? totalAttachmentCount;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public AttachmentFilterPanelModel() : this(State.Dummy) {}
|
public AttachmentFilterPanelModel() : this(DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
public AttachmentFilterPanelModel(State state, string verb = "Matches") {
|
public AttachmentFilterPanelModel(IDatabaseFile db, string verb = "Matches") {
|
||||||
this.state = state;
|
this.db = db;
|
||||||
this.verb = verb;
|
this.verb = verb;
|
||||||
|
|
||||||
this.matchingAttachmentCountComputer = AsyncValueComputer<long>.WithResultProcessor(SetAttachmentCounts).Build();
|
this.matchingAttachmentCountComputer = AsyncValueComputer<long>.WithResultProcessor(SetAttachmentCounts).Build();
|
||||||
@@ -67,11 +66,11 @@ sealed class AttachmentFilterPanelModel : BaseModel, IDisposable {
|
|||||||
UpdateFilterStatistics();
|
UpdateFilterStatistics();
|
||||||
|
|
||||||
PropertyChanged += OnPropertyChanged;
|
PropertyChanged += OnPropertyChanged;
|
||||||
state.Db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
state.Db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
||||||
@@ -82,7 +81,7 @@ sealed class AttachmentFilterPanelModel : BaseModel, IDisposable {
|
|||||||
|
|
||||||
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
||||||
if (e.PropertyName == nameof(DatabaseStatistics.TotalAttachments)) {
|
if (e.PropertyName == nameof(DatabaseStatistics.TotalAttachments)) {
|
||||||
totalAttachmentCount = state.Db.Statistics.TotalAttachments;
|
totalAttachmentCount = db.Statistics.TotalAttachments;
|
||||||
UpdateFilterStatistics();
|
UpdateFilterStatistics();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -97,7 +96,7 @@ sealed class AttachmentFilterPanelModel : BaseModel, IDisposable {
|
|||||||
else {
|
else {
|
||||||
matchingAttachmentCount = null;
|
matchingAttachmentCount = null;
|
||||||
UpdateFilterStatisticsText();
|
UpdateFilterStatisticsText();
|
||||||
matchingAttachmentCountComputer.Compute(() => state.Db.CountAttachments(filter));
|
matchingAttachmentCountComputer.Compute(() => db.CountAttachments(filter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -8,7 +8,6 @@ using Avalonia.Controls;
|
|||||||
using DHT.Desktop.Common;
|
using DHT.Desktop.Common;
|
||||||
using DHT.Desktop.Dialogs.CheckBox;
|
using DHT.Desktop.Dialogs.CheckBox;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Server;
|
|
||||||
using DHT.Server.Data;
|
using DHT.Server.Data;
|
||||||
using DHT.Server.Data.Filters;
|
using DHT.Server.Data.Filters;
|
||||||
using DHT.Server.Database;
|
using DHT.Server.Database;
|
||||||
@@ -63,7 +62,7 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HashSet<ulong> IncludedChannels {
|
public HashSet<ulong> IncludedChannels {
|
||||||
get => includedChannels ?? state.Db.GetAllChannels().Select(static channel => channel.Id).ToHashSet();
|
get => includedChannels ?? db.GetAllChannels().Select(static channel => channel.Id).ToHashSet();
|
||||||
set => Change(ref includedChannels, value);
|
set => Change(ref includedChannels, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +72,7 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public HashSet<ulong> IncludedUsers {
|
public HashSet<ulong> IncludedUsers {
|
||||||
get => includedUsers ?? state.Db.GetAllUsers().Select(static user => user.Id).ToHashSet();
|
get => includedUsers ?? db.GetAllUsers().Select(static user => user.Id).ToHashSet();
|
||||||
set => Change(ref includedUsers, value);
|
set => Change(ref includedUsers, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +91,7 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private readonly Window window;
|
private readonly Window window;
|
||||||
private readonly State state;
|
private readonly IDatabaseFile db;
|
||||||
private readonly string verb;
|
private readonly string verb;
|
||||||
|
|
||||||
private readonly AsyncValueComputer<long> exportedMessageCountComputer;
|
private readonly AsyncValueComputer<long> exportedMessageCountComputer;
|
||||||
@@ -100,11 +99,11 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
private long? totalMessageCount;
|
private long? totalMessageCount;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public MessageFilterPanelModel() : this(null!, State.Dummy) {}
|
public MessageFilterPanelModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
public MessageFilterPanelModel(Window window, State state, string verb = "Matches") {
|
public MessageFilterPanelModel(Window window, IDatabaseFile db, string verb = "Matches") {
|
||||||
this.window = window;
|
this.window = window;
|
||||||
this.state = state;
|
this.db = db;
|
||||||
this.verb = verb;
|
this.verb = verb;
|
||||||
|
|
||||||
this.exportedMessageCountComputer = AsyncValueComputer<long>.WithResultProcessor(SetExportedMessageCount).Build();
|
this.exportedMessageCountComputer = AsyncValueComputer<long>.WithResultProcessor(SetExportedMessageCount).Build();
|
||||||
@@ -114,11 +113,11 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
UpdateUserFilterLabel();
|
UpdateUserFilterLabel();
|
||||||
|
|
||||||
PropertyChanged += OnPropertyChanged;
|
PropertyChanged += OnPropertyChanged;
|
||||||
state.Db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
state.Db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e) {
|
||||||
@@ -137,7 +136,7 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
|
|
||||||
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
||||||
if (e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) {
|
if (e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) {
|
||||||
totalMessageCount = state.Db.Statistics.TotalMessages;
|
totalMessageCount = db.Statistics.TotalMessages;
|
||||||
UpdateFilterStatistics();
|
UpdateFilterStatistics();
|
||||||
}
|
}
|
||||||
else if (e.PropertyName == nameof(DatabaseStatistics.TotalChannels)) {
|
else if (e.PropertyName == nameof(DatabaseStatistics.TotalChannels)) {
|
||||||
@@ -158,7 +157,7 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
else {
|
else {
|
||||||
exportedMessageCount = null;
|
exportedMessageCount = null;
|
||||||
UpdateFilterStatisticsText();
|
UpdateFilterStatisticsText();
|
||||||
exportedMessageCountComputer.Compute(() => state.Db.CountMessages(filter));
|
exportedMessageCountComputer.Compute(() => db.CountMessages(filter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,11 +175,11 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async void OpenChannelFilterDialog() {
|
public async void OpenChannelFilterDialog() {
|
||||||
var servers = state.Db.GetAllServers().ToDictionary(static server => server.Id);
|
var servers = db.GetAllServers().ToDictionary(static server => server.Id);
|
||||||
var items = new List<CheckBoxItem<ulong>>();
|
var items = new List<CheckBoxItem<ulong>>();
|
||||||
var included = IncludedChannels;
|
var included = IncludedChannels;
|
||||||
|
|
||||||
foreach (var channel in state.Db.GetAllChannels()) {
|
foreach (var channel in db.GetAllChannels()) {
|
||||||
var channelId = channel.Id;
|
var channelId = channel.Id;
|
||||||
var channelName = channel.Name;
|
var channelName = channel.Name;
|
||||||
|
|
||||||
@@ -224,7 +223,7 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
var items = new List<CheckBoxItem<ulong>>();
|
var items = new List<CheckBoxItem<ulong>>();
|
||||||
var included = IncludedUsers;
|
var included = IncludedUsers;
|
||||||
|
|
||||||
foreach (var user in state.Db.GetAllUsers()) {
|
foreach (var user in db.GetAllUsers()) {
|
||||||
var name = user.Name;
|
var name = user.Name;
|
||||||
var discriminator = user.Discriminator;
|
var discriminator = user.Discriminator;
|
||||||
|
|
||||||
@@ -241,13 +240,13 @@ sealed class MessageFilterPanelModel : BaseModel, IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateChannelFilterLabel() {
|
private void UpdateChannelFilterLabel() {
|
||||||
long total = state.Db.Statistics.TotalChannels;
|
long total = db.Statistics.TotalChannels;
|
||||||
long included = FilterByChannel ? IncludedChannels.Count : total;
|
long included = FilterByChannel ? IncludedChannels.Count : total;
|
||||||
ChannelFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("channel") + ".";
|
ChannelFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("channel") + ".";
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateUserFilterLabel() {
|
private void UpdateUserFilterLabel() {
|
||||||
long total = state.Db.Statistics.TotalUsers;
|
long total = db.Statistics.TotalUsers;
|
||||||
long included = FilterByUser ? IncludedUsers.Count : total;
|
long included = FilterByUser ? IncludedUsers.Count : total;
|
||||||
UserFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("user") + ".";
|
UserFilterLabel = "Selected " + included.Format() + " / " + total.Pluralize("user") + ".";
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ using System;
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Server;
|
using DHT.Desktop.Server;
|
||||||
using DHT.Server;
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Service;
|
using DHT.Server.Service;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ sealed class ServerConfigurationPanelModel : BaseModel, IDisposable {
|
|||||||
private readonly ServerManager serverManager;
|
private readonly ServerManager serverManager;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public ServerConfigurationPanelModel() : this(null!, new ServerManager(State.Dummy)) {}
|
public ServerConfigurationPanelModel() : this(null!, new ServerManager(DummyDatabaseFile.Instance)) {}
|
||||||
|
|
||||||
public ServerConfigurationPanelModel(Window window, ServerManager serverManager) {
|
public ServerConfigurationPanelModel(Window window, ServerManager serverManager) {
|
||||||
this.window = window;
|
this.window = window;
|
||||||
|
@@ -45,17 +45,17 @@
|
|||||||
<Rectangle />
|
<Rectangle />
|
||||||
<StackPanel Orientation="Vertical">
|
<StackPanel Orientation="Vertical">
|
||||||
<TextBlock Classes="label">Servers</TextBlock>
|
<TextBlock Classes="label">Servers</TextBlock>
|
||||||
<TextBlock Classes="value" Text="{Binding DatabaseStatistics.TotalServers, Mode=OneWay, Converter={StaticResource NumberValueConverter}}" />
|
<TextBlock Classes="value" Text="{Binding DatabaseStatistics.TotalServers, Converter={StaticResource NumberValueConverter}}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<Rectangle />
|
<Rectangle />
|
||||||
<StackPanel Orientation="Vertical">
|
<StackPanel Orientation="Vertical">
|
||||||
<TextBlock Classes="label">Channels</TextBlock>
|
<TextBlock Classes="label">Channels</TextBlock>
|
||||||
<TextBlock Classes="value" Text="{Binding DatabaseStatistics.TotalChannels, Mode=OneWay, Converter={StaticResource NumberValueConverter}}" />
|
<TextBlock Classes="value" Text="{Binding DatabaseStatistics.TotalChannels, Converter={StaticResource NumberValueConverter}}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<Rectangle />
|
<Rectangle />
|
||||||
<StackPanel Orientation="Vertical">
|
<StackPanel Orientation="Vertical">
|
||||||
<TextBlock Classes="label">Messages</TextBlock>
|
<TextBlock Classes="label">Messages</TextBlock>
|
||||||
<TextBlock Classes="value" Text="{Binding DatabaseStatistics.TotalMessages, Mode=OneWay, Converter={StaticResource NumberValueConverter}}" />
|
<TextBlock Classes="value" Text="{Binding DatabaseStatistics.TotalMessages, Converter={StaticResource NumberValueConverter}}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
|
@@ -11,7 +11,7 @@
|
|||||||
Width="800" Height="500"
|
Width="800" Height="500"
|
||||||
MinWidth="520" MinHeight="300"
|
MinWidth="520" MinHeight="300"
|
||||||
WindowStartupLocation="CenterScreen"
|
WindowStartupLocation="CenterScreen"
|
||||||
Closing="OnClosing">
|
Closed="OnClosed">
|
||||||
|
|
||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<main:MainWindowModel />
|
<main:MainWindowModel />
|
||||||
|
@@ -1,18 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using DHT.Desktop.Main.Pages;
|
using DHT.Desktop.Main.Pages;
|
||||||
using DHT.Utils.Logging;
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
|
||||||
namespace DHT.Desktop.Main;
|
namespace DHT.Desktop.Main;
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
public sealed partial class MainWindow : Window {
|
public sealed partial class MainWindow : Window {
|
||||||
private static readonly Log Log = Log.ForType<MainWindow>();
|
|
||||||
|
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
public MainWindow() {
|
public MainWindow() {
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@@ -24,24 +20,9 @@ public sealed partial class MainWindow : Window {
|
|||||||
DataContext = new MainWindowModel(this, args);
|
DataContext = new MainWindowModel(this, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void OnClosing(object? sender, WindowClosingEventArgs e) {
|
public void OnClosed(object? sender, EventArgs e) {
|
||||||
e.Cancel = true;
|
if (DataContext is IDisposable disposable) {
|
||||||
Closing -= OnClosing;
|
disposable.Dispose();
|
||||||
|
|
||||||
try {
|
|
||||||
await Dispose();
|
|
||||||
} finally {
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task Dispose() {
|
|
||||||
if (DataContext is MainWindowModel model) {
|
|
||||||
try {
|
|
||||||
await model.DisposeAsync();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Log.Error("Caught exception while disposing window: " + ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var temporaryFile in ViewerPageModel.TemporaryFiles) {
|
foreach (var temporaryFile in ViewerPageModel.TemporaryFiles) {
|
||||||
|
@@ -7,12 +7,12 @@ using Avalonia.Controls;
|
|||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Main.Screens;
|
using DHT.Desktop.Main.Screens;
|
||||||
using DHT.Desktop.Server;
|
using DHT.Desktop.Server;
|
||||||
using DHT.Server;
|
using DHT.Server.Database;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
namespace DHT.Desktop.Main;
|
namespace DHT.Desktop.Main;
|
||||||
|
|
||||||
sealed class MainWindowModel : BaseModel, IAsyncDisposable {
|
sealed class MainWindowModel : BaseModel, IDisposable {
|
||||||
private const string DefaultTitle = "Discord History Tracker";
|
private const string DefaultTitle = "Discord History Tracker";
|
||||||
|
|
||||||
public string Title { get; private set; } = DefaultTitle;
|
public string Title { get; private set; } = DefaultTitle;
|
||||||
@@ -27,7 +27,7 @@ sealed class MainWindowModel : BaseModel, IAsyncDisposable {
|
|||||||
|
|
||||||
private readonly Window window;
|
private readonly Window window;
|
||||||
|
|
||||||
private State? state;
|
private IDatabaseFile? db;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public MainWindowModel() : this(null!, Arguments.Empty) {}
|
public MainWindowModel() : this(null!, Arguments.Empty) {}
|
||||||
@@ -78,21 +78,18 @@ sealed class MainWindowModel : BaseModel, IAsyncDisposable {
|
|||||||
mainContentScreenModel.Dispose();
|
mainContentScreenModel.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state != null) {
|
db?.Dispose();
|
||||||
await state.DisposeAsync();
|
db = welcomeScreenModel.Db;
|
||||||
}
|
|
||||||
|
|
||||||
state = welcomeScreenModel.Db == null ? null : new State(welcomeScreenModel.Db);
|
|
||||||
|
|
||||||
if (state == null) {
|
if (db == null) {
|
||||||
Title = DefaultTitle;
|
Title = DefaultTitle;
|
||||||
mainContentScreenModel = null;
|
mainContentScreenModel = null;
|
||||||
mainContentScreen = null;
|
mainContentScreen = null;
|
||||||
CurrentScreen = welcomeScreen;
|
CurrentScreen = welcomeScreen;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Title = Path.GetFileName(state.Db.Path) + " - " + DefaultTitle;
|
Title = Path.GetFileName(db.Path) + " - " + DefaultTitle;
|
||||||
mainContentScreenModel = new MainContentScreenModel(window, state);
|
mainContentScreenModel = new MainContentScreenModel(window, db);
|
||||||
await mainContentScreenModel.Initialize();
|
await mainContentScreenModel.Initialize();
|
||||||
mainContentScreenModel.DatabaseClosed += MainContentScreenModelOnDatabaseClosed;
|
mainContentScreenModel.DatabaseClosed += MainContentScreenModelOnDatabaseClosed;
|
||||||
mainContentScreen = new MainContentScreen { DataContext = mainContentScreenModel };
|
mainContentScreen = new MainContentScreen { DataContext = mainContentScreenModel };
|
||||||
@@ -110,14 +107,10 @@ sealed class MainWindowModel : BaseModel, IAsyncDisposable {
|
|||||||
welcomeScreenModel.CloseDatabase();
|
welcomeScreenModel.CloseDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask DisposeAsync() {
|
public void Dispose() {
|
||||||
mainContentScreenModel?.Dispose();
|
|
||||||
|
|
||||||
if (state != null) {
|
|
||||||
await state.DisposeAsync();
|
|
||||||
state = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
welcomeScreenModel.Dispose();
|
welcomeScreenModel.Dispose();
|
||||||
|
mainContentScreenModel?.Dispose();
|
||||||
|
db?.Dispose();
|
||||||
|
db = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3,7 +3,7 @@ using Avalonia.Controls;
|
|||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Main.Controls;
|
using DHT.Desktop.Main.Controls;
|
||||||
using DHT.Desktop.Server;
|
using DHT.Desktop.Server;
|
||||||
using DHT.Server;
|
using DHT.Server.Database;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages;
|
namespace DHT.Desktop.Main.Pages;
|
||||||
@@ -12,14 +12,14 @@ sealed class AdvancedPageModel : BaseModel, IDisposable {
|
|||||||
public ServerConfigurationPanelModel ServerConfigurationModel { get; }
|
public ServerConfigurationPanelModel ServerConfigurationModel { get; }
|
||||||
|
|
||||||
private readonly Window window;
|
private readonly Window window;
|
||||||
private readonly State state;
|
private readonly IDatabaseFile db;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public AdvancedPageModel() : this(null!, State.Dummy, new ServerManager(State.Dummy)) {}
|
public AdvancedPageModel() : this(null!, DummyDatabaseFile.Instance, new ServerManager(DummyDatabaseFile.Instance)) {}
|
||||||
|
|
||||||
public AdvancedPageModel(Window window, State state, ServerManager serverManager) {
|
public AdvancedPageModel(Window window, IDatabaseFile db, ServerManager serverManager) {
|
||||||
this.window = window;
|
this.window = window;
|
||||||
this.state = state;
|
this.db = db;
|
||||||
|
|
||||||
ServerConfigurationModel = new ServerConfigurationPanelModel(window, serverManager);
|
ServerConfigurationModel = new ServerConfigurationPanelModel(window, serverManager);
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ sealed class AdvancedPageModel : BaseModel, IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async void VacuumDatabase() {
|
public async void VacuumDatabase() {
|
||||||
state.Db.Vacuum();
|
db.Vacuum();
|
||||||
await Dialog.ShowOk(window, "Vacuum Database", "Done.");
|
await Dialog.ShowOk(window, "Vacuum Database", "Done.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -33,7 +33,7 @@
|
|||||||
<StackPanel Orientation="Vertical" Spacing="20">
|
<StackPanel Orientation="Vertical" Spacing="20">
|
||||||
<DockPanel>
|
<DockPanel>
|
||||||
<Button Command="{Binding OnClickToggleDownload}" Content="{Binding ToggleDownloadButtonText}" IsEnabled="{Binding IsToggleDownloadButtonEnabled}" DockPanel.Dock="Left" />
|
<Button Command="{Binding OnClickToggleDownload}" Content="{Binding ToggleDownloadButtonText}" IsEnabled="{Binding IsToggleDownloadButtonEnabled}" DockPanel.Dock="Left" />
|
||||||
<TextBlock Text="{Binding DownloadMessage}" MinWidth="100" Margin="10 0 0 0" VerticalAlignment="Center" TextAlignment="Right" 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" />
|
<ProgressBar Value="{Binding DownloadProgress}" IsVisible="{Binding IsDownloading}" Margin="15 0" VerticalAlignment="Center" DockPanel.Dock="Right" />
|
||||||
</DockPanel>
|
</DockPanel>
|
||||||
<controls:AttachmentFilterPanel DataContext="{Binding FilterModel}" IsEnabled="{Binding !IsDownloading, RelativeSource={RelativeSource AncestorType=pages:AttachmentsPageModel}}" />
|
<controls:AttachmentFilterPanel DataContext="{Binding FilterModel}" IsEnabled="{Binding !IsDownloading, RelativeSource={RelativeSource AncestorType=pages:AttachmentsPageModel}}" />
|
||||||
@@ -42,8 +42,8 @@
|
|||||||
<DataGrid ItemsSource="{Binding StatisticsRows}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="True">
|
<DataGrid ItemsSource="{Binding StatisticsRows}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" IsReadOnly="True">
|
||||||
<DataGrid.Columns>
|
<DataGrid.Columns>
|
||||||
<DataGridTextColumn Header="State" Binding="{Binding State}" Width="*" />
|
<DataGridTextColumn Header="State" Binding="{Binding State}" Width="*" />
|
||||||
<DataGridTextColumn Header="Attachments" Binding="{Binding Items, Mode=OneWay, Converter={StaticResource NumberValueConverter}}" Width="*" CellStyleClasses="right" />
|
<DataGridTextColumn Header="Attachments" Binding="{Binding Items, Converter={StaticResource NumberValueConverter}}" Width="*" CellStyleClasses="right" />
|
||||||
<DataGridTextColumn Header="Size" Binding="{Binding Size, Mode=OneWay, Converter={StaticResource BytesValueConverter}}" Width="*" CellStyleClasses="right" />
|
<DataGridTextColumn Header="Size" Binding="{Binding Size, Converter={StaticResource BytesValueConverter}}" Width="*" CellStyleClasses="right" />
|
||||||
</DataGrid.Columns>
|
</DataGrid.Columns>
|
||||||
</DataGrid>
|
</DataGrid>
|
||||||
</Expander>
|
</Expander>
|
||||||
|
@@ -2,11 +2,9 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
using DHT.Desktop.Common;
|
using DHT.Desktop.Common;
|
||||||
using DHT.Desktop.Main.Controls;
|
using DHT.Desktop.Main.Controls;
|
||||||
using DHT.Server;
|
|
||||||
using DHT.Server.Data;
|
using DHT.Server.Data;
|
||||||
using DHT.Server.Data.Aggregations;
|
using DHT.Server.Data.Aggregations;
|
||||||
using DHT.Server.Data.Filters;
|
using DHT.Server.Data.Filters;
|
||||||
@@ -18,16 +16,15 @@ using DHT.Utils.Tasks;
|
|||||||
namespace DHT.Desktop.Main.Pages;
|
namespace DHT.Desktop.Main.Pages;
|
||||||
|
|
||||||
sealed class AttachmentsPageModel : BaseModel, IDisposable {
|
sealed class AttachmentsPageModel : BaseModel, IDisposable {
|
||||||
private static readonly DownloadItemFilter EnqueuedItemFilter = new () {
|
private static readonly DownloadItemFilter EnqueuedItemFilter = new() {
|
||||||
IncludeStatuses = new HashSet<DownloadStatus> {
|
IncludeStatuses = new HashSet<DownloadStatus> {
|
||||||
DownloadStatus.Enqueued,
|
DownloadStatus.Enqueued
|
||||||
DownloadStatus.Downloading
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private bool isThreadDownloadButtonEnabled = true;
|
private bool isThreadDownloadButtonEnabled = true;
|
||||||
|
|
||||||
public string ToggleDownloadButtonText => IsDownloading ? "Stop Downloading" : "Start Downloading";
|
public string ToggleDownloadButtonText => downloadThread == null ? "Start Downloading" : "Stop Downloading";
|
||||||
|
|
||||||
public bool IsToggleDownloadButtonEnabled {
|
public bool IsToggleDownloadButtonEnabled {
|
||||||
get => isThreadDownloadButtonEnabled;
|
get => isThreadDownloadButtonEnabled;
|
||||||
@@ -35,7 +32,7 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public string DownloadMessage { get; set; } = "";
|
public string DownloadMessage { get; set; } = "";
|
||||||
public double DownloadProgress => totalItemsToDownloadCount is null or 0 ? 0.0 : 100.0 * doneItemsCount / totalItemsToDownloadCount.Value;
|
public double DownloadProgress => allItemsCount is null or 0 ? 0.0 : 100.0 * doneItemsCount / allItemsCount.Value;
|
||||||
|
|
||||||
public AttachmentFilterPanelModel FilterModel { get; }
|
public AttachmentFilterPanelModel FilterModel { get; }
|
||||||
|
|
||||||
@@ -55,34 +52,33 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsDownloading => state.Downloader.IsDownloading;
|
public bool IsDownloading => downloadThread != null;
|
||||||
public bool HasFailedDownloads => statisticsFailed.Items > 0;
|
public bool HasFailedDownloads => statisticsFailed.Items > 0;
|
||||||
|
|
||||||
private readonly State state;
|
private readonly IDatabaseFile db;
|
||||||
private readonly AsyncValueComputer<DownloadStatusStatistics>.Single downloadStatisticsComputer;
|
private readonly AsyncValueComputer<DownloadStatusStatistics>.Single downloadStatisticsComputer;
|
||||||
|
private BackgroundDownloadThread? downloadThread;
|
||||||
|
|
||||||
private int doneItemsCount;
|
private int doneItemsCount;
|
||||||
private int initialFinishedCount;
|
private int? allItemsCount;
|
||||||
private int? totalItemsToDownloadCount;
|
|
||||||
|
|
||||||
public AttachmentsPageModel() : this(State.Dummy) {}
|
public AttachmentsPageModel() : this(DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
public AttachmentsPageModel(State state) {
|
public AttachmentsPageModel(IDatabaseFile db) {
|
||||||
this.state = state;
|
this.db = db;
|
||||||
|
this.FilterModel = new AttachmentFilterPanelModel(db);
|
||||||
FilterModel = new AttachmentFilterPanelModel(state);
|
|
||||||
|
|
||||||
downloadStatisticsComputer = AsyncValueComputer<DownloadStatusStatistics>.WithResultProcessor(UpdateStatistics).WithOutdatedResults().BuildWithComputer(state.Db.GetDownloadStatusStatistics);
|
this.downloadStatisticsComputer = AsyncValueComputer<DownloadStatusStatistics>.WithResultProcessor(UpdateStatistics).WithOutdatedResults().BuildWithComputer(db.GetDownloadStatusStatistics);
|
||||||
downloadStatisticsComputer.Recompute();
|
this.downloadStatisticsComputer.Recompute();
|
||||||
|
|
||||||
state.Db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
db.Statistics.PropertyChanged += OnDbStatisticsChanged;
|
||||||
state.Downloader.OnItemFinished += DownloaderOnOnItemFinished;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
state.Db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
db.Statistics.PropertyChanged -= OnDbStatisticsChanged;
|
||||||
state.Downloader.OnItemFinished -= DownloaderOnOnItemFinished;
|
|
||||||
FilterModel.Dispose();
|
FilterModel.Dispose();
|
||||||
|
DisposeDownloadThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
|
||||||
@@ -102,7 +98,7 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
|
|||||||
private void EnqueueDownloadItems() {
|
private void EnqueueDownloadItems() {
|
||||||
var filter = FilterModel.CreateFilter();
|
var filter = FilterModel.CreateFilter();
|
||||||
filter.DownloadItemRule = AttachmentFilter.DownloadItemRules.OnlyNotPresent;
|
filter.DownloadItemRule = AttachmentFilter.DownloadItemRules.OnlyNotPresent;
|
||||||
state.Db.EnqueueDownloadItems(filter);
|
db.EnqueueDownloadItems(filter);
|
||||||
|
|
||||||
downloadStatisticsComputer.Recompute();
|
downloadStatisticsComputer.Recompute();
|
||||||
}
|
}
|
||||||
@@ -128,42 +124,45 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
|
|||||||
OnPropertyChanged(nameof(HasFailedDownloads));
|
OnPropertyChanged(nameof(HasFailedDownloads));
|
||||||
}
|
}
|
||||||
|
|
||||||
totalItemsToDownloadCount = statisticsEnqueued.Items + statisticsDownloaded.Items + statisticsFailed.Items - initialFinishedCount;
|
allItemsCount = doneItemsCount + statisticsEnqueued.Items;
|
||||||
UpdateDownloadMessage();
|
UpdateDownloadMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateDownloadMessage() {
|
private void UpdateDownloadMessage() {
|
||||||
DownloadMessage = IsDownloading ? doneItemsCount.Format() + " / " + (totalItemsToDownloadCount?.Format() ?? "?") : "";
|
DownloadMessage = IsDownloading ? doneItemsCount.Format() + " / " + (allItemsCount?.Format() ?? "?") : "";
|
||||||
|
|
||||||
OnPropertyChanged(nameof(DownloadMessage));
|
OnPropertyChanged(nameof(DownloadMessage));
|
||||||
OnPropertyChanged(nameof(DownloadProgress));
|
OnPropertyChanged(nameof(DownloadProgress));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DownloaderOnOnItemFinished(object? sender, DownloadItem e) {
|
private void DownloadThreadOnOnItemFinished(object? sender, DownloadItem e) {
|
||||||
Interlocked.Increment(ref doneItemsCount);
|
Interlocked.Increment(ref doneItemsCount);
|
||||||
|
|
||||||
Dispatcher.UIThread.Invoke(UpdateDownloadMessage);
|
Dispatcher.UIThread.Invoke(UpdateDownloadMessage);
|
||||||
downloadStatisticsComputer.Recompute();
|
downloadStatisticsComputer.Recompute();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnClickToggleDownload() {
|
private void DownloadThreadOnOnServerStopped(object? sender, EventArgs e) {
|
||||||
if (IsDownloading) {
|
downloadStatisticsComputer.Recompute();
|
||||||
IsToggleDownloadButtonEnabled = false;
|
IsToggleDownloadButtonEnabled = true;
|
||||||
await state.Downloader.Stop();
|
}
|
||||||
downloadStatisticsComputer.Recompute();
|
|
||||||
IsToggleDownloadButtonEnabled = true;
|
|
||||||
|
|
||||||
state.Db.RemoveDownloadItems(EnqueuedItemFilter, FilterRemovalMode.RemoveMatching);
|
public void OnClickToggleDownload() {
|
||||||
|
if (downloadThread == null) {
|
||||||
doneItemsCount = 0;
|
EnqueueDownloadItems();
|
||||||
initialFinishedCount = 0;
|
downloadThread = new BackgroundDownloadThread(db);
|
||||||
totalItemsToDownloadCount = null;
|
downloadThread.OnItemFinished += DownloadThreadOnOnItemFinished;
|
||||||
UpdateDownloadMessage();
|
downloadThread.OnServerStopped += DownloadThreadOnOnServerStopped;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
initialFinishedCount = statisticsDownloaded.Items + statisticsFailed.Items;
|
IsToggleDownloadButtonEnabled = false;
|
||||||
await state.Downloader.Start();
|
DisposeDownloadThread();
|
||||||
EnqueueDownloadItems();
|
|
||||||
|
db.RemoveDownloadItems(EnqueuedItemFilter, FilterRemovalMode.RemoveMatching);
|
||||||
|
|
||||||
|
doneItemsCount = 0;
|
||||||
|
allItemsCount = null;
|
||||||
|
UpdateDownloadMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
OnPropertyChanged(nameof(ToggleDownloadButtonText));
|
OnPropertyChanged(nameof(ToggleDownloadButtonText));
|
||||||
@@ -174,18 +173,26 @@ sealed class AttachmentsPageModel : BaseModel, IDisposable {
|
|||||||
var allExceptFailedFilter = new DownloadItemFilter {
|
var allExceptFailedFilter = new DownloadItemFilter {
|
||||||
IncludeStatuses = new HashSet<DownloadStatus> {
|
IncludeStatuses = new HashSet<DownloadStatus> {
|
||||||
DownloadStatus.Enqueued,
|
DownloadStatus.Enqueued,
|
||||||
DownloadStatus.Downloading,
|
|
||||||
DownloadStatus.Success
|
DownloadStatus.Success
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
state.Db.RemoveDownloadItems(allExceptFailedFilter, FilterRemovalMode.KeepMatching);
|
db.RemoveDownloadItems(allExceptFailedFilter, FilterRemovalMode.KeepMatching);
|
||||||
|
|
||||||
if (IsDownloading) {
|
if (IsDownloading) {
|
||||||
EnqueueDownloadItems();
|
EnqueueDownloadItems();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void DisposeDownloadThread() {
|
||||||
|
if (downloadThread != null) {
|
||||||
|
downloadThread.OnItemFinished -= DownloadThreadOnOnItemFinished;
|
||||||
|
downloadThread.StopThread();
|
||||||
|
}
|
||||||
|
|
||||||
|
downloadThread = null;
|
||||||
|
}
|
||||||
|
|
||||||
public sealed class StatisticsRow {
|
public sealed class StatisticsRow {
|
||||||
public string State { get; }
|
public string State { get; }
|
||||||
public int Items { get; set; }
|
public int Items { get; set; }
|
||||||
|
@@ -14,7 +14,6 @@ using DHT.Desktop.Dialogs.File;
|
|||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Dialogs.Progress;
|
using DHT.Desktop.Dialogs.Progress;
|
||||||
using DHT.Desktop.Dialogs.TextBox;
|
using DHT.Desktop.Dialogs.TextBox;
|
||||||
using DHT.Server;
|
|
||||||
using DHT.Server.Data;
|
using DHT.Server.Data;
|
||||||
using DHT.Server.Database;
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Database.Import;
|
using DHT.Server.Database.Import;
|
||||||
@@ -22,7 +21,7 @@ using DHT.Server.Database.Sqlite;
|
|||||||
using DHT.Utils.Logging;
|
using DHT.Utils.Logging;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages;
|
namespace DHT.Desktop.Main.Pages;
|
||||||
|
|
||||||
sealed class DatabasePageModel : BaseModel {
|
sealed class DatabasePageModel : BaseModel {
|
||||||
private static readonly Log Log = Log.ForType<DatabasePageModel>();
|
private static readonly Log Log = Log.ForType<DatabasePageModel>();
|
||||||
@@ -34,11 +33,11 @@ sealed class DatabasePageModel : BaseModel {
|
|||||||
private readonly Window window;
|
private readonly Window window;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public DatabasePageModel() : this(null!, State.Dummy) {}
|
public DatabasePageModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
public DatabasePageModel(Window window, State state) {
|
public DatabasePageModel(Window window, IDatabaseFile db) {
|
||||||
this.window = window;
|
this.window = window;
|
||||||
this.Db = state.Db;
|
this.Db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void OpenDatabaseFolder() {
|
public async void OpenDatabaseFolder() {
|
||||||
|
@@ -6,8 +6,8 @@ using System.Threading.Tasks;
|
|||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Dialogs.Progress;
|
using DHT.Desktop.Dialogs.Progress;
|
||||||
using DHT.Server;
|
|
||||||
using DHT.Server.Data;
|
using DHT.Server.Data;
|
||||||
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Service;
|
using DHT.Server.Service;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
|
|
||||||
@@ -18,14 +18,14 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
public string GenerateMessages { get; set; } = "0";
|
public string GenerateMessages { get; set; } = "0";
|
||||||
|
|
||||||
private readonly Window window;
|
private readonly Window window;
|
||||||
private readonly State state;
|
private readonly IDatabaseFile db;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public DebugPageModel() : this(null!, State.Dummy) {}
|
public DebugPageModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
public DebugPageModel(Window window, State state) {
|
public DebugPageModel(Window window, IDatabaseFile db) {
|
||||||
this.window = window;
|
this.window = window;
|
||||||
this.state = state;
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void OnClickAddRandomDataToDatabase() {
|
public async void OnClickAddRandomDataToDatabase() {
|
||||||
@@ -83,11 +83,11 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
Discriminator = rand.Next(0, 9999).ToString(),
|
Discriminator = rand.Next(0, 9999).ToString(),
|
||||||
}).ToArray();
|
}).ToArray();
|
||||||
|
|
||||||
state.Db.AddServer(server);
|
db.AddServer(server);
|
||||||
state.Db.AddUsers(users);
|
db.AddUsers(users);
|
||||||
|
|
||||||
foreach (var channel in channels) {
|
foreach (var channel in channels) {
|
||||||
state.Db.AddChannel(channel);
|
db.AddChannel(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
var now = DateTimeOffset.Now;
|
var now = DateTimeOffset.Now;
|
||||||
@@ -117,7 +117,7 @@ namespace DHT.Desktop.Main.Pages {
|
|||||||
};
|
};
|
||||||
}).ToArray();
|
}).ToArray();
|
||||||
|
|
||||||
state.Db.AddMessages(messages);
|
db.AddMessages(messages);
|
||||||
|
|
||||||
messageCount -= BatchSize;
|
messageCount -= BatchSize;
|
||||||
await callback.Update("Adding messages in batches of " + BatchSize, ++batchIndex, batchCount);
|
await callback.Update("Adding messages in batches of " + BatchSize, ++batchIndex, batchCount);
|
||||||
|
@@ -13,8 +13,8 @@ using DHT.Desktop.Dialogs.File;
|
|||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Main.Controls;
|
using DHT.Desktop.Main.Controls;
|
||||||
using DHT.Desktop.Server;
|
using DHT.Desktop.Server;
|
||||||
using DHT.Server;
|
|
||||||
using DHT.Server.Data.Filters;
|
using DHT.Server.Data.Filters;
|
||||||
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Database.Export;
|
using DHT.Server.Database.Export;
|
||||||
using DHT.Server.Database.Export.Strategy;
|
using DHT.Server.Database.Export.Strategy;
|
||||||
using DHT.Utils.Models;
|
using DHT.Utils.Models;
|
||||||
@@ -38,16 +38,16 @@ sealed class ViewerPageModel : BaseModel, IDisposable {
|
|||||||
public MessageFilterPanelModel FilterModel { get; }
|
public MessageFilterPanelModel FilterModel { get; }
|
||||||
|
|
||||||
private readonly Window window;
|
private readonly Window window;
|
||||||
private readonly State state;
|
private readonly IDatabaseFile db;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public ViewerPageModel() : this(null!, State.Dummy) {}
|
public ViewerPageModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
public ViewerPageModel(Window window, State state) {
|
public ViewerPageModel(Window window, IDatabaseFile db) {
|
||||||
this.window = window;
|
this.window = window;
|
||||||
this.state = state;
|
this.db = db;
|
||||||
|
|
||||||
FilterModel = new MessageFilterPanelModel(window, state, "Will export");
|
FilterModel = new MessageFilterPanelModel(window, db, "Will export");
|
||||||
FilterModel.FilterPropertyChanged += OnFilterPropertyChanged;
|
FilterModel.FilterPropertyChanged += OnFilterPropertyChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +65,8 @@ sealed class ViewerPageModel : BaseModel, IDisposable {
|
|||||||
string indexFile = await Resources.ReadTextAsync("Viewer/index.html");
|
string indexFile = await Resources.ReadTextAsync("Viewer/index.html");
|
||||||
string viewerTemplate = indexFile.Replace("/*[JS]*/", await Resources.ReadJoinedAsync("Viewer/scripts/", '\n'))
|
string viewerTemplate = indexFile.Replace("/*[JS]*/", await Resources.ReadJoinedAsync("Viewer/scripts/", '\n'))
|
||||||
.Replace("/*[CSS]*/", await Resources.ReadJoinedAsync("Viewer/styles/", '\n'));
|
.Replace("/*[CSS]*/", await Resources.ReadJoinedAsync("Viewer/styles/", '\n'));
|
||||||
|
|
||||||
|
viewerTemplate = strategy.ProcessViewerTemplate(viewerTemplate);
|
||||||
|
|
||||||
int viewerArchiveTagStart = viewerTemplate.IndexOf(ArchiveTag);
|
int viewerArchiveTagStart = viewerTemplate.IndexOf(ArchiveTag);
|
||||||
int viewerArchiveTagEnd = viewerArchiveTagStart + ArchiveTag.Length;
|
int viewerArchiveTagEnd = viewerArchiveTagStart + ArchiveTag.Length;
|
||||||
@@ -72,7 +74,7 @@ sealed class ViewerPageModel : BaseModel, IDisposable {
|
|||||||
string jsonTempFile = path + ".tmp";
|
string jsonTempFile = path + ".tmp";
|
||||||
|
|
||||||
await using (var jsonStream = new FileStream(jsonTempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) {
|
await using (var jsonStream = new FileStream(jsonTempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) {
|
||||||
await ViewerJsonExport.Generate(jsonStream, strategy, state.Db, FilterModel.CreateFilter());
|
await ViewerJsonExport.Generate(jsonStream, strategy, db, FilterModel.CreateFilter());
|
||||||
|
|
||||||
char[] jsonBuffer = new char[Math.Min(32768, jsonStream.Position)];
|
char[] jsonBuffer = new char[Math.Min(32768, jsonStream.Position)];
|
||||||
jsonStream.Position = 0;
|
jsonStream.Position = 0;
|
||||||
@@ -98,7 +100,7 @@ sealed class ViewerPageModel : BaseModel, IDisposable {
|
|||||||
|
|
||||||
public async void OnClickOpenViewer() {
|
public async void OnClickOpenViewer() {
|
||||||
string rootPath = Path.Combine(Path.GetTempPath(), "DiscordHistoryTracker");
|
string rootPath = Path.Combine(Path.GetTempPath(), "DiscordHistoryTracker");
|
||||||
string filenameBase = Path.GetFileNameWithoutExtension(state.Db.Path) + "-" + DateTime.Now.ToString("yyyy-MM-dd");
|
string filenameBase = Path.GetFileNameWithoutExtension(db.Path) + "-" + DateTime.Now.ToString("yyyy-MM-dd");
|
||||||
string fullPath = Path.Combine(rootPath, filenameBase + ".html");
|
string fullPath = Path.Combine(rootPath, filenameBase + ".html");
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
|
|
||||||
@@ -123,8 +125,8 @@ sealed class ViewerPageModel : BaseModel, IDisposable {
|
|||||||
string? path = await window.StorageProvider.SaveFile(new FilePickerSaveOptions {
|
string? path = await window.StorageProvider.SaveFile(new FilePickerSaveOptions {
|
||||||
Title = "Save Viewer",
|
Title = "Save Viewer",
|
||||||
FileTypeChoices = ViewerFileTypes,
|
FileTypeChoices = ViewerFileTypes,
|
||||||
SuggestedFileName = Path.GetFileNameWithoutExtension(state.Db.Path) + ".html",
|
SuggestedFileName = Path.GetFileNameWithoutExtension(db.Path) + ".html",
|
||||||
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, Path.GetDirectoryName(state.Db.Path)),
|
SuggestedStartLocation = await FileDialogs.GetSuggestedStartLocation(window, Path.GetDirectoryName(db.Path)),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
@@ -136,13 +138,13 @@ sealed class ViewerPageModel : BaseModel, IDisposable {
|
|||||||
var filter = FilterModel.CreateFilter();
|
var filter = FilterModel.CreateFilter();
|
||||||
|
|
||||||
if (DatabaseToolFilterModeKeep) {
|
if (DatabaseToolFilterModeKeep) {
|
||||||
if (DialogResult.YesNo.Yes == await Dialog.ShowYesNo(window, "Keep Matching Messages in This Database", state.Db.CountMessages(filter).Pluralize("message") + " will be kept, and the rest will be removed from this database. This action cannot be undone. Proceed?")) {
|
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?")) {
|
||||||
state.Db.RemoveMessages(filter, FilterRemovalMode.KeepMatching);
|
db.RemoveMessages(filter, FilterRemovalMode.KeepMatching);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (DatabaseToolFilterModeRemove) {
|
else if (DatabaseToolFilterModeRemove) {
|
||||||
if (DialogResult.YesNo.Yes == await Dialog.ShowYesNo(window, "Remove Matching Messages in This Database", state.Db.CountMessages(filter).Pluralize("message") + " will be removed from this database. This action cannot be undone. Proceed?")) {
|
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?")) {
|
||||||
state.Db.RemoveMessages(filter, FilterRemovalMode.RemoveMatching);
|
db.RemoveMessages(filter, FilterRemovalMode.RemoveMatching);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@ using DHT.Desktop.Dialogs.Message;
|
|||||||
using DHT.Desktop.Main.Controls;
|
using DHT.Desktop.Main.Controls;
|
||||||
using DHT.Desktop.Main.Pages;
|
using DHT.Desktop.Main.Pages;
|
||||||
using DHT.Desktop.Server;
|
using DHT.Desktop.Server;
|
||||||
using DHT.Server;
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Service;
|
using DHT.Server.Service;
|
||||||
using DHT.Utils.Logging;
|
using DHT.Utils.Logging;
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ sealed class MainContentScreenModel : IDisposable {
|
|||||||
public bool HasDebugPage => true;
|
public bool HasDebugPage => true;
|
||||||
private DebugPageModel DebugPageModel { get; }
|
private DebugPageModel DebugPageModel { get; }
|
||||||
#else
|
#else
|
||||||
public bool HasDebugPage => false;
|
public bool HasDebugPage => false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public StatusBarModel StatusBarModel { get; }
|
public StatusBarModel StatusBarModel { get; }
|
||||||
@@ -53,37 +53,37 @@ sealed class MainContentScreenModel : IDisposable {
|
|||||||
private readonly ServerManager serverManager;
|
private readonly ServerManager serverManager;
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public MainContentScreenModel() : this(null!, State.Dummy) {}
|
public MainContentScreenModel() : this(null!, DummyDatabaseFile.Instance) {}
|
||||||
|
|
||||||
public MainContentScreenModel(Window window, State state) {
|
public MainContentScreenModel(Window window, IDatabaseFile db) {
|
||||||
this.window = window;
|
this.window = window;
|
||||||
this.serverManager = new ServerManager(state);
|
this.serverManager = new ServerManager(db);
|
||||||
|
|
||||||
ServerLauncher.ServerManagementExceptionCaught += ServerLauncherOnServerManagementExceptionCaught;
|
ServerLauncher.ServerManagementExceptionCaught += ServerLauncherOnServerManagementExceptionCaught;
|
||||||
|
|
||||||
DatabasePageModel = new DatabasePageModel(window, state);
|
DatabasePageModel = new DatabasePageModel(window, db);
|
||||||
DatabasePage = new DatabasePage { DataContext = DatabasePageModel };
|
DatabasePage = new DatabasePage { DataContext = DatabasePageModel };
|
||||||
|
|
||||||
TrackingPageModel = new TrackingPageModel(window);
|
TrackingPageModel = new TrackingPageModel(window);
|
||||||
TrackingPage = new TrackingPage { DataContext = TrackingPageModel };
|
TrackingPage = new TrackingPage { DataContext = TrackingPageModel };
|
||||||
|
|
||||||
AttachmentsPageModel = new AttachmentsPageModel(state);
|
AttachmentsPageModel = new AttachmentsPageModel(db);
|
||||||
AttachmentsPage = new AttachmentsPage { DataContext = AttachmentsPageModel };
|
AttachmentsPage = new AttachmentsPage { DataContext = AttachmentsPageModel };
|
||||||
|
|
||||||
ViewerPageModel = new ViewerPageModel(window, state);
|
ViewerPageModel = new ViewerPageModel(window, db);
|
||||||
ViewerPage = new ViewerPage { DataContext = ViewerPageModel };
|
ViewerPage = new ViewerPage { DataContext = ViewerPageModel };
|
||||||
|
|
||||||
AdvancedPageModel = new AdvancedPageModel(window, state, serverManager);
|
AdvancedPageModel = new AdvancedPageModel(window, db, serverManager);
|
||||||
AdvancedPage = new AdvancedPage { DataContext = AdvancedPageModel };
|
AdvancedPage = new AdvancedPage { DataContext = AdvancedPageModel };
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
DebugPageModel = new DebugPageModel(window, state);
|
DebugPageModel = new DebugPageModel(window, db);
|
||||||
DebugPage = new DebugPage { DataContext = DebugPageModel };
|
DebugPage = new DebugPage { DataContext = DebugPageModel };
|
||||||
#else
|
#else
|
||||||
DebugPage = null;
|
DebugPage = null;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
StatusBarModel = new StatusBarModel(state.Db.Statistics);
|
StatusBarModel = new StatusBarModel(db.Statistics);
|
||||||
|
|
||||||
AdvancedPageModel.ServerConfigurationModel.ServerStatusChanged += OnServerStatusChanged;
|
AdvancedPageModel.ServerConfigurationModel.ServerStatusChanged += OnServerStatusChanged;
|
||||||
DatabaseClosed += OnDatabaseClosed;
|
DatabaseClosed += OnDatabaseClosed;
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using DHT.Server;
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Service;
|
using DHT.Server.Service;
|
||||||
|
|
||||||
namespace DHT.Desktop.Server;
|
namespace DHT.Desktop.Server;
|
||||||
@@ -12,10 +12,10 @@ sealed class ServerManager : IDisposable {
|
|||||||
|
|
||||||
public bool IsRunning => ServerLauncher.IsRunning;
|
public bool IsRunning => ServerLauncher.IsRunning;
|
||||||
|
|
||||||
private readonly State state;
|
private readonly IDatabaseFile db;
|
||||||
|
|
||||||
public ServerManager(State state) {
|
public ServerManager(IDatabaseFile db) {
|
||||||
if (state != State.Dummy) {
|
if (db != DummyDatabaseFile.Instance) {
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
throw new InvalidOperationException("Only one instance of ServerManager can exist at the same time!");
|
throw new InvalidOperationException("Only one instance of ServerManager can exist at the same time!");
|
||||||
}
|
}
|
||||||
@@ -23,11 +23,11 @@ sealed class ServerManager : IDisposable {
|
|||||||
instance = this;
|
instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.state = state;
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Launch() {
|
public void Launch() {
|
||||||
ServerLauncher.Relaunch(Port, Token, state.Db);
|
ServerLauncher.Relaunch(Port, Token, db);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Relaunch(ushort port, string token) {
|
public void Relaunch(ushort port, string token) {
|
||||||
|
@@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window.DHT_EMBEDDED = "/*[ARCHIVE]*/";
|
window.DHT_EMBEDDED = "/*[ARCHIVE]*/";
|
||||||
|
window.DHT_SERVER_URL = "/*[SERVER_URL]*/";
|
||||||
|
window.DHT_SERVER_TOKEN = "/*[SERVER_TOKEN]*/";
|
||||||
/*[JS]*/
|
/*[JS]*/
|
||||||
</script>
|
</script>
|
||||||
<style>
|
<style>
|
||||||
|
@@ -182,15 +182,32 @@ const STATE = (function() {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMessageList = function() {
|
const getMessageList = async function(abortSignal) {
|
||||||
if (!loadedMessages) {
|
if (!loadedMessages) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const messages = getMessages(selectedChannel);
|
const messages = getMessages(selectedChannel);
|
||||||
const startIndex = messagesPerPage * (root.getCurrentPage() - 1);
|
const startIndex = messagesPerPage * (root.getCurrentPage() - 1);
|
||||||
|
const slicedMessages = loadedMessages.slice(startIndex, !messagesPerPage ? undefined : startIndex + messagesPerPage);
|
||||||
|
|
||||||
return loadedMessages.slice(startIndex, !messagesPerPage ? undefined : startIndex + messagesPerPage).map(key => {
|
let messageTexts = null;
|
||||||
|
|
||||||
|
if (window.DHT_SERVER_URL !== null) {
|
||||||
|
const messageIds = new Set(slicedMessages);
|
||||||
|
|
||||||
|
for (const key of slicedMessages) {
|
||||||
|
const message = messages[key];
|
||||||
|
|
||||||
|
if ("r" in message) {
|
||||||
|
messageIds.add(message.r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
messageTexts = await getMessageTextsFromServer(messageIds, abortSignal);
|
||||||
|
}
|
||||||
|
|
||||||
|
return slicedMessages.map(key => {
|
||||||
/**
|
/**
|
||||||
* @type {{}}
|
* @type {{}}
|
||||||
* @property {Number} u
|
* @property {Number} u
|
||||||
@@ -216,6 +233,9 @@ const STATE = (function() {
|
|||||||
if ("m" in message) {
|
if ("m" in message) {
|
||||||
obj["contents"] = message.m;
|
obj["contents"] = message.m;
|
||||||
}
|
}
|
||||||
|
else if (messageTexts && key in messageTexts) {
|
||||||
|
obj["contents"] = messageTexts[key];
|
||||||
|
}
|
||||||
|
|
||||||
if ("e" in message) {
|
if ("e" in message) {
|
||||||
obj["embeds"] = message.e.map(embed => JSON.parse(embed));
|
obj["embeds"] = message.e.map(embed => JSON.parse(embed));
|
||||||
@@ -230,15 +250,16 @@ const STATE = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ("r" in message) {
|
if ("r" in message) {
|
||||||
const replyMessage = getMessageById(message.r);
|
const replyId = message.r;
|
||||||
|
const replyMessage = getMessageById(replyId);
|
||||||
const replyUser = replyMessage ? getUser(replyMessage.u) : null;
|
const replyUser = replyMessage ? getUser(replyMessage.u) : null;
|
||||||
const replyAvatar = replyUser && replyUser.avatar ? { id: getUserId(replyMessage.u), path: replyUser.avatar } : null;
|
const replyAvatar = replyUser && replyUser.avatar ? { id: getUserId(replyMessage.u), path: replyUser.avatar } : null;
|
||||||
|
|
||||||
obj["reply"] = replyMessage ? {
|
obj["reply"] = replyMessage ? {
|
||||||
"id": message.r,
|
"id": replyId,
|
||||||
"user": replyUser,
|
"user": replyUser,
|
||||||
"avatar": replyAvatar,
|
"avatar": replyAvatar,
|
||||||
"contents": replyMessage.m
|
"contents": messageTexts != null && replyId in messageTexts ? messageTexts[replyId] : replyMessage.m,
|
||||||
} : null;
|
} : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,9 +271,35 @@ const STATE = (function() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getMessageTextsFromServer = async function(messageIds, abortSignal) {
|
||||||
|
let idParams = "";
|
||||||
|
|
||||||
|
for (const messageId of messageIds) {
|
||||||
|
idParams += "id=" + encodeURIComponent(messageId) + "&";
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(DHT_SERVER_URL + "/get-messages?" + idParams + "token=" + encodeURIComponent(DHT_SERVER_TOKEN), {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
credentials: "omit",
|
||||||
|
redirect: "error",
|
||||||
|
signal: abortSignal
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.status === 200) {
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new Error("Server returned status " + response.status + " " + response.statusText);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let eventOnUsersRefreshed;
|
let eventOnUsersRefreshed;
|
||||||
let eventOnChannelsRefreshed;
|
let eventOnChannelsRefreshed;
|
||||||
let eventOnMessagesRefreshed;
|
let eventOnMessagesRefreshed;
|
||||||
|
let messageLoaderAborter = null;
|
||||||
|
|
||||||
const triggerUsersRefreshed = function() {
|
const triggerUsersRefreshed = function() {
|
||||||
eventOnUsersRefreshed && eventOnUsersRefreshed(getUserList());
|
eventOnUsersRefreshed && eventOnUsersRefreshed(getUserList());
|
||||||
@@ -263,7 +310,22 @@ const STATE = (function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const triggerMessagesRefreshed = function() {
|
const triggerMessagesRefreshed = function() {
|
||||||
eventOnMessagesRefreshed && eventOnMessagesRefreshed(getMessageList());
|
if (!eventOnMessagesRefreshed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (messageLoaderAborter != null) {
|
||||||
|
messageLoaderAborter.abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
const aborter = new AbortController();
|
||||||
|
messageLoaderAborter = aborter;
|
||||||
|
|
||||||
|
getMessageList(aborter.signal).then(eventOnMessagesRefreshed).finally(() => {
|
||||||
|
if (messageLoaderAborter === aborter) {
|
||||||
|
messageLoaderAborter = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFilteredMessageKeys = function(channel) {
|
const getFilteredMessageKeys = function(channel) {
|
||||||
|
@@ -8,6 +8,5 @@ namespace DHT.Server.Data;
|
|||||||
public enum DownloadStatus {
|
public enum DownloadStatus {
|
||||||
Enqueued = 0,
|
Enqueued = 0,
|
||||||
GenericError = 1,
|
GenericError = 1,
|
||||||
Downloading = 2,
|
|
||||||
Success = HttpStatusCode.OK
|
Success = HttpStatusCode.OK
|
||||||
}
|
}
|
||||||
|
@@ -44,7 +44,7 @@ public sealed class DummyDatabaseFile : IDatabaseFile {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Message> GetMessages(MessageFilter? filter = null) {
|
public List<Message> GetMessages(MessageFilter? filter = null, bool includeText = true) {
|
||||||
return new();
|
return new();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ public sealed class DummyDatabaseFile : IDatabaseFile {
|
|||||||
|
|
||||||
public void EnqueueDownloadItems(AttachmentFilter? filter = null) {}
|
public void EnqueueDownloadItems(AttachmentFilter? filter = null) {}
|
||||||
|
|
||||||
public List<DownloadItem> PullEnqueuedDownloadItems(int count) {
|
public List<DownloadItem> GetEnqueuedDownloadItems(int count) {
|
||||||
return new();
|
return new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,3 +1,3 @@
|
|||||||
namespace DHT.Server.Database.Export;
|
namespace DHT.Server.Database.Export;
|
||||||
|
|
||||||
readonly record struct Snowflake(ulong Id);
|
readonly record struct Snowflake(ulong Id);
|
||||||
|
@@ -3,5 +3,7 @@ using DHT.Server.Data;
|
|||||||
namespace DHT.Server.Database.Export.Strategy;
|
namespace DHT.Server.Database.Export.Strategy;
|
||||||
|
|
||||||
public interface IViewerExportStrategy {
|
public interface IViewerExportStrategy {
|
||||||
|
bool IncludeMessageText { get; }
|
||||||
|
string ProcessViewerTemplate(string template);
|
||||||
string GetAttachmentUrl(Attachment attachment);
|
string GetAttachmentUrl(Attachment attachment);
|
||||||
}
|
}
|
||||||
|
@@ -12,6 +12,13 @@ public sealed class LiveViewerExportStrategy : IViewerExportStrategy {
|
|||||||
this.safeToken = WebUtility.UrlEncode(token);
|
this.safeToken = WebUtility.UrlEncode(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IncludeMessageText => false;
|
||||||
|
|
||||||
|
public string ProcessViewerTemplate(string template) {
|
||||||
|
return template.Replace("/*[SERVER_URL]*/", "http://127.0.0.1:" + safePort)
|
||||||
|
.Replace("/*[SERVER_TOKEN]*/", WebUtility.UrlEncode(safeToken));
|
||||||
|
}
|
||||||
|
|
||||||
public string GetAttachmentUrl(Attachment attachment) {
|
public string GetAttachmentUrl(Attachment attachment) {
|
||||||
return "http://127.0.0.1:" + safePort + "/get-attachment/" + WebUtility.UrlEncode(attachment.NormalizedUrl) + "?token=" + safeToken;
|
return "http://127.0.0.1:" + safePort + "/get-attachment/" + WebUtility.UrlEncode(attachment.NormalizedUrl) + "?token=" + safeToken;
|
||||||
}
|
}
|
||||||
|
@@ -7,6 +7,13 @@ public sealed class StandaloneViewerExportStrategy : IViewerExportStrategy {
|
|||||||
|
|
||||||
private StandaloneViewerExportStrategy() {}
|
private StandaloneViewerExportStrategy() {}
|
||||||
|
|
||||||
|
public bool IncludeMessageText => true;
|
||||||
|
|
||||||
|
public string ProcessViewerTemplate(string template) {
|
||||||
|
return template.Replace("\"/*[SERVER_URL]*/\"", "null")
|
||||||
|
.Replace("\"/*[SERVER_TOKEN]*/\"", "null");
|
||||||
|
}
|
||||||
|
|
||||||
public string GetAttachmentUrl(Attachment attachment) {
|
public string GetAttachmentUrl(Attachment attachment) {
|
||||||
// The normalized URL will not load files from Discord CDN once the time limit is enforced.
|
// The normalized URL will not load files from Discord CDN once the time limit is enforced.
|
||||||
|
|
||||||
|
@@ -21,7 +21,7 @@ public static class ViewerJsonExport {
|
|||||||
var includedChannelIds = new HashSet<ulong>();
|
var includedChannelIds = new HashSet<ulong>();
|
||||||
var includedServerIds = new HashSet<ulong>();
|
var includedServerIds = new HashSet<ulong>();
|
||||||
|
|
||||||
var includedMessages = db.GetMessages(filter);
|
var includedMessages = db.GetMessages(filter, strategy.IncludeMessageText);
|
||||||
var includedChannels = new List<Channel>();
|
var includedChannels = new List<Channel>();
|
||||||
|
|
||||||
foreach (var message in includedMessages) {
|
foreach (var message in includedMessages) {
|
||||||
|
@@ -23,7 +23,7 @@ public interface IDatabaseFile : IDisposable {
|
|||||||
|
|
||||||
void AddMessages(Message[] messages);
|
void AddMessages(Message[] messages);
|
||||||
int CountMessages(MessageFilter? filter = null);
|
int CountMessages(MessageFilter? filter = null);
|
||||||
List<Message> GetMessages(MessageFilter? filter = null);
|
List<Message> GetMessages(MessageFilter? filter = null, bool includeText = true);
|
||||||
HashSet<ulong> GetMessageIds(MessageFilter? filter = null);
|
HashSet<ulong> GetMessageIds(MessageFilter? filter = null);
|
||||||
void RemoveMessages(MessageFilter filter, FilterRemovalMode mode);
|
void RemoveMessages(MessageFilter filter, FilterRemovalMode mode);
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ public interface IDatabaseFile : IDisposable {
|
|||||||
DownloadedAttachment? GetDownloadedAttachment(string url);
|
DownloadedAttachment? GetDownloadedAttachment(string url);
|
||||||
|
|
||||||
void EnqueueDownloadItems(AttachmentFilter? filter = null);
|
void EnqueueDownloadItems(AttachmentFilter? filter = null);
|
||||||
List<DownloadItem> PullEnqueuedDownloadItems(int count);
|
List<DownloadItem> GetEnqueuedDownloadItems(int count);
|
||||||
void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode);
|
void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode);
|
||||||
DownloadStatusStatistics GetDownloadStatusStatistics();
|
DownloadStatusStatistics GetDownloadStatusStatistics();
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace DHT.Server.Database.Import;
|
namespace DHT.Server.Database.Import;
|
||||||
|
|
||||||
sealed class DiscordEmbedLegacyJson {
|
sealed class DiscordEmbedLegacyJson {
|
||||||
public required string Url { get; init; }
|
public required string Url { get; init; }
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace DHT.Server.Database.Sqlite;
|
namespace DHT.Server.Database.Sqlite;
|
||||||
|
|
||||||
public interface ISchemaUpgradeCallbacks {
|
public interface ISchemaUpgradeCallbacks {
|
||||||
Task<bool> CanUpgrade();
|
Task<bool> CanUpgrade();
|
||||||
|
@@ -174,8 +174,8 @@ sealed class Schema {
|
|||||||
int processedUrls = -1;
|
int processedUrls = -1;
|
||||||
|
|
||||||
await using (var updateCmd = conn.Command("UPDATE attachments SET download_url = url, url = :normalized_url WHERE attachment_id = :attachment_id")) {
|
await using (var updateCmd = conn.Command("UPDATE attachments SET download_url = url, url = :normalized_url WHERE attachment_id = :attachment_id")) {
|
||||||
updateCmd.Add(":attachment_id", SqliteType.Integer);
|
updateCmd.Parameters.Add(":attachment_id", SqliteType.Integer);
|
||||||
updateCmd.Add(":normalized_url", SqliteType.Text);
|
updateCmd.Parameters.Add(":normalized_url", SqliteType.Text);
|
||||||
|
|
||||||
foreach (var (attachmentId, normalizedUrl) in normalizedUrls) {
|
foreach (var (attachmentId, normalizedUrl) in normalizedUrls) {
|
||||||
if (++processedUrls % 1000 == 0) {
|
if (++processedUrls % 1000 == 0) {
|
||||||
@@ -235,8 +235,8 @@ sealed class Schema {
|
|||||||
tx = conn.BeginTransaction();
|
tx = conn.BeginTransaction();
|
||||||
|
|
||||||
await using (var updateCmd = conn.Command("UPDATE downloads SET download_url = :download_url, url = :normalized_url WHERE url = :download_url")) {
|
await using (var updateCmd = conn.Command("UPDATE downloads SET download_url = :download_url, url = :normalized_url WHERE url = :download_url")) {
|
||||||
updateCmd.Add(":normalized_url", SqliteType.Text);
|
updateCmd.Parameters.Add(":normalized_url", SqliteType.Text);
|
||||||
updateCmd.Add(":download_url", SqliteType.Text);
|
updateCmd.Parameters.Add(":download_url", SqliteType.Text);
|
||||||
|
|
||||||
foreach (var (normalizedUrl, downloadUrl) in normalizedUrlsToOriginalUrls) {
|
foreach (var (normalizedUrl, downloadUrl) in normalizedUrlsToOriginalUrls) {
|
||||||
if (++processedUrls % 100 == 0) {
|
if (++processedUrls % 100 == 0) {
|
||||||
|
@@ -360,7 +360,7 @@ public sealed class SqliteDatabaseFile : IDatabaseFile {
|
|||||||
return reader.Read() ? reader.GetInt32(0) : 0;
|
return reader.Read() ? reader.GetInt32(0) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Message> GetMessages(MessageFilter? filter = null) {
|
public List<Message> GetMessages(MessageFilter? filter = null, bool includeText = true) {
|
||||||
var perf = log.Start();
|
var perf = log.Start();
|
||||||
var list = new List<Message>();
|
var list = new List<Message>();
|
||||||
|
|
||||||
@@ -370,7 +370,7 @@ public sealed class SqliteDatabaseFile : IDatabaseFile {
|
|||||||
|
|
||||||
using var conn = pool.Take();
|
using var conn = pool.Take();
|
||||||
using var cmd = conn.Command($"""
|
using var cmd = conn.Command($"""
|
||||||
SELECT m.message_id, m.sender_id, m.channel_id, m.text, m.timestamp, et.edit_timestamp, rt.replied_to_id
|
SELECT m.message_id, m.sender_id, m.channel_id, {(includeText ? "m.text" : "NULL")}, m.timestamp, et.edit_timestamp, rt.replied_to_id
|
||||||
FROM messages m
|
FROM messages m
|
||||||
LEFT JOIN edit_timestamps et ON m.message_id = et.message_id
|
LEFT JOIN edit_timestamps et ON m.message_id = et.message_id
|
||||||
LEFT JOIN replied_to rt ON m.message_id = rt.message_id
|
LEFT JOIN replied_to rt ON m.message_id = rt.message_id
|
||||||
@@ -385,7 +385,7 @@ public sealed class SqliteDatabaseFile : IDatabaseFile {
|
|||||||
Id = id,
|
Id = id,
|
||||||
Sender = reader.GetUint64(1),
|
Sender = reader.GetUint64(1),
|
||||||
Channel = reader.GetUint64(2),
|
Channel = reader.GetUint64(2),
|
||||||
Text = reader.GetString(3),
|
Text = includeText ? reader.GetString(3) : string.Empty,
|
||||||
Timestamp = reader.GetInt64(4),
|
Timestamp = reader.GetInt64(4),
|
||||||
EditTimestamp = reader.IsDBNull(5) ? null : reader.GetInt64(5),
|
EditTimestamp = reader.IsDBNull(5) ? null : reader.GetInt64(5),
|
||||||
RepliedToId = reader.IsDBNull(6) ? null : reader.GetUint64(6),
|
RepliedToId = reader.IsDBNull(6) ? null : reader.GetUint64(6),
|
||||||
@@ -522,42 +522,25 @@ public sealed class SqliteDatabaseFile : IDatabaseFile {
|
|||||||
cmd.ExecuteNonQuery();
|
cmd.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<DownloadItem> PullEnqueuedDownloadItems(int count) {
|
public List<DownloadItem> GetEnqueuedDownloadItems(int count) {
|
||||||
var found = new List<DownloadItem>();
|
var list = new List<DownloadItem>();
|
||||||
var pulled = new List<DownloadItem>();
|
|
||||||
|
|
||||||
using var conn = pool.Take();
|
using var conn = pool.Take();
|
||||||
using (var cmd = conn.Command("SELECT normalized_url, download_url, size FROM downloads WHERE status = :enqueued LIMIT :limit")) {
|
using var cmd = conn.Command("SELECT normalized_url, download_url, size FROM downloads WHERE status = :enqueued LIMIT :limit");
|
||||||
cmd.AddAndSet(":enqueued", SqliteType.Integer, (int) DownloadStatus.Enqueued);
|
cmd.AddAndSet(":enqueued", SqliteType.Integer, (int) DownloadStatus.Enqueued);
|
||||||
cmd.AddAndSet(":limit", SqliteType.Integer, Math.Max(0, count));
|
cmd.AddAndSet(":limit", SqliteType.Integer, Math.Max(0, count));
|
||||||
|
|
||||||
using var reader = cmd.ExecuteReader();
|
using var reader = cmd.ExecuteReader();
|
||||||
|
|
||||||
while (reader.Read()) {
|
while (reader.Read()) {
|
||||||
found.Add(new DownloadItem {
|
list.Add(new DownloadItem {
|
||||||
NormalizedUrl = reader.GetString(0),
|
NormalizedUrl = reader.GetString(0),
|
||||||
DownloadUrl = reader.GetString(1),
|
DownloadUrl = reader.GetString(1),
|
||||||
Size = reader.GetUint64(2),
|
Size = reader.GetUint64(2),
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found.Count != 0) {
|
return list;
|
||||||
using var cmd = conn.Command("UPDATE downloads SET status = :downloading WHERE normalized_url = :normalized_url AND status = :enqueued");
|
|
||||||
cmd.AddAndSet(":enqueued", SqliteType.Integer, (int) DownloadStatus.Enqueued);
|
|
||||||
cmd.AddAndSet(":downloading", SqliteType.Integer, (int) DownloadStatus.Downloading);
|
|
||||||
cmd.Add(":normalized_url", SqliteType.Text);
|
|
||||||
|
|
||||||
foreach (var item in found) {
|
|
||||||
cmd.Set(":normalized_url", item.NormalizedUrl);
|
|
||||||
|
|
||||||
if (cmd.ExecuteNonQuery() == 1) {
|
|
||||||
pulled.Add(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pulled;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode) {
|
public void RemoveDownloadItems(DownloadItemFilter? filter, FilterRemovalMode mode) {
|
||||||
@@ -579,16 +562,15 @@ public sealed class SqliteDatabaseFile : IDatabaseFile {
|
|||||||
static void LoadSuccessStatistics(ISqliteConnection conn, DownloadStatusStatistics result) {
|
static void LoadSuccessStatistics(ISqliteConnection conn, DownloadStatusStatistics result) {
|
||||||
using var cmd = conn.Command("""
|
using var cmd = conn.Command("""
|
||||||
SELECT
|
SELECT
|
||||||
IFNULL(SUM(CASE WHEN status IN (:enqueued, :downloading) THEN 1 ELSE 0 END), 0),
|
IFNULL(SUM(CASE WHEN status = :enqueued THEN 1 ELSE 0 END), 0),
|
||||||
IFNULL(SUM(CASE WHEN status IN (:enqueued, :downloading) THEN size 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 1 ELSE 0 END), 0),
|
||||||
IFNULL(SUM(CASE WHEN status = :success THEN size ELSE 0 END), 0),
|
IFNULL(SUM(CASE WHEN status = :success THEN size ELSE 0 END), 0),
|
||||||
IFNULL(SUM(CASE WHEN status NOT IN (:enqueued, :downloading) AND status != :success THEN 1 ELSE 0 END), 0),
|
IFNULL(SUM(CASE WHEN status != :enqueued AND status != :success THEN 1 ELSE 0 END), 0),
|
||||||
IFNULL(SUM(CASE WHEN status NOT IN (:enqueued, :downloading) AND status != :success THEN size ELSE 0 END), 0)
|
IFNULL(SUM(CASE WHEN status != :enqueued AND status != :success THEN size ELSE 0 END), 0)
|
||||||
FROM downloads
|
FROM downloads
|
||||||
""");
|
""");
|
||||||
cmd.AddAndSet(":enqueued", SqliteType.Integer, (int) DownloadStatus.Enqueued);
|
cmd.AddAndSet(":enqueued", SqliteType.Integer, (int) DownloadStatus.Enqueued);
|
||||||
cmd.AddAndSet(":downloading", SqliteType.Integer, (int) DownloadStatus.Downloading);
|
|
||||||
cmd.AddAndSet(":success", SqliteType.Integer, (int) DownloadStatus.Success);
|
cmd.AddAndSet(":success", SqliteType.Integer, (int) DownloadStatus.Success);
|
||||||
|
|
||||||
using var reader = cmd.ExecuteReader();
|
using var reader = cmd.ExecuteReader();
|
||||||
|
@@ -62,10 +62,6 @@ static class SqliteExtensions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Add(this SqliteCommand cmd, string key, SqliteType type) {
|
|
||||||
cmd.Parameters.Add(key, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AddAndSet(this SqliteCommand cmd, string key, SqliteType type, object? value) {
|
public static void AddAndSet(this SqliteCommand cmd, string key, SqliteType type, object? value) {
|
||||||
cmd.Parameters.Add(key, type).Value = value ?? DBNull.Value;
|
cmd.Parameters.Add(key, type).Value = value ?? DBNull.Value;
|
||||||
}
|
}
|
||||||
|
130
app/Server/Download/BackgroundDownloadThread.cs
Normal file
130
app/Server/Download/BackgroundDownloadThread.cs
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Net.Http;
|
||||||
|
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 HttpClient client = new ();
|
||||||
|
|
||||||
|
public ThreadInstance() {
|
||||||
|
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async 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>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (!cancellationToken.IsCancellationRequested) {
|
||||||
|
FillQueue(db, queue, cancellationToken);
|
||||||
|
|
||||||
|
while (!cancellationToken.IsCancellationRequested && queue.TryDequeue(out var item)) {
|
||||||
|
var downloadUrl = item.DownloadUrl;
|
||||||
|
Log.Debug("Downloading " + downloadUrl + "...");
|
||||||
|
|
||||||
|
try {
|
||||||
|
db.AddDownload(Data.Download.NewSuccess(item, await client.GetByteArrayAsync(downloadUrl, cancellationToken)));
|
||||||
|
} catch (HttpRequestException e) {
|
||||||
|
db.AddDownload(Data.Download.NewFailure(item, e.StatusCode, item.Size));
|
||||||
|
Log.Error(e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
db.AddDownload(Data.Download.NewFailure(item, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Frozen;
|
using System.Collections.Frozen;
|
||||||
|
|
||||||
namespace DHT.Server.Download;
|
namespace DHT.Server.Download;
|
||||||
|
|
||||||
static class DiscordCdn {
|
static class DiscordCdn {
|
||||||
private static FrozenSet<string> CdnHosts { get; } = new [] {
|
private static FrozenSet<string> CdnHosts { get; } = new [] {
|
||||||
|
@@ -1,49 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
|
|
||||||
namespace DHT.Server.Download;
|
|
||||||
|
|
||||||
public sealed class Downloader {
|
|
||||||
private DownloaderTask? current;
|
|
||||||
public bool IsDownloading => current != null;
|
|
||||||
|
|
||||||
public event EventHandler<DownloadItem>? OnItemFinished;
|
|
||||||
|
|
||||||
private readonly IDatabaseFile db;
|
|
||||||
private readonly SemaphoreSlim semaphore = new (1, 1);
|
|
||||||
|
|
||||||
internal Downloader(IDatabaseFile db) {
|
|
||||||
this.db = db;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Start() {
|
|
||||||
await semaphore.WaitAsync();
|
|
||||||
try {
|
|
||||||
if (current == null) {
|
|
||||||
current = new DownloaderTask(db);
|
|
||||||
current.OnItemFinished += DelegateOnItemFinished;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
semaphore.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task Stop() {
|
|
||||||
await semaphore.WaitAsync();
|
|
||||||
try {
|
|
||||||
if (current != null) {
|
|
||||||
await current.Stop();
|
|
||||||
current.OnItemFinished -= DelegateOnItemFinished;
|
|
||||||
current = null;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
semaphore.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DelegateOnItemFinished(object? sender, DownloadItem e) {
|
|
||||||
OnItemFinished?.Invoke(this, e);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,107 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Channels;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Utils.Logging;
|
|
||||||
using DHT.Utils.Models;
|
|
||||||
using DHT.Utils.Tasks;
|
|
||||||
|
|
||||||
namespace DHT.Server.Download;
|
|
||||||
|
|
||||||
sealed class DownloaderTask : BaseModel {
|
|
||||||
private static readonly Log Log = Log.ForType<DownloaderTask>();
|
|
||||||
|
|
||||||
private const int DownloadTasks = 4;
|
|
||||||
private const int QueueSize = 25;
|
|
||||||
private const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
|
|
||||||
|
|
||||||
internal event EventHandler<DownloadItem>? OnItemFinished;
|
|
||||||
|
|
||||||
private readonly Channel<DownloadItem> downloadQueue = Channel.CreateBounded<DownloadItem>(new BoundedChannelOptions(QueueSize) {
|
|
||||||
SingleReader = false,
|
|
||||||
SingleWriter = true,
|
|
||||||
AllowSynchronousContinuations = false,
|
|
||||||
FullMode = BoundedChannelFullMode.Wait
|
|
||||||
});
|
|
||||||
|
|
||||||
private readonly CancellationTokenSource cancellationTokenSource = new ();
|
|
||||||
private readonly CancellationToken cancellationToken;
|
|
||||||
|
|
||||||
private readonly IDatabaseFile db;
|
|
||||||
private readonly Task queueWriterTask;
|
|
||||||
private readonly Task[] downloadTasks;
|
|
||||||
|
|
||||||
internal DownloaderTask(IDatabaseFile db) {
|
|
||||||
this.cancellationToken = cancellationTokenSource.Token;
|
|
||||||
this.db = db;
|
|
||||||
this.queueWriterTask = Task.Run(RunQueueWriterTask);
|
|
||||||
this.downloadTasks = Enumerable.Range(1, DownloadTasks).Select(taskIndex => Task.Run(() => RunDownloadTask(taskIndex))).ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task RunQueueWriterTask() {
|
|
||||||
while (await downloadQueue.Writer.WaitToWriteAsync(cancellationToken)) {
|
|
||||||
var newItems = db.PullEnqueuedDownloadItems(QueueSize);
|
|
||||||
if (newItems.Count == 0) {
|
|
||||||
await Task.Delay(TimeSpan.FromMilliseconds(50), cancellationToken);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var newItem in newItems) {
|
|
||||||
await downloadQueue.Writer.WriteAsync(newItem, cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task RunDownloadTask(int taskIndex) {
|
|
||||||
var log = Log.ForType<DownloaderTask>("Task " + taskIndex);
|
|
||||||
|
|
||||||
var client = new HttpClient();
|
|
||||||
client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
|
|
||||||
client.Timeout = TimeSpan.FromSeconds(30);
|
|
||||||
|
|
||||||
while (!cancellationToken.IsCancellationRequested) {
|
|
||||||
var item = await downloadQueue.Reader.ReadAsync(cancellationToken);
|
|
||||||
log.Debug("Downloading " + item.DownloadUrl + "...");
|
|
||||||
|
|
||||||
try {
|
|
||||||
var downloadedBytes = await client.GetByteArrayAsync(item.DownloadUrl, cancellationToken);
|
|
||||||
db.AddDownload(Data.Download.NewSuccess(item, downloadedBytes));
|
|
||||||
} catch (OperationCanceledException) {
|
|
||||||
// Ignore.
|
|
||||||
} catch (HttpRequestException e) {
|
|
||||||
db.AddDownload(Data.Download.NewFailure(item, e.StatusCode, item.Size));
|
|
||||||
log.Error(e);
|
|
||||||
} catch (Exception e) {
|
|
||||||
db.AddDownload(Data.Download.NewFailure(item, null, item.Size));
|
|
||||||
log.Error(e);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
OnItemFinished?.Invoke(this, item);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.Error("Caught exception in event handler: " + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal async Task Stop() {
|
|
||||||
try {
|
|
||||||
await cancellationTokenSource.CancelAsync();
|
|
||||||
} catch (Exception) {
|
|
||||||
Log.Warn("Attempted to stop background download twice.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
downloadQueue.Writer.Complete();
|
|
||||||
|
|
||||||
try {
|
|
||||||
await queueWriterTask.WaitIgnoringCancellation();
|
|
||||||
await Task.WhenAll(downloadTasks).WaitIgnoringCancellation();
|
|
||||||
} finally {
|
|
||||||
cancellationTokenSource.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
34
app/Server/Endpoints/GetMessagesEndpoint.cs
Normal file
34
app/Server/Endpoints/GetMessagesEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using DHT.Server.Data.Filters;
|
||||||
|
using DHT.Server.Database;
|
||||||
|
using DHT.Utils.Http;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using GetMessagesJsonContext = DHT.Server.Endpoints.Responses.GetMessagesJsonContext;
|
||||||
|
|
||||||
|
namespace DHT.Server.Endpoints;
|
||||||
|
|
||||||
|
sealed class GetMessagesEndpoint : BaseEndpoint {
|
||||||
|
public GetMessagesEndpoint(IDatabaseFile db) : base(db) {}
|
||||||
|
|
||||||
|
protected override Task<IHttpOutput> Respond(HttpContext ctx) {
|
||||||
|
HashSet<ulong> messageIdSet;
|
||||||
|
try {
|
||||||
|
var messageIds = ctx.Request.Query["id"];
|
||||||
|
messageIdSet = messageIds.Select(ulong.Parse!).ToHashSet();
|
||||||
|
} catch (Exception) {
|
||||||
|
throw new HttpException(HttpStatusCode.BadRequest, "Invalid message ids.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var messageFilter = new MessageFilter {
|
||||||
|
MessageIds = messageIdSet
|
||||||
|
};
|
||||||
|
|
||||||
|
var messages = Db.GetMessages(messageFilter).ToDictionary(static message => message.Id, static message => message.Text);
|
||||||
|
var response = new HttpOutput.Json<Dictionary<ulong, string>>(messages, GetMessagesJsonContext.Default.DictionaryUInt64String);
|
||||||
|
return Task.FromResult<IHttpOutput>(response);
|
||||||
|
}
|
||||||
|
}
|
8
app/Server/Endpoints/Responses/GetMessagesJsonContext.cs
Normal file
8
app/Server/Endpoints/Responses/GetMessagesJsonContext.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace DHT.Server.Endpoints.Responses;
|
||||||
|
|
||||||
|
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, GenerationMode = JsonSourceGenerationMode.Default)]
|
||||||
|
[JsonSerializable(typeof(Dictionary<ulong, string>))]
|
||||||
|
sealed partial class GetMessagesJsonContext : JsonSerializerContext {}
|
@@ -4,7 +4,7 @@ using DHT.Utils.Logging;
|
|||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Http.Extensions;
|
using Microsoft.AspNetCore.Http.Extensions;
|
||||||
|
|
||||||
namespace DHT.Server.Service.Middlewares;
|
namespace DHT.Server.Service.Middlewares;
|
||||||
|
|
||||||
sealed class ServerLoggingMiddleware {
|
sealed class ServerLoggingMiddleware {
|
||||||
private static readonly Log Log = Log.ForType<ServerLoggingMiddleware>();
|
private static readonly Log Log = Log.ForType<ServerLoggingMiddleware>();
|
||||||
|
@@ -16,6 +16,7 @@ sealed class Startup {
|
|||||||
"https://ptb.discord.com",
|
"https://ptb.discord.com",
|
||||||
"https://canary.discord.com",
|
"https://canary.discord.com",
|
||||||
"https://discordapp.com",
|
"https://discordapp.com",
|
||||||
|
"null" // For file:// protocol in the Viewer
|
||||||
};
|
};
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services) {
|
public void ConfigureServices(IServiceCollection services) {
|
||||||
@@ -41,6 +42,7 @@ sealed class Startup {
|
|||||||
|
|
||||||
app.UseEndpoints(endpoints => {
|
app.UseEndpoints(endpoints => {
|
||||||
endpoints.MapGet("/get-tracking-script", new GetTrackingScriptEndpoint(db, parameters).Handle);
|
endpoints.MapGet("/get-tracking-script", new GetTrackingScriptEndpoint(db, parameters).Handle);
|
||||||
|
endpoints.MapGet("/get-messages", new GetMessagesEndpoint(db).Handle);
|
||||||
endpoints.MapGet("/get-attachment/{url}", new GetAttachmentEndpoint(db).Handle);
|
endpoints.MapGet("/get-attachment/{url}", new GetAttachmentEndpoint(db).Handle);
|
||||||
endpoints.MapPost("/track-channel", new TrackChannelEndpoint(db).Handle);
|
endpoints.MapPost("/track-channel", new TrackChannelEndpoint(db).Handle);
|
||||||
endpoints.MapPost("/track-users", new TrackUsersEndpoint(db).Handle);
|
endpoints.MapPost("/track-users", new TrackUsersEndpoint(db).Handle);
|
||||||
|
@@ -1,23 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DHT.Server.Database;
|
|
||||||
using DHT.Server.Download;
|
|
||||||
|
|
||||||
namespace DHT.Server;
|
|
||||||
|
|
||||||
public sealed class State : IAsyncDisposable {
|
|
||||||
public static State Dummy { get; } = new (DummyDatabaseFile.Instance);
|
|
||||||
|
|
||||||
public IDatabaseFile Db { get; }
|
|
||||||
public Downloader Downloader { get; }
|
|
||||||
|
|
||||||
public State(IDatabaseFile db) {
|
|
||||||
Db = db;
|
|
||||||
Downloader = new Downloader(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async ValueTask DisposeAsync() {
|
|
||||||
await Downloader.Stop();
|
|
||||||
Db.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,4 +1,5 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json.Serialization.Metadata;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
@@ -25,6 +26,20 @@ public static class HttpOutput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed class Json<TValue> : IHttpOutput {
|
||||||
|
private readonly TValue value;
|
||||||
|
private readonly JsonTypeInfo<TValue> typeInfo;
|
||||||
|
|
||||||
|
public Json(TValue value, JsonTypeInfo<TValue> typeInfo) {
|
||||||
|
this.value = value;
|
||||||
|
this.typeInfo = typeInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task WriteTo(HttpResponse response) {
|
||||||
|
return response.WriteAsJsonAsync(value, typeInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public sealed class File : IHttpOutput {
|
public sealed class File : IHttpOutput {
|
||||||
private readonly string? contentType;
|
private readonly string? contentType;
|
||||||
private readonly byte[] bytes;
|
private readonly byte[] bytes;
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
|
|
||||||
namespace DHT.Utils.Logging;
|
namespace DHT.Utils.Logging;
|
||||||
|
|
||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
public static partial class WindowsConsole {
|
public static partial class WindowsConsole {
|
||||||
|
@@ -1,12 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace DHT.Utils.Tasks;
|
|
||||||
|
|
||||||
public static class TaskExtensions {
|
|
||||||
public static async Task WaitIgnoringCancellation(this Task task) {
|
|
||||||
try {
|
|
||||||
await task;
|
|
||||||
} catch (OperationCanceledException) {}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -5,7 +5,7 @@ using DHT.Utils;
|
|||||||
[assembly: AssemblyFileVersion(Version.Tag)]
|
[assembly: AssemblyFileVersion(Version.Tag)]
|
||||||
[assembly: AssemblyInformationalVersion(Version.Tag)]
|
[assembly: AssemblyInformationalVersion(Version.Tag)]
|
||||||
|
|
||||||
namespace DHT.Utils;
|
namespace DHT.Utils;
|
||||||
|
|
||||||
static class Version {
|
static class Version {
|
||||||
public const string Tag = "39.1.0.0";
|
public const string Tag = "39.1.0.0";
|
||||||
|
Reference in New Issue
Block a user