DocSpace-buildtools/common/ASC.Core.Common/Data/DbSubscriptionService.cs

341 lines
12 KiB
C#
Raw Normal View History

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;
using System.Collections.Generic;
2019-08-15 12:04:42 +00:00
using System.Linq;
2019-12-02 12:04:22 +00:00
using System.Linq.Expressions;
2019-12-17 13:01:59 +00:00
2020-10-19 15:53:15 +00:00
using ASC.Common;
2019-12-02 12:04:22 +00:00
using ASC.Core.Common.EF;
2019-05-15 14:56:09 +00:00
using ASC.Core.Tenants;
namespace ASC.Core.Data
2020-10-19 15:53:15 +00:00
{
[Scope]
2019-12-02 12:04:22 +00:00
class DbSubscriptionService : ISubscriptionService
{
private Expression<Func<Subscription, SubscriptionRecord>> FromSubscriptionToSubscriptionRecord { get; set; }
private Expression<Func<DbSubscriptionMethod, SubscriptionMethod>> FromDbSubscriptionMethodToSubscriptionMethod { get; set; }
2021-07-15 10:07:47 +00:00
private Lazy<UserDbContext> LazyUserDbContext { get; }
private UserDbContext UserDbContext { get => LazyUserDbContext.Value; }
2019-12-02 12:04:22 +00:00
public DbSubscriptionService(DbContextManager<UserDbContext> dbContextManager)
2019-05-15 14:56:09 +00:00
{
2021-07-15 10:07:47 +00:00
LazyUserDbContext = new Lazy<UserDbContext>(() => dbContextManager.Value);
2019-12-02 12:04:22 +00:00
FromSubscriptionToSubscriptionRecord = r => new SubscriptionRecord
{
ActionId = r.Action,
ObjectId = r.Object,
2020-07-24 14:01:20 +00:00
RecipientId = r.Recipient,
SourceId = r.Source,
2019-12-02 12:04:22 +00:00
Subscribed = !r.Unsubscribed,
Tenant = r.Tenant
};
FromDbSubscriptionMethodToSubscriptionMethod = r => new SubscriptionMethod
{
ActionId = r.Action,
2020-07-24 14:01:20 +00:00
RecipientId = r.Recipient,
SourceId = r.Source,
2019-12-02 12:04:22 +00:00
Tenant = r.Tenant,
2020-07-24 14:01:20 +00:00
MethodsFromDb = r.Sender
2019-12-02 12:04:22 +00:00
};
2020-12-28 13:22:08 +00:00
}
public string[] GetRecipients(int tenant, string sourceId, string actionId, string objectId)
{
if (sourceId == null) throw new ArgumentNullException("sourceId");
if (actionId == null) throw new ArgumentNullException("actionId");
var q = GetQuery(tenant, sourceId, actionId)
.Where(r => r.Object == (objectId ?? string.Empty))
.Where(r => !r.Unsubscribed)
.Select(r => r.Recipient)
.Distinct();
return q.ToArray();
}
2019-05-15 14:56:09 +00:00
public IEnumerable<SubscriptionRecord> GetSubscriptions(int tenant, string sourceId, string actionId)
{
if (sourceId == null) throw new ArgumentNullException("sourceId");
if (actionId == null) throw new ArgumentNullException("actionId");
var q = GetQuery(tenant, sourceId, actionId);
return GetSubscriptions(q, tenant);
}
public IEnumerable<SubscriptionRecord> GetSubscriptions(int tenant, string sourceId, string actionId, string recipientId, string objectId)
{
2019-12-02 12:04:22 +00:00
var q = GetQuery(tenant, sourceId, actionId);
if (recipientId != null)
2019-05-15 14:56:09 +00:00
{
2019-12-02 12:04:22 +00:00
q = q.Where(r => r.Recipient == recipientId);
2019-05-15 14:56:09 +00:00
}
else
2019-12-02 12:04:22 +00:00
{
q = q.Where(r => r.Object == (objectId ?? string.Empty));
2019-05-15 14:56:09 +00:00
}
return GetSubscriptions(q, tenant);
}
public SubscriptionRecord GetSubscription(int tenant, string sourceId, string actionId, string recipientId, string objectId)
{
if (recipientId == null) throw new ArgumentNullException("recipientId");
var q = GetQuery(tenant, sourceId, actionId)
2019-12-02 12:04:22 +00:00
.Where(r => r.Recipient == recipientId)
.Where(r => r.Object == (objectId ?? string.Empty));
2019-05-15 14:56:09 +00:00
2020-12-28 13:22:08 +00:00
return GetSubscriptions(q, tenant).Take(1).FirstOrDefault();
2019-05-15 14:56:09 +00:00
}
2020-12-28 13:22:08 +00:00
public bool IsUnsubscribe(int tenant, string sourceId, string actionId, string recipientId, string objectId)
{
if (recipientId == null) throw new ArgumentNullException("recipientId");
if (sourceId == null) throw new ArgumentNullException("sourceId");
if (actionId == null) throw new ArgumentNullException("actionId");
var q = UserDbContext.Subscriptions
.Where(r => r.Source == sourceId &&
r.Action == actionId &&
r.Tenant == tenant &&
r.Recipient == recipientId &&
r.Unsubscribed);
if (!string.IsNullOrEmpty(objectId))
{
q = q.Where(r=> r.Object == objectId || r.Object == string.Empty);
}
else
{
q = q = q.Where(r => r.Object == string.Empty);;
}
return q.Any();
}
public string[] GetSubscriptions(int tenant, string sourceId, string actionId, string recipientId, bool checkSubscribe)
{
if (recipientId == null) throw new ArgumentNullException("recipientId");
if (sourceId == null) throw new ArgumentNullException("sourceId");
if (actionId == null) throw new ArgumentNullException("actionId");
var q = GetQuery(tenant, sourceId, actionId)
.Where(r=> r.Recipient == recipientId)
.Distinct();
if (checkSubscribe)
{
q = q.Where(r=> !r.Unsubscribed);
}
return q.Select(r=> r.Object).ToArray();
}
2019-05-15 14:56:09 +00:00
public void SaveSubscription(SubscriptionRecord s)
{
if (s == null) throw new ArgumentNullException("s");
2019-12-02 12:04:22 +00:00
var subs = new Subscription
{
Action = s.ActionId,
Object = s.ObjectId ?? string.Empty,
Recipient = s.RecipientId,
Source = s.SourceId,
Unsubscribed = !s.Subscribed,
Tenant = s.Tenant
};
2019-12-17 13:01:59 +00:00
UserDbContext.AddOrUpdate(r => r.Subscriptions, subs);
2019-12-02 12:04:22 +00:00
UserDbContext.SaveChanges();
2019-05-15 14:56:09 +00:00
}
public void RemoveSubscriptions(int tenant, string sourceId, string actionId)
{
RemoveSubscriptions(tenant, sourceId, actionId, string.Empty);
}
public void RemoveSubscriptions(int tenant, string sourceId, string actionId, string objectId)
{
if (sourceId == null) throw new ArgumentNullException("sourceId");
if (actionId == null) throw new ArgumentNullException("actionId");
2019-12-02 12:04:22 +00:00
using var tr = UserDbContext.Database.BeginTransaction();
var q = UserDbContext.Subscriptions
.Where(r => r.Tenant == tenant)
.Where(r => r.Source == sourceId)
.Where(r => r.Action == actionId);
2019-05-15 14:56:09 +00:00
if (objectId != string.Empty)
2019-12-02 12:04:22 +00:00
{
q = q.Where(r => r.Object == (objectId ?? string.Empty));
}
2020-01-20 15:09:52 +00:00
var sub = q.FirstOrDefault();
if (sub != null)
{
UserDbContext.Subscriptions.Remove(sub);
}
2019-12-02 12:04:22 +00:00
tr.Commit();
2019-05-15 14:56:09 +00:00
}
public IEnumerable<SubscriptionMethod> GetSubscriptionMethods(int tenant, string sourceId, string actionId, string recipientId)
{
if (sourceId == null) throw new ArgumentNullException("sourceId");
if (actionId == null) throw new ArgumentNullException("actionId");
2019-12-02 12:04:22 +00:00
var q = UserDbContext.SubscriptionMethods
.Where(r => r.Tenant == -1 || r.Tenant == tenant)
.Where(r => r.Source == sourceId);
if (recipientId != null)
{
q = q.Where(r => r.Recipient == recipientId);
}
2020-07-24 14:01:20 +00:00
var a = q
.OrderBy(r => r.Tenant)
.Distinct();
2019-05-15 14:56:09 +00:00
2020-12-28 13:22:08 +00:00
var methods = a.ToList();
var result = new List<SubscriptionMethod>();
var common = new Dictionary<string, SubscriptionMethod>();
var conv = FromDbSubscriptionMethodToSubscriptionMethod.Compile();
2019-12-02 12:04:22 +00:00
2020-12-28 13:22:08 +00:00
foreach (var r in methods)
{
var m = conv(r);
2019-05-15 14:56:09 +00:00
var key = m.SourceId + m.ActionId + m.RecipientId;
if (m.Tenant == Tenant.DEFAULT_TENANT)
{
m.Tenant = tenant;
2020-12-28 13:22:08 +00:00
common.Add(key, m);
result.Add(m);
2019-05-15 14:56:09 +00:00
}
else
{
2020-12-28 13:22:08 +00:00
if (!common.TryGetValue(key, out var rec))
2019-05-15 14:56:09 +00:00
{
2020-12-28 13:22:08 +00:00
result.Add(rec);
2019-05-15 14:56:09 +00:00
}
}
}
return result;
}
public void SetSubscriptionMethod(SubscriptionMethod m)
{
if (m == null) throw new ArgumentNullException("m");
2019-12-02 12:04:22 +00:00
using var tr = UserDbContext.Database.BeginTransaction();
2019-05-15 14:56:09 +00:00
if (m.Methods == null || m.Methods.Length == 0)
2019-12-02 12:04:22 +00:00
{
var q = UserDbContext.SubscriptionMethods
.Where(r => r.Tenant == m.Tenant)
.Where(r => r.Source == m.SourceId)
.Where(r => r.Recipient == m.RecipientId)
.Where(r => r.Action == m.ActionId);
2020-01-20 15:09:52 +00:00
var sm = q.FirstOrDefault();
if (sm != null)
{
UserDbContext.SubscriptionMethods.Remove(sm);
}
2019-05-15 14:56:09 +00:00
}
else
2019-12-02 12:04:22 +00:00
{
var sm = new DbSubscriptionMethod
{
Action = m.ActionId,
Recipient = m.RecipientId,
Source = m.SourceId,
Tenant = m.Tenant,
Sender = string.Join("|", m.Methods)
};
2019-12-17 13:01:59 +00:00
UserDbContext.AddOrUpdate(r => r.SubscriptionMethods, sm);
2019-12-02 12:04:22 +00:00
}
2019-12-17 13:01:59 +00:00
UserDbContext.SaveChanges();
2019-12-02 12:04:22 +00:00
tr.Commit();
2019-05-15 14:56:09 +00:00
}
2019-12-02 12:04:22 +00:00
private IQueryable<Subscription> GetQuery(int tenant, string sourceId, string actionId)
2019-05-15 14:56:09 +00:00
{
if (sourceId == null) throw new ArgumentNullException("sourceId");
if (actionId == null) throw new ArgumentNullException("actionId");
2019-12-02 12:04:22 +00:00
return
UserDbContext.Subscriptions
.Where(r => r.Source == sourceId)
.Where(r => r.Action == actionId)
.Where(r => r.Tenant == -1 || r.Tenant == tenant)
.OrderBy(r => r.Tenant)
;
2019-05-15 14:56:09 +00:00
}
2019-12-02 12:04:22 +00:00
private IEnumerable<SubscriptionRecord> GetSubscriptions(IQueryable<Subscription> q, int tenant)
2019-05-15 14:56:09 +00:00
{
2020-12-28 13:22:08 +00:00
var subs = q.ToList();
var result = new List<SubscriptionRecord>();
var common = new Dictionary<string, SubscriptionRecord>();
var conv = FromSubscriptionToSubscriptionRecord.Compile();
2019-05-15 14:56:09 +00:00
2020-12-28 13:22:08 +00:00
foreach (var r in subs)
{
var s = conv(r);
2019-05-15 14:56:09 +00:00
var key = s.SourceId + s.ActionId + s.RecipientId + s.ObjectId;
if (s.Tenant == Tenant.DEFAULT_TENANT)
{
s.Tenant = tenant;
2020-12-28 13:22:08 +00:00
common.Add(key, s);
result.Add(s);
2019-05-15 14:56:09 +00:00
}
else
{
2020-12-28 13:22:08 +00:00
if (common.TryGetValue(key, out var rec))
2019-05-15 14:56:09 +00:00
{
2020-12-28 13:22:08 +00:00
result.Remove(rec);
2019-05-15 14:56:09 +00:00
}
}
}
return result;
}
}
}