Formatting output paths (#472)

This commit is contained in:
Lucas LaBuff
2021-01-28 15:01:13 -05:00
committed by GitHub
parent 915f4c8d9f
commit 77b7977324
9 changed files with 152 additions and 31 deletions

View File

@@ -117,26 +117,33 @@ namespace DiscordChatExporter.Domain.Discord
{
var response = await GetJsonResponseAsync($"guilds/{guildId}/channels");
var categories = response
var orderedResponse = response
.EnumerateArray()
.ToDictionary(
j => j.GetProperty("id").GetString(),
j => j.GetProperty("name").GetString()
);
.OrderBy(j => j.GetProperty("position").GetInt32())
.ThenBy(j => ulong.Parse(j.GetProperty("id").GetString()));
foreach (var channelJson in response.EnumerateArray())
var categories = orderedResponse
.Where(j => j.GetProperty("type").GetInt32() == (int)ChannelType.GuildCategory)
.Select((j, index) => ChannelCategory.Parse(j, index + 1))
.ToDictionary(j => j.Id.ToString());
var position = 0;
foreach (var channelJson in orderedResponse)
{
var parentId = channelJson.GetPropertyOrNull("parent_id")?.GetString();
var category = !string.IsNullOrWhiteSpace(parentId)
? categories.GetValueOrDefault(parentId)
: null;
var channel = Channel.Parse(channelJson, category);
var channel = Channel.Parse(channelJson, category, position);
// Skip non-text channels
if (!channel.IsTextChannel)
continue;
position++;
yield return channel;
}
}
@@ -162,10 +169,11 @@ namespace DiscordChatExporter.Domain.Discord
return response?.Pipe(Member.Parse);
}
private async ValueTask<string> GetChannelCategoryAsync(Snowflake channelParentId)
public async ValueTask<ChannelCategory> GetChannelCategoryAsync(Snowflake channelId)
{
var response = await GetJsonResponseAsync($"channels/{channelParentId}");
return response.GetProperty("name").GetString();
var response = await GetJsonResponseAsync($"channels/{channelId}");
return ChannelCategory.Parse(response);
}
public async ValueTask<Channel> GetChannelAsync(Snowflake channelId)

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.Json;
using DiscordChatExporter.Domain.Discord.Models.Common;
using DiscordChatExporter.Domain.Utilities;
@@ -36,56 +37,67 @@ namespace DiscordChatExporter.Domain.Discord.Models
public Snowflake GuildId { get; }
public string Category { get; }
public ChannelCategory Category { get; }
public string Name { get; }
public int Position { get; }
public string? Topic { get; }
public Channel(Snowflake id, ChannelType type, Snowflake guildId, string category, string name, string? topic)
public Channel(Snowflake id, ChannelType type, Snowflake guildId, ChannelCategory? category, string name, int position, string? topic)
{
Id = id;
Type = type;
GuildId = guildId;
Category = category;
Category = category ?? GetDefaultCategory(type);
Name = name;
Position = position;
Topic = topic;
}
public override string ToString() => Name;
}
public partial class Channel
{
private static string GetDefaultCategory(ChannelType channelType) => channelType switch
{
ChannelType.GuildTextChat => "Text",
ChannelType.DirectTextChat => "Private",
ChannelType.DirectGroupTextChat => "Group",
ChannelType.GuildNews => "News",
ChannelType.GuildStore => "Store",
_ => "Default"
};
private static ChannelCategory GetDefaultCategory(ChannelType channelType) => new(
Snowflake.Zero,
channelType switch
{
ChannelType.GuildTextChat => "Text",
ChannelType.DirectTextChat => "Private",
ChannelType.DirectGroupTextChat => "Group",
ChannelType.GuildNews => "News",
ChannelType.GuildStore => "Store",
_ => "Default"
},
0
);
public static Channel Parse(JsonElement json, string? category = null)
public static Channel Parse(JsonElement json, ChannelCategory? category = null, int? position = null)
{
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
var guildId = json.GetPropertyOrNull("guild_id")?.GetString().Pipe(Snowflake.Parse);
var topic = json.GetPropertyOrNull("topic")?.GetString();
var type = (ChannelType) json.GetProperty("type").GetInt32();
var type = (ChannelType)json.GetProperty("type").GetInt32();
var name =
json.GetPropertyOrNull("name")?.GetString() ??
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
id.ToString();
position ??= json.GetProperty("position").GetInt32();
return new Channel(
id,
type,
guildId ?? Guild.DirectMessages.Id,
category ?? GetDefaultCategory(type),
name,
position.Value,
topic
);
}

View File

@@ -0,0 +1,48 @@
using System;
using System.Linq;
using System.Text.Json;
using DiscordChatExporter.Domain.Discord.Models.Common;
using DiscordChatExporter.Domain.Utilities;
using JsonExtensions.Reading;
using Tyrrrz.Extensions;
namespace DiscordChatExporter.Domain.Discord.Models
{
public partial class ChannelCategory : IHasId
{
public Snowflake Id { get; }
public string Name { get; }
public int Position { get; }
public ChannelCategory(Snowflake id, string name, int position)
{
Id = id;
Name = name;
Position = position;
}
public override string ToString() => Name;
}
public partial class ChannelCategory
{
public static ChannelCategory Parse(JsonElement json, int? position = null)
{
var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse);
position ??= json.GetProperty("position").GetInt32();
var name = json.GetPropertyOrNull("name")?.GetString() ??
json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ??
id.ToString();
return new ChannelCategory(
id,
name,
position.Value
);
}
}
}

View File

@@ -0,0 +1,30 @@
using System.Collections.Generic;
namespace DiscordChatExporter.Domain.Discord.Models.Common
{
public partial class ChannelPositionBasedComparer : IComparer<Channel>
{
public int Compare(Channel? x, Channel? y)
{
int result;
if (x != null)
{
result = x.Position.CompareTo(y?.Position);
}
else if (y != null)
{
result = -y.Position.CompareTo(x?.Position);
}
else
{
result = 0;
}
return result;
}
}
public partial class ChannelPositionBasedComparer
{
public static ChannelPositionBasedComparer Instance { get; } = new();
}
}