diff --git a/src/Tgstation.Server.Host/Components/Chat/Providers/DiscordProvider.cs b/src/Tgstation.Server.Host/Components/Chat/Providers/DiscordProvider.cs index 664743ba20..468982d7af 100644 --- a/src/Tgstation.Server.Host/Components/Chat/Providers/DiscordProvider.cs +++ b/src/Tgstation.Server.Host/Components/Chat/Providers/DiscordProvider.cs @@ -196,101 +196,6 @@ namespace Tgstation.Server.Host.Components.Chat.Providers gatewayCts?.Dispose(); } - /// - protected override async Task> MapChannelsImpl(IEnumerable channels, CancellationToken cancellationToken) - { - if (channels == null) - throw new ArgumentNullException(nameof(channels)); - - bool remapRequired = false; - - async Task GetModelChannelFromDBChannel(Api.Models.ChatChannel channelFromDB) - { - if (!channelFromDB.DiscordChannelId.HasValue) - throw new InvalidOperationException("ChatChannel missing DiscordChannelId!"); - - var channelId = channelFromDB.DiscordChannelId.Value; - string connectionName; - string friendlyName; - if (channelId == 0) - { - connectionName = initialUserName; - friendlyName = "(Unmapped accessible channels)"; - } - else - { - var channelsClient = serviceProvider.GetRequiredService(); - var discordChannelResponse = await channelsClient.GetChannelAsync(new Snowflake(channelId), cancellationToken); - if (!discordChannelResponse.IsSuccess) - { - Logger.LogWarning("Error retrieving discord channel {0}: {1}", channelId, discordChannelResponse.Error.Message); - remapRequired = true; - return null; - } - - if (discordChannelResponse.Entity.Type != ChannelType.GuildText) - { - Logger.LogWarning("Cound not map channel {0}! Incorrect type: {1}", channelId, discordChannelResponse.Entity.Type); - return null; - } - - friendlyName = discordChannelResponse.Entity.Name.Value; - - var guildsClient = serviceProvider.GetRequiredService(); - var guildsResponse = await guildsClient.GetGuildAsync( - discordChannelResponse.Entity.GuildID.Value, - false, - cancellationToken); - if (!guildsResponse.IsSuccess) - { - Logger.LogWarning( - "Error retrieving discord guild {0}: {1}", - discordChannelResponse.Entity.GuildID.Value, - discordChannelResponse.Error.Message); - remapRequired = true; - return null; - } - - connectionName = guildsResponse.Entity.Name; - } - - var channelModel = new ChannelRepresentation - { - RealId = channelId, - IsAdminChannel = channelFromDB.IsAdminChannel == true, - ConnectionName = connectionName, - FriendlyName = friendlyName, - IsPrivateChannel = false, - Tag = channelFromDB.Tag, - }; - - Logger.LogTrace("Mapped channel {0}: {1}", channelModel.RealId, channelModel.FriendlyName); - return channelModel; - } - - var tasks = channels - .Select(x => GetModelChannelFromDBChannel(x)) - .ToList(); - - await Task.WhenAll(tasks); - - var enumerator = tasks - .Select(x => x.Result) - .Where(x => x != null) - .ToList(); - - lock (mappedChannels) - { - mappedChannels.Clear(); - mappedChannels.AddRange(enumerator.Select(x => x.RealId)); - } - - if (remapRequired) - EnqueueMessage(null); - - return enumerator; - } - /// public override async Task SendMessage(ulong channelId, string message, CancellationToken cancellationToken) { @@ -678,6 +583,101 @@ namespace Tgstation.Server.Host.Components.Chat.Providers localGatewayCts.Dispose(); } + + /// + protected override async Task> MapChannelsImpl(IEnumerable channels, CancellationToken cancellationToken) + { + if (channels == null) + throw new ArgumentNullException(nameof(channels)); + + bool remapRequired = false; + + async Task GetModelChannelFromDBChannel(Api.Models.ChatChannel channelFromDB) + { + if (!channelFromDB.DiscordChannelId.HasValue) + throw new InvalidOperationException("ChatChannel missing DiscordChannelId!"); + + var channelId = channelFromDB.DiscordChannelId.Value; + string connectionName; + string friendlyName; + if (channelId == 0) + { + connectionName = initialUserName; + friendlyName = "(Unmapped accessible channels)"; + } + else + { + var channelsClient = serviceProvider.GetRequiredService(); + var discordChannelResponse = await channelsClient.GetChannelAsync(new Snowflake(channelId), cancellationToken); + if (!discordChannelResponse.IsSuccess) + { + Logger.LogWarning("Error retrieving discord channel {0}: {1}", channelId, discordChannelResponse.Error.Message); + remapRequired = true; + return null; + } + + if (discordChannelResponse.Entity.Type != ChannelType.GuildText) + { + Logger.LogWarning("Cound not map channel {0}! Incorrect type: {1}", channelId, discordChannelResponse.Entity.Type); + return null; + } + + friendlyName = discordChannelResponse.Entity.Name.Value; + + var guildsClient = serviceProvider.GetRequiredService(); + var guildsResponse = await guildsClient.GetGuildAsync( + discordChannelResponse.Entity.GuildID.Value, + false, + cancellationToken); + if (!guildsResponse.IsSuccess) + { + Logger.LogWarning( + "Error retrieving discord guild {0}: {1}", + discordChannelResponse.Entity.GuildID.Value, + discordChannelResponse.Error.Message); + remapRequired = true; + return null; + } + + connectionName = guildsResponse.Entity.Name; + } + + var channelModel = new ChannelRepresentation + { + RealId = channelId, + IsAdminChannel = channelFromDB.IsAdminChannel == true, + ConnectionName = connectionName, + FriendlyName = friendlyName, + IsPrivateChannel = false, + Tag = channelFromDB.Tag, + }; + + Logger.LogTrace("Mapped channel {0}: {1}", channelModel.RealId, channelModel.FriendlyName); + return channelModel; + } + + var tasks = channels + .Select(x => GetModelChannelFromDBChannel(x)) + .ToList(); + + await Task.WhenAll(tasks); + + var enumerator = tasks + .Select(x => x.Result) + .Where(x => x != null) + .ToList(); + + lock (mappedChannels) + { + mappedChannels.Clear(); + mappedChannels.AddRange(enumerator.Select(x => x.RealId)); + } + + if (remapRequired) + EnqueueMessage(null); + + return enumerator; + } } #pragma warning restore CA1506 }