DocSpace-buildtools/common/ASC.Core.Common/Notify/PushSenderSink.cs

171 lines
6.8 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2022-06-16 13:07:49 +00:00
using ASC.Core.Common.Notify.Push.Dao;
2022-01-31 13:56:30 +00:00
using Constants = ASC.Core.Configuration.Constants;
2022-02-15 11:52:43 +00:00
namespace ASC.Core.Common.Notify;
class PushSenderSink : Sink
2019-05-15 14:56:09 +00:00
{
2022-06-16 13:07:49 +00:00
private static readonly string _senderName = Constants.NotifyPushSenderSysName;
private readonly INotifySender _sender;
2019-11-06 15:03:09 +00:00
2022-06-16 13:07:49 +00:00
public PushSenderSink(INotifySender sender, IServiceProvider serviceProvider)
2022-02-15 11:52:43 +00:00
{
2022-06-16 13:07:49 +00:00
_sender = sender ?? throw new ArgumentNullException(nameof(sender));
2022-02-15 11:52:43 +00:00
_serviceProvider = serviceProvider;
}
2019-11-06 15:03:09 +00:00
2022-02-15 11:52:43 +00:00
private readonly IServiceProvider _serviceProvider;
2019-11-06 15:03:09 +00:00
2022-10-27 10:49:42 +00:00
public override async Task<SendResponse> ProcessMessage(INoticeMessage message)
2022-02-15 11:52:43 +00:00
{
try
2019-05-15 14:56:09 +00:00
{
2022-06-16 13:07:49 +00:00
var result = SendResult.OK;
await using var scope = _serviceProvider.CreateAsyncScope();
2019-09-18 12:14:15 +00:00
2023-03-20 16:16:00 +00:00
var m = await scope.ServiceProvider.GetRequiredService<PushSenderSinkMessageCreator>().CreateNotifyMessageAsync(message, _senderName);
2022-06-27 10:36:38 +00:00
if (string.IsNullOrEmpty(m.Reciever))
2022-02-15 11:52:43 +00:00
{
2022-06-16 13:07:49 +00:00
result = SendResult.IncorrectRecipient;
}
else
2022-02-15 11:52:43 +00:00
{
2023-03-17 08:32:55 +00:00
await _sender.SendAsync(m);
2019-05-15 14:56:09 +00:00
}
2022-06-16 13:07:49 +00:00
return new SendResponse(message, Constants.NotifyPushSenderSysName, result);
2022-02-15 11:52:43 +00:00
}
catch (Exception error)
2019-05-15 14:56:09 +00:00
{
2022-02-15 11:52:43 +00:00
return new SendResponse(message, Constants.NotifyPushSenderSysName, error);
2019-05-15 14:56:09 +00:00
}
}
2022-02-15 11:52:43 +00:00
private T GetTagValue<T>(INoticeMessage message, string tagName)
{
var tag = message.Arguments.FirstOrDefault(arg => arg.Tag == tagName);
return tag != null ? (T)tag.Value : default;
}
2019-05-15 14:56:09 +00:00
}
public class LowerCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name) =>
name.ToLower();
}
2022-06-27 10:36:38 +00:00
[Scope]
public class PushSenderSinkMessageCreator : SinkMessageCreator
{
private readonly UserManager _userManager;
private readonly TenantManager _tenantManager;
public PushSenderSinkMessageCreator(UserManager userManager, TenantManager tenantManager)
{
_tenantManager = tenantManager;
_userManager = userManager;
}
2023-03-20 16:16:00 +00:00
public override async Task<NotifyMessage> CreateNotifyMessageAsync(INoticeMessage message, string senderName)
2022-06-27 10:36:38 +00:00
{
2023-03-20 16:16:00 +00:00
var tenant = await _tenantManager.GetCurrentTenantAsync(false);
2022-06-27 10:36:38 +00:00
if (tenant == null)
{
2023-03-20 16:16:00 +00:00
await _tenantManager.SetCurrentTenantAsync(Tenant.DefaultTenant);
tenant = await _tenantManager.GetCurrentTenantAsync(false);
2022-06-27 10:36:38 +00:00
}
2023-03-24 07:53:49 +00:00
var user = await _userManager.GetUsersAsync(new Guid(message.Recipient.ID));
2022-06-27 10:36:38 +00:00
var username = user.UserName;
var fromTag = message.Arguments.FirstOrDefault(x => x.Tag.Equals("MessageFrom"));
var productID = message.Arguments.FirstOrDefault(x => x.Tag.Equals("__ProductID"));
var originalUrl = message.Arguments.FirstOrDefault(x => x.Tag.Equals("DocumentURL"));
2022-08-15 11:27:54 +00:00
var folderId = message.Arguments.FirstOrDefault(x => x.Tag.Equals("FolderID"));
2022-06-27 10:36:38 +00:00
var rootFolderId = message.Arguments.FirstOrDefault(x => x.Tag.Equals("FolderParentId"));
var rootFolderType = message.Arguments.FirstOrDefault(x => x.Tag.Equals("FolderRootFolderType"));
var notifyData = new NotifyData()
{
Email = user.Email,
2023-03-20 16:16:00 +00:00
Portal = (await _tenantManager.GetCurrentTenantAsync()).TrustedDomains.FirstOrDefault(),
2022-06-27 10:36:38 +00:00
OriginalUrl = originalUrl != null && originalUrl.Value != null ? originalUrl.Value.ToString() : "",
Folder = new NotifyFolderData
{
Id = folderId != null && folderId.Value != null ? folderId.Value.ToString() : "",
ParentId = rootFolderId != null && rootFolderId.Value != null ? rootFolderId.Value.ToString() : "",
RootFolderType = rootFolderType != null && rootFolderType.Value != null ? (int)rootFolderType.Value : 0
},
};
var msg = (NoticeMessage)message;
if (msg.ObjectID.StartsWith("file_"))
{
var documentTitle = message.Arguments.FirstOrDefault(x => x.Tag.Equals("DocumentTitle"));
var documentExtension = message.Arguments.FirstOrDefault(x => x.Tag.Equals("DocumentExtension"));
notifyData.File = new NotifyFileData()
{
Id = msg.ObjectID.Substring(5),
Title = documentTitle != null && documentTitle.Value != null ? documentTitle.Value.ToString() : "",
Extension = documentExtension != null && documentExtension.Value != null ? documentExtension.Value.ToString() : ""
};
}
var serializeOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = new LowerCaseNamingPolicy(),
WriteIndented = true
};
var jsonNotifyData = JsonSerializer.Serialize(notifyData, serializeOptions);
2022-06-27 10:36:38 +00:00
var m = new NotifyMessage
{
2022-07-05 12:55:46 +00:00
TenantId = tenant.Id,
2022-06-27 10:36:38 +00:00
Reciever = username,
Subject = fromTag != null && fromTag.Value != null ? fromTag.Value.ToString() : message.Subject,
ContentType = message.ContentType,
Content = message.Body,
Sender = Constants.NotifyPushSenderSysName,
CreationDate = DateTime.UtcNow,
ProductID = fromTag != null && fromTag.Value != null ? productID.Value.ToString() : null,
Data = jsonNotifyData
};
return m;
}
}