From 0430158c19875f8ecce4eb8367b60746e977c56c Mon Sep 17 00:00:00 2001 From: NikolayRechkin Date: Mon, 23 Mar 2020 15:23:46 +0300 Subject: [PATCH] Calendar: core: api --- .../Server/BusinessObjects/DataProvider.cs | 48 ++++---- .../Server/Controllers/CalendarController.cs | 37 +++++- .../Server/Models/CalendarWrapper.cs | 29 +++-- .../Server/Models/SubscriptionWrapper.cs | 113 +++++++++++------- 4 files changed, 144 insertions(+), 83 deletions(-) diff --git a/products/ASC.Calendar/Server/BusinessObjects/DataProvider.cs b/products/ASC.Calendar/Server/BusinessObjects/DataProvider.cs index ac7e80de1e..f5618fa3cb 100644 --- a/products/ASC.Calendar/Server/BusinessObjects/DataProvider.cs +++ b/products/ASC.Calendar/Server/BusinessObjects/DataProvider.cs @@ -213,35 +213,33 @@ namespace ASC.Calendar.BusinessObjects return cals; } - /* - public List LoadiCalStreamsForUser(Guid userId) - { - var queryGetCalIds = new SqlQuery(_calendarTable) - .Select("cal.id") - .Where("cal.tenant", TenantManager.GetCurrentTenant().TenantId) - .Where("cal.owner_id", userId) - .Where(!Exp.Eq("cal.ical_url", null)); - var calIds = db.ExecuteList(queryGetCalIds).Select(r => r[0]); + public List LoadiCalStreamsForUser(Guid userId) + { + var calIds = CalendarDb.CalendarCalendars.Where(p => + p.Tenant == TenantManager.GetCurrentTenant().TenantId && + p.OwnerId == userId.ToString() && + p.IcalUrl != null) + .Select(s => s.Id).ToArray(); + var calendars = GetCalendarsByIds(calIds.ToArray()); + return calendars; + } - var calendars = GetCalendarsByIds(calIds.ToArray()); - return calendars; - } + public List LoadSubscriptionsForUser(Guid userId) + { + var groups = UserManager.GetUserGroups(userId).Select(g => g.ID).ToList(); + groups.AddRange(UserManager.GetUserGroups(userId, Constants.SysGroupCategoryId).Select(g => g.ID)); - public List LoadSubscriptionsForUser(Guid userId) - { - var groups = UserManager.GetUserGroups(userId).Select(g => g.ID).ToList(); - groups.AddRange(UserManager.GetUserGroups(userId, Core.Users.Constants.SysGroupCategoryId).Select(g => g.ID)); + var calIds = from calItem in CalendarDb.CalendarCalendarItem + join calendar in CalendarDb.CalendarCalendars on calItem.CalendarId equals calendar.Id + where + calendar.Tenant == TenantManager.GetCurrentTenant().TenantId && + (calItem.ItemId == userId || (groups.Contains(calItem.ItemId) && calItem.IsGroup == 1)) + select calItem.CalendarId; - var calIds = db.ExecuteList(new SqlQuery(_calendarItemTable).Select("cal_itm.calendar_id") - .InnerJoin(_calendarTable, Exp.EqColumns("cal.id", "cal_itm.calendar_id")) - .Where("cal.tenant", TenantManager.GetCurrentTenant().TenantId) - .Where(Exp.Eq("cal_itm.item_id", userId) | (Exp.In("cal_itm.item_id", groups.ToArray()) & Exp.Eq("cal_itm.is_group", true))) - ).Select(r => r[0]); - - var calendars = GetCalendarsByIds(calIds.ToArray()); - return calendars; - }*/ + var calendars = GetCalendarsByIds(calIds.ToArray()); + return calendars; + } public TimeZoneInfo GetTimeZoneForSharedEventsCalendar(Guid userId) { diff --git a/products/ASC.Calendar/Server/Controllers/CalendarController.cs b/products/ASC.Calendar/Server/Controllers/CalendarController.cs index b3bcd9c367..1a1ffb0a19 100644 --- a/products/ASC.Calendar/Server/Controllers/CalendarController.cs +++ b/products/ASC.Calendar/Server/Controllers/CalendarController.cs @@ -127,6 +127,8 @@ namespace ASC.Calendar.Controllers public Signature Signature { get; } public SecurityContext SecurityContext { get; } public ExportDataCache ExportDataCache { get; } + public SubscriptionWrapperHelper SubscriptionWrapperHelper { get; } + public CalendarController( @@ -152,7 +154,8 @@ namespace ASC.Calendar.Controllers TodoWrapperHelper todoWrapperHelper, Signature signature, SecurityContext securityContext, - ExportDataCache exportDataCache) + ExportDataCache exportDataCache, + SubscriptionWrapperHelper subscriptionWrapperHelper) { AuthContext = authContext; Authentication = authentication; @@ -176,6 +179,7 @@ namespace ASC.Calendar.Controllers Signature = signature; SecurityContext = securityContext; ExportDataCache = exportDataCache; + SubscriptionWrapperHelper = subscriptionWrapperHelper; CalendarManager.Instance.RegistryCalendar(new SharedEventsCalendar(AuthContext, TimeZoneConverter, TenantManager, DataProvider)); var birthdayReminderCalendar = new BirthdayReminderCalendar(AuthContext, TimeZoneConverter, UserManager, DisplayUserSettingsHelper); @@ -217,6 +221,34 @@ namespace ASC.Calendar.Controllers product.Init(); return new Module(product, true); } + [Read("subscriptions")] + public List LoadSubscriptions() + { + var result = new List(); + + var calendars = DataProvider.LoadSubscriptionsForUser(SecurityContext.CurrentAccount.ID); + result.AddRange(calendars.FindAll(c => !c.OwnerId.Equals(SecurityContext.CurrentAccount.ID)).ConvertAll(c => SubscriptionWrapperHelper.Get(c))); + + var iCalStreams = DataProvider.LoadiCalStreamsForUser(SecurityContext.CurrentAccount.ID); + result.AddRange(iCalStreams.ConvertAll(c => SubscriptionWrapperHelper.Get(c))); + + + var extCalendars = CalendarManager.Instance.GetCalendarsForUser(SecurityContext.CurrentAccount.ID, UserManager); + var viewSettings = DataProvider.GetUserViewSettings(SecurityContext.CurrentAccount.ID, extCalendars.ConvertAll(c => c.Id)); + + result.AddRange(extCalendars.ConvertAll(c => + SubscriptionWrapperHelper.Get(c, viewSettings.Find(o => o.CalendarId.Equals(c.Id, StringComparison.InvariantCultureIgnoreCase))))); + + + //TODO For personal + /*else + { + var iCalStreams = DataProvider.LoadiCalStreamsForUser(SecurityContext.CurrentAccount.ID); + result.AddRange(iCalStreams.ConvertAll(c => new SubscriptionWrapper(c))); + }*/ + + return result; + } [Read("eventdays/{startDate}/{endDate}")] public List GetEventDays(ApiDateTime startDate, ApiDateTime endDate) { @@ -2647,7 +2679,8 @@ namespace ASC.Calendar.Controllers .AddEventHistoryWrapper() .AddEventWrapper() .AddTodoWrapper() - .AddExportDataCache(); + .AddExportDataCache() + .AddSubscriptionWrapperHelper(); } } } \ No newline at end of file diff --git a/products/ASC.Calendar/Server/Models/CalendarWrapper.cs b/products/ASC.Calendar/Server/Models/CalendarWrapper.cs index 3038b6848a..3460ecc3d6 100644 --- a/products/ASC.Calendar/Server/Models/CalendarWrapper.cs +++ b/products/ASC.Calendar/Server/Models/CalendarWrapper.cs @@ -43,6 +43,8 @@ namespace ASC.Calendar.Models [DataContract(Name = "calendar", Namespace = "")] public class CalendarWrapper { + internal UserViewSettings _userViewSettings; + internal Guid _userId; public BaseCalendar UserCalendar { get; set; } [DataMember(Name = "isSubscription", Order = 80)] @@ -64,7 +66,7 @@ namespace ASC.Calendar.Models public bool IsShared { get; set; } [DataMember(Name = "permissions", Order = 70)] - public CalendarPermissions Permissions { get; set; } + public virtual CalendarPermissions Permissions { get; set; } [DataMember(Name = "isEditable", Order = 90)] public bool IsEditable { get; set; } @@ -135,9 +137,6 @@ namespace ASC.Calendar.Models public class CalendarWrapperHelper { - protected UserViewSettings _userViewSettings; - protected Guid _userId; - public AuthContext AuthContext { get; } private AuthManager Authentication { get; } private PermissionContext PermissionContext { get; } @@ -174,22 +173,22 @@ namespace ASC.Calendar.Models { var calendarWraper = new CalendarWrapper(); - _userViewSettings = userViewSettings; - if (_userViewSettings == null && calendar is BusinessObjects.Calendar) + calendarWraper._userViewSettings = userViewSettings; + if (calendarWraper._userViewSettings == null && calendar is BusinessObjects.Calendar) { - _userViewSettings = (calendar as BusinessObjects.Calendar) + calendarWraper._userViewSettings = (calendar as BusinessObjects.Calendar) .ViewSettings.Find(s => s.UserId == AuthContext.CurrentAccount.ID); } - if (_userViewSettings == null) + if (calendarWraper._userViewSettings == null) { calendarWraper.UserCalendar = calendar; - _userId = AuthContext.CurrentAccount.ID; + calendarWraper._userId = AuthContext.CurrentAccount.ID; } else { - calendarWraper.UserCalendar = calendar.GetUserCalendar(_userViewSettings); - _userId = _userViewSettings.UserId; + calendarWraper.UserCalendar = calendar.GetUserCalendar(calendarWraper._userViewSettings); + calendarWraper._userId = calendarWraper._userViewSettings.UserId; } //---IsSubscription @@ -199,7 +198,7 @@ namespace ASC.Calendar.Models calendarWraper.IsSubscription = true; else if (calendarWraper.UserCalendar.Id.Equals(SharedEventsCalendar.CalendarId, StringComparison.InvariantCultureIgnoreCase)) calendarWraper.IsSubscription = true; - else if (calendarWraper.UserCalendar.OwnerId.Equals(_userId)) + else if (calendarWraper.UserCalendar.OwnerId.Equals(calendarWraper._userId)) calendarWraper.IsSubscription = false; else calendarWraper.IsSubscription = true; @@ -217,7 +216,7 @@ namespace ASC.Calendar.Models calendarWraper.IsiCalStream = false; //---IsHidden - calendarWraper.IsHidden = _userViewSettings != null ? _userViewSettings.IsHideEvents : false; + calendarWraper.IsHidden = calendarWraper._userViewSettings != null ? calendarWraper._userViewSettings.IsHideEvents : false; //---CanAlertModify calendarWraper.CanAlertModify = calendarWraper.UserCalendar.Context.CanChangeAlertType; @@ -240,7 +239,7 @@ namespace ASC.Calendar.Models if (calendarWraper.UserCalendar.IsiCalStream()) calendarWraper.IsEditable = false; else if (calendarWraper.UserCalendar is ISecurityObject) - calendarWraper.IsEditable = PermissionContext.PermissionResolver.Check(Authentication.GetAccountByID(TenantManager.GetCurrentTenant().TenantId, _userId), (ISecurityObject)calendarWraper.UserCalendar as ISecurityObject, null, CalendarAccessRights.FullAccessAction); + calendarWraper.IsEditable = PermissionContext.PermissionResolver.Check(Authentication.GetAccountByID(TenantManager.GetCurrentTenant().TenantId, calendarWraper._userId), (ISecurityObject)calendarWraper.UserCalendar as ISecurityObject, null, CalendarAccessRights.FullAccessAction); else calendarWraper.IsEditable = false; @@ -275,7 +274,7 @@ namespace ASC.Calendar.Models calendarWraper.Owner = owner; //---IsAcceptedSubscription - calendarWraper.IsAcceptedSubscription = _userViewSettings == null || _userViewSettings.IsAccepted; + calendarWraper.IsAcceptedSubscription = calendarWraper._userViewSettings == null || calendarWraper._userViewSettings.IsAccepted; //---DefaultAlertType calendarWraper.DefaultAlertType = EventAlertWrapper.ConvertToTypeSurrogated(calendarWraper.UserCalendar.EventAlertType); diff --git a/products/ASC.Calendar/Server/Models/SubscriptionWrapper.cs b/products/ASC.Calendar/Server/Models/SubscriptionWrapper.cs index 153a19ec70..00c9907657 100644 --- a/products/ASC.Calendar/Server/Models/SubscriptionWrapper.cs +++ b/products/ASC.Calendar/Server/Models/SubscriptionWrapper.cs @@ -29,58 +29,24 @@ using System.Runtime.Serialization; using ASC.Web.Core.Calendars; using ASC.Calendar.BusinessObjects; using System.Collections.Generic; +using ASC.Common; namespace ASC.Calendar.Models { - /* [DataContract(Name = "subscription", Namespace = "")] public class SubscriptionWrapper : CalendarWrapper { - public SubscriptionWrapper(BaseCalendar calendar) - : base(calendar) { } - public SubscriptionWrapper(BaseCalendar calendar, UserViewSettings userViewSettings) - : base(calendar, userViewSettings) { } - - [DataMember(Name = "isSubscribed", Order = 100)] - public bool IsAccepted - { - get - { - if(UserCalendar is Calendar.BusinessObjects.Calendar) - return _userViewSettings != null && _userViewSettings.IsAccepted; - - return this.IsAcceptedSubscription; - } - set { } - } - + public bool IsAccepted { get; set; } [DataMember(Name = "isNew", Order = 140)] - public bool IsNew - { - get - { - return _userViewSettings==null; - } - set { } - } + public bool IsNew { get; set; } [DataMember(Name = "group", Order = 130)] - public string Group - { - get { + public string Group { get; set; } - if(UserCalendar.IsiCalStream()) - return Resources.CalendarApiResource.iCalCalendarsGroup; - - return String.IsNullOrEmpty(UserCalendar.Context.Group) ? Resources.CalendarApiResource.SharedCalendarsGroup : UserCalendar.Context.Group; - } - set { } - } - - [DataMember(IsRequired=false)] - public override CalendarPermissions Permissions{get; set;} + [DataMember(IsRequired = false)] + public override CalendarPermissions Permissions { get; set; } public new static object GetSample() { @@ -110,5 +76,70 @@ namespace ASC.Calendar.Models }; } } - */ + + public class SubscriptionWrapperHelper + { + + public CalendarWrapperHelper CalendarWrapperHelper; + public SubscriptionWrapperHelper( + CalendarWrapperHelper calendarWrapperHelper) + { + CalendarWrapperHelper = calendarWrapperHelper; + } + + public SubscriptionWrapper Get(BaseCalendar calendar) + { + var subscriptionWrapper = (SubscriptionWrapper)CalendarWrapperHelper.Get(calendar); + var _userViewSettings = subscriptionWrapper._userViewSettings; + + //isSubscribed + if (subscriptionWrapper.UserCalendar is Calendar.BusinessObjects.Calendar) + subscriptionWrapper.IsAccepted = _userViewSettings != null && _userViewSettings.IsAccepted; + else + subscriptionWrapper.IsAccepted = subscriptionWrapper.IsAcceptedSubscription; + + //isNew + subscriptionWrapper.IsNew = _userViewSettings == null; + + //group + if (subscriptionWrapper.UserCalendar.IsiCalStream()) + subscriptionWrapper.Group = Resources.CalendarApiResource.iCalCalendarsGroup; + else + subscriptionWrapper.Group = String.IsNullOrEmpty(subscriptionWrapper.UserCalendar.Context.Group) ? Resources.CalendarApiResource.SharedCalendarsGroup : subscriptionWrapper.UserCalendar.Context.Group; + + return subscriptionWrapper; + } + public SubscriptionWrapper Get(BaseCalendar calendar, UserViewSettings userViewSettings) + { + var subscriptionWrapper = (SubscriptionWrapper)CalendarWrapperHelper.Get(calendar, userViewSettings); + var _userViewSettings = subscriptionWrapper._userViewSettings; + + //isSubscribed + if (subscriptionWrapper.UserCalendar is Calendar.BusinessObjects.Calendar) + subscriptionWrapper.IsAccepted = _userViewSettings != null && _userViewSettings.IsAccepted; + else + subscriptionWrapper.IsAccepted = subscriptionWrapper.IsAcceptedSubscription; + + //isNew + subscriptionWrapper.IsNew = _userViewSettings == null; + + //group + if (subscriptionWrapper.UserCalendar.IsiCalStream()) + subscriptionWrapper.Group = Resources.CalendarApiResource.iCalCalendarsGroup; + else + subscriptionWrapper.Group = String.IsNullOrEmpty(subscriptionWrapper.UserCalendar.Context.Group) ? Resources.CalendarApiResource.SharedCalendarsGroup : subscriptionWrapper.UserCalendar.Context.Group; + return subscriptionWrapper; + } + } + + public static class SubscriptionWrapperExtension + { + public static DIHelper AddSubscriptionWrapperHelper(this DIHelper services) + { + services.TryAddScoped(); + + return services + .AddCalendarWrapper(); + } + } }