コード例 #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req)
        {
            _logger.LogInformation("C# HTTP trigger function GetAllExtendedCalendarItems processed a request.");
            string         tenant         = req.Headers[Constants.HEADER_TENANT];
            ServerSettings serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);

            string keyWord = req.Headers[Constants.HEADER_KEYWORD];

            if (String.IsNullOrEmpty(keyWord) || !serverSettings.IsAdmin(keyWord))
            {
                _logger.LogWarning("GetAllExtendedCalendarItems called with wrong keyword.");
                return(new BadRequestErrorMessageResult("Keyword is missing or wrong."));
            }
            // Get a list of all CalendarItems

            IEnumerable <CalendarItem> rawListOfCalendarItems = await _cosmosRepository.GetItems();

            List <ExtendedCalendarItem> resultCalendarItems = new List <ExtendedCalendarItem>(50);

            foreach (CalendarItem item in rawListOfCalendarItems)
            {
                ExtendedCalendarItem extendedItem = new ExtendedCalendarItem(item);
                resultCalendarItems.Add(extendedItem);
                // Read all participants for this calendar item
                extendedItem.ParticipantsList = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(extendedItem.Id));

                // Read all comments
                extendedItem.CommentsList = await _commentRepository.GetItems(c => c.CalendarItemId.Equals(extendedItem.Id));
            }
            IEnumerable <ExtendedCalendarItem> orderedList = resultCalendarItems.OrderBy(d => d.StartDate);

            return(new OkObjectResult(orderedList));
        }
コード例 #2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            _logger.LogInformation($"C# HTTP trigger function AddCommentToCalendarItem processed a request.");
            string tenant = req.Headers[Constants.HEADER_TENANT];

            if (String.IsNullOrWhiteSpace(tenant))
            {
                tenant = null;
            }
            ServerSettings serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);

            string keyWord = req.Headers[Constants.HEADER_KEYWORD];

            if (String.IsNullOrEmpty(keyWord) || !serverSettings.IsUser(keyWord))
            {
                return(new BadRequestErrorMessageResult("Keyword is missing or wrong."));
            }
            string          requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            CalendarComment comment     = JsonConvert.DeserializeObject <CalendarComment>(requestBody);

            // Get and check corresponding CalendarItem
            if (String.IsNullOrEmpty(comment.CalendarItemId))
            {
                return(new OkObjectResult(new BackendResult(false, "Terminangabe fehlt.")));
            }
            CalendarItem calendarItem = await _calendarRepository.GetItem(comment.CalendarItemId);

            if (null == calendarItem)
            {
                return(new OkObjectResult(new BackendResult(false, "Angegebenen Termin nicht gefunden.")));
            }
            ExtendedCalendarItem extendedCalendarItem = new ExtendedCalendarItem(calendarItem);

            // Read all participants for this calendar item
            extendedCalendarItem.ParticipantsList = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(extendedCalendarItem.Id));

            // Set TTL for comment the same as for CalendarItem
            System.TimeSpan diffTime = calendarItem.StartDate.Subtract(DateTime.Now);
            comment.TimeToLive = serverSettings.AutoDeleteAfterDays * 24 * 3600 + (int)diffTime.TotalSeconds;
            // Checkindate to track bookings
            comment.CommentDate = DateTime.Now;
            if (!String.IsNullOrWhiteSpace(tenant))
            {
                comment.Tenant = tenant;
            }

            comment = await _cosmosRepository.UpsertItem(comment);

            if (!String.IsNullOrEmpty(comment.Comment))
            {
                await _subscriptionRepository.NotifyParticipants(extendedCalendarItem, comment.AuthorFirstName, comment.AuthorLastName, comment.Comment);
            }

            BackendResult result = new BackendResult(true);

            return(new OkObjectResult(result));
        }
コード例 #3
0
        public async Task NotifyParticipants(ExtendedCalendarItem calendarItem, string firstName, string lastName, string message)
        {
            IEnumerable <NotificationSubscription> subscriptions;
            var publicKey  = "BJkgu1ZbFHQm1gQCkYBvsHgZn8-f_v9f9HzIi9UQlCYq2DfUzv4OEx1Dfg9gD0s88fSQ8Ya8kdE4Ib422JHk_E0";
            var privateKey = _notificationPrivateKey;

            var vapidDetails  = new VapidDetails("mailto:[email protected]", publicKey, privateKey);
            var webPushClient = new WebPushClient();

            _logger.LogInformation($"NotifiyParticpants(<{calendarItem.Title}>, <{firstName}>, <{lastName}>, <{message}>)");
            // Add host as participant to avoid extra handling
            if (!calendarItem.WithoutHost)
            {
                Participant hostAsParticipant = new Participant()
                {
                    ParticipantFirstName = calendarItem.HostFirstName, ParticipantLastName = calendarItem.HostLastName
                };
                calendarItem.ParticipantsList = calendarItem.ParticipantsList.Append(hostAsParticipant);
            }
            foreach (Participant p in calendarItem.ParticipantsList)
            {
                _logger.LogInformation($"NotifiyParticpants: Participant {p.ParticipantFirstName} {p.ParticipantLastName} ");
                if (!p.ParticipantFirstName.Equals(firstName) || !p.ParticipantLastName.Equals(lastName))
                {
                    if (null == calendarItem.Tenant)
                    {
                        subscriptions = await _cosmosDbRepository.GetItems(d => d.UserFirstName.Equals(p.ParticipantFirstName) && d.UserLastName.Equals(p.ParticipantLastName) && (d.Tenant ?? String.Empty) == String.Empty);
                    }
                    else
                    {
                        subscriptions = await _cosmosDbRepository.GetItems(d => d.UserFirstName.Equals(p.ParticipantFirstName) && d.UserLastName.Equals(p.ParticipantLastName) && d.Tenant.Equals(calendarItem.Tenant));
                    }
                    _logger.LogInformation($"NotifiyParticpants: {subscriptions.Count()} subscriptions for {p.ParticipantFirstName} {p.ParticipantLastName} ");
                    foreach (NotificationSubscription subscription in subscriptions)
                    {
                        try
                        {
                            var pushSubscription = new PushSubscription(subscription.Url, subscription.P256dh, subscription.Auth);
                            var payload          = JsonSerializer.Serialize(new
                            {
                                title = calendarItem.Title,
                                message,
                                url = subscription.PlannerUrl,
                            });;
                            _logger.LogInformation($"NotifiyParticpants.SendNotificationAsync({pushSubscription.Endpoint})");
                            await webPushClient.SendNotificationAsync(pushSubscription, payload, vapidDetails);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError("Error sending push notification: " + ex.Message);
                            await _cosmosDbRepository.DeleteItemAsync(subscription.Id);
                        }
                    }
                }
            }
        }
コード例 #4
0
        public async Task <ExtendedCalendarItem> GetExtendedCalendarItem(string tenant, string keyword, string itemId)
        {
            ExtendedCalendarItem calendarItem = await $"https://{_functionsConfig.FunctionAppName}.azurewebsites.net/api/GetExtendedCalendarItem/{itemId}"
                                                .WithHeader(HEADER_FUNCTIONS_KEY, _functionsConfig.ApiKey)
                                                .WithHeader(HEADER_KEYWORD, keyword)
                                                .WithHeader(HEADER_TENANT, tenant)
                                                .GetJsonAsync <ExtendedCalendarItem>();

            return(calendarItem);
        }
コード例 #5
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "GetExtendedCalendarItem/{id}")] HttpRequest req, string id)
        {
            _logger.LogInformation("C# HTTP trigger function GetExtendedCalendarItem processed a request.");
            string tenant = req.Headers[Constants.HEADER_TENANT];

            if (String.IsNullOrWhiteSpace(tenant))
            {
                tenant = null;
            }
            ServerSettings serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);

            string keyWord = req.Headers[Constants.HEADER_KEYWORD];

            if (String.IsNullOrEmpty(keyWord) || !(serverSettings.IsUser(keyWord) || _serverSettingsRepository.IsInvitedGuest(keyWord)))
            {
                _logger.LogWarning("GetExtendedCalendarItem called with wrong keyword.");
                return(new BadRequestErrorMessageResult("Keyword is missing or wrong."));
            }
            // Get CalendarItem by id
            CalendarItem rawCalendarItem = await _cosmosRepository.GetItem(id);

            if (null == rawCalendarItem)
            {
                return(new BadRequestErrorMessageResult("No CalendarItem with given id found."));
            }
            ExtendedCalendarItem extendedItem = new ExtendedCalendarItem(rawCalendarItem);

            // Read all participants for this calendar item
            extendedItem.ParticipantsList = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(extendedItem.Id));

            // Read all comments
            extendedItem.CommentsList = await _commentRepository.GetItems(c => c.CalendarItemId.Equals(extendedItem.Id));

            return(new OkObjectResult(extendedItem));
        }
コード例 #6
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req)
        {
            string tenant = req.Headers[Constants.HEADER_TENANT];

            if (String.IsNullOrWhiteSpace(tenant))
            {
                tenant = null;
            }
            string tenantBadge = null == tenant ? "default" : tenant;

            _logger.LogInformation($"GetExtendedCalendarItems for tenant <{tenantBadge}>");
            ServerSettings serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);

            string keyWord = req.Headers[Constants.HEADER_KEYWORD];

            if (String.IsNullOrEmpty(keyWord) || !serverSettings.IsUser(keyWord))
            {
                _logger.LogWarning($"GetExtendedCalendarItems<{tenantBadge}> called with wrong keyword.");
                return(new BadRequestErrorMessageResult("Keyword is missing or wrong."));
            }
            string privateKeywordsString = req.Query["privatekeywords"];

            string[] privateKeywords = null;
            if (!String.IsNullOrEmpty(privateKeywordsString))
            {
                privateKeywords = privateKeywordsString.Split(';');
            }

            // Get a list of all CalendarItems and filter all applicable ones
            DateTime compareDate = DateTime.Now.AddHours((-serverSettings.CalendarItemsPastWindowHours));

            IEnumerable <CalendarItem> rawListOfCalendarItems;

            if (null == tenant)
            {
                rawListOfCalendarItems = await _cosmosRepository.GetItems(d => d.StartDate > compareDate && (d.Tenant ?? String.Empty) == String.Empty);
            }
            else
            {
                rawListOfCalendarItems = await _cosmosRepository.GetItems(d => d.StartDate > compareDate && d.Tenant.Equals(tenant));
            }
            List <ExtendedCalendarItem> resultCalendarItems = new List <ExtendedCalendarItem>(10);

            foreach (CalendarItem item in rawListOfCalendarItems)
            {
                // Create ExtendedCalendarItem and get comments and participants
                ExtendedCalendarItem extendedItem = new ExtendedCalendarItem(item);
                if (!serverSettings.IsAdmin(keyWord) && extendedItem.PublishDate.CompareTo(DateTime.UtcNow) > 0)
                {
                    // If calendar item is not ready for publishing skip it
                    continue;
                }
                if (String.IsNullOrEmpty(extendedItem.PrivateKeyword))
                {
                    // No private keyword for item ==> use it
                    resultCalendarItems.Add(extendedItem);
                }
                else if (null != privateKeywords)
                {
                    // Private keyword for item ==> check given keyword list against it
                    foreach (string keyword in privateKeywords)
                    {
                        if (keyword.Equals(extendedItem.PrivateKeyword))
                        {
                            resultCalendarItems.Add(extendedItem);
                            break;
                        }
                    }
                }
                // Read all participants for this calendar item
                extendedItem.ParticipantsList = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(extendedItem.Id));

                // Read all comments
                extendedItem.CommentsList = await _commentRepository.GetItems(c => c.CalendarItemId.Equals(extendedItem.Id));
            }
            IEnumerable <ExtendedCalendarItem> orderedList = resultCalendarItems.OrderBy(d => d.StartDate);

            return(new OkObjectResult(orderedList));
        }
コード例 #7
0
        public async Task <IActionResult> GetExtendedCalendarItemForGuest([FromHeader(Name = "x-meetup-tenant")] string tenant, string itemId)
        {
            ExtendedCalendarItem calendarItem = await _meetUpFunctions.GetExtendedCalendarItem(tenant, _meetUpFunctions.InviteGuestKey, itemId);

            return(Ok(calendarItem));
        }
コード例 #8
0
        public async Task <IActionResult> GetExtendedCalendarItem([FromHeader(Name = "x-meetup-tenant")] string tenant, [FromHeader(Name = "x-meetup-keyword")] string keyword, [FromQuery] string itemId)
        {
            ExtendedCalendarItem calendarItem = await _meetUpFunctions.GetExtendedCalendarItem(tenant, keyword, itemId);

            return(Ok(calendarItem));
        }
コード例 #9
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req)
        {
            string tenant = req.Headers[Constants.HEADER_TENANT];

            if (String.IsNullOrWhiteSpace(tenant))
            {
                tenant = null;
            }
            string tenantBadge = null == tenant ? "default" : tenant;

            _logger.LogInformation($"GetScopedCalendarItems for tenant <{tenantBadge}>");
            ServerSettings serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);

            string scope          = req.Query["scope"];
            string scopeToCompare = scope.ToLowerInvariant();

            if (String.IsNullOrEmpty(scope))
            {
                _logger.LogWarning($"GetScopedCalendarItems <{tenantBadge}> called without scope.");
                return(new BadRequestErrorMessageResult("scope is misssing.."));
            }
            _logger.LogInformation($"GetScopedCalendarItems<{tenantBadge}>(scope={scope})");

            // Get a list of all CalendarItems and filter all applicable ones
            DateTime compareDate = DateTime.Now.AddHours((-serverSettings.CalendarItemsPastWindowHours));
            IEnumerable <CalendarItem> rawListOfCalendarItems;

            if (null == tenant)
            {
                rawListOfCalendarItems = await _cosmosRepository.GetItems(d => d.StartDate > compareDate && d.GuestScope != null && (d.Tenant ?? String.Empty) == String.Empty);
            }
            else
            {
                rawListOfCalendarItems = await _cosmosRepository.GetItems(d => d.StartDate > compareDate && d.GuestScope != null && d.Tenant.Equals(tenant));
            }
            List <ExtendedCalendarItem> resultCalendarItems = new List <ExtendedCalendarItem>(10);

            foreach (CalendarItem item in rawListOfCalendarItems)
            {
                // Create ExtendedCalendarItem and get comments and participants
                ExtendedCalendarItem extendedItem = new ExtendedCalendarItem(item);
                if (extendedItem.PublishDate.CompareTo(DateTime.UtcNow) > 0)
                {
                    // If calendar item is not ready for publishing skip it
                    continue;
                }
                if (!extendedItem.GuestScope.ToLowerInvariant().Equals(scopeToCompare))
                {
                    continue;
                }
                resultCalendarItems.Add(extendedItem);
                // Read all participants for this calendar item
                extendedItem.ParticipantsList = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(extendedItem.Id));

                // Read all comments
                extendedItem.CommentsList = await _commentRepository.GetItems(c => c.CalendarItemId.Equals(extendedItem.Id));
            }
            IEnumerable <ExtendedCalendarItem> orderedList = resultCalendarItems.OrderBy(d => d.StartDate);

            return(new OkObjectResult(orderedList));
        }
コード例 #10
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            _logger.LogInformation("C# HTTP trigger function WriteCalendarItem processed a request.");
            string tenant = req.Headers[Constants.HEADER_TENANT];

            if (String.IsNullOrWhiteSpace(tenant))
            {
                tenant = null;
            }
            ServerSettings serverSettings;

            if (null == tenant)
            {
                serverSettings = await _serverSettingsRepository.GetServerSettings();
            }
            else
            {
                serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);
            }

            string keyWord = req.Headers[Constants.HEADER_KEYWORD];

            if (String.IsNullOrEmpty(keyWord) || !serverSettings.IsUser(keyWord))
            {
                return(new BadRequestErrorMessageResult("Keyword is missing or wrong."));
            }
            string       requestBody  = await new StreamReader(req.Body).ReadToEndAsync();
            CalendarItem calendarItem = JsonConvert.DeserializeObject <CalendarItem>(requestBody);

            System.TimeSpan diffTime = calendarItem.StartDate.Subtract(DateTime.Now);
            calendarItem.TimeToLive = serverSettings.AutoDeleteAfterDays * 24 * 3600 + (int)diffTime.TotalSeconds;
            if (null != tenant)
            {
                calendarItem.Tenant = tenant;
            }
            CalendarItem oldCalendarItem = null;

            if (!String.IsNullOrEmpty(calendarItem.Id))
            {
                oldCalendarItem = await _cosmosRepository.GetItem(calendarItem.Id);
            }
            calendarItem = await _cosmosRepository.UpsertItem(calendarItem);

            if (null != oldCalendarItem)
            {
                string message = null;
                // Compare versions and generate message
                if (oldCalendarItem.IsCanceled != calendarItem.IsCanceled)
                {
                    if (calendarItem.IsCanceled)
                    {
                        message = "Abgesagt!";
                    }
                    else
                    {
                        message = "Absage rückgängig gemacht!";
                    }
                }
                else if (!oldCalendarItem.StartDate.Equals(calendarItem.StartDate))
                {
                    message = "Neue Startzeit!";
                }
                else if (!oldCalendarItem.Title.Equals(calendarItem.Title))
                {
                    message = "Titel geändert;";
                }
                else if (!oldCalendarItem.Place.Equals(calendarItem.Place))
                {
                    message = "Startort geändert!";
                }
                else if (!oldCalendarItem.Summary.Equals(calendarItem.Summary))
                {
                    message = "Neue Infos!";
                }
                if (null != message)
                {
                    ExtendedCalendarItem extendedCalendarItem = new ExtendedCalendarItem(calendarItem);
                    // Read all participants for this calendar item
                    extendedCalendarItem.ParticipantsList = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(extendedCalendarItem.Id));

                    await _subscriptionRepository.NotifyParticipants(extendedCalendarItem, extendedCalendarItem.HostFirstName, extendedCalendarItem.HostLastName, message);
                }
            }

            return(new OkObjectResult(calendarItem));
        }
コード例 #11
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            _logger.LogInformation("C# HTTP trigger function ExportTrackingReport processed a request.");
            string tenant = req.Headers[Constants.HEADER_TENANT];

            if (String.IsNullOrWhiteSpace(tenant))
            {
                tenant = null;
            }
            ServerSettings serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);

            string keyWord = req.Headers[Constants.HEADER_KEYWORD];

            if (String.IsNullOrEmpty(keyWord) || !serverSettings.IsAdmin(keyWord))
            {
                _logger.LogWarning("ExportTrackingReport called with wrong keyword.");
                return(new BadRequestErrorMessageResult("Keyword is missing or wrong."));
            }
            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            TrackingReportRequest trackingRequest = JsonConvert.DeserializeObject <TrackingReportRequest>(requestBody);

            if (String.IsNullOrEmpty(trackingRequest.RequestorFirstName) || String.IsNullOrEmpty(trackingRequest.RequestorLastName))
            {
                _logger.LogWarning("ExportTrackingReport called without name of requestor.");
                return(new BadRequestErrorMessageResult("Requestor name missing."));
            }
            if (String.IsNullOrEmpty(trackingRequest.TrackFirstName) || String.IsNullOrEmpty(trackingRequest.TrackLastName))
            {
                _logger.LogWarning("ExportTrackingReport called without name of person to track.");
                return(new BadRequestErrorMessageResult("Track name missing."));
            }
            // Get a list of all CalendarItems
            IEnumerable <CalendarItem> rawListOfCalendarItems;

            if (null == tenant)
            {
                rawListOfCalendarItems = await _cosmosRepository.GetItems(d => (d.Tenant ?? String.Empty) == String.Empty && !d.IsCanceled);
            }
            else
            {
                rawListOfCalendarItems = await _cosmosRepository.GetItems(d => d.Tenant.Equals(tenant) && !d.IsCanceled);
            }
            List <ExtendedCalendarItem> resultCalendarItems = new List <ExtendedCalendarItem>(50);

            // Filter the CalendarItems that are relevant
            foreach (CalendarItem item in rawListOfCalendarItems)
            {
                // Read all participants for this calendar item
                IEnumerable <Participant> participants = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(item.Id));

                // Only events where the person was part of will be used.
                if (!item.WithoutHost && item.EqualsHost(trackingRequest.TrackFirstName, trackingRequest.TrackLastName) || null != participants.Find(trackingRequest.TrackFirstName, trackingRequest.TrackLastName))
                {
                    ExtendedCalendarItem extendedItem = new ExtendedCalendarItem(item);
                    extendedItem.ParticipantsList = participants;
                    resultCalendarItems.Add(extendedItem);
                }
            }
            IEnumerable <ExtendedCalendarItem> orderedList = resultCalendarItems.OrderBy(d => d.StartDate);
            // Build template for marker list corresponding to orderedList above
            List <CompanionCalendarInfo> relevantCalendarList = new List <CompanionCalendarInfo>(50);
            int calendarSize = 0;

            foreach (ExtendedCalendarItem e in orderedList)
            {
                relevantCalendarList.Add(new CompanionCalendarInfo(e));
                ++calendarSize;
            }

            // Assemble report
            TrackingReport report = new TrackingReport(trackingRequest);

            report.CompanionList = new List <Companion>(50);
            report.CalendarList  = relevantCalendarList;
            int calendarIndex = 0;

            foreach (ExtendedCalendarItem calendarItem in orderedList)
            {
                if (!calendarItem.WithoutHost)
                {
                    report.CompanionList.AddCompanion(calendarItem.HostFirstName, calendarItem.HostLastName, calendarItem.HostAdressInfo, calendarSize, calendarIndex);
                }
                foreach (Participant p in calendarItem.ParticipantsList)
                {
                    report.CompanionList.AddCompanion(p.ParticipantFirstName, p.ParticipantLastName, p.ParticipantAdressInfo, calendarSize, calendarIndex);
                }
                ++calendarIndex;
            }
            report.CreationDate = DateTime.Now;
            ExportLogItem log = new ExportLogItem(trackingRequest);

            log.TimeToLive = Constants.LOG_TTL;
            if (null != tenant)
            {
                log.Tenant = tenant;
            }
            await _logRepository.CreateItem(log);

            return(new OkObjectResult(report));
        }