DocSpace-buildtools/common/ASC.Core.Common/Notify/Context.cs

90 lines
2.9 KiB
C#
Raw Normal View History

2022-02-15 11:52:43 +00:00
namespace ASC.Notify;
public sealed class Context : INotifyRegistry
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
public const string SysRecipient = "_#" + SysRecipientId + "#_";
internal const string SysRecipientId = "SYS_RECIPIENT_ID";
internal const string SysRecipientName = "SYS_RECIPIENT_NAME";
internal const string SysRecipientAddress = "SYS_RECIPIENT_ADDRESS";
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
private readonly Dictionary<string, ISenderChannel> _channels = new Dictionary<string, ISenderChannel>(2);
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public NotifyEngine NotifyEngine { get; private set; }
public INotifyRegistry NotifyService => this;
public DispatchEngine DispatchEngine { get; private set; }
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public event Action<Context, INotifyClient> NotifyClientRegistration;
2019-11-06 15:03:09 +00:00
2022-02-15 11:52:43 +00:00
private ILog Logger { get; set; }
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public Context(IServiceProvider serviceProvider)
{
var options = serviceProvider.GetService<IOptionsMonitor<ILog>>();
Logger = options.CurrentValue;
NotifyEngine = new NotifyEngine(this, serviceProvider);
DispatchEngine = new DispatchEngine(this, serviceProvider.GetService<IConfiguration>(), options);
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
void INotifyRegistry.RegisterSender(string senderName, ISink senderSink)
{
lock (_channels)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
_channels[senderName] = new SenderChannel(this, senderName, null, senderSink);
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
void INotifyRegistry.UnregisterSender(string senderName)
{
lock (_channels)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
_channels.Remove(senderName);
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
ISenderChannel INotifyRegistry.GetSender(string senderName)
{
lock (_channels)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
_channels.TryGetValue(senderName, out var channel);
2022-02-15 11:52:43 +00:00
return channel;
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
INotifyClient INotifyRegistry.RegisterClient(INotifySource source, IServiceScope serviceScope)
{
//ValidateNotifySource(source);
var client = new NotifyClientImpl(this, source, serviceScope);
NotifyClientRegistration?.Invoke(this, client);
2022-02-15 11:52:43 +00:00
return client;
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
private void ValidateNotifySource(INotifySource source)
{
foreach (var a in source.GetActionProvider().GetActions())
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
IEnumerable<string> senderNames;
lock (_channels)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
senderNames = _channels.Values.Select(s => s.SenderName);
}
foreach (var s in senderNames)
{
try
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
var pattern = source.GetPatternProvider().GetPattern(a, s);
if (pattern == null)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
throw new NotifyException($"In notify source {source.Id} pattern not found for action {a.ID} and sender {s}");
2019-05-15 14:56:09 +00:00
}
}
2022-02-15 11:52:43 +00:00
catch (Exception error)
{
Logger.ErrorFormat("Source: {0}, action: {1}, sender: {2}, error: {3}", source.Id, a.ID, s, error);
}
2019-05-15 14:56:09 +00:00
}
}
}
}