Browse Source

more implementation of INestedChannel design

pull/1004/head
Chris Johnston 8 years ago
parent
commit
de84fcf493
9 changed files with 40 additions and 45 deletions
  1. +0
    -4
      src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
  2. +10
    -0
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
  3. +6
    -2
      src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
  4. +3
    -0
      src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
  5. +1
    -14
      src/Discord.Net.WebSocket/Entities/Channels/SocketCategoryChannel.cs
  6. +3
    -10
      src/Discord.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs
  7. +5
    -1
      src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
  8. +5
    -2
      src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
  9. +7
    -12
      test/Discord.Net.Tests/Tests.Channels.cs

+ 0
- 4
src/Discord.Net.Core/Entities/Channels/INestedChannel.cs View File

@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord


+ 10
- 0
src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs View File

@@ -313,6 +313,16 @@ namespace Discord.Rest
return models.Select(x => RestWebhook.Create(client, channel, x))
.ToImmutableArray();
}
// Categories
public static async Task<ICategoryChannel> GetCategoryAsync(INestedChannel channel, BaseDiscordClient client, RequestOptions options)
{
// if no category id specified, return null
if (!channel.CategoryId.HasValue)
return null;
// CategoryId will contain a value here
var model = await client.ApiClient.GetChannelAsync(channel.CategoryId.Value, options).ConfigureAwait(false);
return RestCategoryChannel.Create(client, model) as ICategoryChannel;
}

//Helpers
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId)


+ 6
- 2
src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs View File

@@ -32,7 +32,7 @@ namespace Discord.Rest
internal override void Update(Model model)
{
base.Update(model);
CategoryId = model.CategoryId;
Topic = model.Topic.Value;
_nsfw = model.Nsfw.GetValueOrDefault();
}
@@ -47,7 +47,7 @@ namespace Discord.Rest
=> ChannelHelper.GetUserAsync(this, Guild, Discord, id, options);
public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(RequestOptions options = null)
=> ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options);
public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null)
=> ChannelHelper.GetMessageAsync(this, Discord, id, options);
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null)
@@ -84,6 +84,9 @@ namespace Discord.Rest
=> ChannelHelper.GetWebhookAsync(this, Discord, id, options);
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
=> ChannelHelper.GetWebhooksAsync(this, Discord, options);
public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null)
=> ChannelHelper.GetCategoryAsync(this, Discord, options);

private string DebuggerDisplay => $"{Name} ({Id}, Text)";

@@ -110,6 +113,7 @@ namespace Discord.Rest
else
return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>();
}
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)


+ 3
- 0
src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs View File

@@ -39,6 +39,9 @@ namespace Discord.Rest
Update(model);
}

public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null)
=> ChannelHelper.GetCategoryAsync(this, Discord, options);

private string DebuggerDisplay => $"{Name} ({Id}, Voice)";

//IAudioChannel


+ 1
- 14
src/Discord.Net.WebSocket/Entities/Channels/SocketCategoryChannel.cs View File

@@ -20,7 +20,7 @@ namespace Discord.WebSocket
ChannelPermission.ViewChannel)).ToImmutableArray();

public IReadOnlyCollection<SocketGuildChannel> Channels
=> Guild.Channels.Where(x => x.CategoryId == Id).ToImmutableArray();
=> Guild.Channels.Where(x => x is INestedChannel && (x as INestedChannel).CategoryId == Id).ToImmutableArray();

internal SocketCategoryChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
: base(discord, id, guild)
@@ -51,19 +51,6 @@ namespace Discord.WebSocket
internal new SocketCategoryChannel Clone() => MemberwiseClone() as SocketCategoryChannel;

// IGuildChannel

/// <summary>
/// Throws a NotSupportedException because Channel Categories cannot be the child of another Channel Category.
/// </summary>
/// <exception cref="NotSupportedException">A NotSupportedException is always thrown because Channel Categories do not support being nested.</exception>
ulong? IGuildChannel.CategoryId
=> throw new NotSupportedException();
/// <summary>
/// Throws a NotSupportedException because Channel Categories cannot be the child of another Channel Category.
/// </summary>
/// <exception cref="NotSupportedException">A NotSupportedException is always thrown because Channel Categories do not support being nested.</exception>
Task<ICategoryChannel> IGuildChannel.GetCategoryAsync()
=> throw new NotSupportedException();
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)


+ 3
- 10
src/Discord.Net.WebSocket/Entities/Channels/SocketGuildChannel.cs View File

@@ -1,4 +1,4 @@
using Discord.Rest;
using Discord.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
@@ -16,10 +16,7 @@ namespace Discord.WebSocket

public SocketGuild Guild { get; }
public string Name { get; private set; }
public int Position { get; private set; }
public ulong? CategoryId { get; private set; }
public ICategoryChannel Category
=> CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
public int Position { get; private set; }

public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites;
public new virtual IReadOnlyCollection<SocketGuildUser> Users => ImmutableArray.Create<SocketGuildUser>();
@@ -48,8 +45,7 @@ namespace Discord.WebSocket
{
Name = model.Name.Value;
Position = model.Position.Value;
CategoryId = model.CategoryId;

var overwrites = model.PermissionOverwrites.Value;
var newOverwrites = ImmutableArray.CreateBuilder<Overwrite>(overwrites.Length);
for (int i = 0; i < overwrites.Length; i++)
@@ -135,9 +131,6 @@ namespace Discord.WebSocket
IGuild IGuildChannel.Guild => Guild;
ulong IGuildChannel.GuildId => Guild.Id;

Task<ICategoryChannel> IGuildChannel.GetCategoryAsync()
=> Task.FromResult(Category);

async Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options)
=> await GetInvitesAsync(options).ConfigureAwait(false);
async Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options)


+ 5
- 1
src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs View File

@@ -45,7 +45,7 @@ namespace Discord.WebSocket
internal override void Update(ClientState state, Model model)
{
base.Update(state, model);
CategoryId = model.CategoryId;
Topic = model.Topic.Value;
_nsfw = model.Nsfw.GetValueOrDefault();
}
@@ -124,6 +124,10 @@ namespace Discord.WebSocket
public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null)
=> ChannelHelper.GetWebhooksAsync(this, Discord, options);

// Categories
public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null)
=> ChannelHelper.GetCategoryAsync(this, Discord, options);

private string DebuggerDisplay => $"{Name} ({Id}, Text)";
internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel;



+ 5
- 2
src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs View File

@@ -35,7 +35,7 @@ namespace Discord.WebSocket
internal override void Update(ClientState state, Model model)
{
base.Update(state, model);
CategoryId = model.CategoryId;
Bitrate = model.Bitrate.Value;
UserLimit = model.UserLimit.Value != 0 ? model.UserLimit.Value : (int?)null;
}
@@ -55,7 +55,10 @@ namespace Discord.WebSocket
return user;
return null;
}

public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null)
=> ChannelHelper.GetCategoryAsync(this, Discord, options);

private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
internal new SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel;



+ 7
- 12
test/Discord.Net.Tests/Tests.Channels.cs View File

@@ -156,18 +156,6 @@ namespace Discord
var cat1 = await guild.CreateCategoryChannelAsync("Cat1");
var cat2 = await guild.CreateCategoryChannelAsync("Cat2");

// check that both CategoryID and GetCategoryID throw NotSupportedException
// because Categories cannot be nested
Assert.Throws<NotSupportedException>(() =>
{
var x = cat1.CategoryId;
});

Assert.Throws<NotSupportedException>(() =>
{
var x = cat2.GetCategoryAsync();
});

var text1 = await guild.CreateTextChannelAsync("nestedText1");
var voice1 = await guild.CreateVoiceChannelAsync("nestedVoice1");
// set the text channel parent to Cat 1
@@ -185,10 +173,17 @@ namespace Discord

// assert that CategoryId works for text channels
Assert.Equal(text1.CategoryId, cat1.Id);
Assert.True(text1 is INestedChannel);
Assert.Equal((await (text1 as INestedChannel).GetCategoryAsync()).Id, cat1.Id);
Assert.Equal((await text1.GetCategoryAsync()).Id, cat1.Id);
Assert.Equal(text1.CategoryId, cat1.Id);

// and for voice channels
Assert.Equal(voice1.CategoryId, cat2.Id);
Assert.True(voice1 is INestedChannel);
Assert.Equal((await (voice1 as INestedChannel).GetCategoryAsync()).Id, cat2.Id);
Assert.Equal((await voice1.GetCategoryAsync()).Id, cat2.Id);
Assert.Equal(voice1.CategoryId, cat1.Id);

// incomplete test, could use more coverage of other methods
}


Loading…
Cancel
Save