DocSpace-buildtools/common/ASC.MessagingSystem/MessageService.cs

288 lines
10 KiB
C#
Raw Normal View History

2019-06-14 11:56:32 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.MessagingSystem
{
2020-10-19 15:53:15 +00:00
[Scope]
2019-06-17 13:53:10 +00:00
public class MessageService
2019-06-14 11:56:32 +00:00
{
2019-10-17 15:55:35 +00:00
private readonly ILog log;
2019-06-17 13:53:10 +00:00
private readonly IMessageSender sender;
private readonly HttpRequest request;
2019-06-14 11:56:32 +00:00
2019-10-31 11:28:30 +00:00
private MessageFactory MessageFactory { get; }
private MessagePolicy MessagePolicy { get; }
2019-06-14 11:56:32 +00:00
2019-10-31 11:28:30 +00:00
public MessageService(
IConfiguration configuration,
MessageFactory messageFactory,
DbMessageSender sender,
MessagePolicy messagePolicy,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
if (configuration["messaging:enabled"] != "true")
2019-06-14 11:56:32 +00:00
{
return;
}
2019-09-24 10:32:12 +00:00
this.sender = sender;
2019-09-27 15:12:13 +00:00
MessagePolicy = messagePolicy;
2019-09-09 12:56:33 +00:00
MessageFactory = messageFactory;
2019-11-06 15:03:09 +00:00
log = options.CurrentValue;
2019-06-14 11:56:32 +00:00
}
2020-04-28 15:11:10 +00:00
public MessageService(
IConfiguration configuration,
IHttpContextAccessor httpContextAccessor,
MessageFactory messageFactory,
DbMessageSender sender,
MessagePolicy messagePolicy,
IOptionsMonitor<ILog> options)
: this(configuration, messageFactory, sender, messagePolicy, options)
{
request = httpContextAccessor?.HttpContext?.Request;
}
2019-06-14 11:56:32 +00:00
#region HttpRequest
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, string d1)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null, d1);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, string d1, string d2)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null, d1, d2);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, string d1, string d2, string d3)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null, d1, d2, d3);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, string d1, string d2, string d3, string d4)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null, d1, d2, d3, d4);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, IEnumerable<string> d1, string d2)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null, string.Join(", ", d1), d2);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, string d1, IEnumerable<string> d2)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null, d1, string.Join(", ", d2));
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, string d1, string d2, IEnumerable<string> d3)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null, d1, d2, string.Join(", ", d3));
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, IEnumerable<string> d1)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, null, string.Join(", ", d1));
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(string loginName, MessageAction action)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(loginName, action, null);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(string loginName, MessageAction action, string d1)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(loginName, action, null, d1);
2019-06-14 11:56:32 +00:00
}
#endregion
#region HttpRequest & Target
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target, string d1)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target, d1);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target, string d1, string d2)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target, d1, d2);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target, string d1, string d2, string d3)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target, d1, d2, d3);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target, string d1, string d2, string d3, string d4)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target, d1, d2, d3, d4);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target, IEnumerable<string> d1, string d2)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target, string.Join(", ", d1), d2);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target, string d1, IEnumerable<string> d2)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target, d1, string.Join(", ", d2));
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target, string d1, string d2, IEnumerable<string> d3)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target, d1, d2, string.Join(", ", d3));
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(MessageAction action, MessageTarget target, IEnumerable<string> d1)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(null, action, target, string.Join(", ", d1));
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(string loginName, MessageAction action, MessageTarget target)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(loginName, action, target);
2019-06-14 11:56:32 +00:00
}
2019-06-17 13:53:10 +00:00
public void Send(string loginName, MessageAction action, MessageTarget target, string d1)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendRequestMessage(loginName, action, target, d1);
2019-06-14 11:56:32 +00:00
}
#endregion
2019-06-17 13:53:10 +00:00
private void SendRequestMessage(string loginName, MessageAction action, MessageTarget target, params string[] description)
2019-06-14 11:56:32 +00:00
{
if (sender == null) return;
if (request == null)
{
log.Debug(string.Format("Empty Http Request for \"{0}\" type of event", action));
return;
}
var message = MessageFactory.Create(request, loginName, action, target, description);
if (!MessagePolicy.Check(message)) return;
sender.Send(message);
}
#region HttpHeaders
2020-06-01 12:16:24 +00:00
public void Send(MessageUserData userData, IDictionary<string, StringValues> httpHeaders, MessageAction action)
2019-06-14 11:56:32 +00:00
{
SendHeadersMessage(userData, httpHeaders, action, null);
}
2020-06-01 12:16:24 +00:00
public void Send(IDictionary<string, StringValues> httpHeaders, MessageAction action)
2019-06-14 11:56:32 +00:00
{
SendHeadersMessage(null, httpHeaders, action, null);
}
2020-06-01 12:16:24 +00:00
public void Send(IDictionary<string, StringValues> httpHeaders, MessageAction action, string d1)
2019-06-14 11:56:32 +00:00
{
SendHeadersMessage(null, httpHeaders, action, null, d1);
}
2020-06-01 12:16:24 +00:00
public void Send(IDictionary<string, StringValues> httpHeaders, MessageAction action, IEnumerable<string> d1)
2019-06-14 11:56:32 +00:00
{
2019-08-15 13:28:29 +00:00
SendHeadersMessage(null, httpHeaders, action, null, d1?.ToArray());
2019-06-14 11:56:32 +00:00
}
2020-06-01 12:16:24 +00:00
public void Send(MessageUserData userData, IDictionary<string, StringValues> httpHeaders, MessageAction action, MessageTarget target)
2019-06-14 11:56:32 +00:00
{
SendHeadersMessage(userData, httpHeaders, action, target);
}
#endregion
#region HttpHeaders & Target
2020-06-01 12:16:24 +00:00
public void Send(IDictionary<string, StringValues> httpHeaders, MessageAction action, MessageTarget target)
2019-06-14 11:56:32 +00:00
{
SendHeadersMessage(null, httpHeaders, action, target);
}
2020-06-01 12:16:24 +00:00
public void Send(IDictionary<string, StringValues> httpHeaders, MessageAction action, MessageTarget target, string d1)
2019-06-14 11:56:32 +00:00
{
SendHeadersMessage(null, httpHeaders, action, target, d1);
}
2020-06-01 12:16:24 +00:00
public void Send(IDictionary<string, StringValues> httpHeaders, MessageAction action, MessageTarget target, IEnumerable<string> d1)
2019-06-14 11:56:32 +00:00
{
2019-08-15 13:28:29 +00:00
SendHeadersMessage(null, httpHeaders, action, target, d1?.ToArray());
2019-06-14 11:56:32 +00:00
}
#endregion
2020-06-01 12:16:24 +00:00
private void SendHeadersMessage(MessageUserData userData, IDictionary<string, StringValues> httpHeaders, MessageAction action, MessageTarget target, params string[] description)
2019-06-14 11:56:32 +00:00
{
if (sender == null) return;
var message = MessageFactory.Create(userData, httpHeaders, action, target, description);
if (!MessagePolicy.Check(message)) return;
sender.Send(message);
}
#region Initiator
2019-06-17 13:53:10 +00:00
public void Send(MessageInitiator initiator, MessageAction action, params string[] description)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendInitiatorMessage(initiator.ToString(), action, null, description);
2019-06-14 11:56:32 +00:00
}
#endregion
#region Initiator & Target
2019-06-17 13:53:10 +00:00
public void Send(MessageInitiator initiator, MessageAction action, MessageTarget target, params string[] description)
2019-06-14 11:56:32 +00:00
{
2019-06-17 13:53:10 +00:00
SendInitiatorMessage(initiator.ToString(), action, target, description);
2019-06-14 11:56:32 +00:00
}
#endregion
2019-06-17 13:53:10 +00:00
private void SendInitiatorMessage(string initiator, MessageAction action, MessageTarget target, params string[] description)
2019-06-14 11:56:32 +00:00
{
if (sender == null) return;
var message = MessageFactory.Create(request, initiator, action, target, description);
if (!MessagePolicy.Check(message)) return;
sender.Send(message);
}
}
}