DocSpace-buildtools/common/services/ASC.Notify/NotifyService.cs

136 lines
5.5 KiB
C#
Raw Normal View History

2019-07-29 09:15:48 +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.Notify
{
2020-10-27 15:34:22 +00:00
[Singletone(Additional = typeof(NotifyServiceExtension))]
2019-08-01 08:47:15 +00:00
public class NotifyService : INotifyService, IDisposable
2019-07-29 09:15:48 +00:00
{
private readonly ILog _logger;
private readonly ICacheNotify<NotifyMessage> _cacheNotify;
private readonly ICacheNotify<NotifyInvoke> _cacheInvoke;
private readonly DbWorker _db;
private readonly IServiceProvider _serviceProvider;
2019-09-13 11:18:27 +00:00
public NotifyService(DbWorker db, IServiceProvider serviceProvider, ICacheNotify<NotifyMessage> cacheNotify, ICacheNotify<NotifyInvoke> cacheInvoke, IOptionsMonitor<ILog> options)
2019-07-29 09:15:48 +00:00
{
_db = db;
_serviceProvider = serviceProvider;
_cacheNotify = cacheNotify;
_cacheInvoke = cacheInvoke;
_logger = options.CurrentValue;
2019-08-01 08:47:15 +00:00
}
public void Start()
{
_cacheNotify.Subscribe((n) => SendNotifyMessage(n), Common.Caching.CacheNotifyAction.InsertOrUpdate);
_cacheInvoke.Subscribe((n) => InvokeSendMethod(n), Common.Caching.CacheNotifyAction.InsertOrUpdate);
2019-08-01 08:47:15 +00:00
}
public void Stop()
{
_cacheNotify.Unsubscribe(Common.Caching.CacheNotifyAction.InsertOrUpdate);
2019-07-29 09:15:48 +00:00
}
public void SendNotifyMessage(NotifyMessage notifyMessage)
{
try
{
_db.SaveMessage(notifyMessage);
2019-07-29 09:15:48 +00:00
}
catch (Exception e)
{
_logger.Error(e);
2019-07-29 09:15:48 +00:00
}
}
2020-09-16 10:50:53 +00:00
2020-09-08 12:07:59 +00:00
public void InvokeSendMethod(NotifyInvoke notifyInvoke)
2019-07-29 09:15:48 +00:00
{
2020-09-08 12:07:59 +00:00
var service = notifyInvoke.Service;
var method = notifyInvoke.Method;
var tenant = notifyInvoke.Tenant;
var parameters = notifyInvoke.Parameters;
2019-07-29 09:15:48 +00:00
var serviceType = Type.GetType(service, true);
using var scope = _serviceProvider.CreateScope();
2020-09-16 10:50:53 +00:00
var instance = scope.ServiceProvider.GetService(serviceType);
2019-07-29 09:15:48 +00:00
if (instance == null)
{
throw new Exception("Service instance not found.");
}
var methodInfo = serviceType.GetMethod(method);
if (methodInfo == null)
{
throw new Exception("Method not found.");
}
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<NotifyServiceScope>();
2020-09-07 12:01:15 +00:00
var (tenantManager, tenantWhiteLabelSettingsHelper, settingsManager) = scopeClass;
2019-09-13 11:18:27 +00:00
tenantManager.SetCurrentTenant(tenant);
tenantWhiteLabelSettingsHelper.Apply(settingsManager.Load<TenantWhiteLabelSettings>(), tenant);
2020-09-08 12:07:59 +00:00
methodInfo.Invoke(instance, parameters.ToArray());
2019-07-29 09:15:48 +00:00
}
2019-08-01 08:47:15 +00:00
public void Dispose()
{
_cacheNotify.Unsubscribe(CacheNotifyAction.InsertOrUpdate);
_cacheInvoke.Unsubscribe(CacheNotifyAction.InsertOrUpdate);
2019-08-01 08:47:15 +00:00
}
2019-07-29 09:15:48 +00:00
}
2019-11-01 08:49:08 +00:00
2020-10-27 15:34:22 +00:00
[Scope]
2020-08-24 18:41:06 +00:00
public class NotifyServiceScope
2020-07-29 21:57:58 +00:00
{
private readonly TenantManager _tenantManager;
private readonly TenantWhiteLabelSettingsHelper _tenantWhiteLabelSettingsHelper;
private readonly SettingsManager _settingsManager;
2020-07-29 21:57:58 +00:00
2020-08-24 18:41:06 +00:00
public NotifyServiceScope(TenantManager tenantManager, TenantWhiteLabelSettingsHelper tenantWhiteLabelSettingsHelper, SettingsManager settingsManager)
2020-07-29 21:57:58 +00:00
{
_tenantManager = tenantManager;
_tenantWhiteLabelSettingsHelper = tenantWhiteLabelSettingsHelper;
_settingsManager = settingsManager;
2020-07-29 21:57:58 +00:00
}
2020-08-31 08:18:07 +00:00
public void Deconstruct(out TenantManager tenantManager, out TenantWhiteLabelSettingsHelper tenantWhiteLabelSettingsHelper, out SettingsManager settingsManager)
{
tenantManager = _tenantManager;
tenantWhiteLabelSettingsHelper = _tenantWhiteLabelSettingsHelper;
settingsManager = _settingsManager;
2020-08-31 08:18:07 +00:00
}
2020-07-29 21:57:58 +00:00
}
2020-08-24 18:41:06 +00:00
2022-01-21 10:47:56 +00:00
public static class NotifyServiceExtension
2019-11-01 08:49:08 +00:00
{
2020-10-27 15:34:22 +00:00
public static void Register(DIHelper services)
2019-11-01 08:49:08 +00:00
{
2020-10-27 15:34:22 +00:00
services.TryAdd<NotifyServiceScope>();
2019-11-01 08:49:08 +00:00
}
}
2019-07-29 09:15:48 +00:00
}