DocSpace-buildtools/web/ASC.Web.Core/Notify/StudioWhatsNewNotify.cs

363 lines
18 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;
2019-11-06 15:03:09 +00:00
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-02-17 08:58:14 +00:00
using System.Threading;
2020-08-12 09:58:08 +00:00
using ASC.Common;
using ASC.Common.Logging;
using ASC.Common.Utils;
using ASC.Core;
using ASC.Core.Billing;
using ASC.Core.Tenants;
using ASC.Core.Users;
using ASC.Feed;
using ASC.Feed.Data;
using ASC.Notify.Patterns;
using ASC.Web.Core;
2019-11-06 15:03:09 +00:00
using ASC.Web.Core.PublicResources;
using ASC.Web.Core.Users;
2020-02-17 08:58:14 +00:00
using ASC.Web.Studio.Utility;
2020-08-12 09:58:08 +00:00
2019-11-06 15:03:09 +00:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace ASC.Web.Studio.Core.Notify
{
public class StudioWhatsNewNotify
2019-11-06 15:03:09 +00:00
{
2020-08-12 09:58:08 +00:00
private IServiceProvider ServiceProvider { get; }
2019-11-06 15:03:09 +00:00
public IConfiguration Confuguration { get; }
public StudioWhatsNewNotify(IServiceProvider serviceProvider, IConfiguration confuguration)
{
ServiceProvider = serviceProvider;
Confuguration = confuguration;
}
2020-07-30 13:33:54 +00:00
2019-10-11 15:03:03 +00:00
public void SendMsgWhatsNew(DateTime scheduleDate)
{
2019-11-06 15:03:09 +00:00
var log = ServiceProvider.GetService<IOptionsMonitor<ILog>>().Get("ASC.Notify");
var WebItemManager = ServiceProvider.GetService<WebItemManager>();
if (WebItemManager.GetItemsAll<IProduct>().Count == 0)
{
log.Info("No products. Return from function");
return;
}
log.Info("Start send whats new.");
var products = WebItemManager.GetItemsAll().ToDictionary(p => p.GetSysName());
2019-10-10 08:52:21 +00:00
foreach (var tenantid in GetChangedTenants(ServiceProvider.GetService<FeedAggregateDataProvider>(), scheduleDate))
{
try
2019-11-06 15:03:09 +00:00
{
using var scope = ServiceProvider.CreateScope();
2020-08-24 18:41:06 +00:00
var scopeClass = scope.ServiceProvider.GetService<StudioWhatsNewNotifyScope>();
2019-11-06 15:03:09 +00:00
2020-07-30 13:33:54 +00:00
var tenant = scopeClass.TenantManager.GetTenant(tenantid);
if (tenant == null ||
tenant.Status != TenantStatus.Active ||
2020-07-30 13:33:54 +00:00
!TimeToSendWhatsNew(scopeClass.TenantUtil.DateTimeFromUtc(tenant.TimeZone, scheduleDate)) ||
TariffState.NotPaid <= scopeClass.PaymentManager.GetTariff(tenantid).State)
{
continue;
}
2020-07-30 13:33:54 +00:00
scopeClass.TenantManager.SetCurrentTenant(tenant);
var client = WorkContext.NotifyContext.NotifyService.RegisterClient(scopeClass.StudioNotifyHelper.NotifySource, scope);
log.InfoFormat("Start send whats new in {0} ({1}).", tenant.GetTenantDomain(scopeClass.CoreSettings), tenantid);
foreach (var user in scopeClass.UserManager.GetUsers())
{
2020-07-30 13:33:54 +00:00
if (!scopeClass.StudioNotifyHelper.IsSubscribedToNotify(user, Actions.SendWhatsNew))
{
continue;
}
2020-07-30 13:33:54 +00:00
scopeClass.SecurityContext.AuthenticateMe(scopeClass.AuthManager.GetAccountByID(tenant.TenantId, user.ID));
var culture = string.IsNullOrEmpty(user.CultureName) ? tenant.GetCulture() : user.GetCulture();
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
2020-07-30 13:33:54 +00:00
var feeds = scopeClass.FeedAggregateDataProvider.GetFeeds(new FeedApiFilter
{
From = scheduleDate.Date.AddDays(-1),
To = scheduleDate.Date.AddSeconds(-1),
Max = 100,
2019-10-10 08:52:21 +00:00
});
2020-07-30 13:33:54 +00:00
var feedMinWrappers = feeds.ConvertAll(f => f.ToFeedMin(scopeClass.UserManager));
var feedMinGroupedWrappers = feedMinWrappers
.Where(f =>
(f.CreatedDate == DateTime.MaxValue || f.CreatedDate >= scheduleDate.Date.AddDays(-1)) && //'cause here may be old posts with new comments
products.ContainsKey(f.Product) &&
!f.Id.StartsWith("participant")
)
.GroupBy(f => products[f.Product]);
2019-11-06 15:03:09 +00:00
var ProjectsProductName = products["projects"]?.Name; //from ASC.Feed.Aggregator.Modules.ModulesHelper.ProjectsProductName
var activities = feedMinGroupedWrappers
.Where(f => f.Key.Name != ProjectsProductName) //not for project product
.ToDictionary(
g => g.Key.Name,
g => g.Select(f => new WhatsNewUserActivity
{
Date = f.CreatedDate,
2020-07-30 13:33:54 +00:00
UserName = f.Author != null && f.Author.UserInfo != null ? f.Author.UserInfo.DisplayUserName(scopeClass.DisplayUserSettingsHelper) : string.Empty,
UserAbsoluteURL = f.Author != null && f.Author.UserInfo != null ? scopeClass.CommonLinkUtility.GetFullAbsolutePath(f.Author.UserInfo.GetUserProfilePageURL(scopeClass.CommonLinkUtility)) : string.Empty,
Title = HtmlUtil.GetText(f.Title, 512),
2020-07-30 13:33:54 +00:00
URL = scopeClass.CommonLinkUtility.GetFullAbsolutePath(f.ItemUrl),
BreadCrumbs = new string[0],
Action = getWhatsNewActionText(f)
}).ToList());
var projectActivities = feedMinGroupedWrappers
.Where(f => f.Key.Name == ProjectsProductName) // for project product
.SelectMany(f => f);
2019-08-15 13:16:39 +00:00
var projectActivitiesWithoutBreadCrumbs = projectActivities.Where(p => string.IsNullOrEmpty(p.ExtraLocation));
var whatsNewUserActivityGroupByPrjs = new List<WhatsNewUserActivity>();
foreach (var prawbc in projectActivitiesWithoutBreadCrumbs)
{
whatsNewUserActivityGroupByPrjs.Add(
new WhatsNewUserActivity
{
Date = prawbc.CreatedDate,
2020-07-30 13:33:54 +00:00
UserName = prawbc.Author != null && prawbc.Author.UserInfo != null ? prawbc.Author.UserInfo.DisplayUserName(scopeClass.DisplayUserSettingsHelper) : string.Empty,
UserAbsoluteURL = prawbc.Author != null && prawbc.Author.UserInfo != null ? scopeClass.CommonLinkUtility.GetFullAbsolutePath(prawbc.Author.UserInfo.GetUserProfilePageURL(scopeClass.CommonLinkUtility)) : string.Empty,
Title = HtmlUtil.GetText(prawbc.Title, 512),
2020-07-30 13:33:54 +00:00
URL = scopeClass.CommonLinkUtility.GetFullAbsolutePath(prawbc.ItemUrl),
BreadCrumbs = new string[0],
Action = getWhatsNewActionText(prawbc)
});
}
2019-08-15 13:16:39 +00:00
var groupByPrjs = projectActivities.Where(p => !string.IsNullOrEmpty(p.ExtraLocation)).GroupBy(f => f.ExtraLocation);
foreach (var gr in groupByPrjs)
{
var grlist = gr.ToList();
for (var i = 0; i < grlist.Count(); i++)
{
var ls = grlist[i];
whatsNewUserActivityGroupByPrjs.Add(
new WhatsNewUserActivity
{
Date = ls.CreatedDate,
2020-07-30 13:33:54 +00:00
UserName = ls.Author != null && ls.Author.UserInfo != null ? ls.Author.UserInfo.DisplayUserName(scopeClass.DisplayUserSettingsHelper) : string.Empty,
UserAbsoluteURL = ls.Author != null && ls.Author.UserInfo != null ? scopeClass.CommonLinkUtility.GetFullAbsolutePath(ls.Author.UserInfo.GetUserProfilePageURL(scopeClass.CommonLinkUtility)) : string.Empty,
Title = HtmlUtil.GetText(ls.Title, 512),
2020-07-30 13:33:54 +00:00
URL = scopeClass.CommonLinkUtility.GetFullAbsolutePath(ls.ItemUrl),
2019-08-15 12:04:42 +00:00
BreadCrumbs = i == 0 ? new string[1] { gr.Key } : new string[0],
Action = getWhatsNewActionText(ls)
});
}
}
if (whatsNewUserActivityGroupByPrjs.Count > 0)
{
activities.Add(ProjectsProductName, whatsNewUserActivityGroupByPrjs);
2019-11-06 15:03:09 +00:00
}
2019-08-30 12:40:57 +00:00
if (activities.Count > 0)
{
log.InfoFormat("Send whats new to {0}", user.Email);
client.SendNoticeAsync(
2019-08-01 11:44:50 +00:00
Actions.SendWhatsNew, null, user,
new TagValue(Tags.Activities, activities),
new TagValue(Tags.Date, DateToString(scheduleDate.AddDays(-1), culture)),
new TagValue(CommonTags.Priority, 1)
);
}
}
}
catch (Exception error)
{
log.Error(error);
}
}
}
2019-11-06 15:03:09 +00:00
private static string getWhatsNewActionText(FeedMin feed)
2019-08-15 12:04:42 +00:00
{
if (feed.Module == ASC.Feed.Constants.BookmarksModule)
return WebstudioNotifyPatternResource.ActionCreateBookmark;
else if (feed.Module == ASC.Feed.Constants.BlogsModule)
return WebstudioNotifyPatternResource.ActionCreateBlog;
else if (feed.Module == ASC.Feed.Constants.ForumsModule)
{
if (feed.Item == "forumTopic")
return WebstudioNotifyPatternResource.ActionCreateForum;
if (feed.Item == "forumPost")
return WebstudioNotifyPatternResource.ActionCreateForumPost;
if (feed.Item == "forumPoll")
return WebstudioNotifyPatternResource.ActionCreateForumPoll;
}
else if (feed.Module == ASC.Feed.Constants.EventsModule)
return WebstudioNotifyPatternResource.ActionCreateEvent;
else if (feed.Module == ASC.Feed.Constants.ProjectsModule)
return WebstudioNotifyPatternResource.ActionCreateProject;
else if (feed.Module == ASC.Feed.Constants.MilestonesModule)
return WebstudioNotifyPatternResource.ActionCreateMilestone;
else if (feed.Module == ASC.Feed.Constants.DiscussionsModule)
return WebstudioNotifyPatternResource.ActionCreateDiscussion;
else if (feed.Module == ASC.Feed.Constants.TasksModule)
return WebstudioNotifyPatternResource.ActionCreateTask;
else if (feed.Module == ASC.Feed.Constants.CommentsModule)
return WebstudioNotifyPatternResource.ActionCreateComment;
else if (feed.Module == ASC.Feed.Constants.CrmTasksModule)
return WebstudioNotifyPatternResource.ActionCreateTask;
else if (feed.Module == ASC.Feed.Constants.ContactsModule)
return WebstudioNotifyPatternResource.ActionCreateContact;
else if (feed.Module == ASC.Feed.Constants.DealsModule)
return WebstudioNotifyPatternResource.ActionCreateDeal;
else if (feed.Module == ASC.Feed.Constants.CasesModule)
return WebstudioNotifyPatternResource.ActionCreateCase;
else if (feed.Module == ASC.Feed.Constants.FilesModule)
return WebstudioNotifyPatternResource.ActionCreateFile;
else if (feed.Module == ASC.Feed.Constants.FoldersModule)
return WebstudioNotifyPatternResource.ActionCreateFolder;
return "";
}
2019-10-10 08:52:21 +00:00
private static IEnumerable<int> GetChangedTenants(FeedAggregateDataProvider feedAggregateDataProvider, DateTime date)
{
2019-10-10 08:52:21 +00:00
return feedAggregateDataProvider.GetTenants(new TimeInterval(date.Date.AddDays(-1), date.Date.AddSeconds(-1)));
}
2019-09-23 12:20:08 +00:00
private bool TimeToSendWhatsNew(DateTime currentTime)
{
var hourToSend = 7;
2019-09-23 12:20:08 +00:00
if (!string.IsNullOrEmpty(Confuguration["web:whatsnew-time"]))
{
2019-09-23 12:20:08 +00:00
if (int.TryParse(Confuguration["web:whatsnew-time"], out var hour))
{
hourToSend = hour;
}
}
return currentTime.Hour == hourToSend;
}
private static string DateToString(DateTime d, CultureInfo c)
{
return d.ToString(c.TwoLetterISOLanguageName == "ru" ? "d MMMM" : "M", c);
}
class WhatsNewUserActivity
{
public IList<string> BreadCrumbs { get; set; }
public string Title { get; set; }
public string URL { get; set; }
public string UserName { get; set; }
public string UserAbsoluteURL { get; set; }
public DateTime Date { get; set; }
public string Action { get; set; }
}
2020-08-24 18:41:06 +00:00
}
2020-07-30 13:33:54 +00:00
2020-08-24 18:41:06 +00:00
public class StudioWhatsNewNotifyScope
{
internal TenantManager TenantManager { get; }
internal PaymentManager PaymentManager { get; }
internal TenantUtil TenantUtil { get; }
internal StudioNotifyHelper StudioNotifyHelper { get; }
internal UserManager UserManager { get; }
internal SecurityContext SecurityContext { get; }
internal AuthContext AuthContext { get; }
internal AuthManager AuthManager { get; }
internal CommonLinkUtility CommonLinkUtility { get; }
internal DisplayUserSettingsHelper DisplayUserSettingsHelper { get; }
internal FeedAggregateDataProvider FeedAggregateDataProvider { get; }
internal CoreSettings CoreSettings { get; }
2020-07-30 13:33:54 +00:00
2020-08-24 18:41:06 +00:00
public StudioWhatsNewNotifyScope(TenantManager tenantManager,
PaymentManager paymentManager,
TenantUtil tenantUtil,
StudioNotifyHelper studioNotifyHelper,
UserManager userManager,
SecurityContext securityContext,
AuthContext authContext,
AuthManager authManager,
CommonLinkUtility commonLinkUtility,
DisplayUserSettingsHelper displayUserSettingsHelper,
FeedAggregateDataProvider feedAggregateDataProvider,
CoreSettings coreSettings)
{
TenantManager = tenantManager;
PaymentManager = paymentManager;
TenantUtil = tenantUtil;
StudioNotifyHelper = studioNotifyHelper;
UserManager = userManager;
SecurityContext = securityContext;
AuthContext = authContext;
AuthManager = authManager;
CommonLinkUtility = commonLinkUtility;
DisplayUserSettingsHelper = displayUserSettingsHelper;
FeedAggregateDataProvider = feedAggregateDataProvider;
CoreSettings = coreSettings;
}
2019-11-06 15:03:09 +00:00
}
public static class StudioWhatsNewNotifyExtension
2019-11-06 15:03:09 +00:00
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddStudioWhatsNewNotify(this DIHelper services)
2019-11-06 15:03:09 +00:00
{
2020-08-24 18:41:06 +00:00
services.TryAddSingleton<StudioWhatsNewNotify>();
services.TryAddScoped<StudioWhatsNewNotifyScope>();
2019-11-06 15:03:09 +00:00
return services
.AddWebItemManager()
.AddFeedAggregateDataProvider()
.AddTenantManagerService()
.AddPaymentManagerService()
.AddStudioNotifyHelperService()
.AddUserManagerService()
.AddSecurityContextService()
.AddAuthContextService()
.AddAuthManager()
.AddTenantUtilService()
.AddCommonLinkUtilityService()
.AddDisplayUserSettingsService()
.AddCoreSettingsService()
;
}
}
}