From d9bfc5fef42b6a0b0f7051d2a8c08157856a6d99 Mon Sep 17 00:00:00 2001 From: RogueException Date: Mon, 7 Dec 2015 10:26:38 -0400 Subject: [PATCH] Fixed EditProfile not caching changes --- src/Discord.Net/API/Messages/Users.cs | 6 +++--- src/Discord.Net/DiscordAPIClient.cs | 16 +++++++++------- src/Discord.Net/DiscordClient.Users.cs | 2 +- src/Discord.Net/DiscordClient.cs | 8 +++++--- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Discord.Net/API/Messages/Users.cs b/src/Discord.Net/API/Messages/Users.cs index 52ef3dfaf..57c19c707 100644 --- a/src/Discord.Net/API/Messages/Users.cs +++ b/src/Discord.Net/API/Messages/Users.cs @@ -33,13 +33,13 @@ namespace Discord.API { [JsonProperty("password")] public string CurrentPassword; - [JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("email")] public string Email; [JsonProperty("new_password")] public string Password; - [JsonProperty("username", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("username")] public string Username; - [JsonProperty("avatar", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("avatar")] public string Avatar; } public sealed class EditUserResponse : UserInfo { } diff --git a/src/Discord.Net/DiscordAPIClient.cs b/src/Discord.Net/DiscordAPIClient.cs index dbb818e9b..272d0d16b 100644 --- a/src/Discord.Net/DiscordAPIClient.cs +++ b/src/Discord.Net/DiscordAPIClient.cs @@ -303,22 +303,24 @@ namespace Discord return _rest.Delete(Endpoints.Server(serverId)); } - public Task EditServer(long serverId, string name = null, string region = null, Stream icon = null, ImageType iconType = ImageType.Png) + public Task EditServer(long serverId, string name = null, string region = null, + Stream icon = null, ImageType iconType = ImageType.Png, string existingIcon = null) { if (serverId <= 0) throw new ArgumentOutOfRangeException(nameof(serverId)); - var request = new EditServerRequest { Name = name, Region = region, Icon = Base64Picture(icon, iconType) }; + var request = new EditServerRequest { Name = name, Region = region, Icon = Base64Picture(icon, iconType, existingIcon) }; return _rest.Patch(Endpoints.Server(serverId), request); } //User public Task EditProfile(string currentPassword = "", string username = null, string email = null, string password = null, - Stream avatar = null, ImageType avatarType = ImageType.Png) + Stream avatar = null, ImageType avatarType = ImageType.Png, string existingAvatar = null) { if (currentPassword == null) throw new ArgumentNullException(nameof(currentPassword)); - var request = new EditUserRequest { CurrentPassword = currentPassword, Username = username, Email = email, Password = password, Avatar = Base64Picture(avatar, avatarType) }; + var request = new EditUserRequest { CurrentPassword = currentPassword, Username = username, + Email = email, Password = password, Avatar = Base64Picture(avatar, avatarType, existingAvatar) }; return _rest.Patch(Endpoints.UserMe, request); } @@ -326,10 +328,10 @@ namespace Discord public Task GetVoiceRegions() => _rest.Get(Endpoints.VoiceRegions); - private string Base64Picture(Stream stream, ImageType type) + private string Base64Picture(Stream stream, ImageType type, string existingId) { if (type == ImageType.None) - return ""; + return null; else if (stream != null) { byte[] bytes = new byte[stream.Length - stream.Position]; @@ -339,7 +341,7 @@ namespace Discord string imageType = type == ImageType.Jpeg ? "image/jpeg;base64" : "image/png;base64"; return $"data:{imageType},{base64}"; } - return null; + return existingId; } } } diff --git a/src/Discord.Net/DiscordClient.Users.cs b/src/Discord.Net/DiscordClient.Users.cs index 0e69d1753..14f8b7921 100644 --- a/src/Discord.Net/DiscordClient.Users.cs +++ b/src/Discord.Net/DiscordClient.Users.cs @@ -275,7 +275,7 @@ namespace Discord await _api.EditProfile(currentPassword: currentPassword, username: username ?? _privateUser?.Name, email: email ?? _privateUser?.Global.Email, password: password, - avatar: avatar, avatarType: avatarType); + avatar: avatar, avatarType: avatarType, existingAvatar: _privateUser?.AvatarId); if (password != null) { diff --git a/src/Discord.Net/DiscordClient.cs b/src/Discord.Net/DiscordClient.cs index 5468dcc6f..edb4fa660 100644 --- a/src/Discord.Net/DiscordClient.cs +++ b/src/Discord.Net/DiscordClient.cs @@ -814,10 +814,12 @@ namespace Discord case "USER_UPDATE": { var data = e.Payload.ToObject(_webSocket.Serializer); - var user = _globalUsers[data.Id]; - if (user != null) + var globalUser = _globalUsers[data.Id]; + if (globalUser != null) { - user.Update(data); + globalUser.Update(data); + foreach (var user in globalUser.Memberships) + user.Update(data); RaiseProfileUpdated(); } }