Ejemplo n.º 1
0
 public Task RegisterNotification(NotifyPayload payload)
 {
     LatestNotifications = LatestNotifications.Union(new[] { payload })
                           .Where(x => x.Timestamp > _timeService.UtcNow - _latestNotificationsSpan)
                           .ToArray();
     return(Task.CompletedTask);
 }
        public async Task <HttpResponseMessage> ReceiveMessage(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "ReceiveMessage/{phone}/{message}")] HttpRequestMessage req,
            string phone, string message,
            [DurableClient] IDurableEntityClient entityClient,               // need the durable entity client
            [DurableClient] IDurableOrchestrationClient orchestrationClient) // and the Orchestration client
        {
            if (!message.Equals("APPROVE", StringComparison.InvariantCultureIgnoreCase) &&
                !message.Equals("REJECT", StringComparison.InvariantCultureIgnoreCase))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            var notifyPayload = new NotifyPayload()
            {
                Phone = phone, EventName = message.ToUpperInvariant(), Timestamp = TimeService.UtcNow
            };

            var notifySubscriptionEntityId = NotifySubscriptionEntity.BuildId(phone, notifyPayload.EventName);
            var entity = await entityClient.ReadEntityStateAsync <NotifySubscriptionEntity>(notifySubscriptionEntityId);

            await entityClient.SignalEntityAsync(notifySubscriptionEntityId, nameof(NotifySubscriptionEntity.RegisterNotification),
                                                 notifyPayload);                                    // send a signal to the entity

            if (entity.EntityExists && entity.EntityState.InstancesToNotify.Length > 0)             // we have live subscribers
            {
                foreach (var instanceId in entity.EntityState.InstancesToNotify)
                {
                    await orchestrationClient.RaiseEventAsync(instanceId, notifyPayload.EventName, notifyPayload);                     // notify all subscribers
                }
            }
            else
            {
                _log.Trace(LogEventLevel.Verbose, message: "No subscribed instances to notify");
            }


            return(new HttpResponseMessage(HttpStatusCode.OK));
        }