Browse Source

Update invite's references if they dont point to something in cache

tags/docs-0.9
RogueException 9 years ago
parent
commit
adf36d62f2
3 changed files with 41 additions and 7 deletions
  1. +1
    -1
      src/Discord.Net/Helpers/Reference.cs
  2. +31
    -3
      src/Discord.Net/Models/Invite.cs
  3. +9
    -3
      src/Discord.Net/Models/Server.cs

+ 1
- 1
src/Discord.Net/Helpers/Reference.cs View File

@@ -63,6 +63,6 @@ namespace Discord
_getItem = getItem; _getItem = getItem;
_onCache = onCache; _onCache = onCache;
_onUncache = onUncache; _onUncache = onUncache;
}
}
} }
} }

+ 31
- 3
src/Discord.Net/Models/Invite.cs View File

@@ -26,24 +26,45 @@ namespace Discord
[JsonIgnore] [JsonIgnore]
public User Inviter => _inviter.Value; public User Inviter => _inviter.Value;
private readonly Reference<User> _inviter; private readonly Reference<User> _inviter;
private User _generatedInviter;


/// <summary> Returns the server this invite is to. </summary> /// <summary> Returns the server this invite is to. </summary>
[JsonIgnore] [JsonIgnore]
public Server Server => _server.Value; public Server Server => _server.Value;
private readonly Reference<Server> _server; private readonly Reference<Server> _server;
private Server _generatedServer;


/// <summary> Returns the channel this invite is to. </summary> /// <summary> Returns the channel this invite is to. </summary>
[JsonIgnore] [JsonIgnore]
public Channel Channel => _channel.Value; public Channel Channel => _channel.Value;
private readonly Reference<Channel> _channel; private readonly Reference<Channel> _channel;
private Channel _generatedChannel;


internal Invite(DiscordClient client, string code, string xkcdPass, string serverId, string inviterId, string channelId) internal Invite(DiscordClient client, string code, string xkcdPass, string serverId, string inviterId, string channelId)
: base(client, code) : base(client, code)
{ {
XkcdCode = xkcdPass; XkcdCode = xkcdPass;
_server = new Reference<Server>(serverId, x => _client.Servers[x] ?? new Server(client, x));
_inviter = new Reference<User>(serverId, x => _client.Users[x, _server.Id] ?? new User(client, x, _server.Id));
_channel = new Reference<Channel>(serverId, x => _client.Channels[x] ?? new Channel(client, x, _server.Id, null));
_server = new Reference<Server>(serverId, x =>
{
var server = _client.Servers[x];
if (server == null)
server = _generatedServer = new Server(client, x);
return server;
});
_inviter = new Reference<User>(serverId, x =>
{
var inviter = _client.Users[x, _server.Id];
if (inviter == null)
inviter = _generatedInviter = new User(client, x, _server.Id);
return inviter;
});
_channel = new Reference<Channel>(serverId, x =>
{
var channel = _client.Channels[x];
if (channel == null)
channel = _generatedChannel = new Channel(client, x, _server.Id, null);
return channel;
});
} }
internal override void LoadReferences() internal override void LoadReferences()
{ {
@@ -58,6 +79,13 @@ namespace Discord


internal void Update(InviteInfo model) internal void Update(InviteInfo 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.IsRevoked != null) if (model.IsRevoked != null)
IsRevoked = model.IsRevoked.Value; IsRevoked = model.IsRevoked.Value;
if (model.IsTemporary != null) if (model.IsTemporary != null)


+ 9
- 3
src/Discord.Net/Models/Server.cs View File

@@ -123,15 +123,21 @@ namespace Discord
_afkChannel.Unload(); _afkChannel.Unload();
} }


internal void Update(GuildInfo model)
internal void Update(GuildReference model)
{ {
if (model.Name != null)
Name = model.Name;
}

internal void Update(GuildInfo model)
{
Update(model as GuildReference);

if (model.AFKTimeout != null) if (model.AFKTimeout != null)
AFKTimeout = model.AFKTimeout.Value; AFKTimeout = model.AFKTimeout.Value;
if (model.AFKChannelId != null) if (model.AFKChannelId != null)
if (model.JoinedAt != null) if (model.JoinedAt != null)
JoinedAt = model.JoinedAt.Value; JoinedAt = model.JoinedAt.Value;
if (model.Name != null)
Name = model.Name;
if (model.OwnerId != null && _ownerId != model.OwnerId) if (model.OwnerId != null && _ownerId != model.OwnerId)
{ {
_ownerId = model.OwnerId; _ownerId = model.OwnerId;


Loading…
Cancel
Save