Add proper support for more system notifications

Closes #844
This commit is contained in:
Tyrrrz 2023-02-09 19:50:41 +02:00
parent a95d2ef298
commit 3b2c308a25
8 changed files with 126 additions and 27 deletions

View file

@ -99,20 +99,7 @@ public record Message(
.GetDateTimeOffset();
var isPinned = json.GetPropertyOrNull("pinned")?.GetBooleanOrNull() ?? false;
var content = kind switch
{
MessageKind.RecipientAdd => "Added a recipient.",
MessageKind.RecipientRemove => "Removed a recipient.",
MessageKind.Call =>
$"Started a call that lasted {callEndedTimestamp?.Pipe(t => t - timestamp).Pipe(t => (int)t.TotalMinutes) ?? 0} minutes.",
MessageKind.ChannelNameChange => "Changed the channel name.",
MessageKind.ChannelIconChange => "Changed the channel icon.",
MessageKind.ChannelPinnedMessage => "Pinned a message.",
MessageKind.ThreadCreated => "Started a thread.",
MessageKind.GuildMemberJoin => "Joined the server.",
_ => json.GetPropertyOrNull("content")?.GetStringOrNull() ?? ""
};
var content = json.GetPropertyOrNull("content")?.GetStringOrNull() ?? "";
var attachments =
json.GetPropertyOrNull("attachments")?.EnumerateArrayOrNull()?.Select(Attachment.Parse).ToArray() ??

View file

@ -85,7 +85,15 @@ internal partial class CsvMessageWriter : MessageWriter
await _writer.WriteAsync(',');
// Message content
await _writer.WriteAsync(CsvEncode(await FormatMarkdownAsync(message.Content, cancellationToken)));
if (message.Kind.IsSystemNotification())
{
await _writer.WriteAsync(CsvEncode(message.GetFallbackContent()));
}
else
{
await _writer.WriteAsync(CsvEncode(await FormatMarkdownAsync(message.Content, cancellationToken)));
}
await _writer.WriteAsync(',');
// Attachments

View file

@ -299,7 +299,14 @@ internal class JsonMessageWriter : MessageWriter
_writer.WriteBoolean("isPinned", message.IsPinned);
// Content
_writer.WriteString("content", await FormatMarkdownAsync(message.Content, cancellationToken));
if (message.Kind.IsSystemNotification())
{
_writer.WriteString("content", message.GetFallbackContent());
}
else
{
_writer.WriteString("content", await FormatMarkdownAsync(message.Content, cancellationToken));
}
// Author
_writer.WriteStartObject("author");

View file

@ -71,7 +71,21 @@
: userMember?.Nick ?? message.Author.Name;
<div class="chatlog__message-aside">
<svg class="chatlog__system-notification-icon"><use href="#@message.Kind.ToString().ToDashCase().ToLowerInvariant()-icon"></use></svg>
<svg class="chatlog__system-notification-icon">
@{
var icon = message.Kind switch {
MessageKind.RecipientAdd => "join-icon",
MessageKind.RecipientRemove => "leave-icon",
MessageKind.Call => "call-icon",
MessageKind.ChannelNameChange => "pencil-icon",
MessageKind.ChannelIconChange => "pencil-icon",
MessageKind.ChannelPinnedMessage => "pin-icon",
_ => "pencil-icon"
};
}
<use href="#@icon"></use>
</svg>
</div>
<div class="chatlog__message-primary">
@ -80,14 +94,55 @@
@{/* System notification content */}
<span class="chatlog__system-notification-content">
@if (message.Kind == MessageKind.ChannelPinnedMessage && message.Reference is not null)
@if (message.Kind == MessageKind.RecipientAdd && message.MentionedUsers.Any())
{
<span> added </span>
<a class="chatlog__system-notification-link" title="@message.MentionedUsers.First().FullName">@message.MentionedUsers.First().Name</a>
<span> to the group.</span>
}
else if (message.Kind == MessageKind.RecipientRemove && message.MentionedUsers.Any())
{
if (message.Author.Id == message.MentionedUsers.First().Id)
{
<span> left the group.</span>
}
else
{
<span> removed </span>
<a class="chatlog__system-notification-link" title="@message.MentionedUsers.First().FullName">@message.MentionedUsers.First().Name</a>
<span> from the group.</span>
}
}
else if (message.Kind == MessageKind.Call)
{
<span> started a call that lasted @(((message.CallEndedTimestamp ?? message.Timestamp) - message.Timestamp).TotalMinutes) minutes</span>
}
else if (message.Kind == MessageKind.ChannelNameChange)
{
<span> changed the channel name: </span>
<span class="chatlog__system-notification-link">@message.Content</span>
}
else if (message.Kind == MessageKind.ChannelIconChange)
{
<span> changed the channel icon.</span>
}
else if (message.Kind == MessageKind.ChannelPinnedMessage && message.Reference is not null)
{
<span> pinned</span>
<a class="chatlog__system-notification-link" href="#chatlog__message-container-@message.Reference.MessageId"> a message</a>
<span> to this channel.</span>
}
else if (message.Kind == MessageKind.ThreadCreated)
{
<span> started a thread.</span>
}
else if (message.Kind == MessageKind.GuildMemberJoin)
{
<span> joined the server.</span>
}
else
{
<span> </span>
<span>@message.Content.ToLowerInvariant()</span>
}
</span>

View file

@ -0,0 +1,36 @@
using System.Linq;
using DiscordChatExporter.Core.Discord.Data;
using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Core.Exporting;
internal static class PlainTextMessageExtensions
{
public static string GetFallbackContent(this Message message) => message.Kind switch
{
MessageKind.RecipientAdd => message.MentionedUsers.Any()
? $"Added {message.MentionedUsers.First().Name} to the group."
: "Added a recipient.",
MessageKind.RecipientRemove => message.MentionedUsers.Any()
? message.Author.Id == message.MentionedUsers.First().Id
? "Left the group."
: $"Removed {message.MentionedUsers.First().Name} from the group."
: "Removed a recipient.",
MessageKind.Call =>
$"Started a call that lasted {message.CallEndedTimestamp?.Pipe(t => t - message.Timestamp).Pipe(t => (int)t.TotalMinutes) ?? 0} minutes.",
MessageKind.ChannelNameChange =>
!string.IsNullOrWhiteSpace(message.Content)
? $"Changed the channel name: {message.Content}"
: "Changed the channel name.",
MessageKind.ChannelIconChange => "Changed the channel icon.",
MessageKind.ChannelPinnedMessage => "Pinned a message.",
MessageKind.ThreadCreated => "Started a thread.",
MessageKind.GuildMemberJoin => "Joined the server.",
_ => message.Content
};
}

View file

@ -222,7 +222,11 @@ internal class PlainTextMessageWriter : MessageWriter
await WriteMessageHeaderAsync(message);
// Content
if (!string.IsNullOrWhiteSpace(message.Content))
if (message.Kind.IsSystemNotification())
{
await _writer.WriteLineAsync(message.GetFallbackContent());
}
else
{
await _writer.WriteLineAsync(
await FormatMarkdownAsync(message.Content, cancellationToken)

View file

@ -835,15 +835,21 @@
<path fill="#f4f5fb" d="M530,215a25,25,0,0,1-25-25V50a25,25,0,0,1,16.23-23.41L693.41,198.77A25,25,0,0,1,670,215Z" />
<path fill="#7789c4" d="M530,70.71,649.29,190H530V70.71M530,0a50,50,0,0,0-50,50V190a50,50,0,0,0,50,50H670a50,50,0,0,0,50-50Z" />
</symbol>
<symbol id="channel-pinned-message-icon" viewBox="0 0 18 18">
<symbol id="join-icon" viewBox="0 0 18 18">
<path fill="#3ba55c" d="m0 8h14.2l-3.6-3.6 1.4-1.4 6 6-6 6-1.4-1.4 3.6-3.6h-14.2" />
</symbol>
<symbol id="leave-icon" viewBox="0 0 18 18">
<path fill="#ed4245" d="m3.8 8 3.6-3.6-1.4-1.4-6 6 6 6 1.4-1.4-3.6-3.6h14.2v-2"/>
</symbol>
<symbol id="pencil-icon" viewBox="0 0 18 18">
<path fill="#99aab5" d="m0 14.25v3.75h3.75l11.06-11.06-3.75-3.75zm17.71-10.21c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75z" />
</symbol>
<symbol id="pin-icon" viewBox="0 0 18 18">
<path fill="#b9bbbe" d="m16.908 8.39684-8.29587-8.295827-1.18584 1.184157 1.18584 1.18584-4.14834 4.1475v.00167l-1.18583-1.18583-1.185 1.18583 3.55583 3.55502-4.740831 4.74 1.185001 1.185 4.74083-4.74 3.55581 3.555 1.185-1.185-1.185-1.185 4.1475-4.14836h.0009l1.185 1.185z" />
</symbol>
<symbol id="call-icon" viewBox="0 0 18 18">
<path fill="#3ba55c" fill-rule="evenodd" d="M17.7163041 15.36645368c-.0190957.02699568-1.9039523 2.6680735-2.9957762 2.63320406-3.0676659-.09785935-6.6733809-3.07188394-9.15694343-5.548738C3.08002193 9.9740657.09772497 6.3791404 0 3.3061316v-.024746C0 2.2060575 2.61386252.3152347 2.64082114.2972376c.7110335-.4971705 1.4917101-.3149497 1.80959713.1372281.19320342.2744561 2.19712724 3.2811005 2.42290565 3.6489167.09884826.1608492.14714912.3554431.14714912.5702838 0 .2744561-.07975258.5770327-.23701117.8751101-.1527655.2902036-.65262318 1.1664385-.89862055 1.594995.2673396.3768148.94804468 1.26429792 2.351016 2.66357424 1.39173858 1.39027775 2.28923588 2.07641807 2.67002628 2.34187563.4302146-.2452108 1.3086162-.74238132 1.5972981-.89423205.5447887-.28682915 1.0907006-.31944893 1.4568885-.08661115.3459689.2182151 3.3383754 2.21027167 3.6225641 2.41611376.2695862.19234426.4144887.5399137.4144887.91672846 0 .2969525-.089862.61190215-.2808189.88523346" />
</symbol>
<symbol id="guild-member-join-icon" viewBox="0 0 18 18">
<path fill="#3ba55c" d="m0 8h14.2l-3.6-3.6 1.4-1.4 6 6-6 6-1.4-1.4 3.6-3.6h-14.2" />
</symbol>
</defs>
</svg>
</head>

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Utils.Extensions;
@ -27,9 +26,6 @@ public static class StringExtensions
}
}
public static string ToDashCase(this string str) =>
Regex.Replace(str, @"(\p{Ll})(\p{Lu})", "$1-$2");
public static T? ParseEnumOrNull<T>(this string str, bool ignoreCase = true) where T : struct, Enum =>
Enum.TryParse<T>(str, ignoreCase, out var result)
? result