DocSpace-client/web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs

227 lines
9.4 KiB
C#
Raw Normal View History

/*
*
* (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;
using System.Globalization;
using System.Linq;
using System.Threading;
2020-02-17 08:58:14 +00:00
using ASC.Common;
using ASC.Common.Caching;
using ASC.Core;
2020-10-30 14:57:31 +00:00
using ASC.Core.Common;
2019-09-26 11:54:53 +00:00
using ASC.Core.Configuration;
using ASC.Notify.Model;
using ASC.Notify.Patterns;
using ASC.Notify.Recipients;
using ASC.Web.Studio.Utility;
2020-02-17 08:58:14 +00:00
2019-09-23 12:20:08 +00:00
using Microsoft.Extensions.Configuration;
2019-09-09 12:56:33 +00:00
using Microsoft.Extensions.DependencyInjection;
namespace ASC.Web.Studio.Core.Notify
{
2020-10-28 12:26:27 +00:00
[Singletone(Additional = typeof(ServiceLauncherExtension))]
public class StudioNotifyServiceSender
{
2019-09-26 11:54:53 +00:00
private static string EMailSenderName { get { return Constants.NotifyEMailSenderSysName; } }
2020-08-12 09:58:08 +00:00
private IServiceProvider ServiceProvider { get; }
private IConfiguration Configuration { get; }
2019-09-09 12:56:33 +00:00
2019-10-11 15:03:03 +00:00
public StudioNotifyServiceSender(IServiceProvider serviceProvider, IConfiguration configuration, ICacheNotify<NotifyItem> cache)
{
cache.Subscribe(OnMessage, CacheNotifyAction.Any);
2019-09-09 12:56:33 +00:00
ServiceProvider = serviceProvider;
2019-09-23 12:20:08 +00:00
Configuration = configuration;
}
2020-10-12 16:23:15 +00:00
public void OnMessage(NotifyItem item)
{
2019-09-09 12:56:33 +00:00
using var scope = ServiceProvider.CreateScope();
2020-10-30 14:57:31 +00:00
var commonLinkUtilitySettings = scope.ServiceProvider.GetService<CommonLinkUtilitySettings>();
commonLinkUtilitySettings.ServerUri = item.BaseUrl;
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<StudioNotifyServiceSenderScope>();
2020-09-07 12:01:15 +00:00
var (tenantManager, userManager, securityContext, studioNotifyHelper, _, _) = scopeClass;
2020-10-12 19:39:23 +00:00
tenantManager.SetCurrentTenant(item.TenantId);
CultureInfo culture = null;
2020-08-31 08:18:07 +00:00
var client = WorkContext.NotifyContext.NotifyService.RegisterClient(studioNotifyHelper.NotifySource, scope);
2019-09-09 12:56:33 +00:00
2020-08-31 08:18:07 +00:00
var tenant = tenantManager.GetCurrentTenant(false);
2019-09-09 12:56:33 +00:00
if (tenant != null)
{
culture = tenant.GetCulture();
}
2019-09-26 11:54:53 +00:00
if (Guid.TryParse(item.UserId, out var userId) && !userId.Equals(Constants.Guest.ID) && !userId.Equals(Guid.Empty))
{
2021-08-18 14:04:16 +00:00
securityContext.AuthenticateMeWithoutCookie(Guid.Parse(item.UserId));
2020-08-31 08:18:07 +00:00
var user = userManager.GetUsers(userId);
2019-09-26 11:54:53 +00:00
if (!string.IsNullOrEmpty(user.CultureName))
{
culture = CultureInfo.GetCultureInfo(user.CultureName);
}
}
if (culture != null && !Equals(Thread.CurrentThread.CurrentCulture, culture))
{
Thread.CurrentThread.CurrentCulture = culture;
}
if (culture != null && !Equals(Thread.CurrentThread.CurrentUICulture, culture))
{
Thread.CurrentThread.CurrentUICulture = culture;
}
client.SendNoticeToAsync(
(NotifyAction)item.Action,
item.ObjectID,
2019-08-15 12:04:42 +00:00
item.Recipients?.Select(r => r.IsGroup ? new RecipientsGroup(r.ID, r.Name) : (IRecipient)new DirectRecipient(r.ID, r.Name, r.Addresses.ToArray(), r.CheckActivation)).ToArray(),
item.SenderNames.Any() ? item.SenderNames.ToArray() : null,
item.CheckSubsciption,
item.Tags.Select(r => new TagValue(r.Tag_, r.Value)).ToArray());
}
public void RegisterSendMethod()
{
2019-09-23 12:20:08 +00:00
var cron = Configuration["core:notify:cron"] ?? "0 0 5 ? * *"; // 5am every day
2019-09-19 11:34:54 +00:00
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<StudioNotifyServiceSenderScope>();
2020-10-12 16:23:15 +00:00
var (_, _, _, _, tenantExtra, coreBaseSettings) = scopeClass;
2019-09-23 12:20:08 +00:00
if (Configuration["core:notify:tariff"] != "false")
{
2020-08-31 08:18:07 +00:00
if (tenantExtra.Enterprise)
{
2019-10-11 15:03:03 +00:00
WorkContext.RegisterSendMethod(SendEnterpriseTariffLetters, cron);
}
2020-08-31 08:18:07 +00:00
else if (tenantExtra.Opensource)
{
2019-10-11 15:03:03 +00:00
WorkContext.RegisterSendMethod(SendOpensourceTariffLetters, cron);
}
2020-08-31 08:18:07 +00:00
else if (tenantExtra.Saas)
{
2020-08-31 08:18:07 +00:00
if (coreBaseSettings.Personal)
{
2019-10-11 15:03:03 +00:00
WorkContext.RegisterSendMethod(SendLettersPersonal, cron);
}
else
{
2019-10-11 15:03:03 +00:00
WorkContext.RegisterSendMethod(SendSaasTariffLetters, cron);
}
}
}
2020-08-31 08:18:07 +00:00
if (!coreBaseSettings.Personal)
{
2019-10-11 15:03:03 +00:00
WorkContext.RegisterSendMethod(SendMsgWhatsNew, "0 0 * ? * *"); // every hour
}
}
public void SendSaasTariffLetters(DateTime scheduleDate)
{
2019-10-11 08:31:21 +00:00
//remove client
2019-11-01 08:49:08 +00:00
using var scope = ServiceProvider.CreateScope();
scope.ServiceProvider.GetService<StudioPeriodicNotify>().SendSaasLetters(EMailSenderName, scheduleDate);
}
public void SendEnterpriseTariffLetters(DateTime scheduleDate)
{
2019-11-01 08:49:08 +00:00
using var scope = ServiceProvider.CreateScope();
scope.ServiceProvider.GetService<StudioPeriodicNotify>().SendEnterpriseLetters(EMailSenderName, scheduleDate);
}
public void SendOpensourceTariffLetters(DateTime scheduleDate)
{
2019-11-01 08:49:08 +00:00
using var scope = ServiceProvider.CreateScope();
scope.ServiceProvider.GetService<StudioPeriodicNotify>().SendOpensourceLetters(EMailSenderName, scheduleDate);
}
public void SendLettersPersonal(DateTime scheduleDate)
{
2019-11-01 08:49:08 +00:00
using var scope = ServiceProvider.CreateScope();
scope.ServiceProvider.GetService<StudioPeriodicNotify>().SendPersonalLetters(EMailSenderName, scheduleDate);
}
public void SendMsgWhatsNew(DateTime scheduleDate)
{
2019-09-09 12:56:33 +00:00
using var scope = ServiceProvider.CreateScope();
2019-10-11 15:03:03 +00:00
scope.ServiceProvider.GetService<StudioWhatsNewNotify>().SendMsgWhatsNew(scheduleDate);
}
2020-08-24 18:41:06 +00:00
}
2020-07-30 13:33:54 +00:00
2020-10-28 12:26:27 +00:00
[Scope]
2020-08-24 18:41:06 +00:00
public class StudioNotifyServiceSenderScope
{
2020-08-31 08:18:07 +00:00
private TenantManager TenantManager { get; }
private UserManager UserManager { get; }
private SecurityContext SecurityContext { get; }
private StudioNotifyHelper StudioNotifyHelper { get; }
private TenantExtra TenantExtra { get; }
private CoreBaseSettings CoreBaseSettings { get; }
2020-08-24 18:41:06 +00:00
public StudioNotifyServiceSenderScope(TenantManager tenantManager,
UserManager userManager,
SecurityContext securityContext,
StudioNotifyHelper studioNotifyHelper,
TenantExtra tenantExtra,
CoreBaseSettings coreBaseSettings)
2020-07-30 13:33:54 +00:00
{
2020-08-24 18:41:06 +00:00
TenantManager = tenantManager;
UserManager = userManager;
SecurityContext = securityContext;
StudioNotifyHelper = studioNotifyHelper;
TenantExtra = tenantExtra;
CoreBaseSettings = coreBaseSettings;
2020-07-30 13:33:54 +00:00
}
2020-08-31 08:18:07 +00:00
public void Deconstruct(out TenantManager tenantManager,
out UserManager userManager,
out SecurityContext securityContext,
out StudioNotifyHelper studioNotifyHelper,
2020-10-12 16:23:15 +00:00
out TenantExtra tenantExtra,
2020-08-31 08:18:07 +00:00
out CoreBaseSettings coreBaseSettings)
{
tenantManager = TenantManager;
userManager = UserManager;
securityContext = SecurityContext;
studioNotifyHelper = StudioNotifyHelper;
tenantExtra = TenantExtra;
coreBaseSettings = CoreBaseSettings;
}
}
2019-11-01 08:49:08 +00:00
public static class ServiceLauncherExtension
2019-11-01 08:49:08 +00:00
{
2020-10-28 12:26:27 +00:00
public static void Register(DIHelper services)
2019-11-01 08:49:08 +00:00
{
2020-10-28 12:26:27 +00:00
services.TryAdd<StudioNotifyServiceSenderScope>();
services.TryAdd<StudioPeriodicNotify>();
services.TryAdd<StudioWhatsNewNotify>();
2019-11-01 08:49:08 +00:00
}
}
}