DocSpace-client/common/ASC.Core.Common/Context/WorkContext.cs

165 lines
7.0 KiB
C#
Raw Normal View History

2019-05-15 14:56:09 +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 Constants = ASC.Core.Configuration.Constants;
2019-11-06 15:03:09 +00:00
using NotifyContext = ASC.Notify.Context;
2019-05-15 14:56:09 +00:00
namespace ASC.Core
{
public static class WorkContext
{
private static readonly object _syncRoot = new object();
private static bool _notifyStarted;
private static bool? _isMono;
private static string _monoVersion;
2019-05-15 14:56:09 +00:00
2019-09-09 12:56:33 +00:00
public static NotifyContext NotifyContext { get; private set; }
2019-05-15 14:56:09 +00:00
public static string[] DefaultClientSenders => new[] { Constants.NotifyEMailSenderSysName, };
2019-05-15 14:56:09 +00:00
public static bool IsMono
{
get
{
if (_isMono.HasValue)
2019-05-15 14:56:09 +00:00
{
return _isMono.Value;
2019-05-15 14:56:09 +00:00
}
var monoRuntime = Type.GetType("Mono.Runtime");
_isMono = monoRuntime != null;
2019-05-15 14:56:09 +00:00
if (monoRuntime != null)
{
var dispalayName = monoRuntime.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
if (dispalayName != null)
{
_monoVersion = dispalayName.Invoke(null, null) as string;
2019-05-15 14:56:09 +00:00
}
}
2019-05-15 14:56:09 +00:00
return _isMono.Value;
2019-05-15 14:56:09 +00:00
}
}
public static string MonoVersion => IsMono ? _monoVersion : null;
2019-05-15 14:56:09 +00:00
2019-09-09 12:56:33 +00:00
public static void NotifyStartUp(IServiceProvider serviceProvider)
2019-05-15 14:56:09 +00:00
{
if (_notifyStarted)
{
return;
}
lock (_syncRoot)
2019-05-15 14:56:09 +00:00
{
if (_notifyStarted)
{
return;
}
2019-11-06 15:03:09 +00:00
var configuration = serviceProvider.GetService<IConfiguration>();
var cacheNotify = serviceProvider.GetService<ICacheNotify<NotifyMessage>>();
2020-09-08 12:07:59 +00:00
var cacheInvoke = serviceProvider.GetService<ICacheNotify<NotifyInvoke>>();
2020-10-29 14:30:08 +00:00
var options = serviceProvider.GetService<IOptionsMonitor<ILog>>();
2019-10-17 15:55:35 +00:00
2019-10-11 08:31:21 +00:00
NotifyContext = new NotifyContext(serviceProvider);
2019-05-15 14:56:09 +00:00
2020-09-08 12:07:59 +00:00
INotifySender jabberSender = new NotifyServiceSender(cacheNotify, cacheInvoke);
INotifySender emailSender = new NotifyServiceSender(cacheNotify, cacheInvoke);
2020-10-29 14:30:08 +00:00
INotifySender telegramSender = new TelegramSender(options, serviceProvider);
2019-05-15 14:56:09 +00:00
2019-09-24 10:32:12 +00:00
var postman = configuration["core:notify:postman"];
2019-05-15 14:56:09 +00:00
if ("ases".Equals(postman, StringComparison.InvariantCultureIgnoreCase) || "smtp".Equals(postman, StringComparison.InvariantCultureIgnoreCase))
{
2019-09-09 12:56:33 +00:00
jabberSender = new JabberSender(serviceProvider);
2019-05-15 14:56:09 +00:00
2019-11-06 15:03:09 +00:00
var properties = new Dictionary<string, string>
{
["useCoreSettings"] = "true"
2019-08-15 13:56:54 +00:00
};
2019-05-15 14:56:09 +00:00
if ("ases".Equals(postman, StringComparison.InvariantCultureIgnoreCase))
{
2019-10-17 15:55:35 +00:00
emailSender = new AWSSender(serviceProvider, options);
2019-09-24 10:32:12 +00:00
properties["accessKey"] = configuration["ses:accessKey"];
properties["secretKey"] = configuration["ses:secretKey"];
properties["refreshTimeout"] = configuration["ses:refreshTimeout"];
2019-05-15 14:56:09 +00:00
}
else
{
2019-10-17 15:55:35 +00:00
emailSender = new SmtpSender(serviceProvider, options);
}
2019-05-15 14:56:09 +00:00
emailSender.Init(properties);
}
NotifyContext.NotifyService.RegisterSender(Constants.NotifyEMailSenderSysName, new EmailSenderSink(emailSender, serviceProvider, options));
2019-09-09 12:56:33 +00:00
NotifyContext.NotifyService.RegisterSender(Constants.NotifyMessengerSenderSysName, new JabberSenderSink(jabberSender, serviceProvider));
NotifyContext.NotifyService.RegisterSender(Constants.NotifyTelegramSenderSysName, new TelegramSenderSink(telegramSender, serviceProvider));
2019-05-15 14:56:09 +00:00
2019-09-09 12:56:33 +00:00
NotifyContext.NotifyEngine.BeforeTransferRequest += NotifyEngine_BeforeTransferRequest;
NotifyContext.NotifyEngine.AfterTransferRequest += NotifyEngine_AfterTransferRequest;
_notifyStarted = true;
2019-05-15 14:56:09 +00:00
}
2019-11-06 15:03:09 +00:00
}
2019-10-11 15:03:03 +00:00
public static void RegisterSendMethod(Action<DateTime> method, string cron)
{
NotifyContext.NotifyEngine.RegisterSendMethod(method, cron);
2019-05-15 14:56:09 +00:00
}
2019-10-11 15:03:03 +00:00
public static void UnregisterSendMethod(Action<DateTime> method)
{
NotifyContext.NotifyEngine.UnregisterSendMethod(method);
}
2019-10-11 08:31:21 +00:00
private static void NotifyEngine_BeforeTransferRequest(NotifyEngine sender, NotifyRequest request, IServiceScope serviceScope)
2019-05-15 14:56:09 +00:00
{
2019-10-11 08:31:21 +00:00
request.Properties.Add("Tenant", serviceScope.ServiceProvider.GetService<TenantManager>().GetCurrentTenant(false));
2019-05-15 14:56:09 +00:00
}
2019-10-11 08:31:21 +00:00
private static void NotifyEngine_AfterTransferRequest(NotifyEngine sender, NotifyRequest request, IServiceScope scope)
2019-05-15 14:56:09 +00:00
{
2019-08-15 13:41:38 +00:00
if ((request.Properties.Contains("Tenant") ? request.Properties["Tenant"] : null) is Tenant tenant)
2019-11-06 15:03:09 +00:00
{
2019-10-11 08:31:21 +00:00
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
tenantManager.SetCurrentTenant(tenant);
2019-05-15 14:56:09 +00:00
}
}
2020-10-28 12:26:27 +00:00
}
2022-01-21 10:47:56 +00:00
public static class WorkContextExtension
2020-10-28 12:26:27 +00:00
{
public static void Register(DIHelper dIHelper)
{
dIHelper.TryAdd<TelegramHelper>();
2020-10-28 20:02:03 +00:00
dIHelper.TryAdd<EmailSenderSinkScope>();
dIHelper.TryAdd<JabberSenderSinkScope>();
2020-10-28 12:26:27 +00:00
}
2019-05-15 14:56:09 +00:00
}
}