Beispiel #1
0
 /// <summary>
 /// Set serialize format
 /// </summary>
 /// <param name="services"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static CalendarServiceCollection SetSerializationFormatSettings(this CalendarServiceCollection services,
                                                                        Action <JsonSerializerSettings> options)
 {
     options.Invoke(CalendarServiceCollection.JsonSerializerSettings);
     CalendarServiceCollection.JsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
     return(services);
 }
        /// <summary>
        /// Register google calendar
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <returns></returns>
        public static CalendarServiceCollection RegisterGoogleCalendarProvider(this CalendarServiceCollection serviceCollection)
        {
            serviceCollection.RegisterExternalCalendarProvider(options =>
            {
                options.ProviderName    = nameof(GoogleCalendarProvider);
                options.ProviderType    = typeof(GoogleCalendarProvider);
                options.DisplayName     = "Google";
                options.FontAwesomeIcon = "google";
            });

            return(serviceCollection);
        }
        /// <summary>
        /// Register external calendar provider
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static CalendarServiceCollection RegisterExternalCalendarProvider(this CalendarServiceCollection serviceCollection, Action <ExternalProviderConfig> options)
        {
            var configuration = new ExternalProviderConfig();

            options(configuration);
            if (configuration.ProviderName.IsNullOrEmpty() || configuration.ProviderType == null)
            {
                throw new FailRegisterProviderException();
            }
            IoC.RegisterService <IExternalCalendarProvider>(configuration.ProviderName, configuration.ProviderType);
            CalendarProviders.RegisterProviderInMemory(configuration);
            return(serviceCollection);
        }
Beispiel #4
0
        /// <summary>
        /// Register page render
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static CalendarServiceCollection AddCalendarRazorUIModule(this CalendarServiceCollection services)
        {
            services.Services.ConfigureOptions(typeof(InternalCalendarFileConfiguration));

            MenuEvents.Menu.OnMenuSeed += (sender, args) =>
            {
                GearApplication.BackgroundTaskQueue.PushBackgroundWorkItemInQueue(async x =>
                {
                    var service = IoC.Resolve <IMenuService>();
                    await service.AppendMenuItemsAsync(new CalendarMenuInitializer());
                });
            };
            return(services);
        }
 /// <summary>
 /// Add calendar module storage
 /// </summary>
 /// <typeparam name="TDbContext"></typeparam>
 /// <param name="configuration"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public static CalendarServiceCollection AddCalendarModuleStorage <TDbContext>(
     this CalendarServiceCollection configuration, Action <DbContextOptionsBuilder> options)
     where TDbContext : DbContext, ICalendarDbContext
 {
     Arg.NotNull(configuration.Services, nameof(AddCalendarModuleStorage));
     configuration.Services.AddDbContext <TDbContext>(options, ServiceLifetime.Transient);
     configuration.Services.AddScopedContextFactory <ICalendarDbContext, TDbContext>();
     configuration.Services.RegisterAuditFor <ICalendarDbContext>($"{nameof(Calendar)} module");
     SystemEvents.Database.OnAllMigrate += (sender, args) =>
     {
         GearApplication.GetHost <IWebHost>().MigrateDbContext <TDbContext>();
     };
     return(configuration);
 }
Beispiel #6
0
        /// <summary>
        /// Add calendar graph
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <returns></returns>
        public static CalendarServiceCollection AddCalendarGraphQlApi(this CalendarServiceCollection serviceCollection)
        {
            serviceCollection.Services.AddSingleton <IDocumentExecuter, DocumentExecuter>();
            serviceCollection.Services.AddSingleton <CalendarQuery>();

            //Register types
            serviceCollection.Services.AddTransient <EventType>();
            serviceCollection.Services.AddTransient <UserType>();
            serviceCollection.Services.AddTransient <EventMemberType>();

            var sp = serviceCollection.Services.BuildServiceProvider();

            serviceCollection.Services.AddSingleton <ICalendarSchema>(new CalendarSchema(new FuncDependencyResolver(type => sp.GetService(type))));
            return(serviceCollection);
        }
        /// <summary>
        /// Register outlook provider
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <param name="outlookOptions"></param>
        /// <returns></returns>
        public static CalendarServiceCollection RegisterOutlookCalendarProvider(this CalendarServiceCollection serviceCollection, Action <MsAuthorizationSettings> outlookOptions)
        {
            Arg.NotNull(outlookOptions, nameof(RegisterOutlookCalendarProvider));
            serviceCollection.RegisterExternalCalendarProvider(options =>
            {
                options.ProviderName    = nameof(OutlookCalendarProvider);
                options.ProviderType    = typeof(OutlookCalendarProvider);
                options.DisplayName     = "Outlook";
                options.FontAwesomeIcon = "microsoft";
            });

            var authSettings = new MsAuthorizationSettings();

            outlookOptions(authSettings);
            OutlookAuthSettings.SetAuthSettings(authSettings);

            serviceCollection.Services.AddAuthentication()
            .AddMicrosoftAccount(microsoftOptions =>
            {
                microsoftOptions.ClientId                = authSettings.ClientId;
                microsoftOptions.ClientSecret            = authSettings.ClientSecretId;
                microsoftOptions.SaveTokens              = true;
                microsoftOptions.Events.OnCreatingTicket = ctx =>
                {
                    var tokens = ctx.Properties.GetTokens().ToList();

                    tokens.Add(new AuthenticationToken()
                    {
                        Name  = "TicketCreated",
                        Value = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)
                    });

                    ctx.Properties.StoreTokens(tokens);

                    return(Task.CompletedTask);
                };

                foreach (var scope in authSettings.Scopes)
                {
                    microsoftOptions.Scope.Add(scope);
                }
            });
            return(serviceCollection);
        }
        /// <summary>
        /// Register runtime events
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <returns></returns>
        public static CalendarServiceCollection AddCalendarRuntimeEvents(this CalendarServiceCollection serviceCollection)
        {
            CalendarEvents.RegisterEvents();
            const string calendarLink = "<a href='/calendar'>here</a>";

            CalendarEvents.SystemCalendarEvents.OnEventCreated += async(sender, args) =>
            {
                var subject  = "Changes in the event " + args.Title;
                var message  = $"An event is created for which you are invited, more details {calendarLink}";
                var notifier = IoC.Resolve <INotify <GearRole> >();
                if (notifier == null)
                {
                    return;
                }
                var users = args.Invited?.Select(x => x.ToGuid()).ToList() ?? new List <Guid>();
                if (users.Any())
                {
                    await notifier.SendNotificationAsync(users, NotificationType.Info, subject, message);
                }
            };

            CalendarEvents.SystemCalendarEvents.OnEventUpdated += async(sender, args) =>
            {
                var subject  = "Changes in the event " + args.Title;
                var message  = $"Event {args.Title} has been modified, more details {calendarLink}";
                var notifier = IoC.Resolve <INotify <GearRole> >();
                var users    = args.Invited?.Select(x => x.ToGuid()).ToList() ?? new List <Guid>();
                if (users.Any())
                {
                    await notifier.SendNotificationAsync(users, NotificationType.Info, subject, message);
                }
            };

            CalendarEvents.SystemCalendarEvents.OnEventDeleted += async(sender, args) =>
            {
                var subject  = "Changes in the event " + args.Title;
                var message  = $"Event {args.Title} was canceled";
                var notifier = IoC.Resolve <INotify <GearRole> >();
                var users    = args.Invited?.Select(x => x.ToGuid()).ToList() ?? new List <Guid>();
                if (users.Any())
                {
                    await notifier.SendNotificationAsync(users, NotificationType.Info, subject, message);
                }
            };

            CalendarEvents.SystemCalendarEvents.OnUserChangeAcceptance += async(sender, args) =>
            {
                var userManager = IoC.Resolve <UserManager <GearUser> >();
                if (userManager == null)
                {
                    return;
                }
                var subject = "Changes in the event " + args.Title;
                var user    = await userManager.FindByIdAsync(args.Member.UserId.ToString());

                var message  = $"User {user.Email} responded with {args.AcceptanceState} to event {args.Title}";
                var notifier = IoC.Resolve <INotify <GearRole> >();
                await notifier.SendNotificationAsync(new List <Guid> {
                    args.Organizer
                }, NotificationType.Info, subject, message);
            };

            return(serviceCollection);
        }
 /// <summary>
 /// Register calendar
 /// </summary>
 /// <typeparam name="TService"></typeparam>
 /// <param name="serviceCollection"></param>
 /// <returns></returns>
 public static CalendarServiceCollection RegisterCalendarUserPreferencesProvider <TService>(this CalendarServiceCollection serviceCollection)
     where TService : class, ICalendarUserSettingsService
 {
     IoC.RegisterTransientService <ICalendarUserSettingsService, TService>();
     return(serviceCollection);
 }
 /// <summary>
 /// Register token provider
 /// </summary>
 /// <typeparam name="TProvider"></typeparam>
 /// <param name="serviceCollection"></param>
 /// <returns></returns>
 public static CalendarServiceCollection RegisterTokenProvider <TProvider>(this CalendarServiceCollection serviceCollection)
     where TProvider : class, ICalendarExternalTokenProvider
 {
     IoC.RegisterTransientService <ICalendarExternalTokenProvider, TProvider>();
     return(serviceCollection);
 }
        /// <summary>
        /// Register events
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <returns></returns>
        public static CalendarServiceCollection RegisterSyncOnExternalCalendars(this CalendarServiceCollection serviceCollection)
        {
            //On event created
            CalendarEvents.SystemCalendarEvents.OnEventCreated += async(sender, args) =>
            {
                var calendarManager     = IoC.Resolve <ICalendarManager>();
                var userSettingsService = IoC.Resolve <ICalendarUserSettingsService>();
                var evtRequest          = await calendarManager.GetEventByIdAsync(args.EventId);

                if (!evtRequest.IsSuccess)
                {
                    return;
                }
                var evt       = evtRequest.Result;
                var factory   = new ExternalCalendarProviderFactory();
                var providers = factory.GetProviders();
                foreach (var provider in providers)
                {
                    var isProviderEnabledForUser = await userSettingsService.IsProviderEnabledAsync(evt.Organizer, provider);

                    if (!isProviderEnabledForUser.IsSuccess)
                    {
                        continue;
                    }
                    var providerService = factory.CreateService(provider);
                    var authRequest     = await providerService.AuthorizeAsync(evt.Organizer);

                    if (!authRequest.IsSuccess)
                    {
                        continue;
                    }
                    var syncResult = await providerService.PushEventAsync(EventMapper.Map(evt));

                    if (!syncResult.IsSuccess)
                    {
                        Debug.WriteLine(syncResult.Errors);
                    }
                }

                await calendarManager.SetEventSyncState(evt.Id, true);
            };

            //On event update
            CalendarEvents.SystemCalendarEvents.OnEventUpdated += async(sender, args) =>
            {
                var calendarManager     = IoC.Resolve <ICalendarManager>();
                var userSettingsService = IoC.Resolve <ICalendarUserSettingsService>();
                var evtRequest          = await calendarManager.GetEventByIdAsync(args.EventId);

                if (!evtRequest.IsSuccess)
                {
                    return;
                }
                var evt       = evtRequest.Result;
                var factory   = new ExternalCalendarProviderFactory();
                var providers = factory.GetProviders();
                foreach (var provider in providers)
                {
                    var isProviderEnabledForUser = await userSettingsService.IsProviderEnabledAsync(evt.Organizer, provider);

                    if (!isProviderEnabledForUser.IsSuccess)
                    {
                        continue;
                    }
                    var providerService = factory.CreateService(provider);
                    var authRequest     = await providerService.AuthorizeAsync(evt.Organizer);

                    if (!authRequest.IsSuccess)
                    {
                        continue;
                    }

                    if (!evt.Synced)
                    {
                        await providerService.PushEventAsync(EventMapper.Map(evt));
                    }
                    else
                    {
                        var attrRequest = await userSettingsService.GetEventAttributeAsync(evt.Id, $"{provider}_evtId");

                        if (attrRequest.IsSuccess)
                        {
                            var providerEventId = attrRequest.Result;
                            var syncResult      = await providerService.UpdateEventAsync(EventMapper.Map(evt), providerEventId);

                            if (!syncResult.IsSuccess)
                            {
                                Debug.WriteLine(syncResult.Errors);
                            }
                        }
                        else
                        {
                            await providerService.PushEventAsync(EventMapper.Map(evt));
                        }
                    }
                }

                if (!evt.Synced)
                {
                    await calendarManager.SetEventSyncState(evt.Id, true);
                }
            };

            //On delete Event
            CalendarEvents.SystemCalendarEvents.OnEventDeleted += async(sender, args) =>
            {
                var calendarManager     = IoC.Resolve <ICalendarManager>();
                var userSettingsService = IoC.Resolve <ICalendarUserSettingsService>();
                var evtRequest          = await calendarManager.GetEventByIdAsync(args.EventId);

                if (!evtRequest.IsSuccess)
                {
                    return;
                }
                var evt = evtRequest.Result;
                if (!evt.Synced)
                {
                    return;
                }
                var factory   = new ExternalCalendarProviderFactory();
                var providers = factory.GetProviders();

                foreach (var provider in providers)
                {
                    var isProviderEnabledForUser = await userSettingsService.IsProviderEnabledAsync(evt.Organizer, provider);

                    if (!isProviderEnabledForUser.IsSuccess)
                    {
                        continue;
                    }
                    var providerService = factory.CreateService(provider);
                    var authRequest     = await providerService.AuthorizeAsync(evt.Organizer);

                    if (!authRequest.IsSuccess)
                    {
                        continue;
                    }
                    var attrRequest = await userSettingsService.GetEventAttributeAsync(evt.Id, $"{provider}_evtId");

                    if (!attrRequest.IsSuccess)
                    {
                        continue;
                    }
                    var providerEventId = attrRequest.Result;
                    await providerService.DeleteEventAsync(providerEventId);
                }
            };

            return(serviceCollection);
        }