Skip to content

Commit 7d6f4f3

Browse files
authored
fix: Clone being created on updated entity (#2077)
* Patch clone being created on updated entity * Clone globaluser as its not within the same instance * Clone the globaluser alongside the guilduser * Patch, non-assigned * Update user entities for globaluser setter
1 parent 9116c9a commit 7d6f4f3

File tree

9 files changed

+18
-14
lines changed

9 files changed

+18
-14
lines changed

src/Discord.Net.WebSocket/DiscordSocketClient.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,14 +1289,13 @@ private async Task ProcessMessageAsync(GatewayOpCode opCode, int? seq, string ty
12891289

12901290
if (user != null)
12911291
{
1292-
var globalBefore = user.GlobalUser.Clone();
1292+
var before = user.Clone();
12931293
if (user.GlobalUser.Update(State, data.User))
12941294
{
12951295
//Global data was updated, trigger UserUpdated
1296-
await TimedInvokeAsync(_userUpdatedEvent, nameof(UserUpdated), globalBefore, user).ConfigureAwait(false);
1296+
await TimedInvokeAsync(_userUpdatedEvent, nameof(UserUpdated), before.GlobalUser, user).ConfigureAwait(false);
12971297
}
12981298

1299-
var before = user.Clone();
13001299
user.Update(State, data);
13011300

13021301
var cacheableBefore = new Cacheable<SocketGuildUser, ulong>(before, user.Id, true, () => null);

src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Diagnostics;
23
using System.Linq;
34
using Model = Discord.API.User;
@@ -14,7 +15,7 @@ internal class SocketGlobalUser : SocketUser
1415
internal override SocketPresence Presence { get; set; }
1516

1617
public override bool IsWebhook => false;
17-
internal override SocketGlobalUser GlobalUser => this;
18+
internal override SocketGlobalUser GlobalUser { get => this; set => throw new NotImplementedException(); }
1819

1920
private readonly object _lockObj = new object();
2021
private ushort _references;

src/Discord.Net.WebSocket/Entities/Users/SocketGroupUser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class SocketGroupUser : SocketUser, IGroupUser
1919
/// </returns>
2020
public SocketGroupChannel Channel { get; }
2121
/// <inheritdoc />
22-
internal override SocketGlobalUser GlobalUser { get; }
22+
internal override SocketGlobalUser GlobalUser { get; set; }
2323

2424
/// <inheritdoc />
2525
public override bool IsBot { get { return GlobalUser.IsBot; } internal set { GlobalUser.IsBot = value; } }

src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class SocketGuildUser : SocketUser, IGuildUser
2424
private long? _joinedAtTicks;
2525
private ImmutableArray<ulong> _roleIds;
2626

27-
internal override SocketGlobalUser GlobalUser { get; }
27+
internal override SocketGlobalUser GlobalUser { get; set; }
2828
/// <summary>
2929
/// Gets the guild the user is in.
3030
/// </summary>
@@ -248,7 +248,13 @@ public string GetGuildAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort si
248248
=> CDN.GetGuildUserAvatarUrl(Id, Guild.Id, GuildAvatarId, size, format);
249249

250250
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")}, Guild)";
251-
internal new SocketGuildUser Clone() => MemberwiseClone() as SocketGuildUser;
251+
252+
internal new SocketGuildUser Clone()
253+
{
254+
var clone = MemberwiseClone() as SocketGuildUser;
255+
clone.GlobalUser = GlobalUser.Clone();
256+
return clone;
257+
}
252258
#endregion
253259

254260
#region IGuildUser

src/Discord.Net.WebSocket/Entities/Users/SocketSelfUser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SocketSelfUser : SocketUser, ISelfUser
1818
public bool IsVerified { get; private set; }
1919
/// <inheritdoc />
2020
public bool IsMfaEnabled { get; private set; }
21-
internal override SocketGlobalUser GlobalUser { get; }
21+
internal override SocketGlobalUser GlobalUser { get; set; }
2222

2323
/// <inheritdoc />
2424
public override bool IsBot { get { return GlobalUser.IsBot; } internal set { GlobalUser.IsBot = value; } }

src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ internal void Update(Model model)
199199

200200
string IGuildUser.GetGuildAvatarUrl(ImageFormat format, ushort size) => GuildUser.GetGuildAvatarUrl(format, size);
201201

202-
internal override SocketGlobalUser GlobalUser => GuildUser.GlobalUser;
202+
internal override SocketGlobalUser GlobalUser { get => GuildUser.GlobalUser; set => GuildUser.GlobalUser = value; }
203203

204204
internal override SocketPresence Presence { get => GuildUser.Presence; set => GuildUser.Presence = value; }
205205

src/Discord.Net.WebSocket/Entities/Users/SocketUnknownUser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public class SocketUnknownUser : SocketUser
2929
internal override SocketPresence Presence { get { return new SocketPresence(UserStatus.Offline, null, null); } set { } }
3030
/// <inheritdoc />
3131
/// <exception cref="NotSupportedException">This field is not supported for an unknown user.</exception>
32-
internal override SocketGlobalUser GlobalUser =>
33-
throw new NotSupportedException();
32+
internal override SocketGlobalUser GlobalUser { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
3433

3534
internal SocketUnknownUser(DiscordSocketClient discord, ulong id)
3635
: base(discord, id)

src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public abstract class SocketUser : SocketEntity<ulong>, IUser
2929
public abstract bool IsWebhook { get; }
3030
/// <inheritdoc />
3131
public UserProperties? PublicFlags { get; private set; }
32-
internal abstract SocketGlobalUser GlobalUser { get; }
32+
internal abstract SocketGlobalUser GlobalUser { get; set; }
3333
internal abstract SocketPresence Presence { get; set; }
3434

3535
/// <inheritdoc />

src/Discord.Net.WebSocket/Entities/Users/SocketWebhookUser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public class SocketWebhookUser : SocketUser, IWebhookUser
3434
public override bool IsWebhook => true;
3535
/// <inheritdoc />
3636
internal override SocketPresence Presence { get { return new SocketPresence(UserStatus.Offline, null, null); } set { } }
37-
internal override SocketGlobalUser GlobalUser =>
38-
throw new NotSupportedException();
37+
internal override SocketGlobalUser GlobalUser { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
3938

4039
internal SocketWebhookUser(SocketGuild guild, ulong id, ulong webhookId)
4140
: base(guild.Discord, id)

0 commit comments

Comments
 (0)