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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2019-06-17 13:53:10 +00:00
|
|
|
|
2019-06-14 11:56:32 +00:00
|
|
|
using ASC.Common.Logging;
|
|
|
|
using ASC.MessagingSystem.DbSender;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-06-17 13:53:10 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2019-06-14 11:56:32 +00:00
|
|
|
|
|
|
|
namespace ASC.MessagingSystem
|
|
|
|
{
|
2019-06-17 13:53:10 +00:00
|
|
|
public class MessageService
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
private static readonly ILog log = LogManager.GetLogger("ASC.Messaging");
|
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-06-17 13:53:10 +00:00
|
|
|
public MessageService(IConfiguration configuration, IHttpContextAccessor httpContextAccessor)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
sender = new DbMessageSender();
|
2019-06-17 13:53:10 +00:00
|
|
|
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
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
public void Send(MessageUserData userData, Dictionary<string, string> httpHeaders, MessageAction action)
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
SendHeadersMessage(userData, httpHeaders, action, null);
|
|
|
|
}
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
public void Send(Dictionary<string, string> httpHeaders, MessageAction action)
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
SendHeadersMessage(null, httpHeaders, action, null);
|
|
|
|
}
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
public void Send(Dictionary<string, string> httpHeaders, MessageAction action, string d1)
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
SendHeadersMessage(null, httpHeaders, action, null, d1);
|
|
|
|
}
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
public void Send(Dictionary<string, string> httpHeaders, MessageAction action, IEnumerable<string> d1)
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
SendHeadersMessage(null, httpHeaders, action, null, d1 != null ? d1.ToArray() : null);
|
|
|
|
}
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
public void Send(MessageUserData userData, Dictionary<string, string> httpHeaders, MessageAction action, MessageTarget target)
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
SendHeadersMessage(userData, httpHeaders, action, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region HttpHeaders & Target
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
public void Send(Dictionary<string, string> httpHeaders, MessageAction action, MessageTarget target)
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
SendHeadersMessage(null, httpHeaders, action, target);
|
|
|
|
}
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
public void Send(Dictionary<string, string> httpHeaders, MessageAction action, MessageTarget target, string d1)
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
SendHeadersMessage(null, httpHeaders, action, target, d1);
|
|
|
|
}
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
public void Send(Dictionary<string, string> httpHeaders, MessageAction action, MessageTarget target, IEnumerable<string> d1)
|
2019-06-14 11:56:32 +00:00
|
|
|
{
|
|
|
|
SendHeadersMessage(null, httpHeaders, action, target, d1 != null ? d1.ToArray() : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2019-06-17 13:53:10 +00:00
|
|
|
private void SendHeadersMessage(MessageUserData userData, Dictionary<string, string> 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|