diff --git a/NetCord/Gateway/ConcurrentGatewayClientCache.cs b/NetCord/Gateway/ConcurrentGatewayClientCache.cs index f4b8752f..fee72aa8 100644 --- a/NetCord/Gateway/ConcurrentGatewayClientCache.cs +++ b/NetCord/Gateway/ConcurrentGatewayClientCache.cs @@ -49,7 +49,7 @@ internal ConcurrentGatewayClientCache() internal ConcurrentGatewayClientCache(JsonGatewayClientCache jsonModel, ulong clientId, RestClient client) { if (jsonModel.User is { } userModel) - _user = new CurrentUser(userModel, client); + _user = new(userModel, client); _guilds = CreateConcurrentDictionary(jsonModel.Guilds, g => g.Id, g => new Guild(g, clientId, client, this)); } @@ -70,7 +70,7 @@ public JsonGatewayClientCache ToJsonModel() if (user is not null) jsonModel.User = ((IJsonModel)user).JsonModel; - jsonModel.Guilds = _guilds.Values.Select(g => ((IJsonModel)g).JsonModel).ToArray(); + jsonModel.Guilds = [.. _guilds.Values.Select(g => ((IJsonModel)g).JsonModel)]; return jsonModel; } @@ -225,10 +225,28 @@ public IGatewayClientCache SyncGuildStickers(ulong guildId, IReadOnlyDictionary< return this; } - public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyDictionary threads) + public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyList? channelIds, IReadOnlyDictionary threads) { if (_guilds.TryGetValue(guildId, out var guild)) - guild.ActiveThreads = threads; + { + if (channelIds is null) + guild.ActiveThreads = threads; + else + { + var activeThreads = Cast(guild.ActiveThreads); + + HashSet channelIdsSet = [.. channelIds]; + + foreach (var thread in activeThreads) + { + if (channelIdsSet.Contains(thread.Value.ParentId) && !threads.ContainsKey(thread.Key)) + activeThreads.TryRemove(thread.Key, out _); + } + + foreach (var thread in threads) + activeThreads[thread.Key] = thread.Value; + } + } return this; } diff --git a/NetCord/Gateway/GatewayClient.cs b/NetCord/Gateway/GatewayClient.cs index bb645c89..b27828f8 100644 --- a/NetCord/Gateway/GatewayClient.cs +++ b/NetCord/Gateway/GatewayClient.cs @@ -1229,7 +1229,7 @@ await InvokeEventAsync(_ready, this, (Args: args, State: state, ConnectionState: { var json = data.ToObject(Serialization.Default.JsonGuildThreadListSyncEventArgs); GuildThreadListSyncEventArgs args = new(json, Rest, Cache); - await InvokeEventAsync(_guildThreadListSync, this, args, static (client, args) => client.Cache = client.Cache.SyncGuildActiveThreads(args.GuildId, args.Threads)).ConfigureAwait(false); + await InvokeEventAsync(_guildThreadListSync, this, args, static (client, args) => client.Cache = client.Cache.SyncGuildActiveThreads(args.GuildId, args.ChannelIds, args.Threads)).ConfigureAwait(false); } break; case "THREAD_MEMBER_UPDATE": diff --git a/NetCord/Gateway/IGatewayClientCache.cs b/NetCord/Gateway/IGatewayClientCache.cs index a95d3ecf..3820d092 100644 --- a/NetCord/Gateway/IGatewayClientCache.cs +++ b/NetCord/Gateway/IGatewayClientCache.cs @@ -20,7 +20,7 @@ public interface IGatewayClientCache : IDictionaryProvider, IDisposable public IGatewayClientCache SyncGuildEmojis(ulong guildId, IReadOnlyDictionary emojis); public IGatewayClientCache SyncGuildStickers(ulong guildId, IReadOnlyDictionary stickers); - public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyDictionary threads); + public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyList? channelIds, IReadOnlyDictionary threads); public IGatewayClientCache SyncGuilds(IReadOnlyList guildIds); public IGatewayClientCache RemoveGuild(ulong guildId); diff --git a/NetCord/Gateway/ImmutableGatewayClientCache.cs b/NetCord/Gateway/ImmutableGatewayClientCache.cs index 72fbfabc..b646c091 100644 --- a/NetCord/Gateway/ImmutableGatewayClientCache.cs +++ b/NetCord/Gateway/ImmutableGatewayClientCache.cs @@ -88,7 +88,7 @@ public JsonGatewayClientCache ToJsonModel() if (user is not null) jsonModel.User = ((IJsonModel)user).JsonModel; - jsonModel.Guilds = _guilds.Values.Select(g => ((IJsonModel)g).JsonModel).ToArray(); + jsonModel.Guilds = [.. _guilds.Values.Select(g => ((IJsonModel)g).JsonModel)]; return jsonModel; } @@ -293,13 +293,23 @@ public IGatewayClientCache SyncGuildStickers(ulong guildId, IReadOnlyDictionary< return this; } - public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyDictionary threads) + public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyList? channelIds, IReadOnlyDictionary threads) { var guilds = _guilds; if (guilds.TryGetValue(guildId, out var guild)) { var newGuild = guild.Clone(); - newGuild.ActiveThreads = threads; + if (channelIds is null) + newGuild.ActiveThreads = threads; + else + { + var activeThreads = Cast(guild.ActiveThreads); + + HashSet channelIdsSet = [.. channelIds]; + + newGuild.ActiveThreads = activeThreads.RemoveRange(activeThreads.Where(t => channelIdsSet.Contains(t.Value.ParentId)).Select(t => t.Key)) + .SetItems(threads); + } return Create(_user, guilds.SetItem(guildId, newGuild));