Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uninstall: adds more handling for nulls #11

Merged
merged 1 commit into from
Dec 1, 2023
Merged
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
36 changes: 27 additions & 9 deletions source/src/Slackbot.Net.Endpoints/Middlewares/Uninstall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@ public Uninstall(RequestDelegate next, ILogger<Uninstall> logger, IServiceProvid
{
_logger = logger;
_uninstaller = provider.GetService<IUninstall>() ?? new NoopUninstaller(provider.GetService<ILogger<NoopUninstaller>>());
_tokenStore = provider.GetService<ITokenStore>();
_tokenStore = provider.GetService<ITokenStore>() ?? new NoopTokenStore(provider.GetService<ILogger<NoopTokenStore>>());
}

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;
}

Expand All @@ -37,17 +46,26 @@ public static bool ShouldRun(HttpContext ctx)
}
}

public class NoopUninstaller : IUninstall
public class NoopUninstaller(ILogger<NoopUninstaller> logger) : IUninstall
{
private readonly ILogger<NoopUninstaller> _logger;
public Task OnUninstalled(string teamId, string teamName)
{
logger.LogDebug("No OnUninstall function registered. No-op.");
return Task.CompletedTask;
}
}

public NoopUninstaller(ILogger<NoopUninstaller> logger)
public class NoopTokenStore(ILogger<NoopTokenStore> logger) : ITokenStore
{
public Task<Workspace> Delete(string teamId)
{
_logger = logger;
logger.LogDebug("No-op. Returning null for deleting workspace!");
return Task.FromResult<Workspace>(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;
}
}