Fix usage of CacheMode.AllowDownload in channels (#2154)

Co-Authored-By:  <25006819+sabihoshi@users.noreply.github.com>

Co-authored-by:  <25006819+sabihoshi@users.noreply.github.com>
This commit is contained in:
Quin Lynch
2022-03-02 19:22:59 -04:00
committed by GitHub
parent 6bf5818e72
commit b3370c33e2
11 changed files with 97 additions and 35 deletions

View File

@@ -227,7 +227,7 @@ namespace Discord.Rest
/// <inheritdoc /> /// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>(); //Overridden //Overridden in Text/Voice => AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>(); //Overridden in Text/Voice
/// <inheritdoc /> /// <inheritdoc />
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(null); //Overridden in Text/Voice => Task.FromResult<IGuildUser>(null); //Overridden in Text/Voice

View File

@@ -364,10 +364,9 @@ namespace Discord.Rest
/// <inheritdoc /> /// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
{ {
if (mode == CacheMode.AllowDownload) return mode == CacheMode.AllowDownload
return GetUsersAsync(options); ? GetUsersAsync(options)
else : AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();
return AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();
} }
#endregion #endregion

View File

@@ -317,8 +317,15 @@ namespace Discord.WebSocket
=> await CreateGuildAsync(name, region, jpegIcon, options).ConfigureAwait(false); => await CreateGuildAsync(name, region, jpegIcon, options).ConfigureAwait(false);
/// <inheritdoc /> /// <inheritdoc />
Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) async Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IUser>(GetUser(id)); {
var user = GetUser(id);
if (user is not null || mode == CacheMode.CacheOnly)
return user;
return await Rest.GetUserAsync(id, options).ConfigureAwait(false);
}
/// <inheritdoc /> /// <inheritdoc />
Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator, RequestOptions options) Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator, RequestOptions options)
=> Task.FromResult<IUser>(GetUser(username, discriminator)); => Task.FromResult<IUser>(GetUser(username, discriminator));

View File

@@ -533,8 +533,15 @@ namespace Discord.WebSocket
=> await CreateGuildAsync(name, region, jpegIcon).ConfigureAwait(false); => await CreateGuildAsync(name, region, jpegIcon).ConfigureAwait(false);
/// <inheritdoc /> /// <inheritdoc />
Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) async Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IUser>(GetUser(id)); {
var user = GetUser(id);
if (user is not null || mode == CacheMode.CacheOnly)
return user;
return await Rest.GetUserAsync(id, options).ConfigureAwait(false);
}
/// <inheritdoc /> /// <inheritdoc />
Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator, RequestOptions options) Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator, RequestOptions options)
=> Task.FromResult<IUser>(GetUser(username, discriminator)); => Task.FromResult<IUser>(GetUser(username, discriminator));

View File

@@ -3113,7 +3113,14 @@ namespace Discord.WebSocket
/// <inheritdoc /> /// <inheritdoc />
async Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) async Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> mode == CacheMode.AllowDownload ? await GetUserAsync(id, options).ConfigureAwait(false) : GetUser(id); {
var user = GetUser(id);
if (user is not null || mode == CacheMode.CacheOnly)
return user;
return await Rest.GetUserAsync(id, options).ConfigureAwait(false);
}
/// <inheritdoc /> /// <inheritdoc />
Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator, RequestOptions options) Task<IUser> IDiscordClient.GetUserAsync(string username, string discriminator, RequestOptions options)
=> Task.FromResult<IUser>(GetUser(username, discriminator)); => Task.FromResult<IUser>(GetUser(username, discriminator));

View File

@@ -4,6 +4,7 @@ using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Rest;
using Model = Discord.API.Channel; using Model = Discord.API.Channel;
namespace Discord.WebSocket namespace Discord.WebSocket
@@ -64,21 +65,44 @@ namespace Discord.WebSocket
#endregion #endregion
#region IGuildChannel #region IGuildChannel
/// <inheritdoc /> /// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode,
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable(); RequestOptions options)
{
return mode == CacheMode.AllowDownload
? ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options)
: ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
}
/// <inheritdoc /> /// <inheritdoc />
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) async Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(GetUser(id)); {
var user = GetUser(id);
if (user is not null || mode == CacheMode.CacheOnly)
return user;
return await ChannelHelper.GetUserAsync(this, Guild, Discord, id, options).ConfigureAwait(false);
}
#endregion #endregion
#region IChannel #region IChannel
/// <inheritdoc /> /// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options) IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable(); {
return mode == CacheMode.AllowDownload
? ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options)
: ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
}
/// <inheritdoc /> /// <inheritdoc />
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) async Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IUser>(GetUser(id)); {
var user = GetUser(id);
if (user is not null || mode == CacheMode.CacheOnly)
return user;
return await ChannelHelper.GetUserAsync(this, Guild, Discord, id, options).ConfigureAwait(false);
}
#endregion #endregion
} }
} }

View File

@@ -214,10 +214,10 @@ namespace Discord.WebSocket
/// <inheritdoc /> /// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable(); => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable(); //Overridden in Text/Voice
/// <inheritdoc /> /// <inheritdoc />
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(GetUser(id)); => Task.FromResult<IGuildUser>(GetUser(id)); //Overridden in Text/Voice
#endregion #endregion
#region IChannel #region IChannel

View File

@@ -355,11 +355,22 @@ namespace Discord.WebSocket
#region IGuildChannel #region IGuildChannel
/// <inheritdoc /> /// <inheritdoc />
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) async Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(GetUser(id)); {
var user = GetUser(id);
if (user is not null || mode == CacheMode.CacheOnly)
return user;
return await ChannelHelper.GetUserAsync(this, Guild, Discord, id, options).ConfigureAwait(false);
}
/// <inheritdoc /> /// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable(); {
return mode == CacheMode.AllowDownload
? ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options)
: ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
}
#endregion #endregion
#region IMessageChannel #region IMessageChannel

View File

@@ -1928,8 +1928,15 @@ namespace Discord.WebSocket
async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options) async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options)
=> await AddGuildUserAsync(userId, accessToken, func, options); => await AddGuildUserAsync(userId, accessToken, func, options);
/// <inheritdoc /> /// <inheritdoc />
Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) async Task<IGuildUser> IGuild.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(GetUser(id)); {
var user = GetUser(id);
if (user is not null || mode == CacheMode.CacheOnly)
return user;
return await GuildHelper.GetUserAsync(this, Discord, id, options).ConfigureAwait(false);
}
/// <inheritdoc /> /// <inheritdoc />
Task<IGuildUser> IGuild.GetCurrentUserAsync(CacheMode mode, RequestOptions options) Task<IGuildUser> IGuild.GetCurrentUserAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IGuildUser>(CurrentUser); => Task.FromResult<IGuildUser>(CurrentUser);