DocSpace-buildtools/web/ASC.Web.Core/Notify/StudioNotifyServiceHelper.cs
2019-08-15 15:04:42 +03:00

104 lines
3.9 KiB
C#

using System.Linq;
using ASC.Common.Caching;
using ASC.Core;
using ASC.Notify.Model;
using ASC.Notify.Patterns;
using ASC.Notify.Recipients;
using ASC.Web.Studio.Core.Notify;
namespace ASC.Web.Core.Notify
{
public class StudioNotifyServiceHelper
{
private readonly ICacheNotify<NotifyItem> cache;
public StudioNotifyServiceHelper()
{
cache = new KafkaCache<NotifyItem>();
}
public void SendNoticeToAsync(INotifyAction action, IRecipient[] recipients, string[] senderNames, params ITagValue[] args)
{
SendNoticeToAsync(action, null, recipients, senderNames, false, args);
}
public void SendNoticeToAsync(INotifyAction action, string objectID, IRecipient[] recipients, string[] senderNames, params ITagValue[] args)
{
SendNoticeToAsync(action, objectID, recipients, senderNames, false, args);
}
public void SendNoticeToAsync(INotifyAction action, string objectID, IRecipient[] recipients, params ITagValue[] args)
{
SendNoticeToAsync(action, objectID, recipients, null, false, args);
}
public void SendNoticeToAsync(INotifyAction action, string objectID, IRecipient[] recipients, bool checkSubscription, params ITagValue[] args)
{
SendNoticeToAsync(action, objectID, recipients, null, checkSubscription, args);
}
public void SendNoticeAsync(INotifyAction action, string objectID, IRecipient recipient, params ITagValue[] args)
{
SendNoticeToAsync(action, objectID, new[] { recipient }, null, false, args);
}
public void SendNoticeAsync(int tenantId, INotifyAction action, string objectID, params ITagValue[] args)
{
var subscriptionSource = StudioNotifyHelper.NotifySource.GetSubscriptionProvider();
var recipients = subscriptionSource.GetRecipients(tenantId, action, objectID);
SendNoticeToAsync(action, objectID, recipients, null, false, args);
}
public void SendNoticeAsync(INotifyAction action, string objectID, IRecipient recipient, bool checkSubscription, params ITagValue[] args)
{
SendNoticeToAsync(action, objectID, new[] { recipient }, null, checkSubscription, args);
}
public void SendNoticeToAsync(INotifyAction action, string objectID, IRecipient[] recipients, string[] senderNames, bool checkSubsciption, params ITagValue[] args)
{
var item = new NotifyItem
{
TenantId = CoreContext.TenantManager.GetCurrentTenant().TenantId,
UserId = SecurityContext.CurrentAccount.ID.ToString(),
Action = (NotifyAction)action,
CheckSubsciption = checkSubsciption
};
if (objectID != null)
{
item.ObjectID = objectID;
}
if (recipients != null)
{
foreach (var r in recipients)
{
var recipient = new Recipient { ID = r.ID, Name = r.Name };
if (r is IDirectRecipient d)
{
recipient.Addresses.AddRange(d.Addresses);
recipient.CheckActivation = d.CheckActivation;
}
if (r is IRecipientsGroup g)
{
recipient.IsGroup = true;
}
item.Recipients.Add(recipient);
}
}
if (senderNames != null)
{
item.SenderNames.AddRange(senderNames);
}
item.Tags.AddRange(args.Select(r => new Tag { Tag_ = r.Tag, Value = r.Value.ToString() }));
cache.Publish(item, CacheNotifyAction.Any);
}
}
}