Readded navigational props to interfaces

This commit is contained in:
RogueException
2016-10-15 12:58:32 -03:00
parent 8ebc437674
commit c497f95d35
16 changed files with 98 additions and 22 deletions

View File

@@ -47,7 +47,7 @@ namespace Discord.Rest
RequestOptions options)
{
var models = await client.ApiClient.GetChannelInvitesAsync(channel.Id, options).ConfigureAwait(false);
return models.Select(x => RestInviteMetadata.Create(client, x)).ToImmutableArray();
return models.Select(x => RestInviteMetadata.Create(client, null, channel, x)).ToImmutableArray();
}
public static async Task<RestInviteMetadata> CreateInviteAsync(IChannel channel, BaseDiscordClient client,
int? maxAge, int? maxUses, bool isTemporary, RequestOptions options)
@@ -58,7 +58,7 @@ namespace Discord.Rest
if (maxUses.HasValue)
args.MaxUses = maxUses.Value;
var model = await client.ApiClient.CreateChannelInviteAsync(channel.Id, args, options).ConfigureAwait(false);
return RestInviteMetadata.Create(client, model);
return RestInviteMetadata.Create(client, null, channel, model);
}
//Messages

View File

@@ -125,6 +125,16 @@ namespace Discord.Rest
public override string ToString() => Name;
//IGuildChannel
IGuild IGuildChannel.Guild
{
get
{
if (Guild != null)
return Guild;
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
}
}
async Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options)
=> await GetInvitesAsync(options).ConfigureAwait(false);
async Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, RequestOptions options)

View File

@@ -117,14 +117,14 @@ namespace Discord.Rest
RequestOptions options)
{
var models = await client.ApiClient.GetGuildIntegrationsAsync(guild.Id, options).ConfigureAwait(false);
return models.Select(x => RestGuildIntegration.Create(client, x)).ToImmutableArray();
return models.Select(x => RestGuildIntegration.Create(client, guild, x)).ToImmutableArray();
}
public static async Task<RestGuildIntegration> CreateIntegrationAsync(IGuild guild, BaseDiscordClient client,
ulong id, string type, RequestOptions options)
{
var args = new CreateGuildIntegrationParams(id, type);
var model = await client.ApiClient.CreateGuildIntegrationAsync(guild.Id, args, options).ConfigureAwait(false);
return RestGuildIntegration.Create(client, model);
return RestGuildIntegration.Create(client, guild, model);
}
//Invites
@@ -132,7 +132,7 @@ namespace Discord.Rest
RequestOptions options)
{
var models = await client.ApiClient.GetGuildInvitesAsync(guild.Id, options).ConfigureAwait(false);
return models.Select(x => RestInviteMetadata.Create(client, x)).ToImmutableArray();
return models.Select(x => RestInviteMetadata.Create(client, guild, null, x)).ToImmutableArray();
}
//Roles

View File

@@ -21,16 +21,18 @@ namespace Discord.Rest
public ulong RoleId { get; private set; }
public RestUser User { get; private set; }
public IntegrationAccount Account { get; private set; }
internal IGuild Guild { get; private set; }
public DateTimeOffset SyncedAt => DateTimeUtils.FromTicks(_syncedAtTicks);
internal RestGuildIntegration(BaseDiscordClient discord, ulong id)
internal RestGuildIntegration(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, id)
{
Guild = guild;
}
internal static RestGuildIntegration Create(BaseDiscordClient discord, Model model)
internal static RestGuildIntegration Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestGuildIntegration(discord, model.Id);
var entity = new RestGuildIntegration(discord, guild, model.Id);
entity.Update(model);
return entity;
}
@@ -71,6 +73,15 @@ namespace Discord.Rest
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id}{(IsEnabled ? ", Enabled" : "")})";
IGuild IGuildIntegration.Guild
{
get
{
if (Guild != null)
return Guild;
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
}
}
IUser IGuildIntegration.User => User;
}
}

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.Invite;
@@ -11,17 +12,21 @@ namespace Discord.Rest
public string GuildName { get; private set; }
public ulong ChannelId { get; private set; }
public ulong GuildId { get; private set; }
internal IChannel Channel { get; private set; }
internal IGuild Guild { get; private set; }
public string Code => Id;
public string Url => $"{DiscordConfig.InviteUrl}/{Code}";
internal RestInvite(BaseDiscordClient discord, string id)
internal RestInvite(BaseDiscordClient discord, IGuild guild, IChannel channel, string id)
: base(discord, id)
{
Guild = guild;
Channel = channel;
}
internal static RestInvite Create(BaseDiscordClient discord, Model model)
internal static RestInvite Create(BaseDiscordClient discord, IGuild guild, IChannel channel, Model model)
{
var entity = new RestInvite(discord, model.Code);
var entity = new RestInvite(discord, guild, channel, model.Code);
entity.Update(model);
return entity;
}
@@ -46,7 +51,27 @@ namespace Discord.Rest
public override string ToString() => Url;
private string DebuggerDisplay => $"{Url} ({GuildName} / {ChannelName})";
string IEntity<string>.Id => Code;
IGuild IInvite.Guild
{
get
{
if (Guild != null)
return Guild;
var guildChannel = Channel as IGuildChannel;
if (guildChannel != null)
return guildChannel.Guild; //If it fails, it'll still return this exception
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
}
}
IChannel IInvite.Channel
{
get
{
if (Channel != null)
return Channel;
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
}
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using Model = Discord.API.InviteMetadata;
namespace Discord.Rest
@@ -17,13 +16,13 @@ namespace Discord.Rest
public DateTimeOffset CreatedAt => DateTimeUtils.FromTicks(_createdAtTicks);
internal RestInviteMetadata(BaseDiscordClient discord, string id)
: base(discord, id)
internal RestInviteMetadata(BaseDiscordClient discord, IGuild guild, IChannel channel, string id)
: base(discord, guild, channel, id)
{
}
internal static RestInviteMetadata Create(BaseDiscordClient discord, Model model)
internal static RestInviteMetadata Create(BaseDiscordClient discord, IGuild guild, IChannel channel, Model model)
{
var entity = new RestInviteMetadata(discord, model.Code);
var entity = new RestInviteMetadata(discord, guild, channel, model.Code);
entity.Update(model);
return entity;
}

View File

@@ -86,6 +86,17 @@ namespace Discord.Rest
return new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, guildPerms.RawValue));
}
//IGuildUser
IGuild IGuildUser.Guild
{
get
{
if (Guild != null)
return Guild;
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
}
}
//IVoiceState
bool IVoiceState.IsSelfDeafened => false;
bool IVoiceState.IsSelfMuted => false;