From 95a4217ab3257c7cb960f85e3e26ea52b5e70026 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Tue, 30 Oct 2018 22:52:28 +0200 Subject: [PATCH] Refactor resolving chat log to a separate service to allow reusability across CLI and GUI --- DiscordChatExporter.Cli/Container.cs | 1 + .../Verbs/ExportChatVerb.cs | 30 +++--------- .../Services/ChatLogService.cs | 48 +++++++++++++++++++ .../Services/DataService.cs | 4 ++ .../Services/IChatLogService.cs | 15 ++++++ DiscordChatExporter.Gui/Container.cs | 1 + .../ViewModels/MainViewModel.cs | 22 +++------ 7 files changed, 81 insertions(+), 40 deletions(-) create mode 100644 DiscordChatExporter.Core/Services/ChatLogService.cs create mode 100644 DiscordChatExporter.Core/Services/IChatLogService.cs diff --git a/DiscordChatExporter.Cli/Container.cs b/DiscordChatExporter.Cli/Container.cs index e16dec56..f678354e 100644 --- a/DiscordChatExporter.Cli/Container.cs +++ b/DiscordChatExporter.Cli/Container.cs @@ -12,6 +12,7 @@ namespace DiscordChatExporter.Cli SimpleIoc.Default.Reset(); // Services + SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); diff --git a/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs b/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs index 8188517e..d2d619c7 100644 --- a/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs +++ b/DiscordChatExporter.Cli/Verbs/ExportChatVerb.cs @@ -20,8 +20,7 @@ namespace DiscordChatExporter.Cli.Verbs // Get services var container = new Container(); var settingsService = container.Resolve(); - var dataService = container.Resolve(); - var messageGroupService = container.Resolve(); + var chatLogService = container.Resolve(); var exportService = container.Resolve(); // Configure settings @@ -30,37 +29,20 @@ namespace DiscordChatExporter.Cli.Verbs if (Options.MessageGroupLimit > 0) settingsService.MessageGroupLimit = Options.MessageGroupLimit; - // Get channel and guild - var channel = await dataService.GetChannelAsync(Options.GetToken(), Options.ChannelId); - var guild = channel.GuildId == Guild.DirectMessages.Id - ? Guild.DirectMessages - : await dataService.GetGuildAsync(Options.GetToken(), channel.GuildId); + // Get chat log + var chatLog = await chatLogService.GetChatLogAsync(Options.GetToken(), Options.ChannelId, + Options.After, Options.Before); // Generate file path if not set var filePath = Options.FilePath; if (filePath == null || filePath.EndsWith("/") || filePath.EndsWith("\\")) { - filePath += $"{guild.Name} - {channel.Name}.{Options.ExportFormat.GetFileExtension()}" + filePath += $"{chatLog.Guild.Name} - {chatLog.Channel.Name}.{Options.ExportFormat.GetFileExtension()}" .Replace(Path.GetInvalidFileNameChars(), '_'); } - // TODO: extract this to make it reusable across implementations - // Get messages - var messages = - await dataService.GetChannelMessagesAsync(Options.GetToken(), channel.Id, - Options.After, Options.Before); - - // Group messages - var messageGroups = messageGroupService.GroupMessages(messages); - - // Get mentionables - var mentionables = await dataService.GetMentionablesAsync(Options.GetToken(), guild.Id, messages); - - // Create log - var log = new ChatLog(guild, channel, Options.After, Options.Before, messageGroups, mentionables); - // Export - exportService.Export(Options.ExportFormat, filePath, log); + exportService.Export(Options.ExportFormat, filePath, chatLog); // Print result Console.WriteLine($"Exported chat to [{filePath}]"); diff --git a/DiscordChatExporter.Core/Services/ChatLogService.cs b/DiscordChatExporter.Core/Services/ChatLogService.cs new file mode 100644 index 00000000..5592847d --- /dev/null +++ b/DiscordChatExporter.Core/Services/ChatLogService.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using DiscordChatExporter.Core.Models; + +namespace DiscordChatExporter.Core.Services +{ + public class ChatLogService : IChatLogService + { + private readonly IDataService _dataService; + private readonly IMessageGroupService _messageGroupService; + + public ChatLogService(IDataService dataService, IMessageGroupService messageGroupService) + { + _dataService = dataService; + _messageGroupService = messageGroupService; + } + + public async Task GetChatLogAsync(AuthToken token, Guild guild, Channel channel, + DateTime? from = null, DateTime? to = null, IProgress progress = null) + { + // Get messages + var messages = await _dataService.GetChannelMessagesAsync(token, channel.Id, from, to, progress); + + // Group messages + var messageGroups = _messageGroupService.GroupMessages(messages); + + // Get mentionables + var mentionables = await _dataService.GetMentionablesAsync(token, guild.Id, messages); + + return new ChatLog(guild, channel, from, to, messageGroups, mentionables); + } + + public async Task GetChatLogAsync(AuthToken token, string channelId, + DateTime? from = null, DateTime? to = null, IProgress progress = null) + { + // Get channel + var channel = await _dataService.GetChannelAsync(token, channelId); + + // Get guild + var guild = channel.GuildId == Guild.DirectMessages.Id + ? Guild.DirectMessages + : await _dataService.GetGuildAsync(token, channel.GuildId); + + // Get the chat log + return await GetChatLogAsync(token, guild, channel, from, to, progress); + } + } +} \ No newline at end of file diff --git a/DiscordChatExporter.Core/Services/DataService.cs b/DiscordChatExporter.Core/Services/DataService.cs index 3a863d51..1fccc4c3 100644 --- a/DiscordChatExporter.Core/Services/DataService.cs +++ b/DiscordChatExporter.Core/Services/DataService.cs @@ -65,6 +65,10 @@ namespace DiscordChatExporter.Core.Services public async Task GetGuildAsync(AuthToken token, string guildId) { + // Special case for direct messages pseudo-guild + if (guildId == Guild.DirectMessages.Id) + return Guild.DirectMessages; + var response = await GetApiResponseAsync(token, "guilds", guildId); var guild = ParseGuild(response); diff --git a/DiscordChatExporter.Core/Services/IChatLogService.cs b/DiscordChatExporter.Core/Services/IChatLogService.cs new file mode 100644 index 00000000..1de69e35 --- /dev/null +++ b/DiscordChatExporter.Core/Services/IChatLogService.cs @@ -0,0 +1,15 @@ +using System; +using System.Threading.Tasks; +using DiscordChatExporter.Core.Models; + +namespace DiscordChatExporter.Core.Services +{ + public interface IChatLogService + { + Task GetChatLogAsync(AuthToken token, Guild guild, Channel channel, + DateTime? from = null, DateTime? to = null, IProgress progress = null); + + Task GetChatLogAsync(AuthToken token, string channelId, + DateTime? from = null, DateTime? to = null, IProgress progress = null); + } +} \ No newline at end of file diff --git a/DiscordChatExporter.Gui/Container.cs b/DiscordChatExporter.Gui/Container.cs index ddb61392..d2adc942 100644 --- a/DiscordChatExporter.Gui/Container.cs +++ b/DiscordChatExporter.Gui/Container.cs @@ -17,6 +17,7 @@ namespace DiscordChatExporter.Gui SimpleIoc.Default.Reset(); // Services + SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); diff --git a/DiscordChatExporter.Gui/ViewModels/MainViewModel.cs b/DiscordChatExporter.Gui/ViewModels/MainViewModel.cs index fc750919..24594521 100644 --- a/DiscordChatExporter.Gui/ViewModels/MainViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/MainViewModel.cs @@ -19,7 +19,7 @@ namespace DiscordChatExporter.Gui.ViewModels private readonly ISettingsService _settingsService; private readonly IUpdateService _updateService; private readonly IDataService _dataService; - private readonly IMessageGroupService _messageGroupService; + private readonly IChatLogService _chatLogService; private readonly IExportService _exportService; private readonly Dictionary> _guildChannelsMap; @@ -111,12 +111,12 @@ namespace DiscordChatExporter.Gui.ViewModels public RelayCommand ShowExportSetupCommand { get; } public MainViewModel(ISettingsService settingsService, IUpdateService updateService, IDataService dataService, - IMessageGroupService messageGroupService, IExportService exportService) + IChatLogService chatLogService, IExportService exportService) { _settingsService = settingsService; _updateService = updateService; _dataService = dataService; - _messageGroupService = messageGroupService; + _chatLogService = chatLogService; _exportService = exportService; _guildChannelsMap = new Dictionary>(); @@ -256,21 +256,11 @@ namespace DiscordChatExporter.Gui.ViewModels try { - // TODO: extract this to make it reusable across implementations - // Get messages - var messages = await _dataService.GetChannelMessagesAsync(token, channel.Id, from, to, progressHandler); - - // Group messages - var messageGroups = _messageGroupService.GroupMessages(messages); - - // Get mentionables - var mentionables = await _dataService.GetMentionablesAsync(token, guild.Id, messages); - - // Create log - var log = new ChatLog(guild, channel, from, to, messageGroups, mentionables); + // Get chat log + var chatLog = await _chatLogService.GetChatLogAsync(token, guild, channel, from, to, progressHandler); // Export - _exportService.Export(format, filePath, log); + _exportService.Export(format, filePath, chatLog); // Open Process.Start(filePath);