From 1fb6156187b66f2636a66b9a348ba25b2ffc16f1 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Wed, 6 Nov 2024 19:36:34 +0200 Subject: [PATCH] Refactor --- .../Discord/Data/Application.cs | 2 +- .../Discord/Data/Attachment.cs | 4 ++-- .../Discord/Data/Channel.cs | 12 ++++++----- .../Discord/Data/ChannelConnection.cs | 21 +++++++++++++++++++ .../Discord/Data/ChannelNode.cs | 21 ------------------- .../Embeds/SpotifyTrackEmbedProjection.cs | 2 +- .../Data/Embeds/TwitchClipEmbedProjection.cs | 2 +- .../Embeds/YouTubeVideoEmbedProjection.cs | 2 +- .../Discord/Data/Guild.cs | 2 +- .../Discord/Data/Member.cs | 2 +- .../Discord/Data/Message.cs | 6 +++--- .../Discord/Data/Sticker.cs | 2 +- DiscordChatExporter.Core/Discord/Data/User.cs | 2 +- .../Components/DashboardViewModel.cs | 6 +++--- .../Views/Components/DashboardView.axaml.cs | 4 +++- 15 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 DiscordChatExporter.Core/Discord/Data/ChannelConnection.cs delete mode 100644 DiscordChatExporter.Core/Discord/Data/ChannelNode.cs diff --git a/DiscordChatExporter.Core/Discord/Data/Application.cs b/DiscordChatExporter.Core/Discord/Data/Application.cs index 230f8e21..05309e64 100644 --- a/DiscordChatExporter.Core/Discord/Data/Application.cs +++ b/DiscordChatExporter.Core/Discord/Data/Application.cs @@ -7,7 +7,7 @@ namespace DiscordChatExporter.Core.Discord.Data; // https://discord.com/developers/docs/resources/application#application-object public partial record Application(Snowflake Id, string Name, ApplicationFlags Flags) { - public bool IsMessageContentIntentEnabled => + public bool IsMessageContentIntentEnabled { get; } = Flags.HasFlag(ApplicationFlags.GatewayMessageContent) || Flags.HasFlag(ApplicationFlags.GatewayMessageContentLimited); } diff --git a/DiscordChatExporter.Core/Discord/Data/Attachment.cs b/DiscordChatExporter.Core/Discord/Data/Attachment.cs index 79d03cf1..7d0a7ca6 100644 --- a/DiscordChatExporter.Core/Discord/Data/Attachment.cs +++ b/DiscordChatExporter.Core/Discord/Data/Attachment.cs @@ -18,7 +18,7 @@ public partial record Attachment( FileSize FileSize ) : IHasId { - public string FileExtension => Path.GetExtension(FileName); + public string FileExtension { get; } = Path.GetExtension(FileName); public bool IsImage => string.Equals(FileExtension, ".jpg", StringComparison.OrdinalIgnoreCase) @@ -41,7 +41,7 @@ public partial record Attachment( || string.Equals(FileExtension, ".flac", StringComparison.OrdinalIgnoreCase) || string.Equals(FileExtension, ".m4a", StringComparison.OrdinalIgnoreCase); - public bool IsSpoiler => FileName.StartsWith("SPOILER_", StringComparison.Ordinal); + public bool IsSpoiler { get; } = FileName.StartsWith("SPOILER_", StringComparison.Ordinal); } public partial record Attachment diff --git a/DiscordChatExporter.Core/Discord/Data/Channel.cs b/DiscordChatExporter.Core/Discord/Data/Channel.cs index 70d8f17a..43642b8a 100644 --- a/DiscordChatExporter.Core/Discord/Data/Channel.cs +++ b/DiscordChatExporter.Core/Discord/Data/Channel.cs @@ -21,21 +21,23 @@ public partial record Channel( Snowflake? LastMessageId ) : IHasId { - public bool IsDirect => Kind is ChannelKind.DirectTextChat or ChannelKind.DirectGroupTextChat; + public bool IsDirect { get; } = + Kind is ChannelKind.DirectTextChat or ChannelKind.DirectGroupTextChat; public bool IsGuild => !IsDirect; - public bool IsCategory => Kind == ChannelKind.GuildCategory; + public bool IsCategory { get; } = Kind == ChannelKind.GuildCategory; - public bool IsVoice => Kind is ChannelKind.GuildVoiceChat or ChannelKind.GuildStageVoice; + public bool IsVoice { get; } = + Kind is ChannelKind.GuildVoiceChat or ChannelKind.GuildStageVoice; - public bool IsThread => + public bool IsThread { get; } = Kind is ChannelKind.GuildNewsThread or ChannelKind.GuildPublicThread or ChannelKind.GuildPrivateThread; - public bool IsEmpty => LastMessageId is null; + public bool IsEmpty { get; } = LastMessageId is null; public IEnumerable GetParents() { diff --git a/DiscordChatExporter.Core/Discord/Data/ChannelConnection.cs b/DiscordChatExporter.Core/Discord/Data/ChannelConnection.cs new file mode 100644 index 00000000..8011b801 --- /dev/null +++ b/DiscordChatExporter.Core/Discord/Data/ChannelConnection.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.Linq; + +namespace DiscordChatExporter.Core.Discord.Data; + +public record ChannelConnection(Channel Channel, IReadOnlyList Children) +{ + public static IReadOnlyList BuildTree(IReadOnlyList channels) + { + IReadOnlyList GetChildren(Channel parent) => + channels + .Where(c => c.Parent?.Id == parent.Id) + .Select(c => new ChannelConnection(c, GetChildren(c))) + .ToArray(); + + return channels + .Where(c => c.Parent is null) + .Select(c => new ChannelConnection(c, GetChildren(c))) + .ToArray(); + } +} diff --git a/DiscordChatExporter.Core/Discord/Data/ChannelNode.cs b/DiscordChatExporter.Core/Discord/Data/ChannelNode.cs deleted file mode 100644 index a465aec8..00000000 --- a/DiscordChatExporter.Core/Discord/Data/ChannelNode.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace DiscordChatExporter.Core.Discord.Data; - -public record ChannelNode(Channel Channel, IReadOnlyList Children) -{ - public static IReadOnlyList BuildTree(IReadOnlyList channels) - { - IReadOnlyList GetChildren(Channel parent) => - channels - .Where(c => c.Parent?.Id == parent.Id) - .Select(c => new ChannelNode(c, GetChildren(c))) - .ToArray(); - - return channels - .Where(c => c.Parent is null) - .Select(c => new ChannelNode(c, GetChildren(c))) - .ToArray(); - } -} diff --git a/DiscordChatExporter.Core/Discord/Data/Embeds/SpotifyTrackEmbedProjection.cs b/DiscordChatExporter.Core/Discord/Data/Embeds/SpotifyTrackEmbedProjection.cs index 4d68d62e..7c45fa03 100644 --- a/DiscordChatExporter.Core/Discord/Data/Embeds/SpotifyTrackEmbedProjection.cs +++ b/DiscordChatExporter.Core/Discord/Data/Embeds/SpotifyTrackEmbedProjection.cs @@ -4,7 +4,7 @@ namespace DiscordChatExporter.Core.Discord.Data.Embeds; public partial record SpotifyTrackEmbedProjection(string TrackId) { - public string Url => $"https://open.spotify.com/embed/track/{TrackId}"; + public string Url { get; } = $"https://open.spotify.com/embed/track/{TrackId}"; } public partial record SpotifyTrackEmbedProjection diff --git a/DiscordChatExporter.Core/Discord/Data/Embeds/TwitchClipEmbedProjection.cs b/DiscordChatExporter.Core/Discord/Data/Embeds/TwitchClipEmbedProjection.cs index 3c7da139..5d507646 100644 --- a/DiscordChatExporter.Core/Discord/Data/Embeds/TwitchClipEmbedProjection.cs +++ b/DiscordChatExporter.Core/Discord/Data/Embeds/TwitchClipEmbedProjection.cs @@ -4,7 +4,7 @@ namespace DiscordChatExporter.Core.Discord.Data.Embeds; public partial record TwitchClipEmbedProjection(string ClipId) { - public string Url => $"https://clips.twitch.tv/embed?clip={ClipId}&parent=localhost"; + public string Url { get; } = $"https://clips.twitch.tv/embed?clip={ClipId}&parent=localhost"; } public partial record TwitchClipEmbedProjection diff --git a/DiscordChatExporter.Core/Discord/Data/Embeds/YouTubeVideoEmbedProjection.cs b/DiscordChatExporter.Core/Discord/Data/Embeds/YouTubeVideoEmbedProjection.cs index df02bf62..ce13bdfd 100644 --- a/DiscordChatExporter.Core/Discord/Data/Embeds/YouTubeVideoEmbedProjection.cs +++ b/DiscordChatExporter.Core/Discord/Data/Embeds/YouTubeVideoEmbedProjection.cs @@ -2,7 +2,7 @@ public partial record YouTubeVideoEmbedProjection(string VideoId) { - public string Url => $"https://www.youtube.com/embed/{VideoId}"; + public string Url { get; } = $"https://www.youtube.com/embed/{VideoId}"; } public partial record YouTubeVideoEmbedProjection diff --git a/DiscordChatExporter.Core/Discord/Data/Guild.cs b/DiscordChatExporter.Core/Discord/Data/Guild.cs index 71a1236e..3e39443b 100644 --- a/DiscordChatExporter.Core/Discord/Data/Guild.cs +++ b/DiscordChatExporter.Core/Discord/Data/Guild.cs @@ -8,7 +8,7 @@ namespace DiscordChatExporter.Core.Discord.Data; // https://discord.com/developers/docs/resources/guild#guild-object public partial record Guild(Snowflake Id, string Name, string IconUrl) : IHasId { - public bool IsDirect => Id == DirectMessages.Id; + public bool IsDirect { get; } = Id == DirectMessages.Id; } public partial record Guild diff --git a/DiscordChatExporter.Core/Discord/Data/Member.cs b/DiscordChatExporter.Core/Discord/Data/Member.cs index d023f373..fbfff9e6 100644 --- a/DiscordChatExporter.Core/Discord/Data/Member.cs +++ b/DiscordChatExporter.Core/Discord/Data/Member.cs @@ -15,7 +15,7 @@ public partial record Member( IReadOnlyList RoleIds ) : IHasId { - public Snowflake Id => User.Id; + public Snowflake Id { get; } = User.Id; } public partial record Member diff --git a/DiscordChatExporter.Core/Discord/Data/Message.cs b/DiscordChatExporter.Core/Discord/Data/Message.cs index 6fa13c1a..56b3b87b 100644 --- a/DiscordChatExporter.Core/Discord/Data/Message.cs +++ b/DiscordChatExporter.Core/Discord/Data/Message.cs @@ -30,15 +30,15 @@ public partial record Message( Interaction? Interaction ) : IHasId { - public bool IsSystemNotification => + public bool IsSystemNotification { get; } = Kind is >= MessageKind.RecipientAdd and <= MessageKind.ThreadCreated; - public bool IsReply => Kind == MessageKind.Reply; + public bool IsReply { get; } = Kind == MessageKind.Reply; // App interactions are rendered as replies in the Discord client, but they are not actually replies public bool IsReplyLike => IsReply || Interaction is not null; - public bool IsEmpty => + public bool IsEmpty { get; } = string.IsNullOrWhiteSpace(Content) && !Attachments.Any() && !Embeds.Any() diff --git a/DiscordChatExporter.Core/Discord/Data/Sticker.cs b/DiscordChatExporter.Core/Discord/Data/Sticker.cs index a99cbcc9..76ea2486 100644 --- a/DiscordChatExporter.Core/Discord/Data/Sticker.cs +++ b/DiscordChatExporter.Core/Discord/Data/Sticker.cs @@ -9,7 +9,7 @@ namespace DiscordChatExporter.Core.Discord.Data; // https://discord.com/developers/docs/resources/sticker#sticker-resource public partial record Sticker(Snowflake Id, string Name, StickerFormat Format, string SourceUrl) { - public bool IsImage => Format != StickerFormat.Lottie; + public bool IsImage { get; } = Format != StickerFormat.Lottie; } public partial record Sticker diff --git a/DiscordChatExporter.Core/Discord/Data/User.cs b/DiscordChatExporter.Core/Discord/Data/User.cs index 33012292..ba1ec8fc 100644 --- a/DiscordChatExporter.Core/Discord/Data/User.cs +++ b/DiscordChatExporter.Core/Discord/Data/User.cs @@ -18,7 +18,7 @@ public partial record User( string AvatarUrl ) : IHasId { - public string DiscriminatorFormatted => + public string DiscriminatorFormatted { get; } = Discriminator is not null ? $"{Discriminator:0000}" : "0000"; // This effectively represents the user's true identity. diff --git a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs index 56a12a0f..43da5b0e 100644 --- a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs @@ -53,7 +53,7 @@ public partial class DashboardViewModel : ViewModelBase private Guild? _selectedGuild; [ObservableProperty] - private IReadOnlyList? _availableChannels; + private IReadOnlyList? _availableChannels; public DashboardViewModel( ViewModelManager viewModelManager, @@ -88,7 +88,7 @@ public partial class DashboardViewModel : ViewModelBase public bool IsProgressIndeterminate => IsBusy && Progress.Current.Fraction is <= 0 or >= 1; - public ObservableCollection SelectedChannels { get; } = []; + public ObservableCollection SelectedChannels { get; } = []; [RelayCommand] private void Initialize() @@ -190,7 +190,7 @@ public partial class DashboardViewModel : ViewModelBase } // Build a hierarchy of channels - var channelTree = ChannelNode.BuildTree( + var channelTree = ChannelConnection.BuildTree( channels .OrderByDescending(c => c.IsDirect ? c.LastMessageId : null) .ThenBy(c => c.Position) diff --git a/DiscordChatExporter.Gui/Views/Components/DashboardView.axaml.cs b/DiscordChatExporter.Gui/Views/Components/DashboardView.axaml.cs index c31982eb..0cf9d82c 100644 --- a/DiscordChatExporter.Gui/Views/Components/DashboardView.axaml.cs +++ b/DiscordChatExporter.Gui/Views/Components/DashboardView.axaml.cs @@ -28,7 +28,9 @@ public partial class DashboardView : UserControl ) { // Hack: unselect categories because they cannot be exported - foreach (var item in args.AddedItems.OfType().Where(x => x.Channel.IsCategory)) + foreach ( + var item in args.AddedItems.OfType().Where(x => x.Channel.IsCategory) + ) { if (AvailableChannelsTreeView.TreeContainerFromItem(item) is TreeViewItem container) container.IsSelected = false;