Skip to content

Commit

Permalink
ExportService is pseudo async now
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Sep 28, 2017
1 parent b7979d6 commit d8db4fc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 84 deletions.
168 changes: 87 additions & 81 deletions DiscordChatExporter/Services/ExportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using System.Resources;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DiscordChatExporter.Models;
using HtmlAgilityPack;
using Tyrrrz.Extensions;
Expand All @@ -18,104 +19,109 @@ public ExportService(ISettingsService settingsService)
_settingsService = settingsService;
}

public void Export(string filePath, ChannelChatLog log, Theme theme)
public Task ExportAsync(string filePath, ChannelChatLog log, Theme theme)
{
var doc = GetTemplate();
var style = GetStyle(theme);
var dateFormat = _settingsService.DateFormat;

// Set theme
var themeHtml = doc.GetElementbyId("theme");
themeHtml.InnerHtml = style;

// Title
var titleHtml = doc.DocumentNode.Element("html").Element("head").Element("title");
titleHtml.InnerHtml = $"{log.Guild.Name} - {log.Channel.Name}";

// Info
var infoHtml = doc.GetElementbyId("info");
var infoLeftHtml = infoHtml.AppendChild(HtmlNode.CreateNode("<div class=\"info-left\"></div>"));
infoLeftHtml.AppendChild(HtmlNode.CreateNode(
$"<img class=\"guild-icon\" src=\"{log.Guild.IconUrl}\" />"));
var infoRightHtml = infoHtml.AppendChild(HtmlNode.CreateNode("<div class=\"info-right\"></div>"));
infoRightHtml.AppendChild(HtmlNode.CreateNode(
$"<div class=\"guild-name\">{log.Guild.Name}</div>"));
infoRightHtml.AppendChild(HtmlNode.CreateNode(
$"<div class=\"channel-name\">{log.Channel.Name}</div>"));
infoRightHtml.AppendChild(HtmlNode.CreateNode(
$"<div class=\"misc\">{log.MessageGroups.SelectMany(g => g.Messages).Count():N0} messages</div>"));

// Log
var logHtml = doc.GetElementbyId("log");
foreach (var messageGroup in log.MessageGroups)
return Task.Run(() =>
{
// Container
var messageHtml = logHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg\"></div>"));
var doc = GetTemplate();
var style = GetStyle(theme);
var dateFormat = _settingsService.DateFormat;
// Set theme
var themeHtml = doc.GetElementbyId("theme");
themeHtml.InnerHtml = style;
// Title
var titleHtml = doc.DocumentNode.Element("html").Element("head").Element("title");
titleHtml.InnerHtml = $"{log.Guild.Name} - {log.Channel.Name}";
// Info
var infoHtml = doc.GetElementbyId("info");
var infoLeftHtml = infoHtml.AppendChild(HtmlNode.CreateNode("<div class=\"info-left\"></div>"));
infoLeftHtml.AppendChild(HtmlNode.CreateNode(
$"<img class=\"guild-icon\" src=\"{log.Guild.IconUrl}\" />"));
var infoRightHtml = infoHtml.AppendChild(HtmlNode.CreateNode("<div class=\"info-right\"></div>"));
infoRightHtml.AppendChild(HtmlNode.CreateNode(
$"<div class=\"guild-name\">{log.Guild.Name}</div>"));
infoRightHtml.AppendChild(HtmlNode.CreateNode(
$"<div class=\"channel-name\">{log.Channel.Name}</div>"));
infoRightHtml.AppendChild(HtmlNode.CreateNode(
$"<div class=\"misc\">{log.MessageGroups.SelectMany(g => g.Messages).Count():N0} messages</div>"));
// Log
var logHtml = doc.GetElementbyId("log");
foreach (var messageGroup in log.MessageGroups)
{
// Container
var messageHtml = logHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg\"></div>"));
// Left
var messageLeftHtml = messageHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg-left\"></div>"));
// Left
var messageLeftHtml =
messageHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg-left\"></div>"));
// Avatar
messageLeftHtml.AppendChild(
HtmlNode.CreateNode($"<img class=\"msg-avatar\" src=\"{messageGroup.Author.AvatarUrl}\" />"));
// Avatar
messageLeftHtml.AppendChild(
HtmlNode.CreateNode($"<img class=\"msg-avatar\" src=\"{messageGroup.Author.AvatarUrl}\" />"));
// Right
var messageRightHtml = messageHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg-right\"></div>"));
// Right
var messageRightHtml =
messageHtml.AppendChild(HtmlNode.CreateNode("<div class=\"msg-right\"></div>"));
// Author
var authorName = HtmlDocument.HtmlEncode(messageGroup.Author.Name);
messageRightHtml.AppendChild(HtmlNode.CreateNode($"<span class=\"msg-user\">{authorName}</span>"));
// Author
var authorName = HtmlDocument.HtmlEncode(messageGroup.Author.Name);
messageRightHtml.AppendChild(HtmlNode.CreateNode($"<span class=\"msg-user\">{authorName}</span>"));
// Date
var timeStamp = HtmlDocument.HtmlEncode(messageGroup.TimeStamp.ToString(dateFormat));
messageRightHtml.AppendChild(HtmlNode.CreateNode($"<span class=\"msg-date\">{timeStamp}</span>"));
// Date
var timeStamp = HtmlDocument.HtmlEncode(messageGroup.TimeStamp.ToString(dateFormat));
messageRightHtml.AppendChild(HtmlNode.CreateNode($"<span class=\"msg-date\">{timeStamp}</span>"));
// Individual messages
foreach (var message in messageGroup.Messages)
{
// Content
if (message.Content.IsNotBlank())
// Individual messages
foreach (var message in messageGroup.Messages)
{
var content = FormatMessageContent(message.Content);
var contentHtml =
messageRightHtml.AppendChild(
HtmlNode.CreateNode($"<div class=\"msg-content\">{content}</div>"));

// Edited timestamp
if (message.EditedTimeStamp != null)
// Content
if (message.Content.IsNotBlank())
{
contentHtml.AppendChild(
HtmlNode.CreateNode(
$"<span class=\"msg-edited\" title=\"{message.EditedTimeStamp.Value.ToString(dateFormat)}\">(edited)</span>"));
var content = FormatMessageContent(message.Content);
var contentHtml =
messageRightHtml.AppendChild(
HtmlNode.CreateNode($"<div class=\"msg-content\">{content}</div>"));
// Edited timestamp
if (message.EditedTimeStamp != null)
{
contentHtml.AppendChild(
HtmlNode.CreateNode(
$"<span class=\"msg-edited\" title=\"{message.EditedTimeStamp.Value.ToString(dateFormat)}\">(edited)</span>"));
}
}
}
// Attachments
foreach (var attachment in message.Attachments)
{
if (attachment.Type == AttachmentType.Image)
{
messageRightHtml.AppendChild(
HtmlNode.CreateNode("<div class=\"msg-attachment\">" +
$"<a href=\"{attachment.Url}\">" +
$"<img class=\"msg-attachment\" src=\"{attachment.Url}\" />" +
"</a>" +
"</div>"));
}
else
// Attachments
foreach (var attachment in message.Attachments)
{
messageRightHtml.AppendChild(
HtmlNode.CreateNode("<div class=\"msg-attachment\">" +
$"<a href=\"{attachment.Url}\">" +
$"Attachment: {attachment.FileName} ({NormalizeFileSize(attachment.FileSize)})" +
"</a>" +
"</div>"));
if (attachment.Type == AttachmentType.Image)
{
messageRightHtml.AppendChild(
HtmlNode.CreateNode("<div class=\"msg-attachment\">" +
$"<a href=\"{attachment.Url}\">" +
$"<img class=\"msg-attachment\" src=\"{attachment.Url}\" />" +
"</a>" +
"</div>"));
}
else
{
messageRightHtml.AppendChild(
HtmlNode.CreateNode("<div class=\"msg-attachment\">" +
$"<a href=\"{attachment.Url}\">" +
$"Attachment: {attachment.FileName} ({NormalizeFileSize(attachment.FileSize)})" +
"</a>" +
"</div>"));
}
}
}
}
}
doc.Save(filePath);
doc.Save(filePath);
});
}
}

Expand Down
5 changes: 3 additions & 2 deletions DiscordChatExporter/Services/IExportService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using DiscordChatExporter.Models;
using System.Threading.Tasks;
using DiscordChatExporter.Models;

namespace DiscordChatExporter.Services
{
public interface IExportService
{
void Export(string filePath, ChannelChatLog log, Theme theme);
Task ExportAsync(string filePath, ChannelChatLog log, Theme theme);
}
}
2 changes: 1 addition & 1 deletion DiscordChatExporter/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private async void ExportChannel(Channel channel)
var chatLog = new ChannelChatLog(SelectedGuild, channel, messageGroups);

// Export
_exportService.Export(sfd.FileName, chatLog, _settingsService.Theme);
await _exportService.ExportAsync(sfd.FileName, chatLog, _settingsService.Theme);

// Show dialog
MessengerInstance.Send(new ShowExportDoneMessage(sfd.FileName));
Expand Down

0 comments on commit d8db4fc

Please sign in to comment.