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

200 lines
8.3 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;
2019-09-26 11:54:53 +00:00
using ASC.Core.Configuration;
using ASC.Notify.Model;
using ASC.Notify.Patterns;
using ASC.Notify.Recipients;
2019-09-24 10:32:12 +00:00
using ASC.Web.Core.Users;
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
{
public class StudioNotifyServiceSender
{
2019-09-26 11:54:53 +00:00
private static string EMailSenderName { get { return Constants.NotifyEMailSenderSysName; } }
2019-09-09 12:56:33 +00:00
public IServiceProvider ServiceProvider { get; }
2019-09-23 12:20:08 +00:00
public 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;
}
public void OnMessage(NotifyItem item)
{
2019-09-09 12:56:33 +00:00
using var scope = ServiceProvider.CreateScope();
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
var userManager = scope.ServiceProvider.GetService<UserManager>();
var securityContext = scope.ServiceProvider.GetService<SecurityContext>();
var authContext = scope.ServiceProvider.GetService<AuthContext>();
var studioNotifyHelper = scope.ServiceProvider.GetService<StudioNotifyHelper>();
2019-09-24 10:32:12 +00:00
var displayUserSettings = scope.ServiceProvider.GetService<DisplayUserSettings>();
2019-09-09 12:56:33 +00:00
tenantManager.SetCurrentTenant(item.TenantId);
CultureInfo culture = null;
2019-10-11 08:31:21 +00:00
var client = WorkContext.NotifyContext.NotifyService.RegisterClient(studioNotifyHelper.NotifySource, scope);
2019-09-09 12:56:33 +00:00
var tenant = tenantManager.GetCurrentTenant(false);
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))
{
securityContext.AuthenticateMe(Guid.Parse(item.UserId));
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();
var tenantExtra = scope.ServiceProvider.GetService<TenantExtra>();
var coreBaseSettings = scope.ServiceProvider.GetService<CoreBaseSettings>();
2019-09-23 12:20:08 +00:00
if (Configuration["core:notify:tariff"] != "false")
{
2019-09-09 12:56:33 +00:00
if (tenantExtra.Enterprise)
{
2019-10-11 15:03:03 +00:00
WorkContext.RegisterSendMethod(SendEnterpriseTariffLetters, cron);
}
2019-09-09 12:56:33 +00:00
else if (tenantExtra.Opensource)
{
2019-10-11 15:03:03 +00:00
WorkContext.RegisterSendMethod(SendOpensourceTariffLetters, cron);
}
2019-09-09 12:56:33 +00:00
else if (tenantExtra.Saas)
{
2019-09-19 11:34:54 +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);
}
}
}
2019-09-19 11:34:54 +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);
}
}
2019-11-01 08:49:08 +00:00
public static class ServiceLauncherExtension
2019-11-01 08:49:08 +00:00
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddStudioNotifyServiceSender(this DIHelper services)
2019-11-01 08:49:08 +00:00
{
services.TryAddSingleton<StudioNotifyServiceSender>();
return services
.AddStudioPeriodicNotify()
.AddStudioWhatsNewNotify()
.AddTenantManagerService()
.AddUserManagerService()
.AddSecurityContextService()
.AddAuthContextService()
.AddStudioNotifyHelperService()
.AddDisplayUserSettingsService()
.AddTenantExtraService()
.AddCoreBaseSettingsService()
;
}
}
}