public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            _logger.LogInformation($"C# HTTP trigger function RemoveCommentFromCalendarItem 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);

            if (String.IsNullOrEmpty(comment.Id))
            {
                return(new OkObjectResult(new BackendResult(false, "Die Id des Kommentars fehlt.")));
            }
            await _cosmosRepository.DeleteItemAsync(comment.Id);

            return(new OkObjectResult(new BackendResult(true)));
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            _logger.LogInformation($"C# HTTP trigger function AssignNewHostToCalendarItem 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();
            Participant participant = JsonConvert.DeserializeObject <Participant>(requestBody);

            // Check if participant has an id
            if (String.IsNullOrEmpty(participant.Id))
            {
                return(new OkObjectResult(new BackendResult(false, "Id des Teilnehmers fehlt.")));
            }
            if (String.IsNullOrEmpty(participant.ParticipantFirstName) || String.IsNullOrEmpty(participant.ParticipantLastName))
            {
                return(new OkObjectResult(new BackendResult(false, "Name unvollständig.")));
            }
            // Get and check corresponding CalendarItem
            if (String.IsNullOrEmpty(participant.CalendarItemId))
            {
                return(new OkObjectResult(new BackendResult(false, "Terminangabe fehlt.")));
            }
            CalendarItem calendarItem = await _calendarRepository.GetItem(participant.CalendarItemId);

            if (null == calendarItem)
            {
                return(new OkObjectResult(new BackendResult(false, "Angegebenen Termin nicht gefunden.")));
            }
            // Assign participant as new host
            calendarItem.HostFirstName  = participant.ParticipantFirstName;
            calendarItem.HostLastName   = participant.ParticipantLastName;
            calendarItem.HostAdressInfo = participant.ParticipantAdressInfo;
            calendarItem.WithoutHost    = false;
            // Set TTL and write CalendarItem to database
            System.TimeSpan diffTime = calendarItem.StartDate.Subtract(DateTime.Now);
            calendarItem.TimeToLive = serverSettings.AutoDeleteAfterDays * 24 * 3600 + (int)diffTime.TotalSeconds;
            calendarItem            = await _calendarRepository.UpsertItem(calendarItem);

            // Delete host as participant
            await _cosmosRepository.DeleteItemAsync(participant.Id);

            BackendResult result = new BackendResult(true);

            return(new OkObjectResult(result));
        }
Esempio n. 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);
                        }
                    }
                }
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            _logger.LogInformation($"C# HTTP trigger function DeleteCalendarItem 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();
            CalendarItem calendarItem = JsonConvert.DeserializeObject <CalendarItem>(requestBody);

            if (String.IsNullOrEmpty(calendarItem.Id))
            {
                return(new OkObjectResult(new BackendResult(false, "Die Id des Kalendar-Eintrags fehlt.")));
            }
            await _cosmosRepository.DeleteItemAsync(calendarItem.Id);

            // Delete all participants
            IEnumerable <Participant> participants = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(calendarItem.Id));

            foreach (Participant p in participants)
            {
                await _participantRepository.DeleteItemAsync(p.Id);
            }

            // Delete all comments
            IEnumerable <CalendarComment> comments = await _commentRepository.GetItems(c => c.CalendarItemId.Equals(calendarItem.Id));

            foreach (CalendarComment c in comments)
            {
                await _commentRepository.DeleteItemAsync(c.Id);
            }

            return(new OkObjectResult(new BackendResult(true)));
        }