mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2024-09-19 20:28:46 -04:00
Refactor resolving chat log to a separate service to allow reusability across CLI and GUI
This commit is contained in:
parent
4e8fb80ac5
commit
95a4217ab3
7 changed files with 81 additions and 40 deletions
|
@ -12,6 +12,7 @@ namespace DiscordChatExporter.Cli
|
|||
SimpleIoc.Default.Reset();
|
||||
|
||||
// Services
|
||||
SimpleIoc.Default.Register<IChatLogService, ChatLogService>();
|
||||
SimpleIoc.Default.Register<IDataService, DataService>();
|
||||
SimpleIoc.Default.Register<IExportService, ExportService>();
|
||||
SimpleIoc.Default.Register<IMessageGroupService, MessageGroupService>();
|
||||
|
|
|
@ -20,8 +20,7 @@ namespace DiscordChatExporter.Cli.Verbs
|
|||
// Get services
|
||||
var container = new Container();
|
||||
var settingsService = container.Resolve<ISettingsService>();
|
||||
var dataService = container.Resolve<IDataService>();
|
||||
var messageGroupService = container.Resolve<IMessageGroupService>();
|
||||
var chatLogService = container.Resolve<IChatLogService>();
|
||||
var exportService = container.Resolve<IExportService>();
|
||||
|
||||
// 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}]");
|
||||
|
|
48
DiscordChatExporter.Core/Services/ChatLogService.cs
Normal file
48
DiscordChatExporter.Core/Services/ChatLogService.cs
Normal file
|
@ -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<ChatLog> GetChatLogAsync(AuthToken token, Guild guild, Channel channel,
|
||||
DateTime? from = null, DateTime? to = null, IProgress<double> 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<ChatLog> GetChatLogAsync(AuthToken token, string channelId,
|
||||
DateTime? from = null, DateTime? to = null, IProgress<double> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -65,6 +65,10 @@ namespace DiscordChatExporter.Core.Services
|
|||
|
||||
public async Task<Guild> 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);
|
||||
|
||||
|
|
15
DiscordChatExporter.Core/Services/IChatLogService.cs
Normal file
15
DiscordChatExporter.Core/Services/IChatLogService.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordChatExporter.Core.Models;
|
||||
|
||||
namespace DiscordChatExporter.Core.Services
|
||||
{
|
||||
public interface IChatLogService
|
||||
{
|
||||
Task<ChatLog> GetChatLogAsync(AuthToken token, Guild guild, Channel channel,
|
||||
DateTime? from = null, DateTime? to = null, IProgress<double> progress = null);
|
||||
|
||||
Task<ChatLog> GetChatLogAsync(AuthToken token, string channelId,
|
||||
DateTime? from = null, DateTime? to = null, IProgress<double> progress = null);
|
||||
}
|
||||
}
|
|
@ -17,6 +17,7 @@ namespace DiscordChatExporter.Gui
|
|||
SimpleIoc.Default.Reset();
|
||||
|
||||
// Services
|
||||
SimpleIoc.Default.Register<IChatLogService, ChatLogService>();
|
||||
SimpleIoc.Default.Register<IDataService, DataService>();
|
||||
SimpleIoc.Default.Register<IExportService, ExportService>();
|
||||
SimpleIoc.Default.Register<IMessageGroupService, MessageGroupService>();
|
||||
|
|
|
@ -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<Guild, IReadOnlyList<Channel>> _guildChannelsMap;
|
||||
|
@ -111,12 +111,12 @@ namespace DiscordChatExporter.Gui.ViewModels
|
|||
public RelayCommand<Channel> 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<Guild, IReadOnlyList<Channel>>();
|
||||
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue