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

154 lines
5.8 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.
*
*/
using System;
2020-09-08 12:07:59 +00:00
using System.Linq;
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-08-01 08:47:15 +00:00
using ASC.Common.Caching;
2019-07-29 09:15:48 +00:00
using ASC.Common.Logging;
using ASC.Core;
using ASC.Core.Common.Settings;
2019-07-29 09:15:48 +00:00
using ASC.Notify.Messages;
using ASC.Web.Core.WhiteLabel;
2020-02-17 08:58:14 +00:00
2019-09-13 11:18:27 +00:00
using Microsoft.Extensions.DependencyInjection;
2019-10-17 15:55:35 +00:00
using Microsoft.Extensions.Options;
2019-07-29 09:15:48 +00:00
namespace ASC.Notify
{
2020-10-19 15:53:15 +00:00
[Singletone]
2019-08-01 08:47:15 +00:00
public class NotifyService : INotifyService, IDisposable
2019-07-29 09:15:48 +00:00
{
2020-09-08 12:07:59 +00:00
private ILog Log { get; }
2019-07-29 09:15:48 +00:00
2020-09-08 12:07:59 +00:00
private ICacheNotify<NotifyMessage> CacheNotify { get; }
private ICacheNotify<NotifyInvoke> CacheInvoke { get; }
private DbWorker Db { get; }
2020-08-12 09:58:08 +00:00
private IServiceProvider ServiceProvider { get; }
2019-09-13 11:18:27 +00:00
2020-09-08 12:07:59 +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
{
2020-09-08 12:07:59 +00:00
Db = db;
2019-09-13 11:18:27 +00:00
ServiceProvider = serviceProvider;
2020-09-08 12:07:59 +00:00
CacheNotify = cacheNotify;
CacheInvoke = cacheInvoke;
Log = options.CurrentValue;
2019-08-01 08:47:15 +00:00
}
public void Start()
{
2020-09-08 12:07:59 +00:00
CacheNotify.Subscribe((n) => SendNotifyMessage(n), CacheNotifyAction.InsertOrUpdate);
CacheInvoke.Subscribe((n) => InvokeSendMethod(n), CacheNotifyAction.InsertOrUpdate);
2019-08-01 08:47:15 +00:00
}
public void Stop()
{
2020-09-08 12:07:59 +00:00
CacheNotify.Unsubscribe(CacheNotifyAction.InsertOrUpdate);
2019-07-29 09:15:48 +00:00
}
public void SendNotifyMessage(NotifyMessage notifyMessage)
{
try
{
2020-09-08 12:07:59 +00:00
Db.SaveMessage(notifyMessage);
2019-07-29 09:15:48 +00:00
}
catch (Exception e)
{
2020-09-08 12:07:59 +00:00
Log.Error(e);
2019-07-29 09:15:48 +00:00
}
}
2020-09-16 10:50:53 +00:00
2019-07-29 09:15:48 +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);
2020-09-16 10:50:53 +00:00
using var scope = ServiceProvider.CreateScope();
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()
{
2020-09-08 12:07:59 +00:00
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-08-24 18:41:06 +00:00
public class NotifyServiceScope
2020-07-29 21:57:58 +00:00
{
2020-08-31 08:18:07 +00:00
private TenantManager TenantManager { get; }
private TenantWhiteLabelSettingsHelper TenantWhiteLabelSettingsHelper { get; }
private SettingsManager SettingsManager { get; }
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-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-07-29 21:57:58 +00:00
}
2020-08-24 18:41:06 +00:00
public static class NotifyServiceExtension
2019-11-01 08:49:08 +00:00
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddNotifyService(this DIHelper services)
2019-11-01 08:49:08 +00:00
{
2020-08-24 18:41:06 +00:00
services.TryAddScoped<NotifyServiceScope>();
2019-11-01 08:49:08 +00:00
2020-10-19 15:53:15 +00:00
return services;
2019-11-01 08:49:08 +00:00
}
}
2019-07-29 09:15:48 +00:00
}