Browse Source

Make Creator property Cacheable

Makes the Creator property into an Optional<Cacheable<IUser, ulong>> type.
Checks to see if users are first contained in the cache, and does not
force the end user to download this User.
pull/1214/head
Chris Johnston 6 years ago
parent
commit
322be8a31a
2 changed files with 15 additions and 8 deletions
  1. +5
    -4
      src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs
  2. +10
    -4
      src/Discord.Net.Rest/Extensions/EntityExtensions.cs

+ 5
- 4
src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs View File

@@ -31,14 +31,15 @@ namespace Discord
/// </returns>
public IReadOnlyList<ulong> RoleIds { get; }
/// <summary>
/// Gets the User that created this emoji.
/// Gets the cached User that created this emoji.
/// </summary>
/// <returns>
/// An optional <see cref="IUser"/> who created this emoji. An unspecified value only indicates that the creator was not supplied as part of the API response.
/// An optional <see cref="Cacheable{TEntity, TId}"/> <see cref="IUser"/> who created this emoji.
/// An unspecified value only indicates that the creator was not supplied as part of the API response.
/// </returns>
public Optional<IUser> Creator { get; }
public Optional<Cacheable<IUser, ulong>> Creator { get; }

internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool requireColons, IReadOnlyList<ulong> roleIds, Optional<IUser> creator) : base(id, name, animated)
internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool requireColons, IReadOnlyList<ulong> roleIds, Optional<Cacheable<IUser, ulong>> creator) : base(id, name, animated)
{
IsManaged = isManaged;
RequireColons = requireColons;


+ 10
- 4
src/Discord.Net.Rest/Extensions/EntityExtensions.cs View File

@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;

namespace Discord.Rest
{
@@ -7,14 +8,19 @@ namespace Discord.Rest
{
public static GuildEmote ToEntity(this API.Emoji model, IGuild guild)
{
return new GuildEmote(model.Id.Value, model.Name, model.Animated.GetValueOrDefault(), model.Managed, model.RequireColons, ImmutableArray.Create(model.Roles), GetEmoteAuthor(model, guild));
return new GuildEmote(model.Id.Value, model.Name, model.Animated.GetValueOrDefault(), model.Managed, model.RequireColons, ImmutableArray.Create(model.Roles), GetEmoteAuthorAsync(model, guild).Result);
}

internal static Optional<IUser> GetEmoteAuthor(API.Emoji model, IGuild guild)
internal static async Task<Optional<Cacheable<IUser, ulong>>> GetEmoteAuthorAsync(API.Emoji model, IGuild guild)
{
if (!model.User.IsSpecified || guild == null)
return new Optional<IUser>();
return new Optional<IUser>(guild.GetUserAsync(model.User.Value.Id).Result);
// api did not provide the user, or guild provided was null
return new Optional<Cacheable<IUser, ulong>>();
// get the cached user, if exists in cache
var cachedUser = await guild.GetUserAsync(model.User.Value.Id, CacheMode.CacheOnly).ConfigureAwait(false);
var isCached = cachedUser != null;
var cacheable = new Cacheable<IUser, ulong>(cachedUser, model.User.Value.Id, isCached, async () => await guild.GetUserAsync(model.User.Value.Id).ConfigureAwait(false));
return new Optional<Cacheable<IUser, ulong>>(cacheable);
}

public static Embed ToEntity(this API.Embed model)


Loading…
Cancel
Save