DocSpace-buildtools/common/ASC.Core.Common/Notify/Engine/DispatchEngine.cs

79 lines
2.7 KiB
C#
Raw Normal View History

2022-02-15 11:52:43 +00:00
namespace ASC.Notify.Engine;
public class DispatchEngine
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
private readonly ILog _logger;
private readonly ILog _messagesLogger;
private readonly Context _context;
private readonly bool _logOnly;
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public DispatchEngine(Context context, IConfiguration configuration, IOptionsMonitor<ILog> options)
{
_logger = options.Get("ASC.Notify");
_messagesLogger = options.Get("ASC.Notify.Messages");
_context = context ?? throw new ArgumentNullException(nameof(context));
_logOnly = "log".Equals(configuration["core:notify:postman"], StringComparison.InvariantCultureIgnoreCase);
_logger.DebugFormat("LogOnly: {0}", _logOnly);
}
2019-05-15 14:56:09 +00:00
2022-02-15 11:52:43 +00:00
public SendResponse Dispatch(INoticeMessage message, string senderName)
{
var response = new SendResponse(message, senderName, SendResult.OK);
if (!_logOnly)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
var sender = _context.NotifyService.GetSender(senderName);
if (sender != null)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
response = sender.DirectSend(message);
}
else
{
response = new SendResponse(message, senderName, SendResult.Impossible);
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
LogResponce(message, response, sender != null ? sender.SenderName : string.Empty);
2019-11-06 15:03:09 +00:00
}
2022-02-15 11:52:43 +00:00
LogMessage(message, senderName);
return response;
}
private void LogResponce(INoticeMessage message, SendResponse response, string senderName)
{
var logmsg = string.Format("[{0}] sended to [{1}] over {2}, status: {3} ", message.Subject, message.Recipient, senderName, response.Result);
if (response.Result == SendResult.Inprogress)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
_logger.Debug(logmsg, response.Exception);
}
else if (response.Result == SendResult.Impossible)
{
_logger.Error(logmsg, response.Exception);
2019-11-06 15:03:09 +00:00
}
2022-02-15 11:52:43 +00:00
else
{
_logger.Debug(logmsg);
}
}
2019-11-06 15:03:09 +00:00
2022-02-15 11:52:43 +00:00
private void LogMessage(INoticeMessage message, string senderName)
{
try
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
if (_messagesLogger.IsDebugEnabled)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
_messagesLogger.DebugFormat("[{5}]->[{1}] by [{6}] to [{2}] at {0}\r\n\r\n[{3}]\r\n{4}\r\n{7}",
DateTime.Now,
message.Recipient.Name,
0 < message.Recipient.Addresses.Length ? message.Recipient.Addresses[0] : string.Empty,
message.Subject,
(message.Body ?? string.Empty).Replace(Environment.NewLine, Environment.NewLine + @" "),
message.Action,
senderName,
new string('-', 80));
2019-05-15 14:56:09 +00:00
}
}
2022-02-15 11:52:43 +00:00
catch { }
2019-05-15 14:56:09 +00:00
}
2022-02-15 11:52:43 +00:00
}