Use channel icon instead of guild icon, when available

Closes #987
pull/1001/head
Oleksii Holub 2 years ago
parent 90c68e3cde
commit b36071cddd

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.Json;
using DiscordChatExporter.Core.Discord.Data.Common;
using DiscordChatExporter.Core.Utils.Extensions;
@ -14,6 +15,7 @@ public partial record Channel(
ChannelCategory Category,
string Name,
int? Position,
string? IconUrl,
string? Topic,
Snowflake? LastMessageId) : IHasId
{
@ -35,6 +37,15 @@ public partial record Channel
null
);
private static string GetIconUrl(Snowflake id, string iconHash)
{
var extension = iconHash.StartsWith("a_", StringComparison.Ordinal)
? "gif"
: "png";
return $"https://cdn.discordapp.com/icons/{id}/{iconHash}.{extension}";
}
public static Channel Parse(JsonElement json, ChannelCategory? category = null, int? positionHint = null)
{
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
@ -59,6 +70,9 @@ public partial record Channel
positionHint ??
json.GetPropertyOrNull("position")?.GetInt32OrNull();
// Only available on group DMs
var iconUrl = json.GetPropertyOrNull("icon")?.GetNonWhiteSpaceStringOrNull()?.Pipe(h => GetIconUrl(id, h));
var topic = json.GetPropertyOrNull("topic")?.GetStringOrNull();
var lastMessageId = json
@ -73,6 +87,7 @@ public partial record Channel
category ?? GetFallbackCategory(kind),
name,
position,
iconUrl,
topic,
lastMessageId
);

@ -32,10 +32,9 @@ public record Guild(Snowflake Id, string Name, string IconUrl) : IHasId
var id = json.GetProperty("id").GetNonWhiteSpaceString().Pipe(Snowflake.Parse);
var name = json.GetProperty("name").GetNonNullString();
var iconHash = json.GetPropertyOrNull("icon")?.GetNonWhiteSpaceStringOrNull();
var iconUrl = !string.IsNullOrWhiteSpace(iconHash)
? GetIconUrl(id, iconHash)
: GetDefaultIconUrl();
var iconUrl =
json.GetPropertyOrNull("icon")?.GetNonWhiteSpaceStringOrNull()?.Pipe(h => GetIconUrl(id, h)) ??
GetDefaultIconUrl();
return new Guild(id, name, iconUrl);
}

@ -244,7 +244,12 @@ internal class JsonMessageWriter : MessageWriter
_writer.WriteStartObject("guild");
_writer.WriteString("id", Context.Request.Guild.Id.ToString());
_writer.WriteString("name", Context.Request.Guild.Name);
_writer.WriteString("iconUrl", await Context.ResolveAssetUrlAsync(Context.Request.Guild.IconUrl, cancellationToken));
_writer.WriteString(
"iconUrl",
await Context.ResolveAssetUrlAsync(Context.Request.Guild.IconUrl, cancellationToken)
);
_writer.WriteEndObject();
// Channel
@ -255,6 +260,15 @@ internal class JsonMessageWriter : MessageWriter
_writer.WriteString("category", Context.Request.Channel.Category.Name);
_writer.WriteString("name", Context.Request.Channel.Name);
_writer.WriteString("topic", Context.Request.Channel.Topic);
if (!string.IsNullOrWhiteSpace(Context.Request.Channel.IconUrl))
{
_writer.WriteString(
"iconUrl",
await Context.ResolveAssetUrlAsync(Context.Request.Channel.IconUrl, cancellationToken)
);
}
_writer.WriteEndObject();
// Date range
@ -295,7 +309,12 @@ internal class JsonMessageWriter : MessageWriter
_writer.WriteString("nickname", Context.TryGetMember(message.Author.Id)?.Nick ?? message.Author.Name);
_writer.WriteString("color", Context.TryGetUserColor(message.Author.Id)?.ToHex());
_writer.WriteBoolean("isBot", message.Author.IsBot);
_writer.WriteString("avatarUrl", await Context.ResolveAssetUrlAsync(message.Author.AvatarUrl, cancellationToken));
_writer.WriteString(
"avatarUrl",
await Context.ResolveAssetUrlAsync(message.Author.AvatarUrl, cancellationToken)
);
_writer.WriteEndObject();
// Attachments

@ -851,7 +851,7 @@
<div class="preamble">
<div class="preamble__guild-icon-container">
<img class="preamble__guild-icon" src="@await ResolveAssetUrlAsync(ExportContext.Request.Guild.IconUrl)" alt="Guild icon" loading="lazy">
<img class="preamble__guild-icon" src="@await ResolveAssetUrlAsync(ExportContext.Request.Channel.IconUrl ?? ExportContext.Request.Guild.IconUrl)" alt="Guild icon" loading="lazy">
</div>
<div class="preamble__entries-container">
<div class="preamble__entry">@ExportContext.Request.Guild.Name</div>

Loading…
Cancel
Save