diff --git a/src/Discord.Net/Models/Invite.cs b/src/Discord.Net/Models/Invite.cs index a9f50af93..841e6353c 100644 --- a/src/Discord.Net/Models/Invite.cs +++ b/src/Discord.Net/Models/Invite.cs @@ -5,7 +5,62 @@ using Newtonsoft.Json; namespace Discord { public sealed class Invite : CachedObject - { + { + public sealed class ServerInfo + { + /// Returns the unique identifier of this server. + public string Id { get; } + /// Returns the name of this server. + public string Name { get; } + + internal ServerInfo(string id, string name) + { + Id = id; + Name = name; + } + } + public sealed class ChannelInfo + { + /// Returns the unique identifier of this channel. + public string Id { get; } + /// Returns the name of this channel. + public string Name { get; } + + internal ChannelInfo(string id, string name) + { + Id = id; + Name = name; + } + } + public sealed class InviterInfo + { + /// Returns the unique identifier for this user. + public string Id { get; } + /// Returns the name of this user. + public string Name { get; } + /// Returns the by-name unique identifier for this user. + public string Discriminator { get; } + /// Returns the unique identifier for this user's avatar. + public string AvatarId { get; } + /// Returns the full path to this user's avatar. + public string AvatarUrl => User.GetAvatarUrl(Id, AvatarId); + + internal InviterInfo(string id, string name, string discriminator, string avatarId) + { + Id = id; + Name = name; + Discriminator = discriminator; + AvatarId = avatarId; + } + } + + /// Returns information about the server this invite is attached to. + public ServerInfo Server { get; private set; } + /// Returns information about the channel this invite is attached to. + public ChannelInfo Channel { get; private set; } + /// Returns information about the user that created this invite. + public InviterInfo Inviter { get; private set; } + /// Returns, if enabled, an alternative human-readable code for URLs. public string XkcdCode { get; } /// Time (in seconds) until the invite expires. Set to 0 to never expire. @@ -23,77 +78,23 @@ namespace Discord /// Returns a URL for this invite using XkcdCode if available or Id if not. public string Url => API.Endpoints.InviteUrl(XkcdCode ?? Id); - /// Returns the user that created this invite. - [JsonIgnore] - public User Inviter => _inviter.Value; - private readonly Reference _inviter; - private User _generatedInviter; - - /// Returns the server this invite is to. - [JsonIgnore] - public Server Server => _server.Value; - private readonly Reference _server; - private Server _generatedServer; - - /// Returns the channel this invite is to. - [JsonIgnore] - public Channel Channel => _channel.Value; - private readonly Reference _channel; - private Channel _generatedChannel; - internal Invite(DiscordClient client, string code, string xkcdPass, string serverId, string inviterId, string channelId) : base(client, code) { XkcdCode = xkcdPass; - _server = new Reference(serverId, x => - { - var server = _client.Servers[x]; - if (server == null) - { - server = _generatedServer = new Server(client, x); - //server.Cache(); - } - return server; - }); - _inviter = new Reference(serverId, x => - { - var inviter = _client.Users[x, _server.Id]; - if (inviter == null) - { - inviter = _generatedInviter = new User(client, x, _server.Id); - //inviter.Cache(); - } - return inviter; - }); - _channel = new Reference(serverId, x => - { - var channel = _client.Channels[x]; - if (channel == null) - { - channel = _generatedChannel = new Channel(client, x, _server.Id, null); - //channel.Cache(); - } - return channel; - }); - } - internal override void LoadReferences() - { - _server.Load(); - _inviter.Load(); - _channel.Load(); } + internal override void LoadReferences() { } internal override void UnloadReferences() { } - internal void Update(InviteReference model) { - if (model.Guild != null && _generatedServer != null) - _generatedServer.Update(model.Guild); - if (model.Inviter != null && _generatedInviter != null) - _generatedInviter.Update(model.Inviter); - if (model.Channel != null && _generatedChannel != null) - _generatedChannel.Update(model.Channel); - } + if (model.Guild != null) + Server = new ServerInfo(model.Guild.Id, model.Guild.Name); + if (model.Channel != null) + Channel = new ChannelInfo(model.Channel.Id, model.Channel.Name); + if (model.Inviter != null) + Inviter = new InviterInfo(model.Inviter.Id, model.Inviter.Username, model.Inviter.Discriminator, model.Inviter.Avatar); + } internal void Update(InviteInfo model) { Update(model as InviteReference); diff --git a/src/Discord.Net/Models/User.cs b/src/Discord.Net/Models/User.cs index 9757864d3..1b75db58c 100644 --- a/src/Discord.Net/Models/User.cs +++ b/src/Discord.Net/Models/User.cs @@ -10,6 +10,7 @@ namespace Discord public class User : CachedObject { internal static string GetId(string userId, string serverId) => (serverId ?? "Private") + '_' + userId; + internal static string GetAvatarUrl(string userId, string avatarId) => avatarId != null ? Endpoints.UserAvatar(userId, avatarId) : null; private ConcurrentDictionary _channels; private ConcurrentDictionary _permissions; @@ -24,7 +25,7 @@ namespace Discord /// Returns the unique identifier for this user's current avatar. public string AvatarId { get; private set; } /// Returns the URL to this user's current avatar. - public string AvatarUrl => AvatarId != null ? API.Endpoints.UserAvatar(Id, AvatarId) : null; + public string AvatarUrl => GetAvatarUrl(Id, AvatarId); /// Returns the datetime that this user joined this server. public DateTime JoinedAt { get; private set; }