Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Nov 6, 2024
1 parent 789e5af commit 1fb6156
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 43 deletions.
2 changes: 1 addition & 1 deletion DiscordChatExporter.Core/Discord/Data/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions DiscordChatExporter.Core/Discord/Data/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -41,7 +41,7 @@ FileSize FileSize
|| 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
Expand Down
12 changes: 7 additions & 5 deletions DiscordChatExporter.Core/Discord/Data/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Channel> GetParents()
{
Expand Down
21 changes: 21 additions & 0 deletions DiscordChatExporter.Core/Discord/Data/ChannelConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Linq;

namespace DiscordChatExporter.Core.Discord.Data;

public record ChannelConnection(Channel Channel, IReadOnlyList<ChannelConnection> Children)
{
public static IReadOnlyList<ChannelConnection> BuildTree(IReadOnlyList<Channel> channels)
{
IReadOnlyList<ChannelConnection> 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();
}
}
21 changes: 0 additions & 21 deletions DiscordChatExporter.Core/Discord/Data/ChannelNode.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion DiscordChatExporter.Core/Discord/Data/Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion DiscordChatExporter.Core/Discord/Data/Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial record Member(
IReadOnlyList<Snowflake> RoleIds
) : IHasId
{
public Snowflake Id => User.Id;
public Snowflake Id { get; } = User.Id;
}

public partial record Member
Expand Down
6 changes: 3 additions & 3 deletions DiscordChatExporter.Core/Discord/Data/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion DiscordChatExporter.Core/Discord/Data/Sticker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion DiscordChatExporter.Core/Discord/Data/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public partial class DashboardViewModel : ViewModelBase
private Guild? _selectedGuild;

[ObservableProperty]
private IReadOnlyList<ChannelNode>? _availableChannels;
private IReadOnlyList<ChannelConnection>? _availableChannels;

public DashboardViewModel(
ViewModelManager viewModelManager,
Expand Down Expand Up @@ -88,7 +88,7 @@ SettingsService settingsService

public bool IsProgressIndeterminate => IsBusy && Progress.Current.Fraction is <= 0 or >= 1;

public ObservableCollection<ChannelNode> SelectedChannels { get; } = [];
public ObservableCollection<ChannelConnection> SelectedChannels { get; } = [];

[RelayCommand]
private void Initialize()
Expand Down Expand Up @@ -190,7 +190,7 @@ var thread in _discord.GetGuildThreadsAsync(
}

// 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ SelectionChangedEventArgs args
)
{
// Hack: unselect categories because they cannot be exported
foreach (var item in args.AddedItems.OfType<ChannelNode>().Where(x => x.Channel.IsCategory))
foreach (
var item in args.AddedItems.OfType<ChannelConnection>().Where(x => x.Channel.IsCategory)
)
{
if (AvailableChannelsTreeView.TreeContainerFromItem(item) is TreeViewItem container)
container.IsSelected = false;
Expand Down

0 comments on commit 1fb6156

Please sign in to comment.