Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions NetCord/Gateway/ConcurrentGatewayClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -70,7 +70,7 @@ public JsonGatewayClientCache ToJsonModel()
if (user is not null)
jsonModel.User = ((IJsonModel<JsonUser>)user).JsonModel;

jsonModel.Guilds = _guilds.Values.Select(g => ((IJsonModel<JsonGuild>)g).JsonModel).ToArray();
jsonModel.Guilds = [.. _guilds.Values.Select(g => ((IJsonModel<JsonGuild>)g).JsonModel)];

return jsonModel;
}
Expand Down Expand Up @@ -225,10 +225,28 @@ public IGatewayClientCache SyncGuildStickers(ulong guildId, IReadOnlyDictionary<
return this;
}

public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyDictionary<ulong, GuildThread> threads)
public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyList<ulong>? channelIds, IReadOnlyDictionary<ulong, GuildThread> 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<ulong> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion NetCord/Gateway/GatewayClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion NetCord/Gateway/IGatewayClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IGatewayClientCache : IDictionaryProvider, IDisposable

public IGatewayClientCache SyncGuildEmojis(ulong guildId, IReadOnlyDictionary<ulong, GuildEmoji> emojis);
public IGatewayClientCache SyncGuildStickers(ulong guildId, IReadOnlyDictionary<ulong, GuildSticker> stickers);
public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyDictionary<ulong, GuildThread> threads);
public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyList<ulong>? channelIds, IReadOnlyDictionary<ulong, GuildThread> threads);
public IGatewayClientCache SyncGuilds(IReadOnlyList<ulong> guildIds);

public IGatewayClientCache RemoveGuild(ulong guildId);
Expand Down
16 changes: 13 additions & 3 deletions NetCord/Gateway/ImmutableGatewayClientCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public JsonGatewayClientCache ToJsonModel()
if (user is not null)
jsonModel.User = ((IJsonModel<JsonUser>)user).JsonModel;

jsonModel.Guilds = _guilds.Values.Select(g => ((IJsonModel<JsonGuild>)g).JsonModel).ToArray();
jsonModel.Guilds = [.. _guilds.Values.Select(g => ((IJsonModel<JsonGuild>)g).JsonModel)];

return jsonModel;
}
Expand Down Expand Up @@ -293,13 +293,23 @@ public IGatewayClientCache SyncGuildStickers(ulong guildId, IReadOnlyDictionary<
return this;
}

public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyDictionary<ulong, GuildThread> threads)
public IGatewayClientCache SyncGuildActiveThreads(ulong guildId, IReadOnlyList<ulong>? channelIds, IReadOnlyDictionary<ulong, GuildThread> 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<ulong> 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));
Expand Down