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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Resgrid.Chatbot.Models;
Expand All @@ -11,17 +12,22 @@ namespace Resgrid.Providers.Chatbot.Services
/// </summary>
public class ChatbotAdapterRegistry : IChatbotAdapterRegistry
{
private readonly Dictionary<ChatbotPlatform, IChatbotPlatformAdapter> _adapters;
// Adapters are resolved lazily: WebChatAdapter's notifier pulls in the chat services, which
// reach AuthorizationService -> CallsService -> CommunicationService -> ChatbotOutboundService
// and back into this registry. Deferring adapter activation until first GetAdapter call keeps
// that loop out of the container's constructor chain (Autofac circular dependency exception).
private readonly Lazy<Dictionary<ChatbotPlatform, IChatbotPlatformAdapter>> _adapters;

public ChatbotAdapterRegistry(IEnumerable<IChatbotPlatformAdapter> adapters)
public ChatbotAdapterRegistry(Lazy<IEnumerable<IChatbotPlatformAdapter>> adapters)
{
_adapters = (adapters ?? Enumerable.Empty<IChatbotPlatformAdapter>())
.GroupBy(a => a.Platform)
.ToDictionary(g => g.Key, g => g.First());
_adapters = new Lazy<Dictionary<ChatbotPlatform, IChatbotPlatformAdapter>>(() =>
(adapters?.Value ?? Enumerable.Empty<IChatbotPlatformAdapter>())
.GroupBy(a => a.Platform)
.ToDictionary(g => g.Key, g => g.First()));
}

public IChatbotPlatformAdapter GetAdapter(ChatbotPlatform platform)
=> _adapters.TryGetValue(platform, out var adapter) ? adapter : null;
=> _adapters.Value.TryGetValue(platform, out var adapter) ? adapter : null;

/// <summary>
/// Whether the bot may send an un-prompted message on this platform. SMS is delivered by the
Expand Down
23 changes: 21 additions & 2 deletions Tests/Resgrid.Tests/Chatbot/ChatbotOutboundTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
Expand Down Expand Up @@ -30,7 +31,7 @@ private static ChatbotOutboundMessage DispatchMsg() =>
[Test]
public void Registry_CanInitiateProactively_ReflectsPlatformConstraints()
{
var registry = new ChatbotAdapterRegistry(new List<IChatbotPlatformAdapter>());
var registry = new ChatbotAdapterRegistry(new Lazy<IEnumerable<IChatbotPlatformAdapter>>(() => new List<IChatbotPlatformAdapter>()));

registry.CanInitiateProactively(ChatbotPlatform.Slack).Should().BeTrue();
registry.CanInitiateProactively(ChatbotPlatform.WebChat).Should().BeTrue();
Expand All @@ -47,12 +48,30 @@ public void Registry_CanInitiateProactively_ReflectsPlatformConstraints()
public void Registry_GetAdapter_ResolvesByPlatform()
{
var slack = new SlackBotAdapter();
var registry = new ChatbotAdapterRegistry(new List<IChatbotPlatformAdapter> { slack });
var registry = new ChatbotAdapterRegistry(new Lazy<IEnumerable<IChatbotPlatformAdapter>>(() => new List<IChatbotPlatformAdapter> { slack }));

registry.GetAdapter(ChatbotPlatform.Slack).Should().BeSameAs(slack);
registry.GetAdapter(ChatbotPlatform.Discord).Should().BeNull();
}

[Test]
public void Registry_DoesNotResolveAdaptersUntilFirstGetAdapter()
{
// Guards the circular-dependency fix: constructing the registry (which happens inside the
// CommunicationService -> ChatbotOutboundService activation chain) must not materialize the
// adapters, because WebChatAdapter's dependency graph loops back through CallsService.
var resolved = false;
var registry = new ChatbotAdapterRegistry(new Lazy<IEnumerable<IChatbotPlatformAdapter>>(() =>
{
resolved = true;
return new List<IChatbotPlatformAdapter>();
}));

resolved.Should().BeFalse();
registry.GetAdapter(ChatbotPlatform.Slack);
resolved.Should().BeTrue();
}

// ---- ChatbotOutboundService ----

private static (Mock<IChatbotUserIdentityService> identities, Mock<IChatbotDepartmentConfigService> config, Mock<IChatbotAdapterRegistry> registry)
Expand Down
Loading