Merge pull request #656 from AntiTcb/fix/GetDMChannelAsync

Remove IUser.CreateDMChannelAsync / Fix SocketGlobalUser.DMChannel
This commit is contained in:
Christopher F
2017-06-16 21:22:56 -04:00
committed by GitHub
7 changed files with 19 additions and 27 deletions

View File

@@ -20,8 +20,6 @@ namespace Discord
string Username { get; } string Username { get; }
/// <summary> Returns a private message channel to this user, creating one if it does not already exist. </summary> /// <summary> Returns a private message channel to this user, creating one if it does not already exist. </summary>
Task<IDMChannel> GetDMChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); Task<IDMChannel> GetOrCreateDMChannelAsync(RequestOptions options = null);
/// <summary> Returns a private message channel to this user, creating one if it does not already exist. </summary>
Task<IDMChannel> CreateDMChannelAsync(RequestOptions options = null);
} }
} }

View File

@@ -54,7 +54,7 @@ namespace Discord.Rest
Update(model); Update(model);
} }
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null) public Task<RestDMChannel> GetOrCreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options); => UserHelper.CreateDMChannelAsync(this, Discord, options);
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
@@ -64,9 +64,7 @@ namespace Discord.Rest
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";
//IUser //IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options) async Task<IDMChannel> IUser.GetOrCreateDMChannelAsync(RequestOptions options)
=> Task.FromResult<IDMChannel>(null); => await GetOrCreateDMChannelAsync(options);
async Task<IDMChannel> IUser.CreateDMChannelAsync(RequestOptions options)
=> await CreateDMChannelAsync(options).ConfigureAwait(false);
} }
} }

View File

@@ -49,7 +49,7 @@ namespace Discord.Rpc
Username = model.Username.Value; Username = model.Username.Value;
} }
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null) public Task<RestDMChannel> GetOrCreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options); => UserHelper.CreateDMChannelAsync(this, Discord, options);
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
@@ -59,9 +59,7 @@ namespace Discord.Rpc
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";
//IUser //IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options) async Task<IDMChannel> IUser.GetOrCreateDMChannelAsync(RequestOptions options)
=> Task.FromResult<IDMChannel>(null); => await GetOrCreateDMChannelAsync(options);
async Task<IDMChannel> IUser.CreateDMChannelAsync(RequestOptions options)
=> await CreateDMChannelAsync(options).ConfigureAwait(false);
} }
} }

View File

@@ -1634,6 +1634,9 @@ namespace Discord.WebSocket
{ {
var channel = SocketChannel.CreatePrivate(this, state, model); var channel = SocketChannel.CreatePrivate(this, state, model);
state.AddChannel(channel as SocketChannel); state.AddChannel(channel as SocketChannel);
if (channel is SocketDMChannel dm)
dm.Recipient.GlobalUser.DMChannel = dm;
return channel; return channel;
} }
internal ISocketPrivateChannel RemovePrivateChannel(ulong id) internal ISocketPrivateChannel RemovePrivateChannel(ulong id)
@@ -1641,6 +1644,9 @@ namespace Discord.WebSocket
var channel = State.RemoveChannel(id) as ISocketPrivateChannel; var channel = State.RemoveChannel(id) as ISocketPrivateChannel;
if (channel != null) if (channel != null)
{ {
if (channel is SocketDMChannel dmChannel)
dmChannel.Recipient.GlobalUser.DMChannel = null;
foreach (var recipient in channel.Recipients) foreach (var recipient in channel.Recipients)
recipient.GlobalUser.RemoveRef(this); recipient.GlobalUser.RemoveRef(this);
} }

View File

@@ -1,4 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using Model = Discord.API.User; using Model = Discord.API.User;
using PresenceModel = Discord.API.Presence; using PresenceModel = Discord.API.Presence;
@@ -51,6 +52,7 @@ namespace Discord.WebSocket
internal void Update(ClientState state, PresenceModel model) internal void Update(ClientState state, PresenceModel model)
{ {
Presence = SocketPresence.Create(model); Presence = SocketPresence.Create(model);
DMChannel = state.DMChannels.FirstOrDefault(x => x.Recipient.Id == Id);
} }
internal new SocketGlobalUser Clone() => MemberwiseClone() as SocketGlobalUser; internal new SocketGlobalUser Clone() => MemberwiseClone() as SocketGlobalUser;

View File

@@ -147,10 +147,6 @@ namespace Discord.WebSocket
ulong IGuildUser.GuildId => Guild.Id; ulong IGuildUser.GuildId => Guild.Id;
IReadOnlyCollection<ulong> IGuildUser.RoleIds => _roleIds; IReadOnlyCollection<ulong> IGuildUser.RoleIds => _roleIds;
//IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IDMChannel>(GlobalUser.DMChannel);
//IVoiceState //IVoiceState
IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel; IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel;
} }

View File

@@ -55,8 +55,8 @@ namespace Discord.WebSocket
return hasChanges; return hasChanges;
} }
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null) public async Task<IDMChannel> GetOrCreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options); => GlobalUser.DMChannel ?? await UserHelper.CreateDMChannelAsync(this, Discord, options) as IDMChannel;
public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format); => CDN.GetUserAvatarUrl(Id, AvatarId, size, format);
@@ -64,11 +64,5 @@ namespace Discord.WebSocket
public override string ToString() => $"{Username}#{Discriminator}"; public override string ToString() => $"{Username}#{Discriminator}";
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";
internal SocketUser Clone() => MemberwiseClone() as SocketUser; internal SocketUser Clone() => MemberwiseClone() as SocketUser;
//IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IDMChannel>(GlobalUser.DMChannel);
async Task<IDMChannel> IUser.CreateDMChannelAsync(RequestOptions options)
=> await CreateDMChannelAsync(options).ConfigureAwait(false);
} }
} }