Fix #995, Move Category Implementation from IGuildChannel to INestedChannel (#1004)

* Fix #995 ICategoryChannel.CategoryID throws NotSupportedException

* Add tests

* change run mode of TestChannelCategories

* Add throw for GetCategoryAsync

* Add xml doc explaining why exception is thrown

* Add test coverage for text and voice channel categories

* initial implementation of INestedChannel

* more implementation of INestedChannel design

* Add case in RestChannel Create for Category type

* set the CategoryID for RestVoiceChannel

* rewrite channel category tests to work with existing pattern

* remove outdated todo

* Make IVoiceChannel implement INestedChannel

* remove redundant interface implementation

* Add c#7 feature from feedback

* Remove redundant GetCategoryAsync methods from socket entities

* Added configureawait to async methods

* change signature of interface GetCategoryAsync

* Add check for cachemode in rest channel GetCategory

* remove redundant IGuildChannel interface from ITextChannel and IVoiceChannel
This commit is contained in:
Chris Johnston
2018-05-26 11:06:35 -07:00
committed by Christopher F
parent 4d8764e124
commit f9cbff5e42
16 changed files with 168 additions and 51 deletions

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 nestedChannel && nestedChannel.CategoryId == Id).ToImmutableArray();
internal SocketCategoryChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
: base(discord, id, guild)

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)

View File

@@ -16,6 +16,9 @@ namespace Discord.WebSocket
private readonly MessageCache _messages;
public string Topic { get; private set; }
public ulong? CategoryId { get; private set; }
public ICategoryChannel Category
=> CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
private bool _nsfw;
public bool IsNsfw => _nsfw || ChannelHelper.IsNsfw(this);
@@ -42,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();
}
@@ -169,5 +172,9 @@ namespace Discord.WebSocket
=> await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false);
IDisposable IMessageChannel.EnterTypingState(RequestOptions options)
=> EnterTypingState(options);
// INestedChannel
Task<ICategoryChannel> INestedChannel.GetCategoryAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult(Category);
}
}

View File

@@ -1,4 +1,4 @@
using Discord.Audio;
using Discord.Audio;
using Discord.Rest;
using System;
using System.Collections.Generic;
@@ -15,6 +15,9 @@ namespace Discord.WebSocket
{
public int Bitrate { get; private set; }
public int? UserLimit { get; private set; }
public ulong? CategoryId { get; private set; }
public ICategoryChannel Category
=> CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
public override IReadOnlyCollection<SocketGuildUser> Users
=> Guild.Users.Where(x => x.VoiceChannel?.Id == Id).ToImmutableArray();
@@ -32,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;
}
@@ -52,7 +55,7 @@ namespace Discord.WebSocket
return user;
return null;
}
private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
internal new SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel;
@@ -61,5 +64,9 @@ namespace Discord.WebSocket
=> Task.FromResult<IGuildUser>(GetUser(id));
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
// INestedChannel
Task<ICategoryChannel> INestedChannel.GetCategoryAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult(Category);
}
}