Render guild invites in HTML

Closes #649
pull/1003/head
Tyrrrz 2 years ago
parent 8e5269f0ab
commit 191294b4fe

@ -156,4 +156,19 @@ public class HtmlEmbedSpecs
message.QuerySelectorAll(".chatlog__embed").Should().ContainSingle();
}
[Fact]
public async Task Message_with_a_guild_invite_link_is_rendered_with_a_widget()
{
// https://github.com/Tyrrrz/DiscordChatExporter/issues/649
// Act
var message = await ExportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases,
Snowflake.Parse("1075116548966064128")
);
// Assert
message.Text().Should().Contain("DiscordChatExporter TestServer");
}
}

@ -0,0 +1,25 @@
using System.Text.Json;
using System.Text.RegularExpressions;
using DiscordChatExporter.Core.Utils.Extensions;
using JsonExtensions.Reading;
namespace DiscordChatExporter.Core.Discord.Data;
// https://discord.com/developers/docs/resources/invite#invite-object
public record Invite(
string Code,
Guild? Guild,
Channel? Channel)
{
public static string? TryGetCodeFromUrl(string url) =>
Regex.Match(url, @"^https?://discord\.gg/(\w+)/?$").Groups[1].Value.NullIfWhiteSpace();
public static Invite Parse(JsonElement json)
{
var code = json.GetProperty("code").GetNonWhiteSpaceString();
var guild = json.GetPropertyOrNull("guild")?.Pipe(Guild.Parse);
var channel = json.GetPropertyOrNull("channel")?.Pipe(c => Channel.Parse(c));
return new Invite(code, guild, channel);
}
}

@ -19,12 +19,6 @@ public partial record Member(
public partial record Member
{
public static Member CreateForUser(User user) => new(
user,
user.Name,
Array.Empty<Snowflake>()
);
public static Member Parse(JsonElement json)
{
var user = json.GetProperty("user").Pipe(User.Parse);

@ -260,10 +260,17 @@ public class DiscordClient
return null;
var response = await TryGetJsonResponseAsync($"guilds/{guildId}/members/{memberId}", cancellationToken);
return response?.Pipe(Member.Parse);
}
public async ValueTask<Invite?> TryGetGuildInviteAsync(
string code,
CancellationToken cancellationToken = default)
{
var response = await TryGetJsonResponseAsync($"invites/{code}", cancellationToken);
return response?.Pipe(Invite.Parse);
}
public async ValueTask<ChannelCategory> GetChannelCategoryAsync(
Snowflake channelId,
CancellationToken cancellationToken = default)

@ -130,7 +130,7 @@ internal partial class HtmlMarkdownVisitor : MarkdownVisitor
// Try to extract the message ID if the link points to a Discord message
var linkedMessageId = Regex.Match(
link.Url,
"^https?://(?:discord|discordapp).com/channels/.*?/(\\d+)/?$"
@"^https?://(?:discord|discordapp)\.com/channels/.*?/(\d+)/?$"
).Groups[1].Value;
_buffer.Append(

@ -4,6 +4,7 @@
@using System.Threading.Tasks
@using DiscordChatExporter.Core.Discord.Data
@using DiscordChatExporter.Core.Discord.Data.Embeds
@using DiscordChatExporter.Core.Markdown.Parsing
@using DiscordChatExporter.Core.Utils.Extensions
@inherits RazorBlade.HtmlTemplate
@ -311,6 +312,49 @@
</div>
}
@{/* Invites */}
@{
var inviteCodes = MarkdownParser
.ExtractLinks(message.Content)
.Select(l => l.Url)
.Select(Invite.TryGetCodeFromUrl)
.WhereNotNull()
.ToArray();
foreach (var inviteCode in inviteCodes)
{
var invite = await ExportContext.Discord.TryGetGuildInviteAsync(inviteCode, CancellationToken);
if (invite is null)
{
continue;
}
<div class="chatlog__embed">
<div class="chatlog__embed-invite-container">
<div class="chatlog__embed-invite-title">Invite to join a server</div>
<div class="chatlog__embed-invite">
<div class="chatlog__embed-invite-guild-icon-container">
<img class="chatlog__embed-invite-guild-icon" src="@await ResolveAssetUrlAsync(invite.Guild?.IconUrl ?? Guild.DirectMessages.IconUrl)" alt="Guild icon" loading="lazy">
</div>
<div class="chatlog__embed-invite-info">
<div class="chatlog__embed-invite-guild-name">
<a href="https://discord.gg/@invite.Code">
@(invite.Guild?.Name ?? "Unknown guild")
</a>
</div>
<div class="chatlog__embed-invite-channel-name">
<svg class="chatlog__embed-invite-channel-icon">
<use href="#channel-icon"></use>
</svg>
<span> @(invite.Channel?.Name ?? "Unknown channel")</span>
</div>
</div>
</div>
</div>
</div>
}
}
@{/* Embeds */}
@foreach (var embed in message.Embeds)
{

@ -595,6 +595,56 @@
border-radius: 3px;
}
.chatlog__embed-invite-container {
min-width: 320px;
padding: 0.6rem 0.7rem;
border: 1px solid @Themed("rgba(46, 48, 54, 0.6)", "rgba(204, 204, 204, 0.3)");
border-radius: 3px;
background-color: @Themed("rgba(46, 48, 54, 0.3)", "rgba(249, 249, 249, 0.3)");
}
.chatlog__embed-invite-title {
margin: 0 0 0.8rem 0;
color: @Themed("#b9bbbe", "#4f5660");
font-size: 0.75rem;
font-weight: 700;
text-transform: uppercase;
}
.chatlog__embed-invite {
display: flex;
}
.chatlog__embed-invite-guild-icon {
width: 50px;
height: 50px;
border-radius: 0.85rem;
}
.chatlog__embed-invite-info {
margin-left: 1rem;
}
.chatlog__embed-invite-guild-name {
color: @Themed("#ffffff", "#36393e");
font-weight: 600;
}
.chatlog__embed-invite-guild-name a {
color: inherit;
}
.chatlog__embed-invite-channel-icon {
width: 18px;
height: 18px;
vertical-align: bottom;
}
.chatlog__embed-invite-channel-name {
font-size: 0.9rem;
font-weight: 600;
}
.chatlog__embed-spotify {
border: 0;
}
@ -851,6 +901,9 @@
<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="channel-icon" viewBox="0 0 24 24">
<path fill="#b9bbbe" d="M5.88657 21C5.57547 21 5.3399 20.7189 5.39427 20.4126L6.00001 17H2.59511C2.28449 17 2.04905 16.7198 2.10259 16.4138L2.27759 15.4138C2.31946 15.1746 2.52722 15 2.77011 15H6.35001L7.41001 9H4.00511C3.69449 9 3.45905 8.71977 3.51259 8.41381L3.68759 7.41381C3.72946 7.17456 3.93722 7 4.18011 7H7.76001L8.39677 3.41262C8.43914 3.17391 8.64664 3 8.88907 3H9.87344C10.1845 3 10.4201 3.28107 10.3657 3.58738L9.76001 7H15.76L16.3968 3.41262C16.4391 3.17391 16.6466 3 16.8891 3H17.8734C18.1845 3 18.4201 3.28107 18.3657 3.58738L17.76 7H21.1649C21.4755 7 21.711 7.28023 21.6574 7.58619L21.4824 8.58619C21.4406 8.82544 21.2328 9 20.9899 9H17.41L16.35 15H19.7549C20.0655 15 20.301 15.2802 20.2474 15.5862L20.0724 16.5862C20.0306 16.8254 19.8228 17 19.5799 17H16L15.3632 20.5874C15.3209 20.8261 15.1134 21 14.8709 21H13.8866C13.5755 21 13.3399 20.7189 13.3943 20.4126L14 17H8.00001L7.36325 20.5874C7.32088 20.8261 7.11337 21 6.87094 21H5.88657ZM9.41045 9L8.35045 15H14.3504L15.4104 9H9.41045Z" />
</symbol>
<symbol id="thread-icon" viewBox="0 0 24 24">
<path fill="#b9bbbe" d="M5.43309 21C5.35842 21 5.30189 20.9325 5.31494 20.859L5.99991 17H2.14274C2.06819 17 2.01168 16.9327 2.02453 16.8593L2.33253 15.0993C2.34258 15.0419 2.39244 15 2.45074 15H6.34991L7.40991 9H3.55274C3.47819 9 3.42168 8.93274 3.43453 8.85931L3.74253 7.09931C3.75258 7.04189 3.80244 7 3.86074 7H7.75991L8.45234 3.09903C8.46251 3.04174 8.51231 3 8.57049 3H10.3267C10.4014 3 10.4579 3.06746 10.4449 3.14097L9.75991 7H15.7599L16.4523 3.09903C16.4625 3.04174 16.5123 3 16.5705 3H18.3267C18.4014 3 18.4579 3.06746 18.4449 3.14097L17.7599 7H21.6171C21.6916 7 21.7481 7.06725 21.7353 7.14069L21.4273 8.90069C21.4172 8.95811 21.3674 9 21.3091 9H17.4099L17.0495 11.04H15.05L15.4104 9H9.41035L8.35035 15H10.5599V17H7.99991L7.30749 20.901C7.29732 20.9583 7.24752 21 7.18934 21H5.43309Z" />
<path fill="#b9bbbe" d="M13.4399 12.96C12.9097 12.96 12.4799 13.3898 12.4799 13.92V20.2213C12.4799 20.7515 12.9097 21.1813 13.4399 21.1813H14.3999C14.5325 21.1813 14.6399 21.2887 14.6399 21.4213V23.4597C14.6399 23.6677 14.8865 23.7773 15.0408 23.6378L17.4858 21.4289C17.6622 21.2695 17.8916 21.1813 18.1294 21.1813H22.5599C23.0901 21.1813 23.5199 20.7515 23.5199 20.2213V13.92C23.5199 13.3898 23.0901 12.96 22.5599 12.96H13.4399Z" />

@ -2,4 +2,7 @@
namespace DiscordChatExporter.Core.Markdown;
internal record FormattingNode(FormattingKind Kind, IReadOnlyList<MarkdownNode> Children) : MarkdownNode;
internal record FormattingNode(
FormattingKind Kind,
IReadOnlyList<MarkdownNode> Children
) : MarkdownNode, IContainerNode;

@ -0,0 +1,8 @@
using System.Collections.Generic;
namespace DiscordChatExporter.Core.Markdown;
internal interface IContainerNode
{
IReadOnlyList<MarkdownNode> Children { get; }
}

@ -2,9 +2,10 @@
namespace DiscordChatExporter.Core.Markdown;
// Named links can contain child nodes (e.g. [**bold URL**](https://test.com))
internal record LinkNode(
string Url,
IReadOnlyList<MarkdownNode> Children) : MarkdownNode
IReadOnlyList<MarkdownNode> Children) : MarkdownNode, IContainerNode
{
public LinkNode(string url)
: this(url, new[] { new TextNode(url) })

@ -385,12 +385,32 @@ internal static partial class MarkdownParser
private static IReadOnlyList<MarkdownNode> Parse(StringSegment segment) =>
Parse(segment, AggregateNodeMatcher);
public static IReadOnlyList<MarkdownNode> Parse(string markdown) =>
Parse(new StringSegment(markdown));
private static IReadOnlyList<MarkdownNode> ParseMinimal(StringSegment segment) =>
Parse(segment, MinimalAggregateNodeMatcher);
public static IReadOnlyList<MarkdownNode> Parse(string input) =>
Parse(new StringSegment(input));
public static IReadOnlyList<MarkdownNode> ParseMinimal(string markdown) =>
ParseMinimal(new StringSegment(markdown));
private static void ExtractLinks(IEnumerable<MarkdownNode> nodes, ICollection<LinkNode> links)
{
foreach (var node in nodes)
{
if (node is LinkNode linkNode)
links.Add(linkNode);
if (node is IContainerNode containerNode)
ExtractLinks(containerNode.Children, links);
}
}
public static IReadOnlyList<LinkNode> ExtractLinks(string markdown)
{
var links = new List<LinkNode>();
ExtractLinks(Parse(markdown), links);
public static IReadOnlyList<MarkdownNode> ParseMinimal(string input) =>
ParseMinimal(new StringSegment(input));
return links;
}
}
Loading…
Cancel
Save