From b9e7b58a9278c10857c7f01f2a80a112f4b756d5 Mon Sep 17 00:00:00 2001 From: John Korsnes Date: Fri, 1 Dec 2023 14:30:30 +0100 Subject: [PATCH] Uninstall: adds more handling for nulls --- .../Middlewares/Uninstall.cs | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/source/src/Slackbot.Net.Endpoints/Middlewares/Uninstall.cs b/source/src/Slackbot.Net.Endpoints/Middlewares/Uninstall.cs index 571b81e..277bf9b 100644 --- a/source/src/Slackbot.Net.Endpoints/Middlewares/Uninstall.cs +++ b/source/src/Slackbot.Net.Endpoints/Middlewares/Uninstall.cs @@ -17,15 +17,24 @@ public Uninstall(RequestDelegate next, ILogger logger, IServiceProvid { _logger = logger; _uninstaller = provider.GetService() ?? new NoopUninstaller(provider.GetService>()); - _tokenStore = provider.GetService(); + _tokenStore = provider.GetService() ?? new NoopTokenStore(provider.GetService>()); } public async Task Invoke(HttpContext context) { var metadata = context.Items[HttpItemKeys.EventMetadataKey] as EventMetaData; + _logger.LogInformation($"Deleting team with TeamId: `{metadata.Team_Id}`"); var deleted = await _tokenStore.Delete(metadata.Team_Id); - await _uninstaller.OnUninstalled(deleted.TeamId, deleted.TeamName); - _logger.LogInformation($"Deleted team with TeamId: `{metadata.Team_Id}`"); + if (deleted is null) + { + _logger.LogWarning("Token store returned null for '{TeamId}'. Will not trigger registered OnUninstalled handlers. ", metadata.Team_Id); + } + else + { + await _uninstaller.OnUninstalled(deleted?.TeamId, deleted?.TeamName); + _logger.LogInformation($"Deleted team with TeamId: `{metadata.Team_Id}`"); + } + context.Response.StatusCode = 200; } @@ -37,17 +46,26 @@ public static bool ShouldRun(HttpContext ctx) } } -public class NoopUninstaller : IUninstall +public class NoopUninstaller(ILogger logger) : IUninstall { - private readonly ILogger _logger; + public Task OnUninstalled(string teamId, string teamName) + { + logger.LogDebug("No OnUninstall function registered. No-op."); + return Task.CompletedTask; + } +} - public NoopUninstaller(ILogger logger) +public class NoopTokenStore(ILogger logger) : ITokenStore +{ + public Task Delete(string teamId) { - _logger = logger; + logger.LogDebug("No-op. Returning null for deleting workspace!"); + return Task.FromResult(null); } - public Task OnUninstalled(string teamId, string teamName) + + public Task Insert(Workspace slackTeam) { - _logger.LogDebug("No OnUninstall function registered. No-op."); + logger.LogDebug("No-op. Not storing workspace!"); return Task.CompletedTask; } } \ No newline at end of file