2019-05-15 14:56:09 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (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-08-15 12:04:42 +00:00
|
|
|
using System.Collections.Generic;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
using ASC.Notify.Recipients;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ASC.Notify.Model
|
|
|
|
{
|
|
|
|
public class TopSubscriptionProvider : ISubscriptionProvider
|
|
|
|
{
|
2022-01-19 10:24:11 +00:00
|
|
|
private readonly string[] defaultSenderMethods = Array.Empty<string>();
|
2019-05-15 14:56:09 +00:00
|
|
|
private readonly ISubscriptionProvider subscriptionProvider;
|
|
|
|
private readonly IRecipientProvider recipientProvider;
|
|
|
|
|
|
|
|
|
|
|
|
public TopSubscriptionProvider(IRecipientProvider recipientProvider, ISubscriptionProvider directSubscriptionProvider)
|
2019-08-15 13:20:23 +00:00
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
this.recipientProvider = recipientProvider ?? throw new ArgumentNullException(nameof(recipientProvider));
|
|
|
|
subscriptionProvider = directSubscriptionProvider ?? throw new ArgumentNullException(nameof(directSubscriptionProvider));
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public TopSubscriptionProvider(IRecipientProvider recipientProvider, ISubscriptionProvider directSubscriptionProvider, string[] defaultSenderMethods)
|
|
|
|
: this(recipientProvider, directSubscriptionProvider)
|
|
|
|
{
|
|
|
|
this.defaultSenderMethods = defaultSenderMethods;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public virtual string[] GetSubscriptionMethod(INotifyAction action, IRecipient recipient)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
|
|
|
if (recipient == null) throw new ArgumentNullException(nameof(recipient));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
var senders = subscriptionProvider.GetSubscriptionMethod(action, recipient);
|
2019-05-15 14:56:09 +00:00
|
|
|
if (senders == null || senders.Length == 0)
|
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
var parents = WalkUp(recipient);
|
2019-05-15 14:56:09 +00:00
|
|
|
foreach (var parent in parents)
|
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
senders = subscriptionProvider.GetSubscriptionMethod(action, parent);
|
2019-05-15 14:56:09 +00:00
|
|
|
if (senders != null && senders.Length != 0) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return senders != null && 0 < senders.Length ? senders : defaultSenderMethods;
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public virtual IRecipient[] GetRecipients(INotifyAction action, string objectID)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
var recipents = new List<IRecipient>(5);
|
2019-09-12 11:34:58 +00:00
|
|
|
var directRecipients = subscriptionProvider.GetRecipients(action, objectID) ?? new IRecipient[0];
|
2019-05-15 14:56:09 +00:00
|
|
|
recipents.AddRange(directRecipients);
|
|
|
|
return recipents.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool IsUnsubscribe(IDirectRecipient recipient, INotifyAction action, string objectID)
|
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
|
|
|
if (recipient == null) throw new ArgumentNullException(nameof(recipient));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
return subscriptionProvider.IsUnsubscribe(recipient, action, objectID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void Subscribe(INotifyAction action, string objectID, IRecipient recipient)
|
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
|
|
|
if (recipient == null) throw new ArgumentNullException(nameof(recipient));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
subscriptionProvider.Subscribe(action, objectID, recipient);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void UnSubscribe(INotifyAction action, string objectID, IRecipient recipient)
|
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
|
|
|
if (recipient == null) throw new ArgumentNullException(nameof(recipient));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
subscriptionProvider.UnSubscribe(action, objectID, recipient);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnSubscribe(INotifyAction action, string objectID)
|
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
subscriptionProvider.UnSubscribe(action, objectID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnSubscribe(INotifyAction action)
|
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
subscriptionProvider.UnSubscribe(action);
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public virtual void UnSubscribe(INotifyAction action, IRecipient recipient)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
var objects = GetSubscriptions(action, recipient);
|
2019-08-15 13:03:57 +00:00
|
|
|
foreach (var objectID in objects)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
subscriptionProvider.UnSubscribe(action, objectID, recipient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void UpdateSubscriptionMethod(INotifyAction action, IRecipient recipient, params string[] senderNames)
|
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
|
|
|
if (recipient == null) throw new ArgumentNullException(nameof(recipient));
|
|
|
|
if (senderNames == null) throw new ArgumentNullException(nameof(senderNames));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
subscriptionProvider.UpdateSubscriptionMethod(action, recipient, senderNames);
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public virtual object GetSubscriptionRecord(INotifyAction action, IRecipient recipient, string objectID)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (recipient == null) throw new ArgumentNullException(nameof(recipient));
|
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
var subscriptionRecord = subscriptionProvider.GetSubscriptionRecord(action, recipient, objectID);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
if (subscriptionRecord != null) return subscriptionRecord;
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
var parents = WalkUp(recipient);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
foreach (var parent in parents)
|
|
|
|
{
|
2019-09-12 11:34:58 +00:00
|
|
|
subscriptionRecord = subscriptionProvider.GetSubscriptionRecord(action, parent, objectID);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
if (subscriptionRecord != null) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return subscriptionRecord;
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
public virtual string[] GetSubscriptions(INotifyAction action, IRecipient recipient, bool checkSubscription = true)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2022-01-24 10:49:00 +00:00
|
|
|
if (recipient == null) throw new ArgumentNullException(nameof(recipient));
|
|
|
|
if (action == null) throw new ArgumentNullException(nameof(action));
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
var objects = new List<string>();
|
2022-01-19 10:24:11 +00:00
|
|
|
var direct = subscriptionProvider.GetSubscriptions(action, recipient, checkSubscription) ?? Array.Empty<string>();
|
2019-05-15 14:56:09 +00:00
|
|
|
MergeObjects(objects, direct);
|
2019-09-12 11:34:58 +00:00
|
|
|
var parents = WalkUp(recipient);
|
2019-05-15 14:56:09 +00:00
|
|
|
foreach (var parent in parents)
|
|
|
|
{
|
2022-01-19 10:24:11 +00:00
|
|
|
direct = subscriptionProvider.GetSubscriptions(action, parent, checkSubscription) ?? Array.Empty<string>();
|
2019-05-15 14:56:09 +00:00
|
|
|
if (recipient is IDirectRecipient)
|
|
|
|
{
|
|
|
|
foreach (var groupsubscr in direct)
|
|
|
|
{
|
|
|
|
if (!objects.Contains(groupsubscr) && !subscriptionProvider.IsUnsubscribe(recipient as IDirectRecipient, action, groupsubscr))
|
|
|
|
{
|
|
|
|
objects.Add(groupsubscr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MergeObjects(objects, direct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return objects.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-12 11:34:58 +00:00
|
|
|
private List<IRecipient> WalkUp(IRecipient recipient)
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
|
|
|
var parents = new List<IRecipient>();
|
2019-09-12 11:34:58 +00:00
|
|
|
var groups = recipientProvider.GetGroups(recipient) ?? new IRecipientsGroup[0];
|
2019-05-15 14:56:09 +00:00
|
|
|
foreach (var group in groups)
|
|
|
|
{
|
|
|
|
parents.Add(group);
|
2019-09-12 11:34:58 +00:00
|
|
|
parents.AddRange(WalkUp(group));
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
return parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void MergeActions(List<INotifyAction> result, IEnumerable<INotifyAction> additions)
|
|
|
|
{
|
|
|
|
foreach (var addition in additions)
|
|
|
|
{
|
|
|
|
if (!result.Contains(addition)) result.Add(addition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void MergeObjects(List<string> result, IEnumerable<string> additions)
|
|
|
|
{
|
|
|
|
foreach (var addition in additions)
|
|
|
|
{
|
|
|
|
if (!result.Contains(addition)) result.Add(addition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|