Hide message content in HTML when linking to an image

Closes #695
This commit is contained in:
Oleksii Holub
2022-06-29 21:43:43 +03:00
parent 356b1a6afd
commit edba9f36a7
13 changed files with 121 additions and 117 deletions

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;
using AngleSharp.Dom;
using DiscordChatExporter.Cli.Tests.Fixtures;
using DiscordChatExporter.Cli.Tests.TestData;
@@ -26,8 +27,6 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
Snowflake.Parse("885587844989612074")
);
var fileUrl = message.QuerySelector(".chatlog__attachment a")?.GetAttribute("href");
// Assert
message.Text().Should().ContainAll(
"Generic file attachment",
@@ -35,9 +34,13 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
"11 bytes"
);
fileUrl.Should().StartWithEquivalentOf(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
);
message
.QuerySelectorAll("a")
.Select(e => e.GetAttribute("href"))
.Should()
.Contain(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
);
}
[Fact]
@@ -49,14 +52,16 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
Snowflake.Parse("885654862656843786")
);
var imageUrl = message.QuerySelector(".chatlog__attachment img")?.GetAttribute("src");
// Assert
message.Text().Should().Contain("Image attachment");
imageUrl.Should().StartWithEquivalentOf(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
);
message
.QuerySelectorAll("img")
.Select(e => e.GetAttribute("src"))
.Should()
.Contain(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
);
}
[Fact]
@@ -68,12 +73,11 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
Snowflake.Parse("885655761919836171")
);
var videoUrl = message.QuerySelector("video source")?.GetAttribute("src");
// Assert
message.Text().Should().Contain("Video attachment");
videoUrl.Should().StartWithEquivalentOf(
var videoUrl = message.QuerySelector("video source")?.GetAttribute("src");
videoUrl.Should().StartWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
);
}
@@ -87,12 +91,11 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
Snowflake.Parse("885656175620808734")
);
var audioUrl = message.QuerySelector("audio source")?.GetAttribute("src");
// Assert
message.Text().Should().Contain("Audio attachment");
audioUrl.Should().StartWithEquivalentOf(
var audioUrl = message.QuerySelector("audio source")?.GetAttribute("src");
audioUrl.Should().StartWith(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
);
}

View File

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using AngleSharp.Dom;
using DiscordChatExporter.Cli.Tests.Fixtures;
using DiscordChatExporter.Cli.Tests.TestData;
using DiscordChatExporter.Core.Discord;
using FluentAssertions;
using Xunit;
@@ -35,7 +36,7 @@ public class ContentSpecs : IClassFixture<ExportWrapperFixture>
"885169254029213696"
);
messages.Select(e => e.QuerySelector(".chatlog__content")?.Text().Trim()).Should().Equal(
messages.SelectMany(e => e.Text()).Should().ContainInOrder(
"Hello world",
"Goodbye world",
"Foo bar",
@@ -46,4 +47,18 @@ public class ContentSpecs : IClassFixture<ExportWrapperFixture>
"Yeet"
);
}
[Fact]
public async Task Message_content_is_hidden_if_it_only_contains_a_link_to_an_image()
{
// Act
var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases,
Snowflake.Parse("991768701126852638")
);
// Assert
var content = message.QuerySelector(".chatlog__content")?.Text();
content.Should().BeNullOrEmpty();
}
}

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Linq;
using System.Threading.Tasks;
using AngleSharp.Dom;
using DiscordChatExporter.Cli.Tests.Fixtures;
using DiscordChatExporter.Cli.Tests.TestData;
@@ -39,18 +40,20 @@ public class EmbedSpecs : IClassFixture<ExportWrapperFixture>
}
[Fact]
public async Task Message_with_a_link_to_an_image_is_rendered_with_that_image()
public async Task Message_with_a_link_to_an_image_contains_an_embed_of_that_image()
{
// Act
var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases,
Snowflake.Parse("991758772349440053")
Snowflake.Parse("991768701126852638")
);
var imageSrc = message.QuerySelector("img")?.GetAttribute("src");
// Assert
imageSrc.Should().StartWithEquivalentOf("https://i.redd.it/f8w05ja8s4e61.png");
message
.QuerySelectorAll("img")
.Select(e => e.GetAttribute("src"))
.Should()
.Contain("https://i.redd.it/f8w05ja8s4e61.png");
}
[Fact]
@@ -62,10 +65,9 @@ public class EmbedSpecs : IClassFixture<ExportWrapperFixture>
Snowflake.Parse("867886632203976775")
);
var iframeSrc = message.QuerySelector("iframe")?.GetAttribute("src");
// Assert
iframeSrc.Should().StartWithEquivalentOf("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP");
var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src");
iframeUrl.Should().StartWith("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP");
}
[Fact]
@@ -77,9 +79,8 @@ public class EmbedSpecs : IClassFixture<ExportWrapperFixture>
Snowflake.Parse("866472508588294165")
);
var iframeSrc = message.QuerySelector("iframe")?.GetAttribute("src");
// Assert
iframeSrc.Should().StartWithEquivalentOf("https://www.youtube.com/embed/qOWW4OlgbvE");
var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src");
iframeUrl.Should().StartWith("https://www.youtube.com/embed/qOWW4OlgbvE");
}
}

View File

@@ -27,7 +27,7 @@ public class MentionSpecs : IClassFixture<ExportWrapperFixture>
);
// Assert
message.Text().Trim().Should().Contain("User mention: @Tyrrrz");
message.Text().Should().Contain("User mention: @Tyrrrz");
message.InnerHtml.Should().Contain("Tyrrrz#5447");
}
@@ -41,7 +41,7 @@ public class MentionSpecs : IClassFixture<ExportWrapperFixture>
);
// Assert
message.Text().Trim().Should().Contain("Text channel mention: #mention-tests");
message.Text().Should().Contain("Text channel mention: #mention-tests");
}
[Fact]
@@ -54,7 +54,7 @@ public class MentionSpecs : IClassFixture<ExportWrapperFixture>
);
// Assert
message.Text().Trim().Should().Contain("Voice channel mention: 🔊chaos-vc");
message.Text().Should().Contain("Voice channel mention: 🔊chaos-vc");
}
[Fact]
@@ -67,6 +67,6 @@ public class MentionSpecs : IClassFixture<ExportWrapperFixture>
);
// Assert
message.Text().Trim().Should().Contain("Role mention: @Role 1");
message.Text().Should().Contain("Role mention: @Role 1");
}
}

View File

@@ -27,8 +27,8 @@ public class ReplySpecs : IClassFixture<ExportWrapperFixture>
);
// Assert
message.Text().Trim().Should().Contain("reply to original");
message.QuerySelector(".chatlog__reference-link")?.Text().Trim().Should().Contain("original");
message.Text().Should().Contain("reply to original");
message.QuerySelector(".chatlog__reference-link")?.Text().Should().Contain("original");
}
[Fact]
@@ -41,9 +41,10 @@ public class ReplySpecs : IClassFixture<ExportWrapperFixture>
);
// Assert
message.Text().Trim().Should().Contain("reply to deleted");
message.QuerySelector(".chatlog__reference-link")?.Text().Trim().Should()
.Contain("Original message was deleted or could not be loaded.");
message.Text().Should().Contain("reply to deleted");
message.QuerySelector(".chatlog__reference-link")?.Text().Should().Contain(
"Original message was deleted or could not be loaded."
);
}
[Fact]
@@ -56,8 +57,7 @@ public class ReplySpecs : IClassFixture<ExportWrapperFixture>
);
// Assert
message.Text().Trim().Should().Contain("reply to attachment");
message.QuerySelector(".chatlog__reference-link")?.Text().Trim().Should()
.Contain("Click to see attachment");
message.Text().Should().Contain("reply to attachment");
message.QuerySelector(".chatlog__reference-link")?.Text().Should().Contain("Click to see attachment");
}
}

View File

@@ -25,12 +25,9 @@ public class StickerSpecs : IClassFixture<ExportWrapperFixture>
Snowflake.Parse("939670623158943754")
);
var container = message.QuerySelector("[title='rock']");
var sourceUrl = container?.QuerySelector("img")?.GetAttribute("src");
// Assert
container.Should().NotBeNull();
sourceUrl.Should().Be("https://discord.com/stickers/904215665597120572.png");
var stickerUrl = message.QuerySelector("[title='rock'] img")?.GetAttribute("src");
stickerUrl.Should().Be("https://discord.com/stickers/904215665597120572.png");
}
[Fact]
@@ -42,11 +39,8 @@ public class StickerSpecs : IClassFixture<ExportWrapperFixture>
Snowflake.Parse("939670526517997590")
);
var container = message.QuerySelector("[title='Yikes']");
var sourceUrl = container?.QuerySelector("div[data-source]")?.GetAttribute("data-source");
// Assert
container.Should().NotBeNull();
sourceUrl.Should().Be("https://discord.com/stickers/816087132447178774.json");
var stickerUrl = message.QuerySelector("[title='Yikes'] div[data-source]")?.GetAttribute("data-source");
stickerUrl.Should().Be("https://discord.com/stickers/816087132447178774.json");
}
}