diff --git a/DiscordChatExporter/Messages/StartExportMessage.cs b/DiscordChatExporter/Messages/StartExportMessage.cs index bf16927..2f1e528 100644 --- a/DiscordChatExporter/Messages/StartExportMessage.cs +++ b/DiscordChatExporter/Messages/StartExportMessage.cs @@ -1,4 +1,5 @@ -using DiscordChatExporter.Models; +using System; +using DiscordChatExporter.Models; namespace DiscordChatExporter.Messages { @@ -10,11 +11,18 @@ namespace DiscordChatExporter.Messages public ExportFormat Format { get; } - public StartExportMessage(Channel channel, string filePath, ExportFormat format) + public DateTime? From { get; } + + public DateTime? To { get; } + + public StartExportMessage(Channel channel, string filePath, ExportFormat format, + DateTime? from, DateTime? to) { Channel = channel; FilePath = filePath; Format = format; + From = from; + To = to; } } } \ No newline at end of file diff --git a/DiscordChatExporter/Services/DataService.cs b/DiscordChatExporter/Services/DataService.cs index 979feea..e40c8de 100644 --- a/DiscordChatExporter/Services/DataService.cs +++ b/DiscordChatExporter/Services/DataService.cs @@ -71,7 +71,7 @@ namespace DiscordChatExporter.Services return channels; } - public async Task> GetChannelMessagesAsync(string token, string channelId) + public async Task> GetChannelMessagesAsync(string token, string channelId, DateTime? from, DateTime? to) { var result = new List(); @@ -102,6 +102,9 @@ namespace DiscordChatExporter.Services // If no messages - break if (currentMessageId == null) break; + // If last message is older than from date - break + if (from != null && result.Last().TimeStamp < from) break; + // Otherwise offset the next request beforeId = currentMessageId; } @@ -109,6 +112,12 @@ namespace DiscordChatExporter.Services // Messages appear newest first, we need to reverse result.Reverse(); + // Filter + if (from != null) + result.RemoveAll(m => m.TimeStamp < from); + if (to != null) + result.RemoveAll(m => m.TimeStamp > to); + return result; } diff --git a/DiscordChatExporter/Services/IDataService.cs b/DiscordChatExporter/Services/IDataService.cs index 43bd517..25cf794 100644 --- a/DiscordChatExporter/Services/IDataService.cs +++ b/DiscordChatExporter/Services/IDataService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using DiscordChatExporter.Models; @@ -12,6 +13,7 @@ namespace DiscordChatExporter.Services Task> GetGuildChannelsAsync(string token, string guildId); - Task> GetChannelMessagesAsync(string token, string channelId); + Task> GetChannelMessagesAsync(string token, string channelId, + DateTime? from, DateTime? to); } } \ No newline at end of file diff --git a/DiscordChatExporter/ViewModels/ExportSetupViewModel.cs b/DiscordChatExporter/ViewModels/ExportSetupViewModel.cs index 7814000..f343beb 100644 --- a/DiscordChatExporter/ViewModels/ExportSetupViewModel.cs +++ b/DiscordChatExporter/ViewModels/ExportSetupViewModel.cs @@ -17,6 +17,8 @@ namespace DiscordChatExporter.ViewModels private string _filePath; private ExportFormat _format; + private DateTime? _from; + private DateTime? _to; public Guild Guild { get; private set; } @@ -40,6 +42,18 @@ namespace DiscordChatExporter.ViewModels set => Set(ref _format, value); } + public DateTime? From + { + get => _from; + set => Set(ref _from, value); + } + + public DateTime? To + { + get => _to; + set => Set(ref _to, value); + } + // Commands public RelayCommand ExportCommand { get; } @@ -59,14 +73,17 @@ namespace DiscordChatExporter.ViewModels Guild = m.Guild; Channel = m.Channel; SelectedFormat = _settingsService.LastExportFormat; - FilePath = Path.Combine($"{Guild} - {Channel}.{SelectedFormat.GetFileExtension()}"); + FilePath = $"{Guild} - {Channel}.{SelectedFormat.GetFileExtension()}" + .Replace(Path.GetInvalidFileNameChars(), '_'); + From = null; + To = null; }); } private void Export() { _settingsService.LastExportFormat = SelectedFormat; - MessengerInstance.Send(new StartExportMessage(Channel, FilePath, SelectedFormat)); + MessengerInstance.Send(new StartExportMessage(Channel, FilePath, SelectedFormat, From, To)); } } } \ No newline at end of file diff --git a/DiscordChatExporter/ViewModels/IExportSetupViewModel.cs b/DiscordChatExporter/ViewModels/IExportSetupViewModel.cs index a21a8d1..8107730 100644 --- a/DiscordChatExporter/ViewModels/IExportSetupViewModel.cs +++ b/DiscordChatExporter/ViewModels/IExportSetupViewModel.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using DiscordChatExporter.Models; using GalaSoft.MvvmLight.CommandWpf; @@ -11,6 +12,8 @@ namespace DiscordChatExporter.ViewModels string FilePath { get; set; } IReadOnlyList AvailableFormats { get; } ExportFormat SelectedFormat { get; set; } + DateTime? From { get; set; } + DateTime? To { get; set; } RelayCommand ExportCommand { get; } } diff --git a/DiscordChatExporter/ViewModels/MainViewModel.cs b/DiscordChatExporter/ViewModels/MainViewModel.cs index 9bd348a..891789f 100644 --- a/DiscordChatExporter/ViewModels/MainViewModel.cs +++ b/DiscordChatExporter/ViewModels/MainViewModel.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; @@ -104,7 +105,7 @@ namespace DiscordChatExporter.ViewModels // Messages MessengerInstance.Register(this, m => { - Export(m.Channel, m.FilePath, m.Format); + Export(m.Channel, m.FilePath, m.Format, m.From, m.To); }); } @@ -161,14 +162,14 @@ namespace DiscordChatExporter.ViewModels MessengerInstance.Send(new ShowExportSetupMessage(SelectedGuild, channel)); } - private async void Export(Channel channel, string filePath, ExportFormat format) + private async void Export(Channel channel, string filePath, ExportFormat format, DateTime? from, DateTime? to) { IsBusy = true; try { // Get messages - var messages = await _dataService.GetChannelMessagesAsync(_cachedToken, channel.Id); + var messages = await _dataService.GetChannelMessagesAsync(_cachedToken, channel.Id, from, to); // Group them var messageGroups = _messageGroupService.GroupMessages(messages); diff --git a/DiscordChatExporter/Views/ExportSetupDialog.ammy b/DiscordChatExporter/Views/ExportSetupDialog.ammy index c92e75c..a44a567 100644 --- a/DiscordChatExporter/Views/ExportSetupDialog.ammy +++ b/DiscordChatExporter/Views/ExportSetupDialog.ammy @@ -33,6 +33,27 @@ UserControl "DiscordChatExporter.Views.ExportSetupDialog" { } } + // Date range + Grid { + #TwoColumns("*", "*") + + DatePicker { + Grid.Column: 0 + Margin: "16 16 8 8" + HintAssist.Hint: "From" + HintAssist.IsFloating: true + SelectedDate: bind From + } + + DatePicker { + Grid.Column: 1 + Margin: "8 16 16 8" + HintAssist.Hint: "To" + HintAssist.IsFloating: true + SelectedDate: bind To + } + } + // Buttons @StackPanelHorizontal { HorizontalAlignment: Right